blob: 45e1aa9d6571aa6f8a0bf595073865799cad9605 [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 Horstmann42015332024-03-13 15:42:31 +0000109#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_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
David Horstmannb80e35a2023-12-14 18:12:47 +0000185/* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC()
David Horstmann1f532132023-12-08 14:08:18 +0000186 * after first copying back its contents to the original buffer.
David Horstmann926193a2023-12-13 15:55:25 +0000187 *
David Horstmann1f532132023-12-08 14:08:18 +0000188 * Assumptions:
David Horstmann1f532132023-12-08 14:08:18 +0000189 * - psa_status_t status exists
David Horstmannb80e35a2023-12-14 18:12:47 +0000190 * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC()
191 * - output is the name of the original buffer that was copied
David Horstmann1f532132023-12-08 14:08:18 +0000192 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000193#define LOCAL_OUTPUT_FREE(output, output_copy) \
194 output_copy = NULL; \
David Horstmann2b70a662023-12-13 14:09:08 +0000195 do { \
196 psa_status_t local_output_status; \
David Horstmann926193a2023-12-13 15:55:25 +0000197 local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmann2b70a662023-12-13 14:09:08 +0000198 if (local_output_status != PSA_SUCCESS) { \
199 /* Since this error case is an internal error, it's more serious than \
200 * any existing error code and so it's fine to overwrite the existing \
201 * status. */ \
202 status = local_output_status; \
203 } \
204 } while (0)
David Horstmann42015332024-03-13 15:42:31 +0000205#else /* !MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS */
David Horstmannb80e35a2023-12-14 18:12:47 +0000206#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
207 const uint8_t *input_copy_name = NULL;
208#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
209 input_copy = input;
210#define LOCAL_INPUT_FREE(input, input_copy) \
211 input_copy = NULL;
212#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
213 uint8_t *output_copy_name = NULL;
214#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
215 output_copy = output;
216#define LOCAL_OUTPUT_FREE(output, output_copy) \
217 output_copy = NULL;
David Horstmann42015332024-03-13 15:42:31 +0000218#endif /* !MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS */
David Horstmann34980bd2023-11-29 17:07:13 +0000219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100221{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100222 /* Mbed TLS error codes can combine a high-level error code and a
223 * low-level error code. The low-level error usually reflects the
224 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 int low_level_ret = -(-ret & 0x007f);
226 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100227 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
230 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
231 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
232 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100236
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200237 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200239
Gilles Peskine9a944802018-06-21 09:35:35 +0200240 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
241 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
242 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
243 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
244 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200246 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200248 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200250
Jaeden Amero93e21112019-02-20 13:57:28 +0000251#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000252 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000253#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
254 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
255#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100256 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100258 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100260
Jaeden Amero93e21112019-02-20 13:57:28 +0000261#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000262 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000263#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
264 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
265#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100268 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100270
271 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100273 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100275 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100277
Gilles Peskine26869f22019-05-06 15:25:00 +0200278 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200280
281 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200283 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200285
Gilles Peskinea5905292018-02-07 20:59:33 +0100286 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100290 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100300 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100302
303 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
307 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100308 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
309 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100312 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
313 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100317#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100318
319 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100321 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100323
Gilles Peskinee59236f2018-01-27 23:32:46 +0100324 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
325 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
326 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100328
329 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100331 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100333 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100335
Gilles Peskine82e57d12020-11-13 21:31:17 +0100336#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100338 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
339 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100340 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100342 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
343 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100345 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100347#endif
348
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200349 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
350 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
351 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200353
Gilles Peskinea5905292018-02-07 20:59:33 +0100354 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100356 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100358 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100360 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200362 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100364
Gilles Peskinef76aa772018-10-29 19:24:33 +0100365 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100367 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100369 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100371 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100373 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100375 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100377 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100379 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100381
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100382 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100384 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
385 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100387 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100389 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
390 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100392 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100394 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
395 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100396 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100397 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100399 case MBEDTLS_ERR_PK_INVALID_ALG:
400 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
401 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100403 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100405 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100407
Gilles Peskineff2d2002019-05-06 15:26:23 +0200408 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200410 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200412
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200413 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200415
Gilles Peskinea5905292018-02-07 20:59:33 +0100416 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100418 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100420 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100422 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100424 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
425 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100427 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100429 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100431 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100433 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100435 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100437
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200438 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
439 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
440 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200442
Gilles Peskinea5905292018-02-07 20:59:33 +0100443 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100445 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100447
itayzafrir5c753392018-05-08 11:18:38 +0300448 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300449 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300451 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300453 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300455 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
456 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300458 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100460 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300462 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100464
Janos Follatha13b9052019-11-22 12:48:59 +0000465 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100466 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300467
Gilles Peskinee59236f2018-01-27 23:32:46 +0100468 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100470 }
471}
472
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200473
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200474
475
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100476/****************************************************************/
477/* Key management */
478/****************************************************************/
479
John Durkop9814fa22020-11-04 12:28:15 -0800480#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800481 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100482 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
483 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
484 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
486 size_t bits,
487 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200488{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100490 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100492#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100493 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100494 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100495#endif
496#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100497 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100498 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100499#endif
500#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100501 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100503#endif
504#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100505 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100507#endif
508#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100509 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100511 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100512 if (bits_is_sloppy) {
513 return MBEDTLS_ECP_DP_SECP521R1;
514 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100515 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100516#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100517 }
518 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100519
Paul Elliott8ff510a2020-06-02 17:19:28 +0100520 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100521 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100522#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100523 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100525#endif
526#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100527 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100529#endif
530#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100531 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532 return MBEDTLS_ECP_DP_BP512R1;
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_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100539#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100540 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100542 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100543 if (bits_is_sloppy) {
544 return MBEDTLS_ECP_DP_CURVE25519;
545 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100546 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100547#endif
548#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100549 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100551#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100552 }
553 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100554
Paul Elliott8ff510a2020-06-02 17:19:28 +0100555 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100557#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100558 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100559 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100560#endif
561#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100562 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100564#endif
565#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100566 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100568#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100569 }
570 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200571 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100572
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100573 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200575}
John Durkop9814fa22020-11-04 12:28:15 -0800576#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100577 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
578 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
579 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
580 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
583 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200584{
585 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200587 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200588 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200589 case PSA_KEY_TYPE_DERIVE:
590 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100591#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200592 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 if (bits != 128 && bits != 192 && bits != 256) {
594 return PSA_ERROR_INVALID_ARGUMENT;
595 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200596 break;
597#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200598#if defined(PSA_WANT_KEY_TYPE_ARIA)
599 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100600 if (bits != 128 && bits != 192 && bits != 256) {
601 return PSA_ERROR_INVALID_ARGUMENT;
602 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200603 break;
604#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100605#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200606 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100607 if (bits != 128 && bits != 192 && bits != 256) {
608 return PSA_ERROR_INVALID_ARGUMENT;
609 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200610 break;
611#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100612#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200613 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100614 if (bits != 64 && bits != 128 && bits != 192) {
615 return PSA_ERROR_INVALID_ARGUMENT;
616 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200617 break;
618#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100619#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200620 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100621 if (bits < 8 || bits > 2048) {
622 return PSA_ERROR_INVALID_ARGUMENT;
623 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200624 break;
625#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100626#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200627 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100628 if (bits != 256) {
629 return PSA_ERROR_INVALID_ARGUMENT;
630 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200631 break;
632#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200633 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100634 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200635 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 if (bits % 8 != 0) {
637 return PSA_ERROR_INVALID_ARGUMENT;
638 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200641}
642
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100643/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100644 *
Steven Cooremancd640932021-03-08 12:00:27 +0100645 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
646 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100647 *
648 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100649 * \param[in] key_type The key type of the key to be used with the
650 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100651 *
652 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100653 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100654 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100655 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100656 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100657MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100658 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100660{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100661 if (PSA_ALG_IS_HMAC(algorithm)) {
662 if (key_type == PSA_KEY_TYPE_HMAC) {
663 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100664 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100665 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100667 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
668 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
669 * key. */
670 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
671 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
672 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
673 * the block length (larger than 1) for block ciphers. */
674 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
675 return PSA_SUCCESS;
676 }
677 }
678 }
679
680 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100681}
682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
684 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200685{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100686 if (slot->key.data != NULL) {
687 return PSA_ERROR_ALREADY_EXISTS;
688 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100690 slot->key.data = mbedtls_calloc(1, buffer_length);
691 if (slot->key.data == NULL) {
692 return PSA_ERROR_INSUFFICIENT_MEMORY;
693 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200694
Ronald Cronea0f8a62020-11-25 17:52:23 +0100695 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100696 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200697}
698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100699psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
700 const uint8_t *data,
701 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200702{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100703 psa_status_t status = psa_allocate_buffer_to_slot(slot,
704 data_length);
705 if (status != PSA_SUCCESS) {
706 return status;
707 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709 memcpy(slot->key.data, data, data_length);
710 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200711}
712
Ronald Crona0fe59f2020-11-29 11:14:53 +0100713psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100714 const psa_key_attributes_t *attributes,
715 const uint8_t *data, size_t data_length,
716 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100717 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100718{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100719 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
720 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100721
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200722 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100723 if (data_length == 0) {
724 return PSA_ERROR_NOT_SUPPORTED;
725 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100727 if (key_type_is_raw_bytes(type)) {
728 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200729
Steven Cooreman75b74362020-07-28 14:30:13 +0200730 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100731 if (data_length > SIZE_MAX / 8) {
732 return PSA_ERROR_NOT_SUPPORTED;
733 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200734
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200735 /* Enforce a size limit, and in particular ensure that the bit
736 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100737 if ((*bits) > PSA_MAX_KEY_BITS) {
738 return PSA_ERROR_NOT_SUPPORTED;
739 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200740
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100741 status = validate_unstructured_key_bit_size(type, *bits);
742 if (status != PSA_SUCCESS) {
743 return status;
744 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200745
Ronald Crondd04d422020-11-28 15:14:42 +0100746 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100747 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100748 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100749 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100751 return PSA_SUCCESS;
752 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800753#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100754 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
755 if (PSA_KEY_TYPE_IS_ECC(type)) {
756 return mbedtls_psa_ecp_import_key(attributes,
757 data, data_length,
758 key_buffer, key_buffer_size,
759 key_buffer_length,
760 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100761 }
John Durkop9814fa22020-11-04 12:28:15 -0800762#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
763 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700764#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100765 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
766 if (PSA_KEY_TYPE_IS_RSA(type)) {
767 return mbedtls_psa_rsa_import_key(attributes,
768 data, data_length,
769 key_buffer, key_buffer_size,
770 key_buffer_length,
771 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200772 }
John Durkop9814fa22020-11-04 12:28:15 -0800773#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
774 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100775 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100777 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100778}
779
Gilles Peskinef603c712019-01-19 13:40:11 +0100780/** Calculate the intersection of two algorithm usage policies.
781 *
782 * Return 0 (which allows no operation) on incompatibility.
783 */
784static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100785 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100786 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100787 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100788{
Gilles Peskine549ea862019-05-22 11:45:59 +0200789 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790 if (alg1 == alg2) {
791 return alg1;
792 }
Steven Cooreman03488022021-02-18 12:07:20 +0100793 /* If the policies are from the same hash-and-sign family, check
794 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100795 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
796 PSA_ALG_IS_SIGN_HASH(alg2) &&
797 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
798 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
799 return alg2;
800 }
801 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
802 return alg1;
803 }
Steven Cooreman03488022021-02-18 12:07:20 +0100804 }
805 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100806 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100807 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100808 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
809 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
810 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
811 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
812 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100813 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100814
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100815 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100816 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
817 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
818 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
819 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100820 }
821 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100822 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
823 (alg1_len <= alg2_len)) {
824 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100825 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100826 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
827 (alg2_len <= alg1_len)) {
828 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100829 }
830 }
831 /* If the policies are from the same MAC family, check whether one
832 * of them is a minimum-MAC-length policy. Calculate the most
833 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100834 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
835 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
836 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100837 /* Validate the combination of key type and algorithm. Since the base
838 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
840 return 0;
841 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100842
Steven Cooreman31a876d2021-03-03 20:47:40 +0100843 /* Get the (exact or at-least) output lengths for both sides of the
844 * requested intersection. None of the currently supported algorithms
845 * have an output length dependent on the actual key size, so setting it
846 * to a bogus value of 0 is currently OK.
847 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100848 * Note that for at-least-this-length wildcard algorithms, the output
849 * length is set to the shortest allowed length, which allows us to
850 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100851 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
852 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100853 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100854
Steven Cooremana1d83222021-02-25 10:20:29 +0100855 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100856 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
857 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
858 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100859 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100860
861 /* If only one is an at-least-this-length policy, the intersection would
862 * be the other (fixed-length) policy as long as said fixed length is
863 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100864 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
865 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100866 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100867 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
868 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100869 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100870
Steven Cooremancd640932021-03-08 12:00:27 +0100871 /* If none of them are wildcards, check whether they define the same tag
872 * length. This is still possible here when one is default-length and
873 * the other specific-length. Ensure to always return the
874 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100875 if (alg1_len == alg2_len) {
876 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
877 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100878 }
879 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100880 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100881}
882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100883static int psa_key_algorithm_permits(psa_key_type_t key_type,
884 psa_algorithm_t policy_alg,
885 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200886{
Gilles Peskine549ea862019-05-22 11:45:59 +0200887 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100888 if (requested_alg == policy_alg) {
889 return 1;
890 }
Steven Cooreman03488022021-02-18 12:07:20 +0100891 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
892 * and requested_alg is the same hash-and-sign family with any hash,
893 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
895 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
896 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
897 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100898 }
899 /* If policy_alg is a wildcard AEAD algorithm of the same base as
900 * the requested algorithm, check the requested tag length to be
901 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100902 if (PSA_ALG_IS_AEAD(policy_alg) &&
903 PSA_ALG_IS_AEAD(requested_alg) &&
904 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
905 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
906 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
907 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
908 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100909 }
Steven Cooremancd640932021-03-08 12:00:27 +0100910 /* If policy_alg is a MAC algorithm of the same base as the requested
911 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100912 if (PSA_ALG_IS_MAC(policy_alg) &&
913 PSA_ALG_IS_MAC(requested_alg) &&
914 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
915 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100916 /* Validate the combination of key type and algorithm. Since the policy
917 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100918 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
919 return 0;
920 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100921
Steven Cooreman31a876d2021-03-03 20:47:40 +0100922 /* Get both the requested output length for the algorithm which is to be
923 * verified, and the default output length for the base algorithm.
924 * Note that none of the currently supported algorithms have an output
925 * length dependent on actual key size, so setting it to a bogus value
926 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100927 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100928 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100929 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 key_type, 0,
931 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100932
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100933 /* If the policy is default-length, only allow an algorithm with
934 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100935 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
936 return requested_output_length == default_output_length;
937 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100938
939 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100940 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100941 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
942 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
943 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100944 }
945
Steven Cooremancd640932021-03-08 12:00:27 +0100946 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
947 * check for the requested MAC length to be equal to or longer than the
948 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100949 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
950 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
951 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100952 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200953 }
Steven Cooremance48e852020-10-05 16:02:45 +0200954 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200955 * a key derivation with that key agreement should also be allowed. This
956 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100957 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
958 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
959 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
960 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200961 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100962 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100963 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200964}
965
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100966/** Test whether a policy permits an algorithm.
967 *
968 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100969 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100970 * \note This function requires providing the key type for which the policy is
971 * being validated, since some algorithm policy definitions (e.g. MAC)
972 * have different properties depending on what kind of cipher it is
973 * combined with.
974 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100975 * \retval PSA_SUCCESS When \p alg is a specific algorithm
976 * allowed by the \p policy.
977 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
978 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
979 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100980 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100981static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
982 psa_key_type_t key_type,
983 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100984{
Steven Cooremand788fab2021-02-25 11:29:17 +0100985 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100986 if (alg == 0) {
987 return PSA_ERROR_INVALID_ARGUMENT;
988 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100989
Steven Cooreman7e39f052021-02-23 14:18:32 +0100990 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100991 if (PSA_ALG_IS_WILDCARD(alg)) {
992 return PSA_ERROR_INVALID_ARGUMENT;
993 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100995 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
996 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
997 return PSA_SUCCESS;
998 } else {
999 return PSA_ERROR_NOT_PERMITTED;
1000 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001001}
1002
Gilles Peskinef603c712019-01-19 13:40:11 +01001003/** Restrict a key policy based on a constraint.
1004 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001005 * \note This function requires providing the key type for which the policy is
1006 * being restricted, since some algorithm policy definitions (e.g. MAC)
1007 * have different properties depending on what kind of cipher it is
1008 * combined with.
1009 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001010 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001011 * \param[in,out] policy The policy to restrict.
1012 * \param[in] constraint The policy constraint to apply.
1013 *
1014 * \retval #PSA_SUCCESS
1015 * \c *policy contains the intersection of the original value of
1016 * \c *policy and \c *constraint.
1017 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001018 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001019 * \c *policy is unchanged.
1020 */
1021static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001022 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001023 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001024 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001025{
1026 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001027 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1028 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001029 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001030 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1031 constraint->alg2);
1032 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1033 return PSA_ERROR_INVALID_ARGUMENT;
1034 }
1035 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1036 return PSA_ERROR_INVALID_ARGUMENT;
1037 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001038 policy->usage &= constraint->usage;
1039 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001040 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001041 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001042}
1043
Ronald Cron5c522922020-11-14 16:35:34 +01001044/** Get the description of a key given its identifier and policy constraints
1045 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001046 *
Ronald Cron5c522922020-11-14 16:35:34 +01001047 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +01001048 * nonzero, the key must allow operations with this algorithm. If \p alg is
1049 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +01001050 *
Ronald Cron5c522922020-11-14 16:35:34 +01001051 * In case of a persistent key, the function loads the description of the key
1052 * into a key slot if not already done.
1053 *
1054 * On success, the returned key slot is locked. It is the responsibility of
1055 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001056 */
Ronald Cron5c522922020-11-14 16:35:34 +01001057static psa_status_t psa_get_and_lock_key_slot_with_policy(
1058 mbedtls_svc_key_id_t key,
1059 psa_key_slot_t **p_slot,
1060 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001061 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001062{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001063 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1064 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001066 status = psa_get_and_lock_key_slot(key, p_slot);
1067 if (status != PSA_SUCCESS) {
1068 return status;
1069 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001070 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001071
1072 /* Enforce that usage policy for the key slot contains all the flags
1073 * required by the usage parameter. There is one exception: public
1074 * keys can always be exported, so we treat public key objects as
1075 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001076 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001077 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001078 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001080 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001081 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001082 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001083 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001084
Shaun Case0e7791f2021-12-20 21:14:10 -08001085 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001086 if (alg != 0) {
1087 status = psa_key_policy_permits(&slot->attr.policy,
1088 slot->attr.type,
1089 alg);
1090 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001091 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001092 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001093 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001095 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001096
1097error:
1098 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001099 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001101 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001102}
Darryl Green940d72c2018-07-13 13:18:51 +01001103
Ronald Cron5c522922020-11-14 16:35:34 +01001104/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001105 *
1106 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001107 * available, as opposed to a key in a secure element and/or to be used
1108 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001109 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001110 * This is a temporary function that may be used instead of
1111 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1112 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001113 *
Ronald Cron5c522922020-11-14 16:35:34 +01001114 * On success, the returned key slot is locked. It is the responsibility of the
1115 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001116 */
Ronald Cron5c522922020-11-14 16:35:34 +01001117static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1118 mbedtls_svc_key_id_t key,
1119 psa_key_slot_t **p_slot,
1120 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001121 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001122{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001123 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1124 usage, alg);
1125 if (status != PSA_SUCCESS) {
1126 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001127 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001129 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1130 psa_unlock_key_slot(*p_slot);
1131 *p_slot = NULL;
1132 return PSA_ERROR_NOT_SUPPORTED;
1133 }
1134
1135 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001136}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001138psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001139{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001140 /* Data pointer will always be either a valid pointer or NULL in an
1141 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001142 if (slot->key.data != NULL) {
1143 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1144 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001146 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001147 slot->key.data = NULL;
1148 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001150 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001151}
1152
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001153/** Completely wipe a slot in memory, including its policy.
1154 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001155psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001156{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001157 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001158
1159 /*
1160 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001161 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001162 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1163 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001164 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001165 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001166 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001167#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001168 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001169#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001170 status = PSA_ERROR_CORRUPTION_DETECTED;
1171 }
1172
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001173 /* Multipart operations may still be using the key. This is safe
1174 * because all multipart operation objects are independent from
1175 * the key slot: if they need to access the key after the setup
1176 * phase, they have a copy of the key. Note that this means that
1177 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001178 /* At this point, key material and other type-specific content has
1179 * been wiped. Clear remaining metadata. We can call memset and not
1180 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001181 memset(slot, 0, sizeof(*slot));
1182 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001183}
1184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001185psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001186{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001187 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001188 psa_status_t status; /* status of the last operation */
1189 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001190#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1191 psa_se_drv_table_entry_t *driver;
1192#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001194 if (mbedtls_svc_key_id_is_null(key)) {
1195 return PSA_SUCCESS;
1196 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001197
Ronald Cronf2911112020-10-29 17:51:10 +01001198 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001199 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001200 * key, this will load the key description from persistent memory if not
1201 * done yet. We cannot avoid this loading as without it we don't know if
1202 * the key is operated by an SE or not and this information is needed by
1203 * the current implementation.
1204 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001205 status = psa_get_and_lock_key_slot(key, &slot);
1206 if (status != PSA_SUCCESS) {
1207 return status;
1208 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001209
Ronald Cronf2911112020-10-29 17:51:10 +01001210 /*
1211 * If the key slot containing the key description is under access by the
1212 * library (apart from the present access), the key cannot be destroyed
1213 * yet. For the time being, just return in error. Eventually (to be
1214 * implemented), the key should be destroyed when all accesses have
1215 * stopped.
1216 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001217 if (slot->lock_count > 1) {
1218 psa_unlock_key_slot(slot);
1219 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001220 }
1221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001222 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001223 /* Refuse the destruction of a read-only key (which may or may not work
1224 * if we attempt it, depending on whether the key is merely read-only
1225 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001226 * Just do the best we can, which is to wipe the copy in memory
1227 * (done in this function's cleanup code). */
1228 overall_status = PSA_ERROR_NOT_PERMITTED;
1229 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001230 }
1231
Gilles Peskine354f7672019-07-12 23:46:38 +02001232#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001233 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1234 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001235 /* For a key in a secure element, we need to do three things:
1236 * remove the key file in internal storage, destroy the
1237 * key inside the secure element, and update the driver's
1238 * persistent data. Start a transaction that will encompass these
1239 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001240 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001241 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001242 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001243 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001244 status = psa_crypto_save_transaction();
1245 if (status != PSA_SUCCESS) {
1246 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001247 /* We should still try to destroy the key in the secure
1248 * element and the key metadata in storage. This is especially
1249 * important if the error is that the storage is full.
1250 * But how to do it exactly without risking an inconsistent
1251 * state after a reset?
1252 * https://github.com/ARMmbed/mbed-crypto/issues/215
1253 */
1254 overall_status = status;
1255 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001256 }
1257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001258 status = psa_destroy_se_key(driver,
1259 psa_key_slot_get_slot_number(slot));
1260 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001261 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001262 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001263 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001264#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1265
Darryl Greend49a4992018-06-18 17:27:26 +01001266#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001267 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1268 status = psa_destroy_persistent_key(slot->attr.id);
1269 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001270 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001271 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001272
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001273 /* TODO: other slots may have a copy of the same key. We should
1274 * invalidate them.
1275 * https://github.com/ARMmbed/mbed-crypto/issues/214
1276 */
Darryl Greend49a4992018-06-18 17:27:26 +01001277 }
1278#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001279
Gilles Peskinefc762652019-07-22 19:30:34 +02001280#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001281 if (driver != NULL) {
1282 status = psa_save_se_persistent_data(driver);
1283 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001284 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001285 }
1286 status = psa_crypto_stop_transaction();
1287 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001288 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001289 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001290 }
1291#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1292
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001293exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001294 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001295 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001296 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001297 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001298 }
1299 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001300}
1301
John Durkop07cc04a2020-11-16 22:08:34 -08001302#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001303 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001304static psa_status_t psa_get_rsa_public_exponent(
1305 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001306 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001307{
1308 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001310 uint8_t *buffer = NULL;
1311 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001312 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001314 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1315 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001316 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001317 }
1318 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001319 /* It's the default value, which is reported as an empty string,
1320 * so there's nothing to do. */
1321 goto exit;
1322 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001324 buflen = mbedtls_mpi_size(&mpi);
1325 buffer = mbedtls_calloc(1, buflen);
1326 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001327 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1328 goto exit;
1329 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001330 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1331 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001332 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001333 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001334 attributes->domain_parameters = buffer;
1335 attributes->domain_parameters_size = buflen;
1336
1337exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001338 mbedtls_mpi_free(&mpi);
1339 if (ret != 0) {
1340 mbedtls_free(buffer);
1341 }
1342 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001343}
John Durkop07cc04a2020-11-16 22:08:34 -08001344#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001345 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001346
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001347/** Retrieve all the publicly-accessible attributes of a key.
1348 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001349psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1350 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001351{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001352 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001353 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001354 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001356 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001358 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1359 if (status != PSA_SUCCESS) {
1360 return status;
1361 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001362
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001363 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001364 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1365 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001366
1367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001368 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1369 psa_set_key_slot_number(attributes,
1370 psa_key_slot_get_slot_number(slot));
1371 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001374 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001375#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001376 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001377 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001378 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001379 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001380 * is not yet implemented.
1381 * https://github.com/ARMmbed/mbed-crypto/issues/216
1382 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001383 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001384 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001385
Ronald Cron90857082020-11-25 14:58:33 +01001386 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001387 slot->attr.type,
1388 slot->key.data,
1389 slot->key.bytes,
1390 &rsa);
1391 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001392 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001393 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001395 status = psa_get_rsa_public_exponent(rsa,
1396 attributes);
1397 mbedtls_rsa_free(rsa);
1398 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001399 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001400 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001401#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001402 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001403 default:
1404 /* Nothing else to do. */
1405 break;
1406 }
1407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001408 if (status != PSA_SUCCESS) {
1409 psa_reset_key_attributes(attributes);
1410 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001412 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001414 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001415}
1416
Gilles Peskinec8000c02019-08-02 20:15:51 +02001417#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1418psa_status_t psa_get_key_slot_number(
1419 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001420 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001421{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001422 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001423 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001424 return PSA_SUCCESS;
1425 } else {
1426 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001427 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001428}
1429#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001431static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1432 size_t key_buffer_size,
1433 uint8_t *data,
1434 size_t data_size,
1435 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001436{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001437 if (key_buffer_size > data_size) {
1438 return PSA_ERROR_BUFFER_TOO_SMALL;
1439 }
1440 memcpy(data, key_buffer, key_buffer_size);
1441 memset(data + key_buffer_size, 0,
1442 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001443 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001444 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001445}
1446
Ronald Cron7285cda2020-11-26 14:46:37 +01001447psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001448 const psa_key_attributes_t *attributes,
1449 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001450 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001451{
Ronald Crond18b5f82020-11-25 16:46:05 +01001452 psa_key_type_t type = attributes->core.type;
1453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001454 if (key_type_is_raw_bytes(type) ||
1455 PSA_KEY_TYPE_IS_RSA(type) ||
1456 PSA_KEY_TYPE_IS_ECC(type)) {
1457 return psa_export_key_buffer_internal(
1458 key_buffer, key_buffer_size,
1459 data, data_size, data_length);
1460 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001461 /* This shouldn't happen in the reference implementation, but
1462 it is valid for a special-purpose implementation to omit
1463 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001464 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001465 }
1466}
1467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001468psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Ryan Everette3e760c2024-01-19 16:03:50 +00001469 uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001470 size_t data_size,
1471 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001472{
1473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1474 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1475 psa_key_slot_t *slot;
Ryan Everette3e760c2024-01-19 16:03:50 +00001476 LOCAL_OUTPUT_DECLARE(data_external, data);
Ronald Crone907e552021-01-18 13:22:38 +01001477 /* Reject a zero-length output buffer now, since this can never be a
1478 * valid key representation. This way we know that data must be a valid
1479 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001480 if (data_size == 0) {
1481 return PSA_ERROR_BUFFER_TOO_SMALL;
1482 }
Ronald Crone907e552021-01-18 13:22:38 +01001483
Ronald Cron9486f9d2020-11-26 13:36:23 +01001484 /* Set the key to empty now, so that even when there are errors, we always
1485 * set data_length to a value between 0 and data_size. On error, setting
1486 * the key to empty is a good choice because an empty key representation is
1487 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001488 *data_length = 0;
1489
Ronald Cron9486f9d2020-11-26 13:36:23 +01001490 /* Export requires the EXPORT flag. There is an exception for public keys,
1491 * which don't require any flag, but
1492 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1493 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001494 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1495 PSA_KEY_USAGE_EXPORT, 0);
1496 if (status != PSA_SUCCESS) {
1497 return status;
1498 }
mohammad160306e79202018-03-28 13:17:44 +03001499
Ryan Everette3e760c2024-01-19 16:03:50 +00001500 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1501
Ronald Crond18b5f82020-11-25 16:46:05 +01001502 psa_key_attributes_t attributes = {
1503 .core = slot->attr
1504 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001505 status = psa_driver_wrapper_export_key(&attributes,
1506 slot->key.data, slot->key.bytes,
1507 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001508
David Horstmann42015332024-03-13 15:42:31 +00001509#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everette3e760c2024-01-19 16:03:50 +00001510exit:
Ryan Everett3a4153a2024-01-25 12:04:55 +00001511#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001512 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001513
Ryan Everette3e760c2024-01-19 16:03:50 +00001514 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001515 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001516}
1517
Ronald Cron7285cda2020-11-26 14:46:37 +01001518psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001519 const psa_key_attributes_t *attributes,
1520 const uint8_t *key_buffer,
1521 size_t key_buffer_size,
1522 uint8_t *data,
1523 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001524 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001525{
Ronald Crond18b5f82020-11-25 16:46:05 +01001526 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001528 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1529 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001530 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001531 return psa_export_key_buffer_internal(
1532 key_buffer, key_buffer_size,
1533 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001534 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001536 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001537#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001538 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1539 return mbedtls_psa_rsa_export_public_key(attributes,
1540 key_buffer,
1541 key_buffer_size,
1542 data,
1543 data_size,
1544 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001545#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001546 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001547 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001548#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1549 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001550 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001551#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001552 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1553 return mbedtls_psa_ecp_export_public_key(attributes,
1554 key_buffer,
1555 key_buffer_size,
1556 data,
1557 data_size,
1558 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001559#else
1560 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001561 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001562#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1563 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001564 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001565 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001566 /* This shouldn't happen in the reference implementation, but
1567 it is valid for a special-purpose implementation to omit
1568 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001569 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001570 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001571}
Ronald Crond18b5f82020-11-25 16:46:05 +01001572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001573psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Ryan Everett30827912024-01-19 16:05:00 +00001574 uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001575 size_t data_size,
1576 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001577{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001578 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001579 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001580 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001581 psa_key_slot_t *slot;
Ryan Everett30827912024-01-19 16:05:00 +00001582 LOCAL_OUTPUT_DECLARE(data_external, data);
Darryl Greendd8fb772018-11-07 16:00:44 +00001583
Ronald Crone907e552021-01-18 13:22:38 +01001584 /* Reject a zero-length output buffer now, since this can never be a
1585 * valid key representation. This way we know that data must be a valid
1586 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001587 if (data_size == 0) {
1588 return PSA_ERROR_BUFFER_TOO_SMALL;
1589 }
Ronald Crone907e552021-01-18 13:22:38 +01001590
Darryl Greendd8fb772018-11-07 16:00:44 +00001591 /* Set the key to empty now, so that even when there are errors, we always
1592 * set data_length to a value between 0 and data_size. On error, setting
1593 * the key to empty is a good choice because an empty key representation is
1594 * unlikely to be accepted anywhere. */
1595 *data_length = 0;
1596
1597 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001598 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1599 if (status != PSA_SUCCESS) {
1600 return status;
1601 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001602
Ryan Everett30827912024-01-19 16:05:00 +00001603 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001605 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1606 status = PSA_ERROR_INVALID_ARGUMENT;
1607 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001608 }
1609
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001610 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001611 .core = slot->attr
1612 };
Ronald Cron67227982020-11-26 15:16:05 +01001613 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001614 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001615 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001616
1617exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001618 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001619
Ryan Everett30827912024-01-19 16:05:00 +00001620 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001621 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001622}
1623
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001624MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
Gilles Peskinef555a4e2024-06-12 19:13:19 +02001625 "One or more key attribute flag is listed as both external-only and dual-use");
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001626MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
Gilles Peskinef555a4e2024-06-12 19:13:19 +02001627 "One or more key attribute flag is listed as both internal-only and dual-use");
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001628MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
Gilles Peskinef555a4e2024-06-12 19:13:19 +02001629 "One or more key attribute flag is listed as both internal-only and external-only");
Gilles Peskine91e8c332019-08-02 19:19:39 +02001630
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001631/** Validate that a key policy is internally well-formed.
1632 *
1633 * This function only rejects invalid policies. It does not validate the
1634 * consistency of the policy with respect to other attributes of the key
1635 * such as the key type.
1636 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001637static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001638{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001639 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1640 PSA_KEY_USAGE_COPY |
1641 PSA_KEY_USAGE_ENCRYPT |
1642 PSA_KEY_USAGE_DECRYPT |
1643 PSA_KEY_USAGE_SIGN_MESSAGE |
1644 PSA_KEY_USAGE_VERIFY_MESSAGE |
1645 PSA_KEY_USAGE_SIGN_HASH |
1646 PSA_KEY_USAGE_VERIFY_HASH |
1647 PSA_KEY_USAGE_DERIVE)) != 0) {
1648 return PSA_ERROR_INVALID_ARGUMENT;
1649 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001651 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001652}
1653
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001654/** Validate the internal consistency of key attributes.
1655 *
1656 * This function only rejects invalid attribute values. If does not
1657 * validate the consistency of the attributes with any key data that may
1658 * be involved in the creation of the key.
1659 *
1660 * Call this function early in the key creation process.
1661 *
1662 * \param[in] attributes Key attributes for the new key.
1663 * \param[out] p_drv On any return, the driver for the key, if any.
1664 * NULL for a transparent key.
1665 *
1666 */
1667static psa_status_t psa_validate_key_attributes(
1668 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001669 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001670{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001671 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001672 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1673 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001675 status = psa_validate_key_location(lifetime, p_drv);
1676 if (status != PSA_SUCCESS) {
1677 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001678 }
1679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001680 status = psa_validate_key_persistence(lifetime);
1681 if (status != PSA_SUCCESS) {
1682 return status;
1683 }
1684
1685 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1686 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1687 return PSA_ERROR_INVALID_ARGUMENT;
1688 }
1689 } else {
1690 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1691 return PSA_ERROR_INVALID_ARGUMENT;
1692 }
1693 }
1694
1695 status = psa_validate_key_policy(&attributes->core.policy);
1696 if (status != PSA_SUCCESS) {
1697 return status;
1698 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001699
1700 /* Refuse to create overly large keys.
1701 * Note that this doesn't trigger on import if the attributes don't
1702 * explicitly specify a size (so psa_get_key_bits returns 0), so
1703 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001704 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1705 return PSA_ERROR_NOT_SUPPORTED;
1706 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001707
Gilles Peskine91e8c332019-08-02 19:19:39 +02001708 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001709 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1710 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1711 return PSA_ERROR_INVALID_ARGUMENT;
1712 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001714 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001715}
1716
Gilles Peskine4747d192019-04-17 15:05:45 +02001717/** Prepare a key slot to receive key material.
1718 *
1719 * This function allocates a key slot and sets its metadata.
1720 *
1721 * If this function fails, call psa_fail_key_creation().
1722 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001723 * This function is intended to be used as follows:
1724 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001725 * it with the specified attributes, and in case of a volatile key assign it
1726 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001727 * -# Populate the slot with the key material.
1728 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1729 * In case of failure at any step, stop the sequence and call
1730 * psa_fail_key_creation().
1731 *
Ronald Cron5c522922020-11-14 16:35:34 +01001732 * On success, the key slot is locked. It is the responsibility of the caller
1733 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001734 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001735 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001736 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001737 * \param[out] p_slot On success, a pointer to the prepared slot.
1738 * \param[out] p_drv On any return, the driver for the key, if any.
1739 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001740 *
1741 * \retval #PSA_SUCCESS
1742 * The key slot is ready to receive key material.
1743 * \return If this function fails, the key slot is an invalid state.
1744 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001745 */
1746static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001747 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001748 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001749 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001750 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001751{
1752 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001753 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001754 psa_key_slot_t *slot;
1755
Gilles Peskinedf179142019-07-15 22:02:14 +02001756 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001757 *p_drv = NULL;
1758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001759 status = psa_validate_key_attributes(attributes, p_drv);
1760 if (status != PSA_SUCCESS) {
1761 return status;
1762 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001764 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1765 if (status != PSA_SUCCESS) {
1766 return status;
1767 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001768 slot = *p_slot;
1769
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001770 /* We're storing the declared bit-size of the key. It's up to each
1771 * creation mechanism to verify that this information is correct.
1772 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001773 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001774 * is optional (import, copy). In case of a volatile key, assign it the
1775 * volatile key identifier associated to the slot returned to contain its
1776 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001777
1778 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001779 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001780#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1781 slot->attr.id = volatile_key_id;
1782#else
1783 slot->attr.id.key_id = volatile_key_id;
1784#endif
1785 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001786
Gilles Peskine91e8c332019-08-02 19:19:39 +02001787 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001788 * external-only flags, query `attributes`. Thanks to the check
1789 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001790 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001791 * may have set. */
1792 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001793
Gilles Peskinecbaff462019-07-12 23:46:04 +02001794#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001795 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001796 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001797 * create the key file in internal storage, create the
1798 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001799 * persistent data. This is done by starting a transaction that will
1800 * encompass these three actions.
1801 * For registering a volatile key, we just need to find an appropriate
1802 * slot number inside the SE. Since the key is designated volatile, creating
1803 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001804 /* The first thing to do is to find a slot number for the new key.
1805 * We save the slot number in persistent storage as part of the
1806 * transaction data. It will be needed to recover if the power
1807 * fails during the key creation process, to clean up on the secure
1808 * element side after restarting. Obtaining a slot number from the
1809 * secure element driver updates its persistent state, but we do not yet
1810 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001811 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001812 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001813 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001814 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1815 &slot_number);
1816 if (status != PSA_SUCCESS) {
1817 return status;
1818 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001820 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1821 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001822 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001823 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001824 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001825 status = psa_crypto_save_transaction();
1826 if (status != PSA_SUCCESS) {
1827 (void) psa_crypto_stop_transaction();
1828 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001829 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001830 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001831
1832 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001833 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Ryan Everett68a85e22023-11-16 16:44:13 +00001834 if (status != PSA_SUCCESS) {
Ryan Everett167ecba2023-11-20 12:16:18 +00001835 return status;
Ryan Everett68a85e22023-11-16 16:44:13 +00001836 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001837 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001839 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001840 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001841 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001842 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001843#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001845 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001846}
Gilles Peskine4747d192019-04-17 15:05:45 +02001847
1848/** Finalize the creation of a key once its key material has been set.
1849 *
1850 * This entails writing the key to persistent storage.
1851 *
1852 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001853 * See the documentation of psa_start_key_creation() for the intended use
1854 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001855 *
Ronald Cron5c522922020-11-14 16:35:34 +01001856 * If the finalization succeeds, the function unlocks the key slot (it was
1857 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1858 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001859 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001860 * \param[in,out] slot Pointer to the slot with key material.
1861 * \param[in] driver The secure element driver for the key,
1862 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001863 * \param[out] key On success, identifier of the key. Note that the
1864 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001865 *
1866 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001867 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001868 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1869 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1870 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1871 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1872 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1873 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001874 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001875 * \return If this function fails, the key slot is an invalid state.
1876 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001877 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001878static psa_status_t psa_finish_key_creation(
1879 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001880 psa_se_drv_table_entry_t *driver,
1881 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001882{
1883 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001884 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001885 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001886
1887#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001888 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001889#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001890 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001891 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001892 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001893 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001894
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001895 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1896 sizeof(data.slot_number),
1897 "Slot number size does not match psa_se_key_data_storage_t");
1898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001899 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1900 status = psa_save_persistent_key(&slot->attr,
1901 (uint8_t *) &data,
1902 sizeof(data));
1903 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001904#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1905 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001906 /* Key material is saved in export representation in the slot, so
1907 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001908 status = psa_save_persistent_key(&slot->attr,
1909 slot->key.data,
1910 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001911 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001912 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001913#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1914
Gilles Peskinecbaff462019-07-12 23:46:04 +02001915#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001916 /* Finish the transaction for a key creation. This does not
1917 * happen when registering an existing key. Detect this case
1918 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001919 * creation of a persistent key in a secure element requires a transaction,
1920 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001921 if (driver != NULL &&
1922 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1923 status = psa_save_se_persistent_data(driver);
1924 if (status != PSA_SUCCESS) {
1925 psa_destroy_persistent_key(slot->attr.id);
1926 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001927 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001928 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001929 }
1930#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001932 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001933 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001934 status = psa_unlock_key_slot(slot);
1935 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001936 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001937 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001938 }
Ronald Cron50972942020-11-14 11:28:25 +01001939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001940 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001941}
1942
1943/** Abort the creation of a key.
1944 *
1945 * You may call this function after calling psa_start_key_creation(),
1946 * or after psa_finish_key_creation() fails. In other circumstances, this
1947 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001948 * See the documentation of psa_start_key_creation() for the intended use
1949 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001950 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001951 * \param[in,out] slot Pointer to the slot with key material.
1952 * \param[in] driver The secure element driver for the key,
1953 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001954 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001955static void psa_fail_key_creation(psa_key_slot_t *slot,
1956 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001957{
Gilles Peskine011e4282019-06-26 18:34:38 +02001958 (void) driver;
1959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001960 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001961 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001962 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001963
1964#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001965 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001966 * element, and the failure happened later (when saving metadata
1967 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001968 * element.
1969 * https://github.com/ARMmbed/mbed-crypto/issues/217
1970 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001971
Gilles Peskined7729582019-08-05 15:55:54 +02001972 /* Abort the ongoing transaction if any (there may not be one if
1973 * the creation process failed before starting one, or if the
1974 * key creation is a registration of a key in a secure element).
1975 * Earlier functions must already have done what it takes to undo any
1976 * partial creation. All that's left is to update the transaction data
1977 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001978 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001979#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001981 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001982}
1983
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001984/** Validate optional attributes during key creation.
1985 *
1986 * Some key attributes are optional during key creation. If they are
1987 * specified in the attributes structure, check that they are consistent
1988 * with the data in the slot.
1989 *
1990 * This function should be called near the end of key creation, after
1991 * the slot in memory is fully populated but before saving persistent data.
1992 */
1993static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001994 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001995 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001996{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001997 if (attributes->core.type != 0) {
1998 if (attributes->core.type != slot->attr.type) {
1999 return PSA_ERROR_INVALID_ARGUMENT;
2000 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002001 }
2002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002003 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07002004#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002005 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2006 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002007 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002008 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002009 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002010
Ronald Cron90857082020-11-25 14:58:33 +01002011 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002012 slot->attr.type,
2013 slot->key.data,
2014 slot->key.bytes,
2015 &rsa);
2016 if (status != PSA_SUCCESS) {
2017 return status;
2018 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002020 mbedtls_mpi_init(&actual);
2021 mbedtls_mpi_init(&required);
2022 ret = mbedtls_rsa_export(rsa,
2023 NULL, NULL, NULL, NULL, &actual);
2024 mbedtls_rsa_free(rsa);
2025 mbedtls_free(rsa);
2026 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002027 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028 }
2029 ret = mbedtls_mpi_read_binary(&required,
2030 attributes->domain_parameters,
2031 attributes->domain_parameters_size);
2032 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002033 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002034 }
2035 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002036 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002037 }
2038rsa_exit:
2039 mbedtls_mpi_free(&actual);
2040 mbedtls_mpi_free(&required);
2041 if (ret != 0) {
2042 return mbedtls_to_psa_error(ret);
2043 }
2044 } else
John Durkop9814fa22020-11-04 12:28:15 -08002045#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2046 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002047 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002048 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002049 }
2050 }
2051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002052 if (attributes->core.bits != 0) {
2053 if (attributes->core.bits != slot->attr.bits) {
2054 return PSA_ERROR_INVALID_ARGUMENT;
2055 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002056 }
2057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002058 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002059}
2060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002061psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Ryan Everett6b970252024-01-19 16:02:59 +00002062 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002063 size_t data_length,
2064 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002065{
2066 psa_status_t status;
Ryan Everett6b970252024-01-19 16:02:59 +00002067 LOCAL_INPUT_DECLARE(data_external, data);
Gilles Peskine4747d192019-04-17 15:05:45 +02002068 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002069 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002070 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002071
Ronald Cron81709fc2020-11-14 12:10:32 +01002072 *key = MBEDTLS_SVC_KEY_ID_INIT;
2073
Gilles Peskine0f84d622019-09-12 19:03:13 +02002074 /* Reject zero-length symmetric keys (including raw data key objects).
2075 * This also rejects any key which might be encoded as an empty string,
2076 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002077 if (data_length == 0) {
2078 return PSA_ERROR_INVALID_ARGUMENT;
2079 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002080
Ryan Everett6b970252024-01-19 16:02:59 +00002081 LOCAL_INPUT_ALLOC(data_external, data_length, data);
2082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002083 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2084 &slot, &driver);
2085 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002086 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002087 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002088
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002089 /* In the case of a transparent key or an opaque key stored in local
2090 * storage (thus not in the case of generating a key in a secure element
2091 * or cryptoprocessor with storage), we have to allocate a buffer to
2092 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002093 if (slot->key.data == NULL) {
2094 status = psa_allocate_buffer_to_slot(slot, data_length);
2095 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002096 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002097 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002098 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002099
2100 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 status = psa_driver_wrapper_import_key(attributes,
2102 data, data_length,
2103 slot->key.data,
2104 slot->key.bytes,
2105 &slot->key.bytes, &bits);
2106 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002107 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002108 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002110 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002111 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002112 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002113 status = PSA_ERROR_INVALID_ARGUMENT;
2114 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002115 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002117 status = psa_validate_optional_attributes(slot, attributes);
2118 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002119 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002120 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002122 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002123exit:
Ryan Everett6b970252024-01-19 16:02:59 +00002124 LOCAL_INPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002125 if (status != PSA_SUCCESS) {
2126 psa_fail_key_creation(slot, driver);
2127 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002129 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002130}
2131
Gilles Peskined7729582019-08-05 15:55:54 +02002132#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2133psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002134 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002135{
2136 psa_status_t status;
2137 psa_key_slot_t *slot = NULL;
2138 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002139 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002140
2141 /* Leaving attributes unspecified is not currently supported.
2142 * It could make sense to query the key type and size from the
2143 * secure element, but not all secure elements support this
2144 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002145 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2146 return PSA_ERROR_NOT_SUPPORTED;
2147 }
2148 if (psa_get_key_bits(attributes) == 0) {
2149 return PSA_ERROR_NOT_SUPPORTED;
2150 }
Gilles Peskined7729582019-08-05 15:55:54 +02002151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2153 &slot, &driver);
2154 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002155 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002156 }
Gilles Peskined7729582019-08-05 15:55:54 +02002157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002158 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002159
2160exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002161 if (status != PSA_SUCCESS) {
2162 psa_fail_key_creation(slot, driver);
2163 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002164
Gilles Peskined7729582019-08-05 15:55:54 +02002165 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002166 psa_close_key(key);
2167 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002168}
2169#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002171static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2172 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002173{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002174 psa_status_t status = psa_copy_key_material_into_slot(target,
2175 source->key.data,
2176 source->key.bytes);
2177 if (status != PSA_SUCCESS) {
2178 return status;
2179 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002180
Steven Cooreman398aee52020-10-13 14:35:45 +02002181 target->attr.type = source->attr.type;
2182 target->attr.bits = source->attr.bits;
2183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002184 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002185}
2186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002187psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2188 const psa_key_attributes_t *specified_attributes,
2189 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002190{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002192 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002193 psa_key_slot_t *source_slot = NULL;
2194 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002195 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002196 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002197
Ronald Cron81709fc2020-11-14 12:10:32 +01002198 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2199
Ronald Cron5c522922020-11-14 16:35:34 +01002200 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002201 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2202 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002203 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002204 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002206 status = psa_validate_optional_attributes(source_slot,
2207 specified_attributes);
2208 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002209 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002210 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002212 status = psa_restrict_key_policy(source_slot->attr.type,
2213 &actual_attributes.core.policy,
2214 &source_slot->attr.policy);
2215 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002216 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002217 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002219 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2220 &target_slot, &driver);
2221 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002222 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002223 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002224
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002225#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002226 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002227 /* Copying to a secure element is not implemented yet. */
2228 status = PSA_ERROR_NOT_SUPPORTED;
2229 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002230 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002231#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002233 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002234 /*
2235 * Copying through an opaque driver is not implemented yet, consider
2236 * a lifetime with an external location as an invalid parameter for
2237 * now.
2238 */
2239 status = PSA_ERROR_INVALID_ARGUMENT;
2240 goto exit;
2241 }
2242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002243 status = psa_copy_key_material(source_slot, target_slot);
2244 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002245 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002246 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002248 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002249exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002250 if (status != PSA_SUCCESS) {
2251 psa_fail_key_creation(target_slot, driver);
2252 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002254 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002256 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002257}
2258
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002259
2260
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002261/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002262/* Message digests */
2263/****************************************************************/
2264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002265psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002267 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002268 if (operation->id == 0) {
2269 return PSA_SUCCESS;
2270 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002272 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002273 operation->id = 0;
2274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002275 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002276}
2277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002278psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2279 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002281 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002282
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002283 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002284 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002285 status = PSA_ERROR_BAD_STATE;
2286 goto exit;
2287 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002289 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002290 status = PSA_ERROR_INVALID_ARGUMENT;
2291 goto exit;
2292 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002293
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002294 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2295 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002296 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002298 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002299
2300exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002301 if (status != PSA_SUCCESS) {
2302 psa_hash_abort(operation);
2303 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002304
2305 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002306}
2307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002308psa_status_t psa_hash_update(psa_hash_operation_t *operation,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002309 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002310 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002311{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002312 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002313 LOCAL_INPUT_DECLARE(input_external, input);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002315 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002316 status = PSA_ERROR_BAD_STATE;
2317 goto exit;
2318 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002319
Steven Cooremanc8288352021-03-04 14:02:19 +01002320 /* Don't require hash implementations to behave correctly on a
2321 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002322 if (input_length == 0) {
2323 return PSA_SUCCESS;
2324 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002325
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002326 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002327 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002328
2329exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002330 if (status != PSA_SUCCESS) {
2331 psa_hash_abort(operation);
2332 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002333
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002334 LOCAL_INPUT_FREE(input_external, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002335 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336}
2337
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002338static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
Thomas Daubneycbf09212024-01-25 17:14:29 +00002339 uint8_t *hash,
2340 size_t hash_size,
2341 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002342{
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002343 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2344
Steven Cooremanfbe09282021-03-08 18:41:12 +01002345 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002346 if (operation->id == 0) {
2347 return PSA_ERROR_BAD_STATE;
2348 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002349
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002350 status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002351 operation, hash, hash_size, hash_length);
2352 psa_hash_abort(operation);
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002353
2354 return status;
2355}
2356
2357psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2358 uint8_t *hash_external,
2359 size_t hash_size,
2360 size_t *hash_length)
2361{
2362 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2363 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2364
2365 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2366 status = psa_hash_finish_internal(operation, hash, hash_size, hash_length);
2367
David Horstmann42015332024-03-13 15:42:31 +00002368#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002369exit:
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002370#endif
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002371 LOCAL_OUTPUT_FREE(hash_external, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002372 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002373}
2374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002375psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002376 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002377 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002378{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002379 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002380 size_t actual_hash_length;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002381 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2382 LOCAL_INPUT_DECLARE(hash_external, hash);
2383
2384 status = psa_hash_finish_internal(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002385 operation,
2386 actual_hash, sizeof(actual_hash),
2387 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002389 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002390 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002391 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002393 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002394 status = PSA_ERROR_INVALID_SIGNATURE;
2395 goto exit;
2396 }
2397
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002398 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002399 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002400 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002401 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002402
2403exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002404 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2405 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002406 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002407 }
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002408 LOCAL_INPUT_FREE(hash_external, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002409 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002410}
2411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002412psa_status_t psa_hash_compute(psa_algorithm_t alg,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002413 const uint8_t *input_external, size_t input_length,
2414 uint8_t *hash_external, size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002415 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002416{
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002417 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2418 LOCAL_INPUT_DECLARE(input_external, input);
2419 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2420
Steven Cooremanfbe09282021-03-08 18:41:12 +01002421 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002422 if (!PSA_ALG_IS_HASH(alg)) {
2423 return PSA_ERROR_INVALID_ARGUMENT;
2424 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002425
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002426 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2427 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2428 status = psa_driver_wrapper_hash_compute(alg, input, input_length,
Thomas Daubneycbf09212024-01-25 17:14:29 +00002429 hash, hash_size, hash_length);
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002430
David Horstmann42015332024-03-13 15:42:31 +00002431#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002432exit:
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002433#endif
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002434 LOCAL_INPUT_FREE(input_external, input);
2435 LOCAL_OUTPUT_FREE(hash_external, hash);
2436 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002437}
2438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002439psa_status_t psa_hash_compare(psa_algorithm_t alg,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002440 const uint8_t *input_external, size_t input_length,
2441 const uint8_t *hash_external, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002442{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002443 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002444 size_t actual_hash_length;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002445 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2446
2447 LOCAL_INPUT_DECLARE(input_external, input);
2448 LOCAL_INPUT_DECLARE(hash_external, hash);
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002450 if (!PSA_ALG_IS_HASH(alg)) {
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002451 status = PSA_ERROR_INVALID_ARGUMENT;
2452 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002453 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002454
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002455 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2456 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002457 alg, input, input_length,
2458 actual_hash, sizeof(actual_hash),
2459 &actual_hash_length);
2460 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002461 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002462 }
2463 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002464 status = PSA_ERROR_INVALID_SIGNATURE;
2465 goto exit;
2466 }
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002467
2468 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002469 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002470 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002471 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002472
2473exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002474 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002475
2476 LOCAL_INPUT_FREE(input_external, input);
2477 LOCAL_INPUT_FREE(hash_external, hash);
2478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002479 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002480}
2481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002482psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2483 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002484{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002485 if (source_operation->id == 0 ||
2486 target_operation->id != 0) {
2487 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002488 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002490 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2491 target_operation);
2492 if (status != PSA_SUCCESS) {
2493 psa_hash_abort(target_operation);
2494 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002496 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002497}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002498
2499
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002500/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002501/* MAC */
2502/****************************************************************/
2503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002504psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002505{
Steven Cooreman07897832021-03-19 18:28:56 +01002506 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002507 if (operation->id == 0) {
2508 return PSA_SUCCESS;
2509 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002511 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002512 operation->mac_size = 0;
2513 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002514 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002516 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002517}
2518
Ronald Cron1c650a12021-06-17 16:33:22 +02002519static psa_status_t psa_mac_finalize_alg_and_key_validation(
2520 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002521 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002522 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002523{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002524 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002525 psa_key_type_t key_type = psa_get_key_type(attributes);
2526 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002528 if (!PSA_ALG_IS_MAC(alg)) {
2529 return PSA_ERROR_INVALID_ARGUMENT;
2530 }
Steven Cooreman07897832021-03-19 18:28:56 +01002531
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002532 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002533 status = psa_mac_key_can_do(alg, key_type);
2534 if (status != PSA_SUCCESS) {
2535 return status;
2536 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002537
Steven Cooremana6474de2021-05-10 11:13:41 +02002538 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002539 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002541 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002542 /* A very short MAC is too short for security since it can be
2543 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2544 * so we make this our minimum, even though 32 bits is still
2545 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002546 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002547 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002549 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2550 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002551 /* It's impossible to "truncate" to a larger length than the full length
2552 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002553 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002554 }
2555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002556 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002557 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2558 * that is disabled in the compile-time configuration. The result can
2559 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2560 * configuration into account. In this case, force a return of
2561 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2562 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2563 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2564 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2565 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002566 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002567 }
2568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002569 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002570}
2571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002572static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2573 mbedtls_svc_key_id_t key,
2574 psa_algorithm_t alg,
2575 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002576{
2577 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2578 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002579 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002580 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002581
2582 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002583 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002584 status = PSA_ERROR_BAD_STATE;
2585 goto exit;
2586 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002587
2588 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002589 key,
2590 &slot,
2591 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2592 alg);
2593 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002594 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002595 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002596
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002597 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002598 .core = slot->attr
2599 };
2600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002601 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2602 &operation->mac_size);
2603 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002604 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002605 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002606
2607 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002608 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002609 if (is_sign) {
2610 status = psa_driver_wrapper_mac_sign_setup(operation,
2611 &attributes,
2612 slot->key.data,
2613 slot->key.bytes,
2614 alg);
2615 } else {
2616 status = psa_driver_wrapper_mac_verify_setup(operation,
2617 &attributes,
2618 slot->key.data,
2619 slot->key.bytes,
2620 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002621 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002622
Gilles Peskinefbfac682018-07-08 20:51:54 +02002623exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002624 if (status != PSA_SUCCESS) {
2625 psa_mac_abort(operation);
2626 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002627
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002628 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002630 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002631}
2632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002633psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2634 mbedtls_svc_key_id_t key,
2635 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002636{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002637 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002638}
2639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002640psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2641 mbedtls_svc_key_id_t key,
2642 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002643{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002644 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002645}
2646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002647psa_status_t psa_mac_update(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002648 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002649 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002650{
Thomas Daubney324f7de2024-01-18 13:18:58 +00002651 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2652 LOCAL_INPUT_DECLARE(input_external, input);
2653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002654 if (operation->id == 0) {
Thomas Daubney324f7de2024-01-18 13:18:58 +00002655 status = PSA_ERROR_BAD_STATE;
2656 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002657 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658
Steven Cooreman11743f92021-03-19 18:38:46 +01002659 /* Don't require hash implementations to behave correctly on a
2660 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002661 if (input_length == 0) {
Thomas Daubney324f7de2024-01-18 13:18:58 +00002662 status = PSA_SUCCESS;
2663 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002664 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002665
Thomas Daubney324f7de2024-01-18 13:18:58 +00002666 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2667 status = psa_driver_wrapper_mac_update(operation, input, input_length);
2668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002669 if (status != PSA_SUCCESS) {
2670 psa_mac_abort(operation);
2671 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002672
David Horstmann42015332024-03-13 15:42:31 +00002673#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney324f7de2024-01-18 13:18:58 +00002674exit:
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002675#endif
Thomas Daubney324f7de2024-01-18 13:18:58 +00002676 LOCAL_INPUT_FREE(input_external, input);
2677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002678 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002679}
2680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002681psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002682 uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002683 size_t mac_size,
2684 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002685{
Steven Cooreman87885df2021-03-19 19:04:39 +01002686 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2687 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002688 LOCAL_OUTPUT_DECLARE(mac_external, mac);
Thomas Daubney301491d2024-02-01 14:32:59 +00002689 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002691 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002692 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002693 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002694 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002696 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002697 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002698 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002699 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002700
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002701 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2702 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002703 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002704 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002705 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002706 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002708 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002709 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002710 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002711 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002713 status = psa_driver_wrapper_mac_sign_finish(operation,
2714 mac, operation->mac_size,
2715 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002716
Dave Rodgman478ab542021-06-25 09:09:02 +01002717exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002718 /* In case of success, set the potential excess room in the output buffer
2719 * to an invalid value, to avoid potentially leaking a longer MAC.
2720 * In case of error, set the output length and content to a safe default,
2721 * such that in case the caller misses an error check, the output would be
2722 * an unachievable MAC.
2723 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002724 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002725 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002726 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002727 }
mohammad16036df908f2018-04-02 08:34:15 -07002728
Thomas Daubney480347d2024-02-01 19:00:26 +00002729 if ((mac != NULL) && (mac_size > operation->mac_size)) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002730 memset(&mac[operation->mac_size], '!',
2731 mac_size - operation->mac_size);
2732 }
Ronald Cron882eb782021-06-18 13:05:48 +02002733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002734 abort_status = psa_mac_abort(operation);
Thomas Daubney324f7de2024-01-18 13:18:58 +00002735 LOCAL_OUTPUT_FREE(mac_external, mac);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002737 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002738}
2739
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002740psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002741 const uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002742 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743{
Steven Cooreman87885df2021-03-19 19:04:39 +01002744 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2745 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002746 LOCAL_INPUT_DECLARE(mac_external, mac);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002747
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002748 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002749 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002750 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002751 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002753 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002754 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002755 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002756 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002758 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002759 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002760 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002761 }
2762
Thomas Daubney324f7de2024-01-18 13:18:58 +00002763 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002764 status = psa_driver_wrapper_mac_verify_finish(operation,
2765 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002766
Dave Rodgman478ab542021-06-25 09:09:02 +01002767exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002768 abort_status = psa_mac_abort(operation);
Thomas Daubney324f7de2024-01-18 13:18:58 +00002769 LOCAL_INPUT_FREE(mac_external, mac);
mohammad16036df908f2018-04-02 08:34:15 -07002770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002771 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002772}
2773
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002774static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2775 psa_algorithm_t alg,
2776 const uint8_t *input,
2777 size_t input_length,
2778 uint8_t *mac,
2779 size_t mac_size,
2780 size_t *mac_length,
2781 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002782{
2783 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002784 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002785 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002786 psa_key_slot_t *slot;
2787 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002788
Ronald Crondbb86462021-06-17 17:17:20 +02002789 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002790 key,
2791 &slot,
2792 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2793 alg);
2794 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002795 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002796 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002797
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002798 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002799 .core = slot->attr
2800 };
2801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002802 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2803 &operation_mac_size);
2804 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002805 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002806 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002807
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002808 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002809 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002810 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002811 }
2812
2813 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002814 &attributes,
2815 slot->key.data, slot->key.bytes,
2816 alg,
2817 input, input_length,
2818 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002819
2820exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002821 /* In case of success, set the potential excess room in the output buffer
2822 * to an invalid value, to avoid potentially leaking a longer MAC.
2823 * In case of error, set the output length and content to a safe default,
2824 * such that in case the caller misses an error check, the output would be
2825 * an unachievable MAC.
2826 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002827 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002828 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002829 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002830 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002831 if (mac_size > operation_mac_size) {
2832 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2833 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002835 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002836
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002837 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002838}
2839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002840psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002841 psa_algorithm_t alg,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002842 const uint8_t *input_external,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002843 size_t input_length,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002844 uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002845 size_t mac_size,
2846 size_t *mac_length)
2847{
Thomas Daubney324f7de2024-01-18 13:18:58 +00002848 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2849 LOCAL_INPUT_DECLARE(input_external, input);
2850 LOCAL_OUTPUT_DECLARE(mac_external, mac);
2851
2852 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2853 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
2854 status = psa_mac_compute_internal(key, alg,
Thomas Daubneyf298f652024-01-30 12:15:36 +00002855 input, input_length,
2856 mac, mac_size, mac_length, 1);
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002857
David Horstmann42015332024-03-13 15:42:31 +00002858#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney324f7de2024-01-18 13:18:58 +00002859exit:
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002860#endif
Thomas Daubney324f7de2024-01-18 13:18:58 +00002861 LOCAL_INPUT_FREE(input_external, input);
2862 LOCAL_OUTPUT_FREE(mac_external, mac);
2863
2864 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002865}
2866
2867psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2868 psa_algorithm_t alg,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002869 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002870 size_t input_length,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002871 const uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002872 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002873{
2874 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002875 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2876 size_t actual_mac_length;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002877 LOCAL_INPUT_DECLARE(input_external, input);
2878 LOCAL_INPUT_DECLARE(mac_external, mac);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002879
Thomas Daubney324f7de2024-01-18 13:18:58 +00002880 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002881 status = psa_mac_compute_internal(key, alg,
2882 input, input_length,
2883 actual_mac, sizeof(actual_mac),
2884 &actual_mac_length, 0);
2885 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002886 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002887 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002889 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002890 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002891 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002892 }
Thomas Daubney324f7de2024-01-18 13:18:58 +00002893
2894 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002895 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002896 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002897 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002898 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002899
2900exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002901 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Thomas Daubney324f7de2024-01-18 13:18:58 +00002902 LOCAL_INPUT_FREE(input_external, input);
2903 LOCAL_INPUT_FREE(mac_external, mac);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002905 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002906}
Gilles Peskine20035e32018-02-03 22:44:14 +01002907
Gilles Peskine20035e32018-02-03 22:44:14 +01002908/****************************************************************/
2909/* Asymmetric cryptography */
2910/****************************************************************/
2911
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002912static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2913 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002914{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002915 if (input_is_message) {
2916 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2917 return PSA_ERROR_INVALID_ARGUMENT;
2918 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002920 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2921 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2922 return PSA_ERROR_INVALID_ARGUMENT;
2923 }
2924 }
2925 } else {
2926 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2927 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002928 }
2929 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002931 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002932}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002934static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2935 int input_is_message,
2936 psa_algorithm_t alg,
2937 const uint8_t *input,
2938 size_t input_length,
2939 uint8_t *signature,
2940 size_t signature_size,
2941 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002942{
2943 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2944 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002945 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002946 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002947
2948 *signature_length = 0;
2949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002950 status = psa_sign_verify_check_alg(input_is_message, alg);
2951 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002952 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002953 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002954
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002955 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002956 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002957 * buffer can in principle be empty since it doesn't actually have
2958 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002959 if (signature_size == 0) {
2960 return PSA_ERROR_BUFFER_TOO_SMALL;
2961 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002962
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002963 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002964 key, &slot,
2965 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2966 PSA_KEY_USAGE_SIGN_HASH,
2967 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002968
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002969 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002970 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002971 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002972
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002973 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002974 status = PSA_ERROR_INVALID_ARGUMENT;
2975 goto exit;
2976 }
2977
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002978 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002979 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002980 };
2981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002982 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002983 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002984 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002985 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002986 signature, signature_size, signature_length);
2987 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002988
2989 status = psa_driver_wrapper_sign_hash(
2990 &attributes, slot->key.data, slot->key.bytes,
2991 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002992 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002993 }
2994
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002995
2996exit:
2997 /* Fill the unused part of the output buffer (the whole buffer on error,
2998 * the trailing part on success) with something that isn't a valid signature
2999 * (barring an attack on the signature and deliberately-crafted input),
3000 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003001 if (status == PSA_SUCCESS) {
3002 memset(signature + *signature_length, '!',
3003 signature_size - *signature_length);
3004 } else {
3005 memset(signature, '!', signature_size);
3006 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003007 /* If signature_size is 0 then we have nothing to do. We must not call
3008 * memset because signature may be NULL in this case. */
3009
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003010 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003012 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003013}
3014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003015static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3016 int input_is_message,
3017 psa_algorithm_t alg,
3018 const uint8_t *input,
3019 size_t input_length,
3020 const uint8_t *signature,
3021 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003022{
3023 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3024 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3025 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003026
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003027 status = psa_sign_verify_check_alg(input_is_message, alg);
3028 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02003029 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003030 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003031
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003032 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003033 key, &slot,
3034 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3035 PSA_KEY_USAGE_VERIFY_HASH,
3036 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003037
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003038 if (status != PSA_SUCCESS) {
3039 return status;
3040 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003041
3042 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003043 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003044 };
3045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003046 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003047 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003048 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003049 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003050 signature, signature_length);
3051 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003052 status = psa_driver_wrapper_verify_hash(
3053 &attributes, slot->key.data, slot->key.bytes,
3054 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003055 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003056 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003058 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003060 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003061
3062}
3063
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003064psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003065 const psa_key_attributes_t *attributes,
3066 const uint8_t *key_buffer,
3067 size_t key_buffer_size,
3068 psa_algorithm_t alg,
3069 const uint8_t *input,
3070 size_t input_length,
3071 uint8_t *signature,
3072 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003073 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003074{
3075 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003077 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003078 size_t hash_length;
3079 uint8_t hash[PSA_HASH_MAX_SIZE];
3080
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003081 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082 PSA_ALG_SIGN_GET_HASH(alg),
3083 input, input_length,
3084 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003086 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003087 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003088 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003089
3090 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003091 attributes, key_buffer, key_buffer_size,
3092 alg, hash, hash_length,
3093 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003094 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003096 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003097}
3098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003099psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3100 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003101 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003102 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003103 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003104 size_t signature_size,
3105 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003106{
Thomas Daubney290aac42024-01-18 17:23:02 +00003107 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3108 LOCAL_INPUT_DECLARE(input_external, input);
3109 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3110
3111 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3112 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3113 status = psa_sign_internal(key, 1, alg, input, input_length, signature,
3114 signature_size, signature_length);
3115
David Horstmann42015332024-03-13 15:42:31 +00003116#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003117exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003118#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003119 LOCAL_INPUT_FREE(input_external, input);
3120 LOCAL_OUTPUT_FREE(signature_external, signature);
3121 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003122}
3123
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003124psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003125 const psa_key_attributes_t *attributes,
3126 const uint8_t *key_buffer,
3127 size_t key_buffer_size,
3128 psa_algorithm_t alg,
3129 const uint8_t *input,
3130 size_t input_length,
3131 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003132 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003133{
3134 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003136 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003137 size_t hash_length;
3138 uint8_t hash[PSA_HASH_MAX_SIZE];
3139
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003140 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003141 PSA_ALG_SIGN_GET_HASH(alg),
3142 input, input_length,
3143 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003145 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003146 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003147 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003148
3149 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003150 attributes, key_buffer, key_buffer_size,
3151 alg, hash, hash_length,
3152 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003153 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003155 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003156}
3157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003158psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3159 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003160 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003161 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003162 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003163 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003164{
Thomas Daubney290aac42024-01-18 17:23:02 +00003165 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3166 LOCAL_INPUT_DECLARE(input_external, input);
3167 LOCAL_INPUT_DECLARE(signature_external, signature);
3168
3169 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3170 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3171 status = psa_verify_internal(key, 1, alg, input, input_length, signature,
3172 signature_length);
3173
David Horstmann42015332024-03-13 15:42:31 +00003174#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003175exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003176#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003177 LOCAL_INPUT_FREE(input_external, input);
3178 LOCAL_INPUT_FREE(signature_external, signature);
3179
3180 return status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003181}
3182
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003183psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003184 const psa_key_attributes_t *attributes,
3185 const uint8_t *key_buffer, size_t key_buffer_size,
3186 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003187 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01003188{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003189 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3190 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3191 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003192#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003193 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3194 return mbedtls_psa_rsa_sign_hash(
3195 attributes,
3196 key_buffer, key_buffer_size,
3197 alg, hash, hash_length,
3198 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003199#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3200 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003201 } else {
3202 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003203 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003204 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3205 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003206#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003207 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3208 return mbedtls_psa_ecdsa_sign_hash(
3209 attributes,
3210 key_buffer, key_buffer_size,
3211 alg, hash, hash_length,
3212 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003213#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3214 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003215 } else {
3216 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003217 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003218 }
Ronald Cron4501c982021-03-19 20:09:32 +01003219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003220 (void) key_buffer;
3221 (void) key_buffer_size;
3222 (void) hash;
3223 (void) hash_length;
3224 (void) signature;
3225 (void) signature_size;
3226 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003228 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003229}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003231psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3232 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003233 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003234 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003235 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003236 size_t signature_size,
3237 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003238{
Thomas Daubney290aac42024-01-18 17:23:02 +00003239 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3240 LOCAL_INPUT_DECLARE(hash_external, hash);
3241 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3242
3243 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3244 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3245 status = psa_sign_internal(key, 0, alg, hash, hash_length, signature,
3246 signature_size, signature_length);
3247
David Horstmann42015332024-03-13 15:42:31 +00003248#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003249exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003250#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003251 LOCAL_INPUT_FREE(hash_external, hash);
3252 LOCAL_OUTPUT_FREE(signature_external, signature);
3253
3254 return status;
itayzafrir5c753392018-05-08 11:18:38 +03003255}
3256
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003257psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003258 const psa_key_attributes_t *attributes,
3259 const uint8_t *key_buffer, size_t key_buffer_size,
3260 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003261 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003262{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003263 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3264 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3265 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003266#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003267 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3268 return mbedtls_psa_rsa_verify_hash(
3269 attributes,
3270 key_buffer, key_buffer_size,
3271 alg, hash, hash_length,
3272 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003273#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3274 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003275 } else {
3276 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003277 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003278 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3279 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003280#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003281 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3282 return mbedtls_psa_ecdsa_verify_hash(
3283 attributes,
3284 key_buffer, key_buffer_size,
3285 alg, hash, hash_length,
3286 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003287#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3288 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003289 } else {
3290 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003291 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003292 }
Ronald Cron4501c982021-03-19 20:09:32 +01003293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003294 (void) key_buffer;
3295 (void) key_buffer_size;
3296 (void) hash;
3297 (void) hash_length;
3298 (void) signature;
3299 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003301 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003302}
3303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003304psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3305 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003306 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003307 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003308 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003309 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003310{
Thomas Daubney290aac42024-01-18 17:23:02 +00003311 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3312 LOCAL_INPUT_DECLARE(hash_external, hash);
3313 LOCAL_INPUT_DECLARE(signature_external, signature);
3314
3315 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3316 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3317 status = psa_verify_internal(key, 0, alg, hash, hash_length, signature,
3318 signature_length);
3319
David Horstmann42015332024-03-13 15:42:31 +00003320#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003321exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003322#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003323 LOCAL_INPUT_FREE(hash_external, hash);
3324 LOCAL_INPUT_FREE(signature_external, signature);
3325
3326 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003327}
3328
John Durkop0e005192020-10-31 22:06:54 -07003329#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003330static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3331 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003332{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003333 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3334 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3335 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3336 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003337}
John Durkop0e005192020-10-31 22:06:54 -07003338#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003340psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3341 psa_algorithm_t alg,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003342 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003343 size_t input_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003344 const uint8_t *salt_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003345 size_t salt_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003346 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003347 size_t output_size,
3348 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003349{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003350 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003351 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003352 psa_key_slot_t *slot;
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003353 LOCAL_INPUT_DECLARE(input_external, input);
3354 LOCAL_INPUT_DECLARE(salt_external, salt);
3355 LOCAL_OUTPUT_DECLARE(output_external, output);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003356
Darryl Green5cc689a2018-07-24 15:34:10 +01003357 (void) input;
3358 (void) input_length;
3359 (void) salt;
3360 (void) output;
3361 (void) output_size;
3362
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003363 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003365 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3366 return PSA_ERROR_INVALID_ARGUMENT;
3367 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003368
Ronald Cron5c522922020-11-14 16:35:34 +01003369 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003370 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3371 if (status != PSA_SUCCESS) {
3372 return status;
3373 }
3374 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3375 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003376 status = PSA_ERROR_INVALID_ARGUMENT;
3377 goto exit;
3378 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003380 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003381#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003382 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003383 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003384 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3385 slot->key.data,
3386 slot->key.bytes,
3387 &rsa);
3388 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003389 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003390 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003392 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003393 status = PSA_ERROR_BUFFER_TOO_SMALL;
3394 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003395 }
Ronald Cron7207d572021-09-08 14:28:35 +02003396#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3397 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003398 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3399 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3400 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003401 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003402#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003403 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003404 mbedtls_rsa_pkcs1_encrypt(rsa,
3405 mbedtls_psa_get_random,
3406 MBEDTLS_PSA_RANDOM_STATE,
3407 MBEDTLS_RSA_PUBLIC,
3408 input_length,
3409 input,
3410 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003411#else
3412 status = PSA_ERROR_NOT_SUPPORTED;
3413#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003414 } else
3415 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003416#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003417 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003418 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003419 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3420 mbedtls_psa_get_random,
3421 MBEDTLS_PSA_RANDOM_STATE,
3422 MBEDTLS_RSA_PUBLIC,
3423 salt, salt_length,
3424 input_length,
3425 input,
3426 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003427#else
3428 status = PSA_ERROR_NOT_SUPPORTED;
3429#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003430 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003431 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003432 }
Ronald Cron7207d572021-09-08 14:28:35 +02003433#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003434 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003435rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003436 if (status == PSA_SUCCESS) {
3437 *output_length = mbedtls_rsa_get_len(rsa);
3438 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003440 mbedtls_rsa_free(rsa);
3441 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003442#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003443 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003444 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003445 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003446 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003447
3448exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003449 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003450
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003451 LOCAL_INPUT_FREE(input_external, input);
3452 LOCAL_INPUT_FREE(salt_external, salt);
3453 LOCAL_OUTPUT_FREE(output_external, output);
3454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003455 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003456}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003458psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3459 psa_algorithm_t alg,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003460 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003461 size_t input_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003462 const uint8_t *salt_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003463 size_t salt_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003464 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003465 size_t output_size,
3466 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003467{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003468 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003469 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003470 psa_key_slot_t *slot;
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003471 LOCAL_INPUT_DECLARE(input_external, input);
3472 LOCAL_INPUT_DECLARE(salt_external, salt);
3473 LOCAL_OUTPUT_DECLARE(output_external, output);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003474
Darryl Green5cc689a2018-07-24 15:34:10 +01003475 (void) input;
3476 (void) input_length;
3477 (void) salt;
3478 (void) output;
3479 (void) output_size;
3480
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003481 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003483 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3484 return PSA_ERROR_INVALID_ARGUMENT;
3485 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003486
Ronald Cron5c522922020-11-14 16:35:34 +01003487 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003488 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3489 if (status != PSA_SUCCESS) {
3490 return status;
3491 }
3492 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003493 status = PSA_ERROR_INVALID_ARGUMENT;
3494 goto exit;
3495 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003497 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003498#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003499 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003500 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003501 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3502 slot->key.data,
3503 slot->key.bytes,
3504 &rsa);
3505 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003506 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003507 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003509 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003510 status = PSA_ERROR_INVALID_ARGUMENT;
3511 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003512 }
Ronald Cron7207d572021-09-08 14:28:35 +02003513#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3514 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003515 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3516 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3517 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003518 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003519#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003520 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003521 mbedtls_rsa_pkcs1_decrypt(rsa,
3522 mbedtls_psa_get_random,
3523 MBEDTLS_PSA_RANDOM_STATE,
3524 MBEDTLS_RSA_PRIVATE,
3525 output_length,
3526 input,
3527 output,
3528 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003529#else
3530 status = PSA_ERROR_NOT_SUPPORTED;
3531#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003532 } else
3533 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003534#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003535 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003536 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003537 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3538 mbedtls_psa_get_random,
3539 MBEDTLS_PSA_RANDOM_STATE,
3540 MBEDTLS_RSA_PRIVATE,
3541 salt, salt_length,
3542 output_length,
3543 input,
3544 output,
3545 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003546#else
3547 status = PSA_ERROR_NOT_SUPPORTED;
3548#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003549 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003550 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003551 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003552
Ronald Cron7207d572021-09-08 14:28:35 +02003553#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003554 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003555rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003556 mbedtls_rsa_free(rsa);
3557 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003558#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3559 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003560 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003561 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003562 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003563
3564exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003565 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003566
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003567 LOCAL_INPUT_FREE(input_external, input);
3568 LOCAL_INPUT_FREE(salt_external, salt);
3569 LOCAL_OUTPUT_FREE(output_external, output);
3570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003571 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003572}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003573
David Horstmann65bf12c2024-02-06 15:27:49 +00003574static psa_status_t psa_generate_random_internal(uint8_t *output,
3575 size_t output_size)
3576{
3577 GUARD_MODULE_INITIALIZED;
3578
3579 psa_status_t status;
3580
3581#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
3582
3583 size_t output_length = 0;
3584 status = mbedtls_psa_external_get_random(&global_data.rng,
3585 output, output_size,
3586 &output_length);
3587 if (status != PSA_SUCCESS) {
3588 goto exit;
3589 }
3590 /* Breaking up a request into smaller chunks is currently not supported
3591 * for the external RNG interface. */
3592 if (output_length != output_size) {
3593 status = PSA_ERROR_INSUFFICIENT_ENTROPY;
3594 goto exit;
3595 }
3596 status = PSA_SUCCESS;
3597
3598#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
3599
3600 while (output_size > 0) {
3601 size_t request_size =
3602 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
3603 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
3604 output_size);
3605 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
3606 output, request_size);
3607 if (ret != 0) {
3608 status = mbedtls_to_psa_error(ret);
3609 goto exit;
3610 }
3611 output_size -= request_size;
3612 output += request_size;
3613 }
3614 status = PSA_SUCCESS;
3615#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
3616
3617exit:
3618 return status;
3619}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003620
3621
mohammad1603503973b2018-03-12 15:59:30 +02003622/****************************************************************/
3623/* Symmetric cryptography */
3624/****************************************************************/
3625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003626static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3627 mbedtls_svc_key_id_t key,
3628 psa_algorithm_t alg,
3629 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003630{
3631 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3632 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003633 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003634 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003635 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3636 PSA_KEY_USAGE_ENCRYPT :
3637 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003638
3639 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003640 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003641 status = PSA_ERROR_BAD_STATE;
3642 goto exit;
3643 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003645 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003646 status = PSA_ERROR_INVALID_ARGUMENT;
3647 goto exit;
3648 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003650 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3651 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003652 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003653 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003654
Ronald Cron49fafa92021-03-10 08:34:23 +01003655 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003656 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003657 * so we only set it (in the driver wrapper) after resources have been
3658 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003659 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003660 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003661 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003662 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003663 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003664 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003665 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003666 }
3667 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003668
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003669 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003670 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003671 };
3672
Ronald Cronab99ac22020-10-01 16:38:28 +02003673 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003674 if (cipher_operation == MBEDTLS_ENCRYPT) {
3675 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3676 &attributes,
3677 slot->key.data,
3678 slot->key.bytes,
3679 alg);
3680 } else {
3681 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3682 &attributes,
3683 slot->key.data,
3684 slot->key.bytes,
3685 alg);
3686 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003687
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003688exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003689 if (status != PSA_SUCCESS) {
3690 psa_cipher_abort(operation);
3691 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003693 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003695 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003696}
3697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003698psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3699 mbedtls_svc_key_id_t key,
3700 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003701{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003702 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003703}
3704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003705psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3706 mbedtls_svc_key_id_t key,
3707 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003708{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003709 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003710}
3711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003712psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Gabor Mezei8677edd2024-02-07 18:10:13 +01003713 uint8_t *iv_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714 size_t iv_size,
3715 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003716{
Ronald Cron6d051732020-10-01 14:10:20 +02003717 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Sergey04eb7c02023-03-06 15:37:23 -07003718 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003719
Gabor Mezei8677edd2024-02-07 18:10:13 +01003720 LOCAL_OUTPUT_DECLARE(iv_external, iv);
3721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003722 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003723 status = PSA_ERROR_BAD_STATE;
3724 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003725 }
3726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003727 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003728 status = PSA_ERROR_BAD_STATE;
3729 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003730 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003731
Ronald Cronc423acb2021-07-05 12:31:44 +02003732 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003733 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003734 status = PSA_ERROR_BUFFER_TOO_SMALL;
3735 goto exit;
3736 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003738 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003739 status = PSA_ERROR_GENERIC_ERROR;
3740 goto exit;
3741 }
3742
Gabor Mezei8677edd2024-02-07 18:10:13 +01003743 LOCAL_OUTPUT_ALLOC(iv_external, default_iv_length, iv);
3744
Gabor Mezeif3c35042024-03-04 17:15:08 +01003745 status = psa_generate_random_internal(iv, default_iv_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003746 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003747 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003748 }
Ronald Cron5618a392021-03-26 09:52:26 +01003749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003750 status = psa_driver_wrapper_cipher_set_iv(operation,
Gabor Mezei8677edd2024-02-07 18:10:13 +01003751 iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003752
3753exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003754 if (status == PSA_SUCCESS) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003755 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003756 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003757 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003758 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003759 psa_cipher_abort(operation);
Gabor Mezei8677edd2024-02-07 18:10:13 +01003760 if (iv != NULL) {
3761 mbedtls_platform_zeroize(iv, default_iv_length);
3762 }
Ronald Cronc423acb2021-07-05 12:31:44 +02003763 }
Ronald Cron6d051732020-10-01 14:10:20 +02003764
Gabor Mezei8677edd2024-02-07 18:10:13 +01003765 LOCAL_OUTPUT_FREE(iv_external, iv);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003766 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003767}
3768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003769psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Gabor Mezei282bb532024-02-01 10:39:26 +01003770 const uint8_t *iv_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003771 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003772{
Ronald Cron6d051732020-10-01 14:10:20 +02003773 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003774
Gabor Mezei282bb532024-02-01 10:39:26 +01003775 LOCAL_INPUT_DECLARE(iv_external, iv);
3776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003777 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003778 status = PSA_ERROR_BAD_STATE;
3779 goto exit;
3780 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003782 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003783 status = PSA_ERROR_BAD_STATE;
3784 goto exit;
3785 }
Ronald Crona0d68172021-03-26 10:15:08 +01003786
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003787 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003788 status = PSA_ERROR_INVALID_ARGUMENT;
3789 goto exit;
3790 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003791
Gabor Mezei282bb532024-02-01 10:39:26 +01003792 LOCAL_INPUT_ALLOC(iv_external, iv_length, iv);
3793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003794 status = psa_driver_wrapper_cipher_set_iv(operation,
3795 iv,
3796 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003797
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003798exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003799 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003800 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003801 } else {
3802 psa_cipher_abort(operation);
3803 }
Gabor Mezei282bb532024-02-01 10:39:26 +01003804
3805 LOCAL_INPUT_FREE(iv_external, iv);
3806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003807 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003808}
3809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003810psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003811 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003812 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003813 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003814 size_t output_size,
3815 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003816{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003817 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003818
Gabor Mezei13a15c22024-01-24 16:56:00 +01003819 LOCAL_INPUT_DECLARE(input_external, input);
3820 LOCAL_OUTPUT_DECLARE(output_external, output);
3821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003822 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003823 status = PSA_ERROR_BAD_STATE;
3824 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003825 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003826
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003827 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003828 status = PSA_ERROR_BAD_STATE;
3829 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003830 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003831
Gabor Mezeied96d682024-01-31 17:45:29 +01003832 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gabor Mezeiff783e02024-02-29 10:08:16 +00003833 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezeied96d682024-01-31 17:45:29 +01003834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003835 status = psa_driver_wrapper_cipher_update(operation,
3836 input,
3837 input_length,
3838 output,
3839 output_size,
3840 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003841
3842exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003843 if (status != PSA_SUCCESS) {
3844 psa_cipher_abort(operation);
3845 }
Ronald Cron6d051732020-10-01 14:10:20 +02003846
Gabor Mezei13a15c22024-01-24 16:56:00 +01003847 LOCAL_INPUT_FREE(input_external, input);
3848 LOCAL_OUTPUT_FREE(output_external, output);
3849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003850 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003851}
3852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003853psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003854 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003855 size_t output_size,
3856 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003857{
David Saadab4ecc272019-02-14 13:48:10 +02003858 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003859
Gabor Mezei13a15c22024-01-24 16:56:00 +01003860 LOCAL_OUTPUT_DECLARE(output_external, output);
3861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003862 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003863 status = PSA_ERROR_BAD_STATE;
3864 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003865 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003866
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003867 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003868 status = PSA_ERROR_BAD_STATE;
3869 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003870 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003871
Gabor Mezeiff783e02024-02-29 10:08:16 +00003872 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezeied96d682024-01-31 17:45:29 +01003873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003874 status = psa_driver_wrapper_cipher_finish(operation,
3875 output,
3876 output_size,
3877 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003878
3879exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003880 if (status == PSA_SUCCESS) {
Gabor Mezei13a15c22024-01-24 16:56:00 +01003881 status = psa_cipher_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003882 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003883 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003884 (void) psa_cipher_abort(operation);
Steven Cooremanef8575e2020-09-11 11:44:50 +02003885 }
Gabor Mezei13a15c22024-01-24 16:56:00 +01003886
3887 LOCAL_OUTPUT_FREE(output_external, output);
3888
3889 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003890}
3891
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003892psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003893{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003894 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003895 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003896 * in use. It's ok to call abort on such an object, and there's
3897 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003898 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003899 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003900
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003901 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003902
Ronald Cron49fafa92021-03-10 08:34:23 +01003903 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003904 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003905 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003907 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003908}
3909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003910psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3911 psa_algorithm_t alg,
David Horstmann926193a2023-12-13 15:55:25 +00003912 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003913 size_t input_length,
David Horstmann926193a2023-12-13 15:55:25 +00003914 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003915 size_t output_size,
3916 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003917{
3918 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003919 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003920 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003921 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003922 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3923 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003924
David Horstmannb80e35a2023-12-14 18:12:47 +00003925 LOCAL_INPUT_DECLARE(input_external, input);
3926 LOCAL_OUTPUT_DECLARE(output_external, output);
3927
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003928 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003929 status = PSA_ERROR_INVALID_ARGUMENT;
3930 goto exit;
3931 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003933 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3934 PSA_KEY_USAGE_ENCRYPT,
3935 alg);
3936 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003937 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003938 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003939
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003940 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003941 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003942 };
3943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003944 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3945 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003946 status = PSA_ERROR_GENERIC_ERROR;
3947 goto exit;
3948 }
3949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003950 if (default_iv_length > 0) {
3951 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003952 status = PSA_ERROR_BUFFER_TOO_SMALL;
3953 goto exit;
3954 }
3955
David Horstmann65bf12c2024-02-06 15:27:49 +00003956 status = psa_generate_random_internal(local_iv, default_iv_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003957 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003958 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003959 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003960 }
3961
Gabor Mezeied96d682024-01-31 17:45:29 +01003962 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3963 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
3964
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003965 status = psa_driver_wrapper_cipher_encrypt(
3966 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003967 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003968 mbedtls_buffer_offset(output, default_iv_length),
3969 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003970
3971exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003972 unlock_status = psa_unlock_key_slot(slot);
3973 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003974 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003975 }
Ronald Cron16377072021-07-08 19:00:07 +02003976
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003977 if (status == PSA_SUCCESS) {
3978 if (default_iv_length > 0) {
3979 memcpy(output, local_iv, default_iv_length);
3980 }
3981 *output_length += default_iv_length;
3982 } else {
3983 *output_length = 0;
3984 }
3985
David Horstmannb80e35a2023-12-14 18:12:47 +00003986 LOCAL_INPUT_FREE(input_external, input);
3987 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003988
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003989 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003990}
3991
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003992psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3993 psa_algorithm_t alg,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003994 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003995 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003996 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003997 size_t output_size,
3998 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003999{
4000 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004001 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004002 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02004003 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02004004
Gabor Mezei13a15c22024-01-24 16:56:00 +01004005 LOCAL_INPUT_DECLARE(input_external, input);
4006 LOCAL_OUTPUT_DECLARE(output_external, output);
4007
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004008 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02004009 status = PSA_ERROR_INVALID_ARGUMENT;
4010 goto exit;
4011 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004012
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004013 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4014 PSA_KEY_USAGE_DECRYPT,
4015 alg);
4016 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004017 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004018 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004019
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004020 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004021 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004022 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004023
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004024 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02004025 status = PSA_ERROR_INVALID_ARGUMENT;
4026 goto exit;
4027 }
4028
Gabor Mezeied96d682024-01-31 17:45:29 +01004029 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4030 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4031
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004032 status = psa_driver_wrapper_cipher_decrypt(
4033 &attributes, slot->key.data, slot->key.bytes,
4034 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004035 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004036
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02004037exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004038 unlock_status = psa_unlock_key_slot(slot);
4039 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004040 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004041 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004043 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004044 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004045 }
Ronald Cron16377072021-07-08 19:00:07 +02004046
Gabor Mezei13a15c22024-01-24 16:56:00 +01004047 LOCAL_INPUT_FREE(input_external, input);
4048 LOCAL_OUTPUT_FREE(output_external, output);
4049
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004050 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004051}
4052
4053
mohammad16035955c982018-04-26 00:53:03 +03004054/****************************************************************/
4055/* AEAD */
4056/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004058psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4059 psa_algorithm_t alg,
David Horstmann21c1a942023-11-28 19:25:00 +00004060 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004061 size_t nonce_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004062 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004063 size_t additional_data_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004064 const uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004065 size_t plaintext_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004066 uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004067 size_t ciphertext_size,
4068 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004069{
Ronald Cron215633c2021-03-16 17:15:37 +01004070 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4071 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004072
David Horstmann21c1a942023-11-28 19:25:00 +00004073 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4074 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4075 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4076 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4077
mohammad1603f08a5502018-06-03 15:05:47 +03004078 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004080 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4081 return PSA_ERROR_NOT_SUPPORTED;
4082 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004083
Ronald Cron9a986162021-03-26 12:40:07 +01004084 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004085 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4086 if (status != PSA_SUCCESS) {
4087 return status;
4088 }
mohammad16035955c982018-04-26 00:53:03 +03004089
Ronald Cron215633c2021-03-16 17:15:37 +01004090 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004091 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004092 };
mohammad16035955c982018-04-26 00:53:03 +03004093
David Horstmann21c1a942023-11-28 19:25:00 +00004094 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4095 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4096 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4097 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4098
Ronald Cronde822812021-03-17 16:08:20 +01004099 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004100 &attributes, slot->key.data, slot->key.bytes,
4101 alg,
4102 nonce, nonce_length,
4103 additional_data, additional_data_length,
4104 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004105 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004107 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4108 memset(ciphertext, 0, ciphertext_size);
4109 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004110
David Horstmann21c1a942023-11-28 19:25:00 +00004111/* Exit label is only used for buffer copying, prevent unused warnings. */
David Horstmann42015332024-03-13 15:42:31 +00004112#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann21c1a942023-11-28 19:25:00 +00004113exit:
4114#endif
4115 LOCAL_INPUT_FREE(nonce_external, nonce);
4116 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4117 LOCAL_INPUT_FREE(plaintext_external, plaintext);
4118 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
4119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004120 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004122 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004123}
4124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004125psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4126 psa_algorithm_t alg,
David Horstmann6baf6e92023-11-28 19:43:53 +00004127 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004128 size_t nonce_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004129 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004130 size_t additional_data_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004131 const uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004132 size_t ciphertext_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004133 uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004134 size_t plaintext_size,
4135 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004136{
Ronald Cron215633c2021-03-16 17:15:37 +01004137 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4138 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004139
David Horstmann6baf6e92023-11-28 19:43:53 +00004140 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4141 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4142 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
4143 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
4144
Gilles Peskineee652a32018-06-01 19:23:52 +02004145 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004147 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4148 return PSA_ERROR_NOT_SUPPORTED;
4149 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004150
Ronald Cron9a986162021-03-26 12:40:07 +01004151 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004152 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4153 if (status != PSA_SUCCESS) {
4154 return status;
4155 }
mohammad16035955c982018-04-26 00:53:03 +03004156
Ronald Cron215633c2021-03-16 17:15:37 +01004157 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004158 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004159 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004160
David Horstmann6baf6e92023-11-28 19:43:53 +00004161 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4162 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
4163 additional_data);
4164 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
4165 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
4166
Ronald Cronde822812021-03-17 16:08:20 +01004167 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004168 &attributes, slot->key.data, slot->key.bytes,
4169 alg,
4170 nonce, nonce_length,
4171 additional_data, additional_data_length,
4172 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004173 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004175 if (status != PSA_SUCCESS && plaintext_size != 0) {
4176 memset(plaintext, 0, plaintext_size);
4177 }
mohammad160360a64d02018-06-03 17:20:42 +03004178
David Horstmann6baf6e92023-11-28 19:43:53 +00004179/* Exit label is only used for buffer copying, prevent unused warnings. */
David Horstmann42015332024-03-13 15:42:31 +00004180#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann6baf6e92023-11-28 19:43:53 +00004181exit:
4182#endif
4183 LOCAL_INPUT_FREE(nonce_external, nonce);
4184 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4185 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
4186 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
4187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004188 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004190 return status;
mohammad16035955c982018-04-26 00:53:03 +03004191}
4192
Gilles Peskinee59236f2018-01-27 23:32:46 +01004193/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004194/* Generators */
4195/****************************************************************/
4196
John Durkop07cc04a2020-11-16 22:08:34 -08004197#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4198 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4199 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4200#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02004201#endif /* At least one builtin KDF */
4202
4203#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4204 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4205 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4206static psa_status_t psa_key_derivation_start_hmac(
4207 psa_mac_operation_t *operation,
4208 psa_algorithm_t hash_alg,
4209 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004210 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02004211{
4212 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004214 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4215 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4216 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02004217
Steven Cooreman15f0d922021-05-07 14:14:37 +02004218 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004219 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02004220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004221 status = psa_driver_wrapper_mac_sign_setup(operation,
4222 &attributes,
4223 hmac_key, hmac_key_length,
4224 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02004225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004226 psa_reset_key_attributes(&attributes);
4227 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02004228}
4229#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004230
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004231#define HKDF_STATE_INIT 0 /* no input yet */
4232#define HKDF_STATE_STARTED 1 /* got salt */
4233#define HKDF_STATE_KEYED 2 /* got key */
4234#define HKDF_STATE_OUTPUT 3 /* output started */
4235
Gilles Peskinecbe66502019-05-16 16:59:18 +02004236static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004237 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004238{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004239 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4240 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4241 } else {
4242 return operation->alg;
4243 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004244}
4245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004246psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004247{
4248 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004249 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4250 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004251 /* The object has (apparently) been initialized but it is not
4252 * in use. It's ok to call abort on such an object, and there's
4253 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004254 } else
John Durkopd0321952020-10-29 21:37:36 -07004255#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004256 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4257 mbedtls_free(operation->ctx.hkdf.info);
4258 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4259 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004260#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4261#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4262 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004263 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4264 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4265 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4266 if (operation->ctx.tls12_prf.secret != NULL) {
4267 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
4268 operation->ctx.tls12_prf.secret_length);
4269 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004270 }
4271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004272 if (operation->ctx.tls12_prf.seed != NULL) {
4273 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
4274 operation->ctx.tls12_prf.seed_length);
4275 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01004276 }
4277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004278 if (operation->ctx.tls12_prf.label != NULL) {
4279 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
4280 operation->ctx.tls12_prf.label_length);
4281 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01004282 }
4283
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004284 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004285
4286 /* We leave the fields Ai and output_block to be erased safely by the
4287 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004288 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004289#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4290 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004291 {
4292 status = PSA_ERROR_BAD_STATE;
4293 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004294 mbedtls_platform_zeroize(operation, sizeof(*operation));
4295 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004296}
4297
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004298psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004299 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004300{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004301 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004302 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004303 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004304 }
4305
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004306 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004307 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004308}
4309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004310psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4311 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004312{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004313 if (operation->alg == 0) {
4314 return PSA_ERROR_BAD_STATE;
4315 }
4316 if (capacity > operation->capacity) {
4317 return PSA_ERROR_INVALID_ARGUMENT;
4318 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004319 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004320 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004321}
4322
John Durkopd0321952020-10-29 21:37:36 -07004323#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004324/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004325 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004326static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
4327 psa_algorithm_t hash_alg,
4328 uint8_t *output,
4329 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004330{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004331 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02004332 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004333 psa_status_t status;
4334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004335 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
4336 return PSA_ERROR_BAD_STATE;
4337 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004338 hkdf->state = HKDF_STATE_OUTPUT;
4339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004340 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004341 /* Copy what remains of the current block */
4342 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004343 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004344 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004345 }
4346 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004347 output += n;
4348 output_length -= n;
4349 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004350 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004351 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004352 }
Gilles Peskined54931c2018-07-17 21:06:59 +02004353 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004354 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004355 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004356 * object was corrupted or if this function is called directly
4357 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004358 if (hkdf->block_number == 0xff) {
4359 return PSA_ERROR_BAD_STATE;
4360 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004361
4362 /* We need a new block */
4363 ++hkdf->block_number;
4364 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004366 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4367 hash_alg,
4368 hkdf->prk,
4369 hash_length);
4370 if (status != PSA_SUCCESS) {
4371 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004372 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004373
4374 if (hkdf->block_number != 1) {
4375 status = psa_mac_update(&hkdf->hmac,
4376 hkdf->output_block,
4377 hash_length);
4378 if (status != PSA_SUCCESS) {
4379 return status;
4380 }
4381 }
4382 status = psa_mac_update(&hkdf->hmac,
4383 hkdf->info,
4384 hkdf->info_length);
4385 if (status != PSA_SUCCESS) {
4386 return status;
4387 }
4388 status = psa_mac_update(&hkdf->hmac,
4389 &hkdf->block_number, 1);
4390 if (status != PSA_SUCCESS) {
4391 return status;
4392 }
4393 status = psa_mac_sign_finish(&hkdf->hmac,
4394 hkdf->output_block,
4395 sizeof(hkdf->output_block),
4396 &hmac_output_length);
4397 if (status != PSA_SUCCESS) {
4398 return status;
4399 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004400 }
4401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004402 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004403}
John Durkop07cc04a2020-11-16 22:08:34 -08004404#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004405
John Durkop07cc04a2020-11-16 22:08:34 -08004406#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4407 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004408static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4409 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004410 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004411{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004412 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4413 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004414 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4415 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004416 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004417
Janos Follath7742fee2019-06-17 12:58:10 +01004418 /* We can't be wanting more output after block 0xff, otherwise
4419 * the capacity check in psa_key_derivation_output_bytes() would have
4420 * prevented this call. It could happen only if the operation
4421 * object was corrupted or if this function is called directly
4422 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004423 if (tls12_prf->block_number == 0xff) {
4424 return PSA_ERROR_CORRUPTION_DETECTED;
4425 }
Janos Follath7742fee2019-06-17 12:58:10 +01004426
4427 /* We need a new block */
4428 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004429 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004430
4431 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4432 *
4433 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4434 *
4435 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4436 * HMAC_hash(secret, A(2) + seed) +
4437 * HMAC_hash(secret, A(3) + seed) + ...
4438 *
4439 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004440 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004441 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004442 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004443 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004444 * is currently extracted as `output_block` and where i is
4445 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004446 */
4447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004448 status = psa_key_derivation_start_hmac(&hmac,
4449 hash_alg,
4450 tls12_prf->secret,
4451 tls12_prf->secret_length);
4452 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004453 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004454 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004455
4456 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004457 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004458 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4459 * the variable seed and in this instance means it in the context of the
4460 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004461 status = psa_mac_update(&hmac,
4462 tls12_prf->label,
4463 tls12_prf->label_length);
4464 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004465 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004466 }
4467 status = psa_mac_update(&hmac,
4468 tls12_prf->seed,
4469 tls12_prf->seed_length);
4470 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004471 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004472 }
4473 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004474 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004475 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4476 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004477 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004478 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004479 }
4480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004481 status = psa_mac_sign_finish(&hmac,
4482 tls12_prf->Ai, hash_length,
4483 &hmac_output_length);
4484 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004485 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004486 }
4487 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004488 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004489 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004490
4491 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004492 status = psa_key_derivation_start_hmac(&hmac,
4493 hash_alg,
4494 tls12_prf->secret,
4495 tls12_prf->secret_length);
4496 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004497 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004498 }
4499 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4500 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004501 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004502 }
4503 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4504 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004505 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004506 }
4507 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4508 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004509 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004510 }
4511 status = psa_mac_sign_finish(&hmac,
4512 tls12_prf->output_block, hash_length,
4513 &hmac_output_length);
4514 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004515 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004516 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004517
Janos Follath7742fee2019-06-17 12:58:10 +01004518
4519cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004520 cleanup_status = psa_mac_abort(&hmac);
4521 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004522 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004523 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004525 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004526}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004527
Janos Follath844eb0e2019-06-19 12:10:49 +01004528static psa_status_t psa_key_derivation_tls12_prf_read(
4529 psa_tls12_prf_key_derivation_t *tls12_prf,
4530 psa_algorithm_t alg,
4531 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004532 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004533{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004534 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4535 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004536 psa_status_t status;
4537 uint8_t offset, length;
4538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004539 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004540 case PSA_TLS12_PRF_STATE_LABEL_SET:
4541 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4542 break;
4543 case PSA_TLS12_PRF_STATE_OUTPUT:
4544 break;
4545 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004546 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004547 }
4548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004549 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004550 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004551 if (tls12_prf->left_in_block == 0) {
4552 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4553 alg);
4554 if (status != PSA_SUCCESS) {
4555 return status;
4556 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004557
4558 continue;
4559 }
4560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004561 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004562 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004563 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004564 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004565 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004566
4567 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004568 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004569 output += length;
4570 output_length -= length;
4571 tls12_prf->left_in_block -= length;
4572 }
4573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004574 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004575}
John Durkop07cc04a2020-11-16 22:08:34 -08004576#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4577 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004578
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004579psa_status_t psa_key_derivation_output_bytes(
4580 psa_key_derivation_operation_t *operation,
Ryan Everett08bd2462024-02-09 16:15:32 +00004581 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004582 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004583{
4584 psa_status_t status;
Ryan Everett08bd2462024-02-09 16:15:32 +00004585 LOCAL_OUTPUT_DECLARE(output_external, output);
4586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004587 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004588
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004589 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004590 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004591 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004592 }
4593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004594 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004595 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004596 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004597 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4598 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004599 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004600 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004601 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004602 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004603
4604 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
4605 if (output_length > operation->capacity) {
4606 operation->capacity = 0;
4607 /* Go through the error path to wipe all confidential data now
4608 * that the operation object is useless. */
4609 status = PSA_ERROR_INSUFFICIENT_DATA;
4610 goto exit;
4611 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004612 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004613
John Durkopd0321952020-10-29 21:37:36 -07004614#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004615 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4616 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4617 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4618 output, output_length);
4619 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004620#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4621#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4622 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4624 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4625 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4626 kdf_alg, output,
4627 output_length);
4628 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004629#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4630 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004631 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004632 (void) kdf_alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004633 status = PSA_ERROR_BAD_STATE;
4634 LOCAL_OUTPUT_FREE(output_external, output);
4635
4636 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004637 }
4638
4639exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004640 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004641 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004642 * This allows us to differentiate between exhausted operations and
4643 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4644 * operations. */
4645 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004646 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004647 operation->alg = alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004648 if (output != NULL) {
4649 memset(output, '!', output_length);
4650 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004651 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004652
4653 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004654 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004655}
4656
David Brown7807bf72021-02-09 16:28:23 -07004657#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004658static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004659{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004660 if (data_size >= 8) {
4661 mbedtls_des_key_set_parity(data);
4662 }
4663 if (data_size >= 16) {
4664 mbedtls_des_key_set_parity(data + 8);
4665 }
4666 if (data_size >= 24) {
4667 mbedtls_des_key_set_parity(data + 16);
4668 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004669}
David Brown7807bf72021-02-09 16:28:23 -07004670#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004671
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004672static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004673 psa_key_slot_t *slot,
4674 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004675 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004676{
4677 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004678 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004679 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004680 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004682 if (!key_type_is_raw_bytes(slot->attr.type)) {
4683 return PSA_ERROR_INVALID_ARGUMENT;
4684 }
4685 if (bits % 8 != 0) {
4686 return PSA_ERROR_INVALID_ARGUMENT;
4687 }
4688 data = mbedtls_calloc(1, bytes);
4689 if (data == NULL) {
4690 return PSA_ERROR_INSUFFICIENT_MEMORY;
4691 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004693 status = psa_key_derivation_output_bytes(operation, data, bytes);
4694 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004695 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004696 }
David Brown12ca5032021-02-11 11:02:00 -07004697#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004698 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4699 psa_des_set_key_parity(data, bytes);
4700 }
David Brown8107e312021-02-12 08:08:46 -07004701#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004703 status = psa_allocate_buffer_to_slot(slot, bytes);
4704 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004705 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004706 }
Ronald Crondd04d422020-11-28 15:14:42 +01004707
Ronald Cronbf33c932020-11-28 18:06:53 +01004708 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004709 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004710 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004711 };
4712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004713 status = psa_driver_wrapper_import_key(&attributes,
4714 data, bytes,
4715 slot->key.data,
4716 slot->key.bytes,
4717 &slot->key.bytes, &bits);
4718 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004719 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004720 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004721
4722exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004723 mbedtls_free(data);
4724 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004725}
4726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004727psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4728 psa_key_derivation_operation_t *operation,
4729 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004730{
4731 psa_status_t status;
4732 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004733 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004734
Ronald Cron81709fc2020-11-14 12:10:32 +01004735 *key = MBEDTLS_SVC_KEY_ID_INIT;
4736
Gilles Peskine0f84d622019-09-12 19:03:13 +02004737 /* Reject any attempt to create a zero-length key so that we don't
4738 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004739 if (psa_get_key_bits(attributes) == 0) {
4740 return PSA_ERROR_INVALID_ARGUMENT;
4741 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004743 if (operation->alg == PSA_ALG_NONE) {
4744 return PSA_ERROR_BAD_STATE;
4745 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004746
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004747 if (!operation->can_output_key) {
4748 return PSA_ERROR_NOT_PERMITTED;
4749 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004751 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4752 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004753#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004754 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004755 /* Deriving a key in a secure element is not implemented yet. */
4756 status = PSA_ERROR_NOT_SUPPORTED;
4757 }
4758#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004759 if (status == PSA_SUCCESS) {
4760 status = psa_generate_derived_key_internal(slot,
4761 attributes->core.bits,
4762 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004763 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004764 if (status == PSA_SUCCESS) {
4765 status = psa_finish_key_creation(slot, driver, key);
4766 }
4767 if (status != PSA_SUCCESS) {
4768 psa_fail_key_creation(slot, driver);
4769 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004771 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004772}
4773
Gilles Peskineeab56e42018-07-12 17:12:33 +02004774
4775
4776/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004777/* Key derivation */
4778/****************************************************************/
4779
Steven Cooreman932ffb72021-02-15 12:14:32 +01004780#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004781static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004782{
4783#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004784 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4785 return 1;
4786 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004787#endif
4788#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004789 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4790 return 1;
4791 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004792#endif
4793#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004794 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4795 return 1;
4796 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004797#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004798 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004799}
4800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004801static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004802{
4803 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004804 psa_status_t status = psa_hash_setup(&operation, alg);
4805 psa_hash_abort(&operation);
4806 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004807}
4808
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304809static psa_status_t psa_key_derivation_set_maximum_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004811 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004812{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004813 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4814 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4815 if (hash_size == 0) {
4816 return PSA_ERROR_NOT_SUPPORTED;
4817 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004818
4819 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4820 * we might fail later, which is somewhat unfriendly and potentially
4821 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004822 psa_status_t status = psa_hash_try_support(hash_alg);
4823 if (status != PSA_SUCCESS) {
4824 return status;
4825 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004826
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304827#if defined(PSA_WANT_ALG_HKDF)
4828 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4829 operation->capacity = 255 * hash_size;
4830 } else
4831#endif
4832#if defined(PSA_WANT_ALG_TLS12_PRF)
4833 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
4834 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4835 operation->capacity = SIZE_MAX;
4836 } else
4837#endif
4838#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
4839 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
4840 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4841 /* Master Secret is always 48 bytes
4842 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
4843 operation->capacity = 48U;
4844 } else
4845#endif
4846 {
4847 (void) hash_size;
4848 status = PSA_ERROR_NOT_SUPPORTED;
4849 }
4850 return status;
4851}
4852
4853
4854static psa_status_t psa_key_derivation_setup_kdf(
4855 psa_key_derivation_operation_t *operation,
4856 psa_algorithm_t kdf_alg)
4857{
4858 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4859 * initialisers for this union leave some bytes unspecified.) */
4860 memset(&operation->ctx, 0, sizeof(operation->ctx));
4861 /* Make sure that kdf_alg is a supported key derivation algorithm. */
4862 if (!is_kdf_alg_supported(kdf_alg)) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004863 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004864 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004865
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304866 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
4867 kdf_alg);
4868 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004869}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004870
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004871static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004872{
4873#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004874 if (alg == PSA_ALG_ECDH) {
4875 return PSA_SUCCESS;
4876 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004877#endif
4878 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004879 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004880}
John Durkop07cc04a2020-11-16 22:08:34 -08004881#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004883psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4884 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004885{
4886 psa_status_t status;
4887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004888 if (operation->alg != 0) {
4889 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004890 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004891
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004892 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4893 return PSA_ERROR_INVALID_ARGUMENT;
4894 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4895#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4896 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4897 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4898 status = psa_key_agreement_try_support(ka_alg);
4899 if (status != PSA_SUCCESS) {
4900 return status;
4901 }
4902 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4903#else
4904 return PSA_ERROR_NOT_SUPPORTED;
4905#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4906 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4907#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4908 status = psa_key_derivation_setup_kdf(operation, alg);
4909#else
4910 return PSA_ERROR_NOT_SUPPORTED;
4911#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4912 } else {
4913 return PSA_ERROR_INVALID_ARGUMENT;
4914 }
4915
4916 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004918 }
4919 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004920}
4921
John Durkopd0321952020-10-29 21:37:36 -07004922#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004923static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4924 psa_algorithm_t hash_alg,
4925 psa_key_derivation_step_t step,
4926 const uint8_t *data,
4927 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004928{
4929 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004930 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004931 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004932 if (hkdf->state != HKDF_STATE_INIT) {
4933 return PSA_ERROR_BAD_STATE;
4934 } else {
4935 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4936 hash_alg,
4937 data, data_length);
4938 if (status != PSA_SUCCESS) {
4939 return status;
4940 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004941 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004942 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004943 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004944 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004945 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004946 if (hkdf->state == HKDF_STATE_INIT) {
4947 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4948 hash_alg,
4949 NULL, 0);
4950 if (status != PSA_SUCCESS) {
4951 return status;
4952 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004953 hkdf->state = HKDF_STATE_STARTED;
4954 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004955 if (hkdf->state != HKDF_STATE_STARTED) {
4956 return PSA_ERROR_BAD_STATE;
4957 }
4958 status = psa_mac_update(&hkdf->hmac,
4959 data, data_length);
4960 if (status != PSA_SUCCESS) {
4961 return status;
4962 }
4963 status = psa_mac_sign_finish(&hkdf->hmac,
4964 hkdf->prk,
4965 sizeof(hkdf->prk),
4966 &data_length);
4967 if (status != PSA_SUCCESS) {
4968 return status;
4969 }
4970 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004971 hkdf->block_number = 0;
4972 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004973 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004974 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004975 if (hkdf->state == HKDF_STATE_OUTPUT) {
4976 return PSA_ERROR_BAD_STATE;
4977 }
4978 if (hkdf->info_set) {
4979 return PSA_ERROR_BAD_STATE;
4980 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004981 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004982 if (data_length != 0) {
4983 hkdf->info = mbedtls_calloc(1, data_length);
4984 if (hkdf->info == NULL) {
4985 return PSA_ERROR_INSUFFICIENT_MEMORY;
4986 }
4987 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004988 }
4989 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004990 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004991 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004992 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004993 }
4994}
John Durkop07cc04a2020-11-16 22:08:34 -08004995#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004996
John Durkop07cc04a2020-11-16 22:08:34 -08004997#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4998 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004999static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
5000 const uint8_t *data,
5001 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01005002{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005003 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
5004 return PSA_ERROR_BAD_STATE;
5005 }
Janos Follathf08e2652019-06-13 09:05:41 +01005006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005007 if (data_length != 0) {
5008 prf->seed = mbedtls_calloc(1, data_length);
5009 if (prf->seed == NULL) {
5010 return PSA_ERROR_INSUFFICIENT_MEMORY;
5011 }
Janos Follathf08e2652019-06-13 09:05:41 +01005012
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005013 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005014 prf->seed_length = data_length;
5015 }
Janos Follathf08e2652019-06-13 09:05:41 +01005016
Gilles Peskine43f958b2020-12-13 14:55:14 +01005017 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005019 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01005020}
5021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005022static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
5023 const uint8_t *data,
5024 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01005025{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005026 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
5027 return PSA_ERROR_BAD_STATE;
5028 }
Janos Follath81550542019-06-13 14:26:34 +01005029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005030 if (data_length != 0) {
5031 prf->secret = mbedtls_calloc(1, data_length);
5032 if (prf->secret == NULL) {
5033 return PSA_ERROR_INSUFFICIENT_MEMORY;
5034 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02005035
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005036 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02005037 prf->secret_length = data_length;
5038 }
Janos Follath81550542019-06-13 14:26:34 +01005039
Gilles Peskine43f958b2020-12-13 14:55:14 +01005040 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005041
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005042 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01005043}
5044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005045static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
5046 const uint8_t *data,
5047 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01005048{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005049 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
5050 return PSA_ERROR_BAD_STATE;
5051 }
Janos Follath63028dd2019-06-13 09:15:47 +01005052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005053 if (data_length != 0) {
5054 prf->label = mbedtls_calloc(1, data_length);
5055 if (prf->label == NULL) {
5056 return PSA_ERROR_INSUFFICIENT_MEMORY;
5057 }
Janos Follath63028dd2019-06-13 09:15:47 +01005058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005059 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005060 prf->label_length = data_length;
5061 }
Janos Follath63028dd2019-06-13 09:15:47 +01005062
Gilles Peskine43f958b2020-12-13 14:55:14 +01005063 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005064
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005065 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01005066}
5067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005068static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
5069 psa_key_derivation_step_t step,
5070 const uint8_t *data,
5071 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01005072{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005073 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01005074 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005075 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01005076 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005077 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01005078 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005079 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01005080 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005081 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01005082 }
5083}
John Durkop07cc04a2020-11-16 22:08:34 -08005084#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5085 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5086
5087#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5088static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5089 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005090 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005091 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08005092{
5093 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005094 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08005095 uint8_t *cur = pms;
5096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005097 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
5098 return PSA_ERROR_INVALID_ARGUMENT;
5099 }
John Durkop07cc04a2020-11-16 22:08:34 -08005100
5101 /* Quoting RFC 4279, Section 2:
5102 *
5103 * The premaster secret is formed as follows: if the PSK is N octets
5104 * long, concatenate a uint16 with the value N, N zero octets, a second
5105 * uint16 with the value N, and the PSK itself.
5106 */
5107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005108 *cur++ = MBEDTLS_BYTE_1(data_length);
5109 *cur++ = MBEDTLS_BYTE_0(data_length);
5110 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005111 cur += data_length;
5112 *cur++ = pms[0];
5113 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005114 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005115 cur += data_length;
5116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005117 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08005118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005119 mbedtls_platform_zeroize(pms, sizeof(pms));
5120 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08005121}
Janos Follath6660f0e2019-06-17 08:44:03 +01005122
5123static psa_status_t psa_tls12_prf_psk_to_ms_input(
5124 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005125 psa_key_derivation_step_t step,
5126 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005127 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01005128{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005129 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
5130 return psa_tls12_prf_psk_to_ms_set_key(prf,
5131 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01005132 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005134 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01005135}
John Durkop07cc04a2020-11-16 22:08:34 -08005136#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005137
Gilles Peskineb8965192019-09-24 16:21:10 +02005138/** Check whether the given key type is acceptable for the given
5139 * input step of a key derivation.
5140 *
5141 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5142 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5143 * Both secret and non-secret inputs can alternatively have the type
5144 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5145 * that the input was passed as a buffer rather than via a key object.
5146 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005147static int psa_key_derivation_check_input_type(
5148 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005149 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02005150{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005151 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005152 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005153 if (key_type == PSA_KEY_TYPE_DERIVE) {
5154 return PSA_SUCCESS;
5155 }
5156 if (key_type == PSA_KEY_TYPE_NONE) {
5157 return PSA_SUCCESS;
5158 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005159 break;
5160 case PSA_KEY_DERIVATION_INPUT_LABEL:
5161 case PSA_KEY_DERIVATION_INPUT_SALT:
5162 case PSA_KEY_DERIVATION_INPUT_INFO:
5163 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005164 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
5165 return PSA_SUCCESS;
5166 }
5167 if (key_type == PSA_KEY_TYPE_NONE) {
5168 return PSA_SUCCESS;
5169 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005170 break;
5171 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005172 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005173}
5174
Janos Follathb80a94e2019-06-12 15:54:46 +01005175static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005176 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005177 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005178 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005179 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005180 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005181{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005182 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005183 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005185 status = psa_key_derivation_check_input_type(step, key_type);
5186 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005187 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005188 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005189
John Durkopd0321952020-10-29 21:37:36 -07005190#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005191 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5192 status = psa_hkdf_input(&operation->ctx.hkdf,
5193 PSA_ALG_HKDF_GET_HASH(kdf_alg),
5194 step, data, data_length);
5195 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005196#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5197#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005198 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5199 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
5200 step, data, data_length);
5201 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005202#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5203#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005204 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5205 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
5206 step, data, data_length);
5207 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005208#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005209 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005210 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005211 (void) data;
5212 (void) data_length;
5213 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005214 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005215 }
5216
Gilles Peskine224b0d62019-09-23 18:13:17 +02005217exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005218 if (status != PSA_SUCCESS) {
5219 psa_key_derivation_abort(operation);
5220 }
5221 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005222}
5223
Janos Follath51f4a0f2019-06-14 11:35:55 +01005224psa_status_t psa_key_derivation_input_bytes(
5225 psa_key_derivation_operation_t *operation,
5226 psa_key_derivation_step_t step,
Ryan Everett6f682062024-02-09 16:18:39 +00005227 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005228 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005229{
Ryan Everett6f682062024-02-09 16:18:39 +00005230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5231 LOCAL_INPUT_DECLARE(data_external, data);
5232
5233 LOCAL_INPUT_ALLOC(data_external, data_length, data);
5234
5235 status = psa_key_derivation_input_internal(operation, step,
5236 PSA_KEY_TYPE_NONE,
5237 data, data_length);
David Horstmann42015332024-03-13 15:42:31 +00005238#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everett6f682062024-02-09 16:18:39 +00005239exit:
Ryan Everettd0d12fb2024-02-12 09:19:29 +00005240#endif
Ryan Everett6f682062024-02-09 16:18:39 +00005241 LOCAL_INPUT_FREE(data_external, data);
5242 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005243}
5244
Janos Follath51f4a0f2019-06-14 11:35:55 +01005245psa_status_t psa_key_derivation_input_key(
5246 psa_key_derivation_operation_t *operation,
5247 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005248 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005249{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005250 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005251 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005252 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005253
Ronald Cron5c522922020-11-14 16:35:34 +01005254 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005255 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5256 if (status != PSA_SUCCESS) {
5257 psa_key_derivation_abort(operation);
5258 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02005259 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005260
5261 /* Passing a key object as a SECRET input unlocks the permission
5262 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005263 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005264 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005265 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005267 status = psa_key_derivation_input_internal(operation,
5268 step, slot->attr.type,
5269 slot->key.data,
5270 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005272 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005274 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005275}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005276
5277
5278
5279/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005280/* Key agreement */
5281/****************************************************************/
5282
John Durkop6ba40d12020-11-10 08:50:04 -08005283#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005284static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
5285 size_t peer_key_length,
5286 const mbedtls_ecp_keypair *our_key,
5287 uint8_t *shared_secret,
5288 size_t shared_secret_size,
5289 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005290{
Steven Cooremand4867872020-08-05 16:31:39 +02005291 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005292 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005293 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005294 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005295 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
5296 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005297
Ronald Cron90857082020-11-25 14:58:33 +01005298 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005299 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5300 bits,
5301 peer_key,
5302 peer_key_length,
5303 &their_key);
5304 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005305 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005306 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005307
Jaeden Amero97271b32019-01-10 19:38:51 +00005308 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005309 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
5310 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005311 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005312 }
Jaeden Amero97271b32019-01-10 19:38:51 +00005313 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005314 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
5315 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005316 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005317 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005318
Jaeden Amero97271b32019-01-10 19:38:51 +00005319 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005320 mbedtls_ecdh_calc_secret(&ecdh,
5321 shared_secret_length,
5322 shared_secret, shared_secret_size,
5323 mbedtls_psa_get_random,
5324 MBEDTLS_PSA_RANDOM_STATE));
5325 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005326 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005327 }
5328 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005329 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005330 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005331
5332exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005333 if (status != PSA_SUCCESS) {
5334 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
5335 }
5336 mbedtls_ecdh_free(&ecdh);
5337 mbedtls_ecp_keypair_free(their_key);
5338 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02005339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005340 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005341}
John Durkop6ba40d12020-11-10 08:50:04 -08005342#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005343
Gilles Peskine01d718c2018-09-18 12:01:02 +02005344#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005346static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
5347 psa_key_slot_t *private_key,
5348 const uint8_t *peer_key,
5349 size_t peer_key_length,
5350 uint8_t *shared_secret,
5351 size_t shared_secret_size,
5352 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005353{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005354 mbedtls_ecp_keypair *ecp = NULL;
5355 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01005356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005357 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08005358#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005359 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005360 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
5361 return PSA_ERROR_INVALID_ARGUMENT;
5362 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005363 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005364 private_key->attr.type,
5365 private_key->attr.bits,
5366 private_key->key.data,
5367 private_key->key.bytes,
5368 &ecp);
5369 if (status != PSA_SUCCESS) {
5370 return status;
5371 }
5372 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
5373 ecp,
5374 shared_secret, shared_secret_size,
5375 shared_secret_length);
5376 mbedtls_ecp_keypair_free(ecp);
5377 mbedtls_free(ecp);
5378 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08005379#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005380 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005381 (void) ecp;
5382 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01005383 (void) private_key;
5384 (void) peer_key;
5385 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005386 (void) shared_secret;
5387 (void) shared_secret_size;
5388 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005389 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005390 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005391}
5392
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005393/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005394 * to potentially free embedded data structures and wipe confidential data.
5395 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005396static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5397 psa_key_derivation_step_t step,
5398 psa_key_slot_t *private_key,
5399 const uint8_t *peer_key,
5400 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005401{
5402 psa_status_t status;
5403 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5404 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005405 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005406
5407 /* Step 1: run the secret agreement algorithm to generate the shared
5408 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005409 status = psa_key_agreement_raw_internal(ka_alg,
5410 private_key,
5411 peer_key, peer_key_length,
5412 shared_secret,
5413 sizeof(shared_secret),
5414 &shared_secret_length);
5415 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005416 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005417 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005418
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005419 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005420 * the shared secret. A shared secret is permitted wherever a key
5421 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005422 status = psa_key_derivation_input_internal(operation, step,
5423 PSA_KEY_TYPE_DERIVE,
5424 shared_secret,
5425 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005426exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005427 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5428 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005429}
5430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005431psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5432 psa_key_derivation_step_t step,
5433 mbedtls_svc_key_id_t private_key,
Thomas Daubney9da359f2024-02-15 13:15:47 +00005434 const uint8_t *peer_key_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005435 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005436{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005437 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005438 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005439 psa_key_slot_t *slot;
Thomas Daubney9da359f2024-02-15 13:15:47 +00005440 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005442 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5443 return PSA_ERROR_INVALID_ARGUMENT;
5444 }
Ronald Cron5c522922020-11-14 16:35:34 +01005445 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005446 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5447 if (status != PSA_SUCCESS) {
5448 return status;
5449 }
Thomas Daubney9da359f2024-02-15 13:15:47 +00005450
Thomas Daubneyca928312024-03-12 17:53:30 +00005451 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005452 status = psa_key_agreement_internal(operation, step,
5453 slot,
5454 peer_key, peer_key_length);
Thomas Daubney9da359f2024-02-15 13:15:47 +00005455
David Horstmann42015332024-03-13 15:42:31 +00005456#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney9da359f2024-02-15 13:15:47 +00005457exit:
Thomas Daubney3c0c6b12024-02-15 14:25:08 +00005458#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005459 if (status != PSA_SUCCESS) {
5460 psa_key_derivation_abort(operation);
5461 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005462 /* If a private key has been added as SECRET, we allow the derived
5463 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005464 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005465 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005466 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005467 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005469 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005470
Thomas Daubney9da359f2024-02-15 13:15:47 +00005471 LOCAL_INPUT_FREE(peer_key_external, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005472 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005473}
5474
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005475psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5476 mbedtls_svc_key_id_t private_key,
Thomas Daubney43042762024-02-15 12:57:26 +00005477 const uint8_t *peer_key_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005478 size_t peer_key_length,
Thomas Daubney43042762024-02-15 12:57:26 +00005479 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005480 size_t output_size,
5481 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005482{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005483 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005484 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005485 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005486 size_t expected_length;
Thomas Daubney43042762024-02-15 12:57:26 +00005487 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
5488 LOCAL_OUTPUT_DECLARE(output_external, output);
Thomas Daubney0736df32024-02-21 12:28:20 +00005489 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005491 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005492 status = PSA_ERROR_INVALID_ARGUMENT;
5493 goto exit;
5494 }
Ronald Cron5c522922020-11-14 16:35:34 +01005495 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005496 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5497 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005498 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005499 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005500
Gilles Peskine42313fb2022-04-14 00:17:15 +02005501 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5502 * for the output size. The PSA specification only guarantees that this
5503 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5504 * but it might be nice to allow smaller buffers if the output fits.
5505 * At the time of writing this comment, with only ECDH implemented,
5506 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5507 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5508 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005509 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005510 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5511 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005512 status = PSA_ERROR_BUFFER_TOO_SMALL;
5513 goto exit;
5514 }
5515
Thomas Daubney43042762024-02-15 12:57:26 +00005516 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005517 status = psa_key_agreement_raw_internal(alg, slot,
5518 peer_key, peer_key_length,
5519 output, output_size,
5520 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005521
5522exit:
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005523 /* Check for successful allocation of output,
5524 * with an unsuccessful status. */
Thomas Daubney2ea8d8f2024-02-21 15:16:01 +00005525 if (output != NULL && status != PSA_SUCCESS) {
5526 /* If an error happens and is not handled properly, the output
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005527 * may be used as a key to protect sensitive data. Arrange for such
5528 * a key to be random, which is likely to result in decryption or
5529 * verification errors. This is better than filling the buffer with
5530 * some constant data such as zeros, which would result in the data
5531 * being protected with a reproducible, easily knowable key.
5532 */
David Horstmann65bf12c2024-02-06 15:27:49 +00005533 psa_generate_random_internal(output, output_size);
Thomas Daubney2ea8d8f2024-02-21 15:16:01 +00005534 *output_length = output_size;
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005535 }
5536
5537 if (output == NULL) {
Thomas Daubney0736df32024-02-21 12:28:20 +00005538 /* output allocation failed. */
5539 *output_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005540 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005542 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005543
Thomas Daubney43042762024-02-15 12:57:26 +00005544 LOCAL_INPUT_FREE(peer_key_external, peer_key);
5545 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005546 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005547}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005548
5549
5550/****************************************************************/
5551/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005552/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005553
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005554#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5555#include "mbedtls/entropy_poll.h"
5556#endif
5557
Gilles Peskine30524eb2020-11-13 17:02:26 +01005558/** Initialize the PSA random generator.
5559 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005560static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005561{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005562#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005563 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005564#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5565
Gilles Peskine30524eb2020-11-13 17:02:26 +01005566 /* Set default configuration if
5567 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005568 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005569 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005570 }
5571 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005572 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005573 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005575 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005576#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5577 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5578 /* The PSA entropy injection feature depends on using NV seed as an entropy
5579 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005580 mbedtls_entropy_add_source(&rng->entropy,
5581 mbedtls_nv_seed_poll, NULL,
5582 MBEDTLS_ENTROPY_BLOCK_SIZE,
5583 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005584#endif
5585
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005586 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005587#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005588}
5589
5590/** Deinitialize the PSA random generator.
5591 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005592static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005593{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005594#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005595 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005596#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005597 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5598 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005599#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005600}
5601
5602/** Seed the PSA random generator.
5603 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005604static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005605{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005606#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5607 /* Do nothing: the external RNG seeds itself. */
5608 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005609 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005610#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005611 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005612 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5613 drbg_seed, sizeof(drbg_seed) - 1);
5614 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005615#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005616}
5617
David Horstmann65bf12c2024-02-06 15:27:49 +00005618psa_status_t psa_generate_random(uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005619 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005620{
David Horstmann65bf12c2024-02-06 15:27:49 +00005621 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005622
David Horstmann65bf12c2024-02-06 15:27:49 +00005623 LOCAL_OUTPUT_DECLARE(output_external, output);
5624 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005625
David Horstmann65bf12c2024-02-06 15:27:49 +00005626 status = psa_generate_random_internal(output, output_size);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005627
David Horstmann42015332024-03-13 15:42:31 +00005628#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann65bf12c2024-02-06 15:27:49 +00005629exit:
David Horstmann10e44f32024-02-28 14:17:10 +00005630#endif
David Horstmann65bf12c2024-02-06 15:27:49 +00005631 LOCAL_OUTPUT_FREE(output_external, output);
5632 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005633}
5634
Gilles Peskine8814fc42020-12-14 15:33:44 +01005635/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005636 *
5637 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5638 * `psa_generate_random(...)`. The state parameter is ignored since the
5639 * PSA API doesn't support passing an explicit state.
5640 *
5641 * In the non-external case, psa_generate_random() calls an
5642 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5643 * and semantics as mbedtls_psa_get_random(). As an optimization,
5644 * instead of doing this back-and-forth between the PSA API and the
5645 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5646 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005647 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005648#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5649int mbedtls_psa_get_random(void *p_rng,
5650 unsigned char *output,
5651 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005652{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005653 /* This function takes a pointer to the RNG state because that's what
5654 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5655 * its own state internally and doesn't let the caller access that state.
5656 * So we just ignore the state parameter, and in practice we'll pass
5657 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005658 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005659 psa_status_t status = psa_generate_random(output, output_size);
5660 if (status == PSA_SUCCESS) {
5661 return 0;
5662 } else {
5663 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5664 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005665}
5666#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5667
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005668#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005669psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5670 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005671{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005672 if (global_data.initialized) {
5673 return PSA_ERROR_NOT_PERMITTED;
5674 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005675
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005676 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5677 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5678 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5679 return PSA_ERROR_INVALID_ARGUMENT;
5680 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005682 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005683}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005684#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005685
Ronald Cron3772afe2021-02-08 16:10:05 +01005686/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005687 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005688 * \param type The key type
5689 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005690 *
5691 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005692 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005693 * \retval #PSA_ERROR_INVALID_ARGUMENT
5694 * The size in bits of the key is not valid.
5695 * \retval #PSA_ERROR_NOT_SUPPORTED
5696 * The type and/or the size in bits of the key or the combination of
5697 * the two is not supported.
5698 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005699static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005700 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005701{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005702 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005704 if (key_type_is_raw_bytes(type)) {
5705 status = validate_unstructured_key_bit_size(type, bits);
5706 if (status != PSA_SUCCESS) {
5707 return status;
5708 }
5709 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005710#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005711 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5712 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5713 return PSA_ERROR_NOT_SUPPORTED;
5714 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005715
Ronald Cron01b2aba2020-10-05 09:42:02 +02005716 /* Accept only byte-aligned keys, for the same reasons as
5717 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005718 if (bits % 8 != 0) {
5719 return PSA_ERROR_NOT_SUPPORTED;
5720 }
5721 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005722#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005723
Ronald Cron5c4d3862020-12-07 11:07:24 +01005724#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005725 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005726 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005727 return PSA_SUCCESS;
5728 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005729#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005730 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005731 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005732 }
5733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005734 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005735}
5736
Ronald Cron55ed0592020-10-05 10:30:40 +02005737psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005738 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005739 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005740{
5741 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005742 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005744 if ((attributes->domain_parameters == NULL) &&
5745 (attributes->domain_parameters_size != 0)) {
5746 return PSA_ERROR_INVALID_ARGUMENT;
5747 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005749 if (key_type_is_raw_bytes(type)) {
5750 status = psa_generate_random(key_buffer, key_buffer_size);
5751 if (status != PSA_SUCCESS) {
5752 return status;
5753 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005754
David Brown12ca5032021-02-11 11:02:00 -07005755#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005756 if (type == PSA_KEY_TYPE_DES) {
5757 psa_des_set_key_parity(key_buffer, key_buffer_size);
5758 }
David Brown8107e312021-02-12 08:08:46 -07005759#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005760 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005761
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005762#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5763 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005764 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5765 return mbedtls_psa_rsa_generate_key(attributes,
5766 key_buffer,
5767 key_buffer_size,
5768 key_buffer_length);
5769 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005770#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5771 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005772
John Durkop9814fa22020-11-04 12:28:15 -08005773#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005774 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5775 return mbedtls_psa_ecp_generate_key(attributes,
5776 key_buffer,
5777 key_buffer_size,
5778 key_buffer_length);
5779 } else
John Durkop9814fa22020-11-04 12:28:15 -08005780#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005781 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005782 (void) key_buffer_length;
5783 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005784 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005786 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005787}
Darryl Green0c6575a2018-11-07 16:05:30 +00005788
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005789psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5790 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005791{
5792 psa_status_t status;
5793 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005794 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005795 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005796
Ronald Cron81709fc2020-11-14 12:10:32 +01005797 *key = MBEDTLS_SVC_KEY_ID_INIT;
5798
Gilles Peskine0f84d622019-09-12 19:03:13 +02005799 /* Reject any attempt to create a zero-length key so that we don't
5800 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005801 if (psa_get_key_bits(attributes) == 0) {
5802 return PSA_ERROR_INVALID_ARGUMENT;
5803 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005804
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005805 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005806 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5807 return PSA_ERROR_INVALID_ARGUMENT;
5808 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005810 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5811 &slot, &driver);
5812 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005813 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005814 }
Gilles Peskine11792082019-08-06 18:36:36 +02005815
Ronald Cron977c2472020-10-13 08:32:21 +02005816 /* In the case of a transparent key or an opaque key stored in local
5817 * storage (thus not in the case of generating a key in a secure element
5818 * or cryptoprocessor with storage), we have to allocate a buffer to
5819 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005820 if (slot->key.data == NULL) {
5821 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5822 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005823 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005824 attributes->core.type, attributes->core.bits);
5825 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005826 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005827 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005828
5829 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005830 attributes->core.type,
5831 attributes->core.bits);
5832 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005833 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005834 attributes, &key_buffer_size);
5835 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005836 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005837 }
Ronald Cron977c2472020-10-13 08:32:21 +02005838 }
Ronald Cron977c2472020-10-13 08:32:21 +02005839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005840 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5841 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005842 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005843 }
Ronald Cron977c2472020-10-13 08:32:21 +02005844 }
5845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005846 status = psa_driver_wrapper_generate_key(attributes,
5847 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005849 if (status != PSA_SUCCESS) {
5850 psa_remove_key_data_from_memory(slot);
5851 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005852
Gilles Peskine11792082019-08-06 18:36:36 +02005853exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005854 if (status == PSA_SUCCESS) {
5855 status = psa_finish_key_creation(slot, driver, key);
5856 }
5857 if (status != PSA_SUCCESS) {
5858 psa_fail_key_creation(slot, driver);
5859 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005861 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005862}
5863
Gilles Peskinee59236f2018-01-27 23:32:46 +01005864/****************************************************************/
5865/* Module setup */
5866/****************************************************************/
5867
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005868#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005869psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005870 void (* entropy_init)(mbedtls_entropy_context *ctx),
5871 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005872{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005873 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5874 return PSA_ERROR_BAD_STATE;
5875 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005876 global_data.rng.entropy_init = entropy_init;
5877 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005878 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005879}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005880#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005882void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005883{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005884 psa_wipe_all_key_slots();
5885 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5886 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005887 }
5888 /* Wipe all remaining data, including configuration.
5889 * In particular, this sets all state indicator to the value
5890 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005891 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005892
5893 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005894 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005895}
5896
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005897#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5898/** Recover a transaction that was interrupted by a power failure.
5899 *
5900 * This function is called during initialization, before psa_crypto_init()
5901 * returns. If this function returns a failure status, the initialization
5902 * fails.
5903 */
5904static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005905 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005906{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005907 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005908 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5909 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005910 /* TODO - fall through to the failure case until this
5911 * is implemented.
5912 * https://github.com/ARMmbed/mbed-crypto/issues/218
5913 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005914 default:
5915 /* We found an unsupported transaction in the storage.
5916 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005917 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005918 }
5919}
5920#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005922psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005923{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005924 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005925
Gilles Peskinec6b69072018-11-20 21:42:52 +01005926 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005927 if (global_data.initialized != 0) {
5928 return PSA_SUCCESS;
5929 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005930
Gilles Peskine30524eb2020-11-13 17:02:26 +01005931 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005932 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005933 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005934 status = mbedtls_psa_random_seed(&global_data.rng);
5935 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005936 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005937 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005938 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005940 status = psa_initialize_key_slots();
5941 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005942 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005943 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005944
Ronald Cron088d5d02021-04-10 16:57:30 +02005945 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005946 status = psa_driver_wrapper_init();
5947 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005948 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005949 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005950
Gilles Peskine4b734222019-07-24 15:56:31 +02005951#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005952 status = psa_crypto_load_transaction();
5953 if (status == PSA_SUCCESS) {
5954 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5955 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005956 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005957 }
5958 status = psa_crypto_stop_transaction();
5959 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005960 /* There's no transaction to complete. It's all good. */
5961 status = PSA_SUCCESS;
5962 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005963#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005964
Gilles Peskinec6b69072018-11-20 21:42:52 +01005965 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005966 global_data.initialized = 1;
5967
5968exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005969 if (status != PSA_SUCCESS) {
5970 mbedtls_psa_crypto_free();
5971 }
5972 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005973}
5974
David Horstmann114d8242023-12-07 18:39:17 +00005975/* Memory copying test hooks. These are called before input copy, after input
5976 * copy, before output copy and after output copy, respectively.
5977 * They are used by memory-poisoning tests to temporarily unpoison buffers
5978 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00005979#if defined(MBEDTLS_TEST_HOOKS)
5980void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5981void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5982void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5983void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5984#endif
5985
David Horstmanndf493552023-11-15 15:18:30 +00005986/** Copy from an input buffer to a local copy.
5987 *
5988 * \param[in] input Pointer to input buffer.
5989 * \param[in] input_len Length of the input buffer.
5990 * \param[out] input_copy Pointer to a local copy in which to store the input data.
5991 * \param[out] input_copy_len Length of the local copy buffer.
5992 * \return #PSA_SUCCESS, if the buffer was successfully
5993 * copied.
5994 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
5995 * copy is too small to hold contents of the
5996 * input buffer.
5997 */
5998MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00005999psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
6000 uint8_t *input_copy, size_t input_copy_len)
6001{
6002 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00006003 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00006004 }
6005
David Horstmann0760b152023-11-24 16:21:04 +00006006#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006007 if (psa_input_pre_copy_hook != NULL) {
6008 psa_input_pre_copy_hook(input, input_len);
6009 }
David Horstmann0760b152023-11-24 16:21:04 +00006010#endif
6011
David Horstmann660027f2023-11-15 17:33:47 +00006012 if (input_len > 0) {
6013 memcpy(input_copy, input, input_len);
6014 }
David Horstmann957f9802023-10-30 20:29:43 +00006015
David Horstmann0760b152023-11-24 16:21:04 +00006016#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006017 if (psa_input_post_copy_hook != NULL) {
6018 psa_input_post_copy_hook(input, input_len);
6019 }
David Horstmann0760b152023-11-24 16:21:04 +00006020#endif
6021
David Horstmann957f9802023-10-30 20:29:43 +00006022 return PSA_SUCCESS;
6023}
6024
David Horstmanndf493552023-11-15 15:18:30 +00006025/** Copy from a local output buffer into a user-supplied one.
6026 *
6027 * \param[in] output_copy Pointer to a local buffer containing the output.
6028 * \param[in] output_copy_len Length of the local buffer.
6029 * \param[out] output Pointer to user-supplied output buffer.
6030 * \param[out] output_len Length of the user-supplied output buffer.
6031 * \return #PSA_SUCCESS, if the buffer was successfully
6032 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00006033 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00006034 * user-supplied output buffer is too small to
6035 * hold the contents of the local buffer.
6036 */
6037MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00006038psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
6039 uint8_t *output, size_t output_len)
6040{
6041 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00006042 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00006043 }
David Horstmann660027f2023-11-15 17:33:47 +00006044
David Horstmann0760b152023-11-24 16:21:04 +00006045#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006046 if (psa_output_pre_copy_hook != NULL) {
6047 psa_output_pre_copy_hook(output, output_len);
6048 }
David Horstmann0760b152023-11-24 16:21:04 +00006049#endif
6050
David Horstmann660027f2023-11-15 17:33:47 +00006051 if (output_copy_len > 0) {
6052 memcpy(output, output_copy, output_copy_len);
6053 }
6054
David Horstmann0760b152023-11-24 16:21:04 +00006055#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006056 if (psa_output_post_copy_hook != NULL) {
6057 psa_output_post_copy_hook(output, output_len);
6058 }
David Horstmann0760b152023-11-24 16:21:04 +00006059#endif
6060
David Horstmann2bd296e2023-10-30 20:37:12 +00006061 return PSA_SUCCESS;
6062}
6063
David Horstmann81a0d572023-11-20 17:15:32 +00006064psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
6065 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006066{
6067 psa_status_t status;
6068
David Horstmann0d52c712023-11-23 15:50:37 +00006069 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006070
David Horstmann726bf052023-11-15 18:11:26 +00006071 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006072 return PSA_SUCCESS;
6073 }
6074
David Horstmann81a0d572023-11-20 17:15:32 +00006075 local_input->buffer = mbedtls_calloc(input_len, 1);
6076 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006077 /* Since we dealt with the zero-length case above, we know that
6078 * a NULL return value means a failure of allocation. */
6079 return PSA_ERROR_INSUFFICIENT_MEMORY;
6080 }
David Horstmann81a0d572023-11-20 17:15:32 +00006081 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006082
David Horstmann81a0d572023-11-20 17:15:32 +00006083 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006084
6085 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00006086 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006087 if (status != PSA_SUCCESS) {
6088 goto error;
6089 }
6090
6091 return PSA_SUCCESS;
6092
6093error:
David Horstmann81a0d572023-11-20 17:15:32 +00006094 mbedtls_free(local_input->buffer);
6095 local_input->buffer = NULL;
6096 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006097 return status;
6098}
6099
David Horstmann81a0d572023-11-20 17:15:32 +00006100void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00006101{
David Horstmann81a0d572023-11-20 17:15:32 +00006102 mbedtls_free(local_input->buffer);
6103 local_input->buffer = NULL;
6104 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00006105}
6106
David Horstmann1a76ab12023-11-20 12:54:09 +00006107psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
6108 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00006109{
David Horstmann0d52c712023-11-23 15:50:37 +00006110 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00006111
David Horstmann726bf052023-11-15 18:11:26 +00006112 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006113 return PSA_SUCCESS;
6114 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006115 local_output->buffer = mbedtls_calloc(output_len, 1);
6116 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006117 /* Since we dealt with the zero-length case above, we know that
6118 * a NULL return value means a failure of allocation. */
6119 return PSA_ERROR_INSUFFICIENT_MEMORY;
6120 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006121 local_output->length = output_len;
6122 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00006123
6124 return PSA_SUCCESS;
6125}
6126
David Horstmann1a76ab12023-11-20 12:54:09 +00006127psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00006128{
David Horstmann761761f2023-11-15 15:57:32 +00006129 psa_status_t status;
6130
David Horstmann1a76ab12023-11-20 12:54:09 +00006131 if (local_output->buffer == NULL) {
6132 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006133 return PSA_SUCCESS;
6134 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006135 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00006136 /* We have an internal copy but nothing to copy back to. */
6137 return PSA_ERROR_CORRUPTION_DETECTED;
6138 }
6139
David Horstmann1a76ab12023-11-20 12:54:09 +00006140 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
6141 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00006142 if (status != PSA_SUCCESS) {
6143 return status;
6144 }
David Horstmannaeeb2742023-11-08 17:44:18 +00006145
David Horstmann1a76ab12023-11-20 12:54:09 +00006146 mbedtls_free(local_output->buffer);
6147 local_output->buffer = NULL;
6148 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006149
6150 return PSA_SUCCESS;
6151}
6152
Gilles Peskinee59236f2018-01-27 23:32:46 +01006153#endif /* MBEDTLS_PSA_CRYPTO_C */