blob: 8b146114addec56b345fe0e7aa582bd489489f06 [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 Peskine37a4fcc2024-06-13 16:06:45 +02002152 /* Not usable with volatile keys, even with an appropriate location,
2153 * due to the API design.
2154 * https://github.com/Mbed-TLS/mbedtls/issues/9253
2155 */
2156 if (PSA_KEY_LIFETIME_IS_VOLATILE(psa_get_key_lifetime(attributes))) {
2157 return PSA_ERROR_INVALID_ARGUMENT;
2158 }
2159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002160 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2161 &slot, &driver);
2162 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002163 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002164 }
Gilles Peskined7729582019-08-05 15:55:54 +02002165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002166 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002167
2168exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002169 if (status != PSA_SUCCESS) {
2170 psa_fail_key_creation(slot, driver);
2171 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002172
Gilles Peskined7729582019-08-05 15:55:54 +02002173 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002174 psa_close_key(key);
2175 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002176}
2177#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002179static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2180 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002181{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002182 psa_status_t status = psa_copy_key_material_into_slot(target,
2183 source->key.data,
2184 source->key.bytes);
2185 if (status != PSA_SUCCESS) {
2186 return status;
2187 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002188
Steven Cooreman398aee52020-10-13 14:35:45 +02002189 target->attr.type = source->attr.type;
2190 target->attr.bits = source->attr.bits;
2191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002192 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002193}
2194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002195psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2196 const psa_key_attributes_t *specified_attributes,
2197 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002198{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002199 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002200 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002201 psa_key_slot_t *source_slot = NULL;
2202 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002203 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002204 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002205
Ronald Cron81709fc2020-11-14 12:10:32 +01002206 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2207
Ronald Cron5c522922020-11-14 16:35:34 +01002208 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002209 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2210 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002211 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002212 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002214 status = psa_validate_optional_attributes(source_slot,
2215 specified_attributes);
2216 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002217 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002218 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002220 status = psa_restrict_key_policy(source_slot->attr.type,
2221 &actual_attributes.core.policy,
2222 &source_slot->attr.policy);
2223 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002224 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002225 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002227 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2228 &target_slot, &driver);
2229 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002230 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002231 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002232
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002233#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002234 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002235 /* Copying to a secure element is not implemented yet. */
2236 status = PSA_ERROR_NOT_SUPPORTED;
2237 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002238 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002239#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002241 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002242 /*
2243 * Copying through an opaque driver is not implemented yet, consider
2244 * a lifetime with an external location as an invalid parameter for
2245 * now.
2246 */
2247 status = PSA_ERROR_INVALID_ARGUMENT;
2248 goto exit;
2249 }
2250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002251 status = psa_copy_key_material(source_slot, target_slot);
2252 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002253 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002254 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002256 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002257exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002258 if (status != PSA_SUCCESS) {
2259 psa_fail_key_creation(target_slot, driver);
2260 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002262 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002264 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002265}
2266
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002267
2268
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002269/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002270/* Message digests */
2271/****************************************************************/
2272
Gilles Peskineb6e362b2024-04-10 20:05:06 +02002273static int is_hash_supported(psa_algorithm_t alg)
2274{
2275 switch (alg) {
2276#if defined(PSA_WANT_ALG_MD2)
2277 case PSA_ALG_MD2:
2278 return 1;
2279#endif
2280#if defined(PSA_WANT_ALG_MD4)
2281 case PSA_ALG_MD4:
2282 return 1;
2283#endif
2284#if defined(PSA_WANT_ALG_MD5)
2285 case PSA_ALG_MD5:
2286 return 1;
2287#endif
2288#if defined(PSA_WANT_ALG_RIPEMD160)
2289 case PSA_ALG_RIPEMD160:
2290 return 1;
2291#endif
2292#if defined(PSA_WANT_ALG_SHA_1)
2293 case PSA_ALG_SHA_1:
2294 return 1;
2295#endif
2296#if defined(PSA_WANT_ALG_SHA_224)
2297 case PSA_ALG_SHA_224:
2298 return 1;
2299#endif
2300#if defined(PSA_WANT_ALG_SHA_256)
2301 case PSA_ALG_SHA_256:
2302 return 1;
2303#endif
2304#if defined(PSA_WANT_ALG_SHA_384)
2305 case PSA_ALG_SHA_384:
2306 return 1;
2307#endif
2308#if defined(PSA_WANT_ALG_SHA_512)
2309 case PSA_ALG_SHA_512:
2310 return 1;
2311#endif
2312 default:
2313 return 0;
2314 }
2315}
2316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002317psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002318{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002319 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002320 if (operation->id == 0) {
2321 return PSA_SUCCESS;
2322 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002324 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002325 operation->id = 0;
2326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002327 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002328}
2329
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002330psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2331 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002333 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002334
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002335 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002336 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002337 status = PSA_ERROR_BAD_STATE;
2338 goto exit;
2339 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002341 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002342 status = PSA_ERROR_INVALID_ARGUMENT;
2343 goto exit;
2344 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002345
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002346 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2347 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002348 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002350 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002351
2352exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002353 if (status != PSA_SUCCESS) {
2354 psa_hash_abort(operation);
2355 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002356
2357 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358}
2359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002360psa_status_t psa_hash_update(psa_hash_operation_t *operation,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002361 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002362 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002363{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002365 LOCAL_INPUT_DECLARE(input_external, input);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002367 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002368 status = PSA_ERROR_BAD_STATE;
2369 goto exit;
2370 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002371
Steven Cooremanc8288352021-03-04 14:02:19 +01002372 /* Don't require hash implementations to behave correctly on a
2373 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002374 if (input_length == 0) {
2375 return PSA_SUCCESS;
2376 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002377
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002378 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002379 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002380
2381exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002382 if (status != PSA_SUCCESS) {
2383 psa_hash_abort(operation);
2384 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002385
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002386 LOCAL_INPUT_FREE(input_external, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002387 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002388}
2389
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002390static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
Thomas Daubneycbf09212024-01-25 17:14:29 +00002391 uint8_t *hash,
2392 size_t hash_size,
2393 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002394{
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002395 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2396
Steven Cooremanfbe09282021-03-08 18:41:12 +01002397 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002398 if (operation->id == 0) {
2399 return PSA_ERROR_BAD_STATE;
2400 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002401
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002402 status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403 operation, hash, hash_size, hash_length);
2404 psa_hash_abort(operation);
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002405
2406 return status;
2407}
2408
2409psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2410 uint8_t *hash_external,
2411 size_t hash_size,
2412 size_t *hash_length)
2413{
2414 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2415 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2416
2417 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2418 status = psa_hash_finish_internal(operation, hash, hash_size, hash_length);
2419
David Horstmann42015332024-03-13 15:42:31 +00002420#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002421exit:
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002422#endif
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002423 LOCAL_OUTPUT_FREE(hash_external, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002424 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002425}
2426
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002427psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002428 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002429 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002430{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002431 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002432 size_t actual_hash_length;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002433 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2434 LOCAL_INPUT_DECLARE(hash_external, hash);
2435
2436 status = psa_hash_finish_internal(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002437 operation,
2438 actual_hash, sizeof(actual_hash),
2439 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002441 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002442 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002443 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002445 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002446 status = PSA_ERROR_INVALID_SIGNATURE;
2447 goto exit;
2448 }
2449
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002450 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002451 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002452 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002453 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002454
2455exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002456 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2457 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002458 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002459 }
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002460 LOCAL_INPUT_FREE(hash_external, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002461 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002462}
2463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002464psa_status_t psa_hash_compute(psa_algorithm_t alg,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002465 const uint8_t *input_external, size_t input_length,
2466 uint8_t *hash_external, size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002467 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002468{
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002469 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2470 LOCAL_INPUT_DECLARE(input_external, input);
2471 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2472
Steven Cooremanfbe09282021-03-08 18:41:12 +01002473 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002474 if (!PSA_ALG_IS_HASH(alg)) {
2475 return PSA_ERROR_INVALID_ARGUMENT;
2476 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002477
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002478 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2479 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2480 status = psa_driver_wrapper_hash_compute(alg, input, input_length,
Thomas Daubneycbf09212024-01-25 17:14:29 +00002481 hash, hash_size, hash_length);
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002482
David Horstmann42015332024-03-13 15:42:31 +00002483#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002484exit:
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002485#endif
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002486 LOCAL_INPUT_FREE(input_external, input);
2487 LOCAL_OUTPUT_FREE(hash_external, hash);
2488 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002489}
2490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002491psa_status_t psa_hash_compare(psa_algorithm_t alg,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002492 const uint8_t *input_external, size_t input_length,
2493 const uint8_t *hash_external, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002494{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002495 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002496 size_t actual_hash_length;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002497 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2498
2499 LOCAL_INPUT_DECLARE(input_external, input);
2500 LOCAL_INPUT_DECLARE(hash_external, hash);
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002501
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002502 if (!PSA_ALG_IS_HASH(alg)) {
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002503 status = PSA_ERROR_INVALID_ARGUMENT;
2504 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002505 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002506
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002507 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2508 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002509 alg, input, input_length,
2510 actual_hash, sizeof(actual_hash),
2511 &actual_hash_length);
2512 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002513 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002514 }
2515 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002516 status = PSA_ERROR_INVALID_SIGNATURE;
2517 goto exit;
2518 }
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002519
2520 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002521 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002522 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002523 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002524
2525exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002526 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002527
2528 LOCAL_INPUT_FREE(input_external, input);
2529 LOCAL_INPUT_FREE(hash_external, hash);
2530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002531 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002532}
2533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002534psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2535 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002536{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002537 if (source_operation->id == 0 ||
2538 target_operation->id != 0) {
2539 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002540 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002542 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2543 target_operation);
2544 if (status != PSA_SUCCESS) {
2545 psa_hash_abort(target_operation);
2546 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002548 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002549}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002550
2551
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002552/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002553/* MAC */
2554/****************************************************************/
2555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002556psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002557{
Steven Cooreman07897832021-03-19 18:28:56 +01002558 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002559 if (operation->id == 0) {
2560 return PSA_SUCCESS;
2561 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002563 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002564 operation->mac_size = 0;
2565 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002566 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002568 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002569}
2570
Ronald Cron1c650a12021-06-17 16:33:22 +02002571static psa_status_t psa_mac_finalize_alg_and_key_validation(
2572 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002573 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002574 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002576 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002577 psa_key_type_t key_type = psa_get_key_type(attributes);
2578 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002580 if (!PSA_ALG_IS_MAC(alg)) {
2581 return PSA_ERROR_INVALID_ARGUMENT;
2582 }
Steven Cooreman07897832021-03-19 18:28:56 +01002583
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002584 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002585 status = psa_mac_key_can_do(alg, key_type);
2586 if (status != PSA_SUCCESS) {
2587 return status;
2588 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002589
Steven Cooremana6474de2021-05-10 11:13:41 +02002590 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002591 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002593 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002594 /* A very short MAC is too short for security since it can be
2595 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2596 * so we make this our minimum, even though 32 bits is still
2597 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002598 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002599 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002601 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2602 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002603 /* It's impossible to "truncate" to a larger length than the full length
2604 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002605 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002606 }
2607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002608 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002609 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2610 * that is disabled in the compile-time configuration. The result can
2611 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2612 * configuration into account. In this case, force a return of
2613 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2614 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2615 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2616 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2617 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002618 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002619 }
2620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002621 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002622}
2623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002624static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2625 mbedtls_svc_key_id_t key,
2626 psa_algorithm_t alg,
2627 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002628{
2629 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2630 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002631 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002632 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002633
2634 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002635 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002636 status = PSA_ERROR_BAD_STATE;
2637 goto exit;
2638 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002639
2640 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002641 key,
2642 &slot,
2643 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2644 alg);
2645 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002646 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002647 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002648
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002649 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002650 .core = slot->attr
2651 };
2652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002653 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2654 &operation->mac_size);
2655 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002656 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002657 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002658
2659 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002660 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002661 if (is_sign) {
2662 status = psa_driver_wrapper_mac_sign_setup(operation,
2663 &attributes,
2664 slot->key.data,
2665 slot->key.bytes,
2666 alg);
2667 } else {
2668 status = psa_driver_wrapper_mac_verify_setup(operation,
2669 &attributes,
2670 slot->key.data,
2671 slot->key.bytes,
2672 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002673 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002674
Gilles Peskinefbfac682018-07-08 20:51:54 +02002675exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002676 if (status != PSA_SUCCESS) {
2677 psa_mac_abort(operation);
2678 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002680 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002682 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002683}
2684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002685psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2686 mbedtls_svc_key_id_t key,
2687 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002688{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002689 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002690}
2691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002692psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2693 mbedtls_svc_key_id_t key,
2694 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002695{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002696 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002697}
2698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002699psa_status_t psa_mac_update(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002700 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002701 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002702{
Thomas Daubney324f7de2024-01-18 13:18:58 +00002703 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2704 LOCAL_INPUT_DECLARE(input_external, input);
2705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002706 if (operation->id == 0) {
Thomas Daubney324f7de2024-01-18 13:18:58 +00002707 status = PSA_ERROR_BAD_STATE;
2708 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002709 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710
Steven Cooreman11743f92021-03-19 18:38:46 +01002711 /* Don't require hash implementations to behave correctly on a
2712 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002713 if (input_length == 0) {
Thomas Daubney324f7de2024-01-18 13:18:58 +00002714 status = PSA_SUCCESS;
2715 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002716 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002717
Thomas Daubney324f7de2024-01-18 13:18:58 +00002718 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2719 status = psa_driver_wrapper_mac_update(operation, input, input_length);
2720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002721 if (status != PSA_SUCCESS) {
2722 psa_mac_abort(operation);
2723 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002724
David Horstmann42015332024-03-13 15:42:31 +00002725#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney324f7de2024-01-18 13:18:58 +00002726exit:
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002727#endif
Thomas Daubney324f7de2024-01-18 13:18:58 +00002728 LOCAL_INPUT_FREE(input_external, input);
2729
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002730 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002731}
2732
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002733psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002734 uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002735 size_t mac_size,
2736 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002737{
Steven Cooreman87885df2021-03-19 19:04:39 +01002738 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2739 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002740 LOCAL_OUTPUT_DECLARE(mac_external, mac);
Thomas Daubney301491d2024-02-01 14:32:59 +00002741 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002743 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002744 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002745 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002746 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002747
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002748 if (!operation->is_sign) {
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 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002752
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002753 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2754 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002755 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002756 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002757 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002758 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002760 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002761 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002762 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002763 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002765 status = psa_driver_wrapper_mac_sign_finish(operation,
2766 mac, operation->mac_size,
2767 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002768
Dave Rodgman478ab542021-06-25 09:09:02 +01002769exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002770 /* In case of success, set the potential excess room in the output buffer
2771 * to an invalid value, to avoid potentially leaking a longer MAC.
2772 * In case of error, set the output length and content to a safe default,
2773 * such that in case the caller misses an error check, the output would be
2774 * an unachievable MAC.
2775 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002776 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002777 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002778 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002779 }
mohammad16036df908f2018-04-02 08:34:15 -07002780
Thomas Daubney480347d2024-02-01 19:00:26 +00002781 if ((mac != NULL) && (mac_size > operation->mac_size)) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002782 memset(&mac[operation->mac_size], '!',
2783 mac_size - operation->mac_size);
2784 }
Ronald Cron882eb782021-06-18 13:05:48 +02002785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002786 abort_status = psa_mac_abort(operation);
Thomas Daubney324f7de2024-01-18 13:18:58 +00002787 LOCAL_OUTPUT_FREE(mac_external, mac);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002788
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002789 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002790}
2791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002792psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002793 const uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002794 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002795{
Steven Cooreman87885df2021-03-19 19:04:39 +01002796 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2797 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002798 LOCAL_INPUT_DECLARE(mac_external, mac);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002800 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002801 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002802 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002803 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002805 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002806 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002807 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002808 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002810 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002811 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002812 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002813 }
2814
Thomas Daubney324f7de2024-01-18 13:18:58 +00002815 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002816 status = psa_driver_wrapper_mac_verify_finish(operation,
2817 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002818
Dave Rodgman478ab542021-06-25 09:09:02 +01002819exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002820 abort_status = psa_mac_abort(operation);
Thomas Daubney324f7de2024-01-18 13:18:58 +00002821 LOCAL_INPUT_FREE(mac_external, mac);
mohammad16036df908f2018-04-02 08:34:15 -07002822
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002823 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002824}
2825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002826static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2827 psa_algorithm_t alg,
2828 const uint8_t *input,
2829 size_t input_length,
2830 uint8_t *mac,
2831 size_t mac_size,
2832 size_t *mac_length,
2833 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002834{
2835 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002836 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002837 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002838 psa_key_slot_t *slot;
2839 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002840
Ronald Crondbb86462021-06-17 17:17:20 +02002841 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002842 key,
2843 &slot,
2844 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2845 alg);
2846 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002847 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002848 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002849
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002850 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002851 .core = slot->attr
2852 };
2853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002854 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2855 &operation_mac_size);
2856 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002857 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002858 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002859
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002860 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002861 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002862 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002863 }
2864
2865 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002866 &attributes,
2867 slot->key.data, slot->key.bytes,
2868 alg,
2869 input, input_length,
2870 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002871
2872exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002873 /* In case of success, set the potential excess room in the output buffer
2874 * to an invalid value, to avoid potentially leaking a longer MAC.
2875 * In case of error, set the output length and content to a safe default,
2876 * such that in case the caller misses an error check, the output would be
2877 * an unachievable MAC.
2878 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002879 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002880 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002881 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002882 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002883 if (mac_size > operation_mac_size) {
2884 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2885 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002887 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002889 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002890}
2891
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002892psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002893 psa_algorithm_t alg,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002894 const uint8_t *input_external,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002895 size_t input_length,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002896 uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002897 size_t mac_size,
2898 size_t *mac_length)
2899{
Thomas Daubney324f7de2024-01-18 13:18:58 +00002900 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2901 LOCAL_INPUT_DECLARE(input_external, input);
2902 LOCAL_OUTPUT_DECLARE(mac_external, mac);
2903
2904 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2905 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
2906 status = psa_mac_compute_internal(key, alg,
Thomas Daubneyf298f652024-01-30 12:15:36 +00002907 input, input_length,
2908 mac, mac_size, mac_length, 1);
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002909
David Horstmann42015332024-03-13 15:42:31 +00002910#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney324f7de2024-01-18 13:18:58 +00002911exit:
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002912#endif
Thomas Daubney324f7de2024-01-18 13:18:58 +00002913 LOCAL_INPUT_FREE(input_external, input);
2914 LOCAL_OUTPUT_FREE(mac_external, mac);
2915
2916 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002917}
2918
2919psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2920 psa_algorithm_t alg,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002921 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 size_t input_length,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002923 const uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002924 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002925{
2926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002927 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2928 size_t actual_mac_length;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002929 LOCAL_INPUT_DECLARE(input_external, input);
2930 LOCAL_INPUT_DECLARE(mac_external, mac);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002931
Thomas Daubney324f7de2024-01-18 13:18:58 +00002932 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002933 status = psa_mac_compute_internal(key, alg,
2934 input, input_length,
2935 actual_mac, sizeof(actual_mac),
2936 &actual_mac_length, 0);
2937 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002938 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002939 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002941 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002942 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002943 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002944 }
Thomas Daubney324f7de2024-01-18 13:18:58 +00002945
2946 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002947 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002948 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002949 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002950 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002951
2952exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002953 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Thomas Daubney324f7de2024-01-18 13:18:58 +00002954 LOCAL_INPUT_FREE(input_external, input);
2955 LOCAL_INPUT_FREE(mac_external, mac);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002956
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002957 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002958}
Gilles Peskine20035e32018-02-03 22:44:14 +01002959
Gilles Peskine20035e32018-02-03 22:44:14 +01002960/****************************************************************/
2961/* Asymmetric cryptography */
2962/****************************************************************/
2963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002964static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2965 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002966{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002967 if (input_is_message) {
2968 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2969 return PSA_ERROR_INVALID_ARGUMENT;
2970 }
Gilles Peskineb6e362b2024-04-10 20:05:06 +02002971 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002972
Gilles Peskineb6e362b2024-04-10 20:05:06 +02002973 psa_algorithm_t hash_alg = 0;
2974 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2975 hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
2976 }
2977
2978 /* Now hash_alg==0 if alg by itself doesn't need a hash.
2979 * This is good enough for sign-hash, but a guaranteed failure for
2980 * sign-message which needs to hash first for all algorithms
2981 * supported at the moment. */
2982
2983 if (hash_alg == 0 && input_is_message) {
2984 return PSA_ERROR_INVALID_ARGUMENT;
2985 }
2986 if (hash_alg == PSA_ALG_ANY_HASH) {
2987 return PSA_ERROR_INVALID_ARGUMENT;
2988 }
2989 /* Give up immediately if the hash is not supported. This has
2990 * several advantages:
2991 * - For mechanisms that don't use the hash at all (e.g.
2992 * ECDSA verification, randomized ECDSA signature), without
2993 * this check, the operation would succeed even though it has
2994 * been given an invalid argument. This would not be insecure
2995 * since the hash was not necessary, but it would be weird.
2996 * - For mechanisms that do use the hash, we avoid an error
2997 * deep inside the execution. In principle this doesn't matter,
2998 * but there is a little more risk of a bug in error handling
2999 * deep inside than in this preliminary check.
3000 * - When calling a driver, the driver might be capable of using
3001 * a hash that the core doesn't support. This could potentially
3002 * result in a buffer overflow if the hash is larger than the
3003 * maximum hash size assumed by the core.
3004 * - Returning a consistent error makes it possible to test
3005 * not-supported hashes in a consistent way.
3006 */
3007 if (hash_alg != 0 && !is_hash_supported(hash_alg)) {
3008 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm56980482021-05-05 13:56:27 +02003009 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02003010
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003011 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02003012}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003013
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003014static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
3015 int input_is_message,
3016 psa_algorithm_t alg,
3017 const uint8_t *input,
3018 size_t input_length,
3019 uint8_t *signature,
3020 size_t signature_size,
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;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003025 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003026 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003027
3028 *signature_length = 0;
3029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003030 status = psa_sign_verify_check_alg(input_is_message, alg);
3031 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02003032 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003033 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003034
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003035 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02003036 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003037 * buffer can in principle be empty since it doesn't actually have
3038 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003039 if (signature_size == 0) {
3040 return PSA_ERROR_BUFFER_TOO_SMALL;
3041 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003042
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003043 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003044 key, &slot,
3045 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
3046 PSA_KEY_USAGE_SIGN_HASH,
3047 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003049 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003050 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003051 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003053 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003054 status = PSA_ERROR_INVALID_ARGUMENT;
3055 goto exit;
3056 }
3057
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003058 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003059 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003060 };
3061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003062 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003063 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003064 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003065 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003066 signature, signature_size, signature_length);
3067 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003068
3069 status = psa_driver_wrapper_sign_hash(
3070 &attributes, slot->key.data, slot->key.bytes,
3071 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003072 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003073 }
3074
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003075
3076exit:
3077 /* Fill the unused part of the output buffer (the whole buffer on error,
3078 * the trailing part on success) with something that isn't a valid signature
3079 * (barring an attack on the signature and deliberately-crafted input),
3080 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003081 if (status == PSA_SUCCESS) {
3082 memset(signature + *signature_length, '!',
3083 signature_size - *signature_length);
3084 } else {
3085 memset(signature, '!', signature_size);
3086 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003087 /* If signature_size is 0 then we have nothing to do. We must not call
3088 * memset because signature may be NULL in this case. */
3089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003090 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003092 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003093}
3094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003095static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3096 int input_is_message,
3097 psa_algorithm_t alg,
3098 const uint8_t *input,
3099 size_t input_length,
3100 const uint8_t *signature,
3101 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003102{
3103 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3104 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3105 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003107 status = psa_sign_verify_check_alg(input_is_message, alg);
3108 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02003109 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003110 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003111
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003112 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003113 key, &slot,
3114 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3115 PSA_KEY_USAGE_VERIFY_HASH,
3116 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003118 if (status != PSA_SUCCESS) {
3119 return status;
3120 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003121
3122 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003123 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003124 };
3125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003126 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003127 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003128 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003129 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003130 signature, signature_length);
3131 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003132 status = psa_driver_wrapper_verify_hash(
3133 &attributes, slot->key.data, slot->key.bytes,
3134 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003135 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003136 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003138 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003140 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003141
3142}
3143
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003144psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003145 const psa_key_attributes_t *attributes,
3146 const uint8_t *key_buffer,
3147 size_t key_buffer_size,
3148 psa_algorithm_t alg,
3149 const uint8_t *input,
3150 size_t input_length,
3151 uint8_t *signature,
3152 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003153 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003154{
3155 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003157 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003158 size_t hash_length;
3159 uint8_t hash[PSA_HASH_MAX_SIZE];
3160
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003161 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003162 PSA_ALG_SIGN_GET_HASH(alg),
3163 input, input_length,
3164 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003166 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003167 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003168 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003169
3170 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003171 attributes, key_buffer, key_buffer_size,
3172 alg, hash, hash_length,
3173 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003174 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003176 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003177}
3178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003179psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3180 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003181 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003182 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003183 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003184 size_t signature_size,
3185 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003186{
Thomas Daubney290aac42024-01-18 17:23:02 +00003187 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3188 LOCAL_INPUT_DECLARE(input_external, input);
3189 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3190
3191 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3192 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3193 status = psa_sign_internal(key, 1, alg, input, input_length, signature,
3194 signature_size, signature_length);
3195
David Horstmann42015332024-03-13 15:42:31 +00003196#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003197exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003198#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003199 LOCAL_INPUT_FREE(input_external, input);
3200 LOCAL_OUTPUT_FREE(signature_external, signature);
3201 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003202}
3203
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003204psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003205 const psa_key_attributes_t *attributes,
3206 const uint8_t *key_buffer,
3207 size_t key_buffer_size,
3208 psa_algorithm_t alg,
3209 const uint8_t *input,
3210 size_t input_length,
3211 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003212 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003213{
3214 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003216 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003217 size_t hash_length;
3218 uint8_t hash[PSA_HASH_MAX_SIZE];
3219
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003220 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003221 PSA_ALG_SIGN_GET_HASH(alg),
3222 input, input_length,
3223 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003225 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003226 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003227 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003228
3229 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003230 attributes, key_buffer, key_buffer_size,
3231 alg, hash, hash_length,
3232 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003233 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003235 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003236}
3237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003238psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3239 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003240 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003241 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003242 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003243 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003244{
Thomas Daubney290aac42024-01-18 17:23:02 +00003245 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3246 LOCAL_INPUT_DECLARE(input_external, input);
3247 LOCAL_INPUT_DECLARE(signature_external, signature);
3248
3249 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3250 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3251 status = psa_verify_internal(key, 1, alg, input, input_length, signature,
3252 signature_length);
3253
David Horstmann42015332024-03-13 15:42:31 +00003254#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003255exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003256#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003257 LOCAL_INPUT_FREE(input_external, input);
3258 LOCAL_INPUT_FREE(signature_external, signature);
3259
3260 return status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003261}
3262
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003263psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003264 const psa_key_attributes_t *attributes,
3265 const uint8_t *key_buffer, size_t key_buffer_size,
3266 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003267 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01003268{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003269 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3270 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3271 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003272#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003273 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3274 return mbedtls_psa_rsa_sign_hash(
3275 attributes,
3276 key_buffer, key_buffer_size,
3277 alg, hash, hash_length,
3278 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003279#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3280 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003281 } else {
3282 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003283 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003284 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3285 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003286#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003287 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3288 return mbedtls_psa_ecdsa_sign_hash(
3289 attributes,
3290 key_buffer, key_buffer_size,
3291 alg, hash, hash_length,
3292 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003293#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3294 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003295 } else {
3296 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003297 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003298 }
Ronald Cron4501c982021-03-19 20:09:32 +01003299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003300 (void) key_buffer;
3301 (void) key_buffer_size;
3302 (void) hash;
3303 (void) hash_length;
3304 (void) signature;
3305 (void) signature_size;
3306 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003308 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003309}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003311psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3312 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003313 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003314 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003315 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003316 size_t signature_size,
3317 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003318{
Thomas Daubney290aac42024-01-18 17:23:02 +00003319 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3320 LOCAL_INPUT_DECLARE(hash_external, hash);
3321 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3322
3323 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3324 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3325 status = psa_sign_internal(key, 0, alg, hash, hash_length, signature,
3326 signature_size, signature_length);
3327
David Horstmann42015332024-03-13 15:42:31 +00003328#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003329exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003330#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003331 LOCAL_INPUT_FREE(hash_external, hash);
3332 LOCAL_OUTPUT_FREE(signature_external, signature);
3333
3334 return status;
itayzafrir5c753392018-05-08 11:18:38 +03003335}
3336
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003337psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003338 const psa_key_attributes_t *attributes,
3339 const uint8_t *key_buffer, size_t key_buffer_size,
3340 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003341 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003342{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003343 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3344 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3345 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003346#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003347 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3348 return mbedtls_psa_rsa_verify_hash(
3349 attributes,
3350 key_buffer, key_buffer_size,
3351 alg, hash, hash_length,
3352 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003353#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3354 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003355 } else {
3356 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003357 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003358 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3359 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003360#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003361 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3362 return mbedtls_psa_ecdsa_verify_hash(
3363 attributes,
3364 key_buffer, key_buffer_size,
3365 alg, hash, hash_length,
3366 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003367#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3368 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003369 } else {
3370 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003371 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003372 }
Ronald Cron4501c982021-03-19 20:09:32 +01003373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003374 (void) key_buffer;
3375 (void) key_buffer_size;
3376 (void) hash;
3377 (void) hash_length;
3378 (void) signature;
3379 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003381 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003382}
3383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003384psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3385 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003386 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003387 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003388 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003389 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003390{
Thomas Daubney290aac42024-01-18 17:23:02 +00003391 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3392 LOCAL_INPUT_DECLARE(hash_external, hash);
3393 LOCAL_INPUT_DECLARE(signature_external, signature);
3394
3395 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3396 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3397 status = psa_verify_internal(key, 0, alg, hash, hash_length, signature,
3398 signature_length);
3399
David Horstmann42015332024-03-13 15:42:31 +00003400#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003401exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003402#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003403 LOCAL_INPUT_FREE(hash_external, hash);
3404 LOCAL_INPUT_FREE(signature_external, signature);
3405
3406 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003407}
3408
John Durkop0e005192020-10-31 22:06:54 -07003409#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003410static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3411 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003412{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003413 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3414 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3415 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3416 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003417}
John Durkop0e005192020-10-31 22:06:54 -07003418#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003420psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3421 psa_algorithm_t alg,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003422 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003423 size_t input_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003424 const uint8_t *salt_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003425 size_t salt_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003426 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003427 size_t output_size,
3428 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003429{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003430 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003431 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003432 psa_key_slot_t *slot;
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003433 LOCAL_INPUT_DECLARE(input_external, input);
3434 LOCAL_INPUT_DECLARE(salt_external, salt);
3435 LOCAL_OUTPUT_DECLARE(output_external, output);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003436
Darryl Green5cc689a2018-07-24 15:34:10 +01003437 (void) input;
3438 (void) input_length;
3439 (void) salt;
3440 (void) output;
3441 (void) output_size;
3442
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003443 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003445 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3446 return PSA_ERROR_INVALID_ARGUMENT;
3447 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003448
Ronald Cron5c522922020-11-14 16:35:34 +01003449 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003450 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3451 if (status != PSA_SUCCESS) {
3452 return status;
3453 }
3454 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3455 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003456 status = PSA_ERROR_INVALID_ARGUMENT;
3457 goto exit;
3458 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003460 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003461#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003462 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003463 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003464 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3465 slot->key.data,
3466 slot->key.bytes,
3467 &rsa);
3468 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003469 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003470 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003472 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003473 status = PSA_ERROR_BUFFER_TOO_SMALL;
3474 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003475 }
Ronald Cron7207d572021-09-08 14:28:35 +02003476#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3477 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003478 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3479 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3480 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003481 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003482#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003483 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003484 mbedtls_rsa_pkcs1_encrypt(rsa,
3485 mbedtls_psa_get_random,
3486 MBEDTLS_PSA_RANDOM_STATE,
3487 MBEDTLS_RSA_PUBLIC,
3488 input_length,
3489 input,
3490 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003491#else
3492 status = PSA_ERROR_NOT_SUPPORTED;
3493#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003494 } else
3495 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003496#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003497 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003498 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003499 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3500 mbedtls_psa_get_random,
3501 MBEDTLS_PSA_RANDOM_STATE,
3502 MBEDTLS_RSA_PUBLIC,
3503 salt, salt_length,
3504 input_length,
3505 input,
3506 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003507#else
3508 status = PSA_ERROR_NOT_SUPPORTED;
3509#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003510 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003511 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003512 }
Ronald Cron7207d572021-09-08 14:28:35 +02003513#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003514 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003515rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003516 if (status == PSA_SUCCESS) {
3517 *output_length = mbedtls_rsa_get_len(rsa);
3518 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003520 mbedtls_rsa_free(rsa);
3521 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003522#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003523 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003524 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003525 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003526 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003527
3528exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003529 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003530
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003531 LOCAL_INPUT_FREE(input_external, input);
3532 LOCAL_INPUT_FREE(salt_external, salt);
3533 LOCAL_OUTPUT_FREE(output_external, output);
3534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003535 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003536}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003538psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3539 psa_algorithm_t alg,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003540 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003541 size_t input_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003542 const uint8_t *salt_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003543 size_t salt_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003544 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003545 size_t output_size,
3546 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003547{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003548 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003549 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003550 psa_key_slot_t *slot;
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003551 LOCAL_INPUT_DECLARE(input_external, input);
3552 LOCAL_INPUT_DECLARE(salt_external, salt);
3553 LOCAL_OUTPUT_DECLARE(output_external, output);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003554
Darryl Green5cc689a2018-07-24 15:34:10 +01003555 (void) input;
3556 (void) input_length;
3557 (void) salt;
3558 (void) output;
3559 (void) output_size;
3560
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003561 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003563 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3564 return PSA_ERROR_INVALID_ARGUMENT;
3565 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003566
Ronald Cron5c522922020-11-14 16:35:34 +01003567 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003568 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3569 if (status != PSA_SUCCESS) {
3570 return status;
3571 }
3572 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003573 status = PSA_ERROR_INVALID_ARGUMENT;
3574 goto exit;
3575 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003577 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003578#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003579 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003580 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003581 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3582 slot->key.data,
3583 slot->key.bytes,
3584 &rsa);
3585 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003586 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003587 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003588
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003589 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003590 status = PSA_ERROR_INVALID_ARGUMENT;
3591 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003592 }
Ronald Cron7207d572021-09-08 14:28:35 +02003593#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3594 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003595 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3596 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3597 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003598 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003599#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003600 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003601 mbedtls_rsa_pkcs1_decrypt(rsa,
3602 mbedtls_psa_get_random,
3603 MBEDTLS_PSA_RANDOM_STATE,
3604 MBEDTLS_RSA_PRIVATE,
3605 output_length,
3606 input,
3607 output,
3608 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003609#else
3610 status = PSA_ERROR_NOT_SUPPORTED;
3611#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003612 } else
3613 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003614#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003615 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003616 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003617 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3618 mbedtls_psa_get_random,
3619 MBEDTLS_PSA_RANDOM_STATE,
3620 MBEDTLS_RSA_PRIVATE,
3621 salt, salt_length,
3622 output_length,
3623 input,
3624 output,
3625 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003626#else
3627 status = PSA_ERROR_NOT_SUPPORTED;
3628#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003629 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003630 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003631 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003632
Ronald Cron7207d572021-09-08 14:28:35 +02003633#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003634 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003635rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003636 mbedtls_rsa_free(rsa);
3637 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003638#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3639 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003640 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003641 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003642 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003643
3644exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003645 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003646
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003647 LOCAL_INPUT_FREE(input_external, input);
3648 LOCAL_INPUT_FREE(salt_external, salt);
3649 LOCAL_OUTPUT_FREE(output_external, output);
3650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003651 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003652}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003653
David Horstmann65bf12c2024-02-06 15:27:49 +00003654static psa_status_t psa_generate_random_internal(uint8_t *output,
3655 size_t output_size)
3656{
3657 GUARD_MODULE_INITIALIZED;
3658
3659 psa_status_t status;
3660
3661#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
3662
3663 size_t output_length = 0;
3664 status = mbedtls_psa_external_get_random(&global_data.rng,
3665 output, output_size,
3666 &output_length);
3667 if (status != PSA_SUCCESS) {
3668 goto exit;
3669 }
3670 /* Breaking up a request into smaller chunks is currently not supported
3671 * for the external RNG interface. */
3672 if (output_length != output_size) {
3673 status = PSA_ERROR_INSUFFICIENT_ENTROPY;
3674 goto exit;
3675 }
3676 status = PSA_SUCCESS;
3677
3678#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
3679
3680 while (output_size > 0) {
3681 size_t request_size =
3682 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
3683 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
3684 output_size);
3685 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
3686 output, request_size);
3687 if (ret != 0) {
3688 status = mbedtls_to_psa_error(ret);
3689 goto exit;
3690 }
3691 output_size -= request_size;
3692 output += request_size;
3693 }
3694 status = PSA_SUCCESS;
3695#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
3696
3697exit:
3698 return status;
3699}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003700
3701
mohammad1603503973b2018-03-12 15:59:30 +02003702/****************************************************************/
3703/* Symmetric cryptography */
3704/****************************************************************/
3705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003706static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3707 mbedtls_svc_key_id_t key,
3708 psa_algorithm_t alg,
3709 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003710{
3711 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3712 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003713 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003714 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003715 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3716 PSA_KEY_USAGE_ENCRYPT :
3717 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003718
3719 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003720 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003721 status = PSA_ERROR_BAD_STATE;
3722 goto exit;
3723 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003725 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003726 status = PSA_ERROR_INVALID_ARGUMENT;
3727 goto exit;
3728 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003729
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003730 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3731 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003732 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003733 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003734
Ronald Cron49fafa92021-03-10 08:34:23 +01003735 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003736 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003737 * so we only set it (in the driver wrapper) after resources have been
3738 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003739 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003740 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003741 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003742 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003743 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003744 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003745 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003746 }
3747 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003748
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003749 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003750 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003751 };
3752
Ronald Cronab99ac22020-10-01 16:38:28 +02003753 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003754 if (cipher_operation == MBEDTLS_ENCRYPT) {
3755 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3756 &attributes,
3757 slot->key.data,
3758 slot->key.bytes,
3759 alg);
3760 } else {
3761 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3762 &attributes,
3763 slot->key.data,
3764 slot->key.bytes,
3765 alg);
3766 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003767
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003768exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003769 if (status != PSA_SUCCESS) {
3770 psa_cipher_abort(operation);
3771 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003773 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003775 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003776}
3777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003778psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3779 mbedtls_svc_key_id_t key,
3780 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003781{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003782 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003783}
3784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003785psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3786 mbedtls_svc_key_id_t key,
3787 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003788{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003789 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003790}
3791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003792psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Gabor Mezei8677edd2024-02-07 18:10:13 +01003793 uint8_t *iv_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003794 size_t iv_size,
3795 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003796{
Ronald Cron6d051732020-10-01 14:10:20 +02003797 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Sergey04eb7c02023-03-06 15:37:23 -07003798 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003799
Gabor Mezei8677edd2024-02-07 18:10:13 +01003800 LOCAL_OUTPUT_DECLARE(iv_external, iv);
3801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003802 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003803 status = PSA_ERROR_BAD_STATE;
3804 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003805 }
3806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003807 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003808 status = PSA_ERROR_BAD_STATE;
3809 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003810 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003811
Ronald Cronc423acb2021-07-05 12:31:44 +02003812 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003813 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003814 status = PSA_ERROR_BUFFER_TOO_SMALL;
3815 goto exit;
3816 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003818 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003819 status = PSA_ERROR_GENERIC_ERROR;
3820 goto exit;
3821 }
3822
Gabor Mezei8677edd2024-02-07 18:10:13 +01003823 LOCAL_OUTPUT_ALLOC(iv_external, default_iv_length, iv);
3824
Gabor Mezeif3c35042024-03-04 17:15:08 +01003825 status = psa_generate_random_internal(iv, default_iv_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003826 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003827 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003828 }
Ronald Cron5618a392021-03-26 09:52:26 +01003829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003830 status = psa_driver_wrapper_cipher_set_iv(operation,
Gabor Mezei8677edd2024-02-07 18:10:13 +01003831 iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003832
3833exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003834 if (status == PSA_SUCCESS) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003835 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003836 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003837 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003838 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003839 psa_cipher_abort(operation);
Gabor Mezei8677edd2024-02-07 18:10:13 +01003840 if (iv != NULL) {
3841 mbedtls_platform_zeroize(iv, default_iv_length);
3842 }
Ronald Cronc423acb2021-07-05 12:31:44 +02003843 }
Ronald Cron6d051732020-10-01 14:10:20 +02003844
Gabor Mezei8677edd2024-02-07 18:10:13 +01003845 LOCAL_OUTPUT_FREE(iv_external, iv);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003846 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003847}
3848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003849psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Gabor Mezei282bb532024-02-01 10:39:26 +01003850 const uint8_t *iv_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003851 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003852{
Ronald Cron6d051732020-10-01 14:10:20 +02003853 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003854
Gabor Mezei282bb532024-02-01 10:39:26 +01003855 LOCAL_INPUT_DECLARE(iv_external, iv);
3856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003857 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003858 status = PSA_ERROR_BAD_STATE;
3859 goto exit;
3860 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003862 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003863 status = PSA_ERROR_BAD_STATE;
3864 goto exit;
3865 }
Ronald Crona0d68172021-03-26 10:15:08 +01003866
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003867 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003868 status = PSA_ERROR_INVALID_ARGUMENT;
3869 goto exit;
3870 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003871
Gabor Mezei282bb532024-02-01 10:39:26 +01003872 LOCAL_INPUT_ALLOC(iv_external, iv_length, iv);
3873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003874 status = psa_driver_wrapper_cipher_set_iv(operation,
3875 iv,
3876 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003877
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003878exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003879 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003880 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003881 } else {
3882 psa_cipher_abort(operation);
3883 }
Gabor Mezei282bb532024-02-01 10:39:26 +01003884
3885 LOCAL_INPUT_FREE(iv_external, iv);
3886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003887 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003888}
3889
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003890psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003891 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003892 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003893 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003894 size_t output_size,
3895 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003896{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003897 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003898
Gabor Mezei13a15c22024-01-24 16:56:00 +01003899 LOCAL_INPUT_DECLARE(input_external, input);
3900 LOCAL_OUTPUT_DECLARE(output_external, output);
3901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003902 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003903 status = PSA_ERROR_BAD_STATE;
3904 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003905 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003907 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003908 status = PSA_ERROR_BAD_STATE;
3909 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003910 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003911
Gabor Mezeied96d682024-01-31 17:45:29 +01003912 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gabor Mezeiff783e02024-02-29 10:08:16 +00003913 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezeied96d682024-01-31 17:45:29 +01003914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003915 status = psa_driver_wrapper_cipher_update(operation,
3916 input,
3917 input_length,
3918 output,
3919 output_size,
3920 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003921
3922exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003923 if (status != PSA_SUCCESS) {
3924 psa_cipher_abort(operation);
3925 }
Ronald Cron6d051732020-10-01 14:10:20 +02003926
Gabor Mezei13a15c22024-01-24 16:56:00 +01003927 LOCAL_INPUT_FREE(input_external, input);
3928 LOCAL_OUTPUT_FREE(output_external, output);
3929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003930 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003931}
3932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003933psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003934 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003935 size_t output_size,
3936 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003937{
David Saadab4ecc272019-02-14 13:48:10 +02003938 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003939
Gabor Mezei13a15c22024-01-24 16:56:00 +01003940 LOCAL_OUTPUT_DECLARE(output_external, output);
3941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003942 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003943 status = PSA_ERROR_BAD_STATE;
3944 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003945 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003947 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003948 status = PSA_ERROR_BAD_STATE;
3949 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003950 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003951
Gabor Mezeiff783e02024-02-29 10:08:16 +00003952 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezeied96d682024-01-31 17:45:29 +01003953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003954 status = psa_driver_wrapper_cipher_finish(operation,
3955 output,
3956 output_size,
3957 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003958
3959exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003960 if (status == PSA_SUCCESS) {
Gabor Mezei13a15c22024-01-24 16:56:00 +01003961 status = psa_cipher_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003962 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003963 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003964 (void) psa_cipher_abort(operation);
Steven Cooremanef8575e2020-09-11 11:44:50 +02003965 }
Gabor Mezei13a15c22024-01-24 16:56:00 +01003966
3967 LOCAL_OUTPUT_FREE(output_external, output);
3968
3969 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003970}
3971
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003972psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003973{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003974 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003975 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003976 * in use. It's ok to call abort on such an object, and there's
3977 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003978 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003979 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003981 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003982
Ronald Cron49fafa92021-03-10 08:34:23 +01003983 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003984 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003985 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003986
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003987 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003988}
3989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003990psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3991 psa_algorithm_t alg,
David Horstmann926193a2023-12-13 15:55:25 +00003992 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003993 size_t input_length,
David Horstmann926193a2023-12-13 15:55:25 +00003994 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003995 size_t output_size,
3996 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003997{
3998 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003999 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004000 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02004001 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02004002 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4003 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02004004
David Horstmannb80e35a2023-12-14 18:12:47 +00004005 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_ENCRYPT,
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 };
4023
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004024 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4025 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02004026 status = PSA_ERROR_GENERIC_ERROR;
4027 goto exit;
4028 }
4029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004030 if (default_iv_length > 0) {
4031 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004032 status = PSA_ERROR_BUFFER_TOO_SMALL;
4033 goto exit;
4034 }
4035
David Horstmann65bf12c2024-02-06 15:27:49 +00004036 status = psa_generate_random_internal(local_iv, default_iv_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004037 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004038 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004039 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004040 }
4041
Gabor Mezeied96d682024-01-31 17:45:29 +01004042 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4043 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4044
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004045 status = psa_driver_wrapper_cipher_encrypt(
4046 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02004047 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004048 mbedtls_buffer_offset(output, default_iv_length),
4049 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004050
4051exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004052 unlock_status = psa_unlock_key_slot(slot);
4053 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004054 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02004055 }
Ronald Cron16377072021-07-08 19:00:07 +02004056
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004057 if (status == PSA_SUCCESS) {
4058 if (default_iv_length > 0) {
4059 memcpy(output, local_iv, default_iv_length);
4060 }
4061 *output_length += default_iv_length;
4062 } else {
4063 *output_length = 0;
4064 }
4065
David Horstmannb80e35a2023-12-14 18:12:47 +00004066 LOCAL_INPUT_FREE(input_external, input);
4067 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00004068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004069 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004070}
4071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004072psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4073 psa_algorithm_t alg,
Gabor Mezei13a15c22024-01-24 16:56:00 +01004074 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004075 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01004076 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004077 size_t output_size,
4078 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004079{
4080 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004081 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004082 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02004083 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02004084
Gabor Mezei13a15c22024-01-24 16:56:00 +01004085 LOCAL_INPUT_DECLARE(input_external, input);
4086 LOCAL_OUTPUT_DECLARE(output_external, output);
4087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004088 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02004089 status = PSA_ERROR_INVALID_ARGUMENT;
4090 goto exit;
4091 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004093 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4094 PSA_KEY_USAGE_DECRYPT,
4095 alg);
4096 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004097 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004098 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004099
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004100 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004101 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004102 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004104 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02004105 status = PSA_ERROR_INVALID_ARGUMENT;
4106 goto exit;
4107 }
4108
Gabor Mezeied96d682024-01-31 17:45:29 +01004109 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4110 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4111
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004112 status = psa_driver_wrapper_cipher_decrypt(
4113 &attributes, slot->key.data, slot->key.bytes,
4114 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004115 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004116
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02004117exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004118 unlock_status = psa_unlock_key_slot(slot);
4119 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004120 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004121 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004123 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004124 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004125 }
Ronald Cron16377072021-07-08 19:00:07 +02004126
Gabor Mezei13a15c22024-01-24 16:56:00 +01004127 LOCAL_INPUT_FREE(input_external, input);
4128 LOCAL_OUTPUT_FREE(output_external, output);
4129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004130 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004131}
4132
4133
mohammad16035955c982018-04-26 00:53:03 +03004134/****************************************************************/
4135/* AEAD */
4136/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004138psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4139 psa_algorithm_t alg,
David Horstmann21c1a942023-11-28 19:25:00 +00004140 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004141 size_t nonce_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004142 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004143 size_t additional_data_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004144 const uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004145 size_t plaintext_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004146 uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004147 size_t ciphertext_size,
4148 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004149{
Ronald Cron215633c2021-03-16 17:15:37 +01004150 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4151 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004152
David Horstmann21c1a942023-11-28 19:25:00 +00004153 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4154 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4155 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4156 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4157
mohammad1603f08a5502018-06-03 15:05:47 +03004158 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004160 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4161 return PSA_ERROR_NOT_SUPPORTED;
4162 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004163
Ronald Cron9a986162021-03-26 12:40:07 +01004164 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004165 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4166 if (status != PSA_SUCCESS) {
4167 return status;
4168 }
mohammad16035955c982018-04-26 00:53:03 +03004169
Ronald Cron215633c2021-03-16 17:15:37 +01004170 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004171 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004172 };
mohammad16035955c982018-04-26 00:53:03 +03004173
David Horstmann21c1a942023-11-28 19:25:00 +00004174 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4175 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4176 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4177 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4178
Ronald Cronde822812021-03-17 16:08:20 +01004179 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004180 &attributes, slot->key.data, slot->key.bytes,
4181 alg,
4182 nonce, nonce_length,
4183 additional_data, additional_data_length,
4184 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004185 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004187 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4188 memset(ciphertext, 0, ciphertext_size);
4189 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004190
David Horstmann21c1a942023-11-28 19:25:00 +00004191/* Exit label is only used for buffer copying, prevent unused warnings. */
David Horstmann42015332024-03-13 15:42:31 +00004192#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann21c1a942023-11-28 19:25:00 +00004193exit:
4194#endif
4195 LOCAL_INPUT_FREE(nonce_external, nonce);
4196 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4197 LOCAL_INPUT_FREE(plaintext_external, plaintext);
4198 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
4199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004200 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004202 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004203}
4204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004205psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4206 psa_algorithm_t alg,
David Horstmann6baf6e92023-11-28 19:43:53 +00004207 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004208 size_t nonce_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004209 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004210 size_t additional_data_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004211 const uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004212 size_t ciphertext_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004213 uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004214 size_t plaintext_size,
4215 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004216{
Ronald Cron215633c2021-03-16 17:15:37 +01004217 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4218 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004219
David Horstmann6baf6e92023-11-28 19:43:53 +00004220 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4221 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4222 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
4223 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
4224
Gilles Peskineee652a32018-06-01 19:23:52 +02004225 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004227 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4228 return PSA_ERROR_NOT_SUPPORTED;
4229 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004230
Ronald Cron9a986162021-03-26 12:40:07 +01004231 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004232 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4233 if (status != PSA_SUCCESS) {
4234 return status;
4235 }
mohammad16035955c982018-04-26 00:53:03 +03004236
Ronald Cron215633c2021-03-16 17:15:37 +01004237 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004238 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004239 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004240
David Horstmann6baf6e92023-11-28 19:43:53 +00004241 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4242 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
4243 additional_data);
4244 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
4245 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
4246
Ronald Cronde822812021-03-17 16:08:20 +01004247 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004248 &attributes, slot->key.data, slot->key.bytes,
4249 alg,
4250 nonce, nonce_length,
4251 additional_data, additional_data_length,
4252 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004253 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004255 if (status != PSA_SUCCESS && plaintext_size != 0) {
4256 memset(plaintext, 0, plaintext_size);
4257 }
mohammad160360a64d02018-06-03 17:20:42 +03004258
David Horstmann6baf6e92023-11-28 19:43:53 +00004259/* Exit label is only used for buffer copying, prevent unused warnings. */
David Horstmann42015332024-03-13 15:42:31 +00004260#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann6baf6e92023-11-28 19:43:53 +00004261exit:
4262#endif
4263 LOCAL_INPUT_FREE(nonce_external, nonce);
4264 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4265 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
4266 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
4267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004268 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004270 return status;
mohammad16035955c982018-04-26 00:53:03 +03004271}
4272
Gilles Peskinee59236f2018-01-27 23:32:46 +01004273/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004274/* Generators */
4275/****************************************************************/
4276
John Durkop07cc04a2020-11-16 22:08:34 -08004277#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4278 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4279 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4280#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02004281#endif /* At least one builtin KDF */
4282
4283#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4284 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4285 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4286static psa_status_t psa_key_derivation_start_hmac(
4287 psa_mac_operation_t *operation,
4288 psa_algorithm_t hash_alg,
4289 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004290 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02004291{
4292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004294 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4295 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4296 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02004297
Steven Cooreman15f0d922021-05-07 14:14:37 +02004298 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004299 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02004300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004301 status = psa_driver_wrapper_mac_sign_setup(operation,
4302 &attributes,
4303 hmac_key, hmac_key_length,
4304 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02004305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004306 psa_reset_key_attributes(&attributes);
4307 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02004308}
4309#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004310
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004311#define HKDF_STATE_INIT 0 /* no input yet */
4312#define HKDF_STATE_STARTED 1 /* got salt */
4313#define HKDF_STATE_KEYED 2 /* got key */
4314#define HKDF_STATE_OUTPUT 3 /* output started */
4315
Gilles Peskinecbe66502019-05-16 16:59:18 +02004316static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004317 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004318{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004319 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4320 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4321 } else {
4322 return operation->alg;
4323 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004324}
4325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004326psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004327{
4328 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004329 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4330 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004331 /* The object has (apparently) been initialized but it is not
4332 * in use. It's ok to call abort on such an object, and there's
4333 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004334 } else
John Durkopd0321952020-10-29 21:37:36 -07004335#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004336 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4337 mbedtls_free(operation->ctx.hkdf.info);
4338 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4339 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004340#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4341#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4342 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004343 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4344 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4345 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4346 if (operation->ctx.tls12_prf.secret != NULL) {
4347 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
4348 operation->ctx.tls12_prf.secret_length);
4349 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004350 }
4351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004352 if (operation->ctx.tls12_prf.seed != NULL) {
4353 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
4354 operation->ctx.tls12_prf.seed_length);
4355 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01004356 }
4357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004358 if (operation->ctx.tls12_prf.label != NULL) {
4359 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
4360 operation->ctx.tls12_prf.label_length);
4361 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01004362 }
4363
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004364 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004365
4366 /* We leave the fields Ai and output_block to be erased safely by the
4367 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004368 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004369#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4370 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004371 {
4372 status = PSA_ERROR_BAD_STATE;
4373 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004374 mbedtls_platform_zeroize(operation, sizeof(*operation));
4375 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004376}
4377
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004378psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004379 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004380{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004381 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004382 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004383 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004384 }
4385
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004386 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004387 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004388}
4389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004390psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4391 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004392{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004393 if (operation->alg == 0) {
4394 return PSA_ERROR_BAD_STATE;
4395 }
4396 if (capacity > operation->capacity) {
4397 return PSA_ERROR_INVALID_ARGUMENT;
4398 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004399 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004400 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004401}
4402
John Durkopd0321952020-10-29 21:37:36 -07004403#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004404/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004405 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004406static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
4407 psa_algorithm_t hash_alg,
4408 uint8_t *output,
4409 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004410{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004411 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02004412 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004413 psa_status_t status;
4414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004415 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
4416 return PSA_ERROR_BAD_STATE;
4417 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004418 hkdf->state = HKDF_STATE_OUTPUT;
4419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004420 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004421 /* Copy what remains of the current block */
4422 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004423 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004424 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004425 }
4426 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004427 output += n;
4428 output_length -= n;
4429 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004430 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004431 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004432 }
Gilles Peskined54931c2018-07-17 21:06:59 +02004433 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004434 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004435 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004436 * object was corrupted or if this function is called directly
4437 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004438 if (hkdf->block_number == 0xff) {
4439 return PSA_ERROR_BAD_STATE;
4440 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004441
4442 /* We need a new block */
4443 ++hkdf->block_number;
4444 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004446 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4447 hash_alg,
4448 hkdf->prk,
4449 hash_length);
4450 if (status != PSA_SUCCESS) {
4451 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004452 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004453
4454 if (hkdf->block_number != 1) {
4455 status = psa_mac_update(&hkdf->hmac,
4456 hkdf->output_block,
4457 hash_length);
4458 if (status != PSA_SUCCESS) {
4459 return status;
4460 }
4461 }
4462 status = psa_mac_update(&hkdf->hmac,
4463 hkdf->info,
4464 hkdf->info_length);
4465 if (status != PSA_SUCCESS) {
4466 return status;
4467 }
4468 status = psa_mac_update(&hkdf->hmac,
4469 &hkdf->block_number, 1);
4470 if (status != PSA_SUCCESS) {
4471 return status;
4472 }
4473 status = psa_mac_sign_finish(&hkdf->hmac,
4474 hkdf->output_block,
4475 sizeof(hkdf->output_block),
4476 &hmac_output_length);
4477 if (status != PSA_SUCCESS) {
4478 return status;
4479 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004480 }
4481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004482 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004483}
John Durkop07cc04a2020-11-16 22:08:34 -08004484#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004485
John Durkop07cc04a2020-11-16 22:08:34 -08004486#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4487 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004488static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4489 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004490 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004491{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004492 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4493 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004494 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4495 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004496 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004497
Janos Follath7742fee2019-06-17 12:58:10 +01004498 /* We can't be wanting more output after block 0xff, otherwise
4499 * the capacity check in psa_key_derivation_output_bytes() would have
4500 * prevented this call. It could happen only if the operation
4501 * object was corrupted or if this function is called directly
4502 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004503 if (tls12_prf->block_number == 0xff) {
4504 return PSA_ERROR_CORRUPTION_DETECTED;
4505 }
Janos Follath7742fee2019-06-17 12:58:10 +01004506
4507 /* We need a new block */
4508 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004509 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004510
4511 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4512 *
4513 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4514 *
4515 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4516 * HMAC_hash(secret, A(2) + seed) +
4517 * HMAC_hash(secret, A(3) + seed) + ...
4518 *
4519 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004520 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004521 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004522 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004523 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004524 * is currently extracted as `output_block` and where i is
4525 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004526 */
4527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004528 status = psa_key_derivation_start_hmac(&hmac,
4529 hash_alg,
4530 tls12_prf->secret,
4531 tls12_prf->secret_length);
4532 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004533 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004534 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004535
4536 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004537 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004538 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4539 * the variable seed and in this instance means it in the context of the
4540 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004541 status = psa_mac_update(&hmac,
4542 tls12_prf->label,
4543 tls12_prf->label_length);
4544 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004545 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004546 }
4547 status = psa_mac_update(&hmac,
4548 tls12_prf->seed,
4549 tls12_prf->seed_length);
4550 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004551 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004552 }
4553 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004554 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004555 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4556 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004557 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004558 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004559 }
4560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004561 status = psa_mac_sign_finish(&hmac,
4562 tls12_prf->Ai, hash_length,
4563 &hmac_output_length);
4564 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004565 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004566 }
4567 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004568 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004569 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004570
4571 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004572 status = psa_key_derivation_start_hmac(&hmac,
4573 hash_alg,
4574 tls12_prf->secret,
4575 tls12_prf->secret_length);
4576 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004577 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004578 }
4579 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4580 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004581 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004582 }
4583 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4584 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004585 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004586 }
4587 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4588 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004589 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004590 }
4591 status = psa_mac_sign_finish(&hmac,
4592 tls12_prf->output_block, hash_length,
4593 &hmac_output_length);
4594 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004595 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004596 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004597
Janos Follath7742fee2019-06-17 12:58:10 +01004598
4599cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004600 cleanup_status = psa_mac_abort(&hmac);
4601 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004602 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004603 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004605 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004606}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004607
Janos Follath844eb0e2019-06-19 12:10:49 +01004608static psa_status_t psa_key_derivation_tls12_prf_read(
4609 psa_tls12_prf_key_derivation_t *tls12_prf,
4610 psa_algorithm_t alg,
4611 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004612 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004613{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004614 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4615 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004616 psa_status_t status;
4617 uint8_t offset, length;
4618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004619 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004620 case PSA_TLS12_PRF_STATE_LABEL_SET:
4621 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4622 break;
4623 case PSA_TLS12_PRF_STATE_OUTPUT:
4624 break;
4625 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004626 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004627 }
4628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004629 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004630 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004631 if (tls12_prf->left_in_block == 0) {
4632 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4633 alg);
4634 if (status != PSA_SUCCESS) {
4635 return status;
4636 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004637
4638 continue;
4639 }
4640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004641 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004642 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004643 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004644 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004645 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004646
4647 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004648 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004649 output += length;
4650 output_length -= length;
4651 tls12_prf->left_in_block -= length;
4652 }
4653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004654 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004655}
John Durkop07cc04a2020-11-16 22:08:34 -08004656#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4657 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004658
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004659psa_status_t psa_key_derivation_output_bytes(
4660 psa_key_derivation_operation_t *operation,
Ryan Everett08bd2462024-02-09 16:15:32 +00004661 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004662 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004663{
4664 psa_status_t status;
Ryan Everett08bd2462024-02-09 16:15:32 +00004665 LOCAL_OUTPUT_DECLARE(output_external, output);
4666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004667 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004669 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004670 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004671 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004672 }
4673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004674 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004676 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004677 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4678 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004679 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004680 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004681 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004682 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004683
4684 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
4685 if (output_length > operation->capacity) {
4686 operation->capacity = 0;
4687 /* Go through the error path to wipe all confidential data now
4688 * that the operation object is useless. */
4689 status = PSA_ERROR_INSUFFICIENT_DATA;
4690 goto exit;
4691 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004692 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004693
John Durkopd0321952020-10-29 21:37:36 -07004694#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004695 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4696 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4697 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4698 output, output_length);
4699 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004700#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4701#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4702 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004703 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4704 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4705 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4706 kdf_alg, output,
4707 output_length);
4708 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004709#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4710 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004711 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004712 (void) kdf_alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004713 status = PSA_ERROR_BAD_STATE;
4714 LOCAL_OUTPUT_FREE(output_external, output);
4715
4716 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004717 }
4718
4719exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004720 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004721 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 * This allows us to differentiate between exhausted operations and
4723 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4724 * operations. */
4725 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004726 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004727 operation->alg = alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004728 if (output != NULL) {
4729 memset(output, '!', output_length);
4730 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004731 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004732
4733 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004734 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004735}
4736
David Brown7807bf72021-02-09 16:28:23 -07004737#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004738static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004739{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004740 if (data_size >= 8) {
4741 mbedtls_des_key_set_parity(data);
4742 }
4743 if (data_size >= 16) {
4744 mbedtls_des_key_set_parity(data + 8);
4745 }
4746 if (data_size >= 24) {
4747 mbedtls_des_key_set_parity(data + 16);
4748 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004749}
David Brown7807bf72021-02-09 16:28:23 -07004750#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004751
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004752static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004753 psa_key_slot_t *slot,
4754 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004755 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004756{
4757 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004758 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004759 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004760 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004761
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004762 if (!key_type_is_raw_bytes(slot->attr.type)) {
4763 return PSA_ERROR_INVALID_ARGUMENT;
4764 }
4765 if (bits % 8 != 0) {
4766 return PSA_ERROR_INVALID_ARGUMENT;
4767 }
4768 data = mbedtls_calloc(1, bytes);
4769 if (data == NULL) {
4770 return PSA_ERROR_INSUFFICIENT_MEMORY;
4771 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004773 status = psa_key_derivation_output_bytes(operation, data, bytes);
4774 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004775 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004776 }
David Brown12ca5032021-02-11 11:02:00 -07004777#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004778 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4779 psa_des_set_key_parity(data, bytes);
4780 }
David Brown8107e312021-02-12 08:08:46 -07004781#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004783 status = psa_allocate_buffer_to_slot(slot, bytes);
4784 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004785 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004786 }
Ronald Crondd04d422020-11-28 15:14:42 +01004787
Ronald Cronbf33c932020-11-28 18:06:53 +01004788 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004789 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004790 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004791 };
4792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004793 status = psa_driver_wrapper_import_key(&attributes,
4794 data, bytes,
4795 slot->key.data,
4796 slot->key.bytes,
4797 &slot->key.bytes, &bits);
4798 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004799 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004800 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004801
4802exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004803 mbedtls_free(data);
4804 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004805}
4806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004807psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4808 psa_key_derivation_operation_t *operation,
4809 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004810{
4811 psa_status_t status;
4812 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004813 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004814
Ronald Cron81709fc2020-11-14 12:10:32 +01004815 *key = MBEDTLS_SVC_KEY_ID_INIT;
4816
Gilles Peskine0f84d622019-09-12 19:03:13 +02004817 /* Reject any attempt to create a zero-length key so that we don't
4818 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004819 if (psa_get_key_bits(attributes) == 0) {
4820 return PSA_ERROR_INVALID_ARGUMENT;
4821 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004822
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004823 if (operation->alg == PSA_ALG_NONE) {
4824 return PSA_ERROR_BAD_STATE;
4825 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004826
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004827 if (!operation->can_output_key) {
4828 return PSA_ERROR_NOT_PERMITTED;
4829 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004831 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4832 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004833#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004834 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004835 /* Deriving a key in a secure element is not implemented yet. */
4836 status = PSA_ERROR_NOT_SUPPORTED;
4837 }
4838#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004839 if (status == PSA_SUCCESS) {
4840 status = psa_generate_derived_key_internal(slot,
4841 attributes->core.bits,
4842 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004843 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004844 if (status == PSA_SUCCESS) {
4845 status = psa_finish_key_creation(slot, driver, key);
4846 }
4847 if (status != PSA_SUCCESS) {
4848 psa_fail_key_creation(slot, driver);
4849 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004851 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004852}
4853
Gilles Peskineeab56e42018-07-12 17:12:33 +02004854
4855
4856/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004857/* Key derivation */
4858/****************************************************************/
4859
Steven Cooreman932ffb72021-02-15 12:14:32 +01004860#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004861static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004862{
4863#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004864 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4865 return 1;
4866 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004867#endif
4868#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004869 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4870 return 1;
4871 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004872#endif
4873#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004874 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4875 return 1;
4876 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004877#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004878 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004879}
4880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004881static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004882{
4883 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004884 psa_status_t status = psa_hash_setup(&operation, alg);
4885 psa_hash_abort(&operation);
4886 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004887}
4888
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304889static psa_status_t psa_key_derivation_set_maximum_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004890 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004891 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004892{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004893 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4894 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4895 if (hash_size == 0) {
4896 return PSA_ERROR_NOT_SUPPORTED;
4897 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004898
4899 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4900 * we might fail later, which is somewhat unfriendly and potentially
4901 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004902 psa_status_t status = psa_hash_try_support(hash_alg);
4903 if (status != PSA_SUCCESS) {
4904 return status;
4905 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004906
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304907#if defined(PSA_WANT_ALG_HKDF)
4908 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4909 operation->capacity = 255 * hash_size;
4910 } else
4911#endif
4912#if defined(PSA_WANT_ALG_TLS12_PRF)
4913 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
4914 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4915 operation->capacity = SIZE_MAX;
4916 } else
4917#endif
4918#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
4919 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
4920 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4921 /* Master Secret is always 48 bytes
4922 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
4923 operation->capacity = 48U;
4924 } else
4925#endif
4926 {
4927 (void) hash_size;
4928 status = PSA_ERROR_NOT_SUPPORTED;
4929 }
4930 return status;
4931}
4932
4933
4934static psa_status_t psa_key_derivation_setup_kdf(
4935 psa_key_derivation_operation_t *operation,
4936 psa_algorithm_t kdf_alg)
4937{
4938 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4939 * initialisers for this union leave some bytes unspecified.) */
4940 memset(&operation->ctx, 0, sizeof(operation->ctx));
4941 /* Make sure that kdf_alg is a supported key derivation algorithm. */
4942 if (!is_kdf_alg_supported(kdf_alg)) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004943 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004944 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004945
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304946 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
4947 kdf_alg);
4948 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004949}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004951static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004952{
4953#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004954 if (alg == PSA_ALG_ECDH) {
4955 return PSA_SUCCESS;
4956 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004957#endif
4958 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004959 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004960}
John Durkop07cc04a2020-11-16 22:08:34 -08004961#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004962
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004963psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4964 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004965{
4966 psa_status_t status;
4967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004968 if (operation->alg != 0) {
4969 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004970 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004971
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004972 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4973 return PSA_ERROR_INVALID_ARGUMENT;
4974 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4975#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4976 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4977 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4978 status = psa_key_agreement_try_support(ka_alg);
4979 if (status != PSA_SUCCESS) {
4980 return status;
4981 }
4982 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4983#else
4984 return PSA_ERROR_NOT_SUPPORTED;
4985#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4986 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4987#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4988 status = psa_key_derivation_setup_kdf(operation, alg);
4989#else
4990 return PSA_ERROR_NOT_SUPPORTED;
4991#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4992 } else {
4993 return PSA_ERROR_INVALID_ARGUMENT;
4994 }
4995
4996 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004997 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004998 }
4999 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005000}
5001
John Durkopd0321952020-10-29 21:37:36 -07005002#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005003static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
5004 psa_algorithm_t hash_alg,
5005 psa_key_derivation_step_t step,
5006 const uint8_t *data,
5007 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005008{
5009 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005010 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02005011 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005012 if (hkdf->state != HKDF_STATE_INIT) {
5013 return PSA_ERROR_BAD_STATE;
5014 } else {
5015 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5016 hash_alg,
5017 data, data_length);
5018 if (status != PSA_SUCCESS) {
5019 return status;
5020 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02005021 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005022 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02005023 }
Gilles Peskine03410b52019-05-16 16:05:19 +02005024 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005025 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005026 if (hkdf->state == HKDF_STATE_INIT) {
5027 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5028 hash_alg,
5029 NULL, 0);
5030 if (status != PSA_SUCCESS) {
5031 return status;
5032 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005033 hkdf->state = HKDF_STATE_STARTED;
5034 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005035 if (hkdf->state != HKDF_STATE_STARTED) {
5036 return PSA_ERROR_BAD_STATE;
5037 }
5038 status = psa_mac_update(&hkdf->hmac,
5039 data, data_length);
5040 if (status != PSA_SUCCESS) {
5041 return status;
5042 }
5043 status = psa_mac_sign_finish(&hkdf->hmac,
5044 hkdf->prk,
5045 sizeof(hkdf->prk),
5046 &data_length);
5047 if (status != PSA_SUCCESS) {
5048 return status;
5049 }
5050 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02005051 hkdf->block_number = 0;
5052 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005053 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02005054 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005055 if (hkdf->state == HKDF_STATE_OUTPUT) {
5056 return PSA_ERROR_BAD_STATE;
5057 }
5058 if (hkdf->info_set) {
5059 return PSA_ERROR_BAD_STATE;
5060 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005061 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005062 if (data_length != 0) {
5063 hkdf->info = mbedtls_calloc(1, data_length);
5064 if (hkdf->info == NULL) {
5065 return PSA_ERROR_INSUFFICIENT_MEMORY;
5066 }
5067 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005068 }
5069 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005070 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005071 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005072 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005073 }
5074}
John Durkop07cc04a2020-11-16 22:08:34 -08005075#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005076
John Durkop07cc04a2020-11-16 22:08:34 -08005077#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5078 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005079static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
5080 const uint8_t *data,
5081 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01005082{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005083 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
5084 return PSA_ERROR_BAD_STATE;
5085 }
Janos Follathf08e2652019-06-13 09:05:41 +01005086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005087 if (data_length != 0) {
5088 prf->seed = mbedtls_calloc(1, data_length);
5089 if (prf->seed == NULL) {
5090 return PSA_ERROR_INSUFFICIENT_MEMORY;
5091 }
Janos Follathf08e2652019-06-13 09:05:41 +01005092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005093 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005094 prf->seed_length = data_length;
5095 }
Janos Follathf08e2652019-06-13 09:05:41 +01005096
Gilles Peskine43f958b2020-12-13 14:55:14 +01005097 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005099 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01005100}
5101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005102static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
5103 const uint8_t *data,
5104 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01005105{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005106 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
5107 return PSA_ERROR_BAD_STATE;
5108 }
Janos Follath81550542019-06-13 14:26:34 +01005109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005110 if (data_length != 0) {
5111 prf->secret = mbedtls_calloc(1, data_length);
5112 if (prf->secret == NULL) {
5113 return PSA_ERROR_INSUFFICIENT_MEMORY;
5114 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02005115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005116 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02005117 prf->secret_length = data_length;
5118 }
Janos Follath81550542019-06-13 14:26:34 +01005119
Gilles Peskine43f958b2020-12-13 14:55:14 +01005120 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005122 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01005123}
5124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005125static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
5126 const uint8_t *data,
5127 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01005128{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005129 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
5130 return PSA_ERROR_BAD_STATE;
5131 }
Janos Follath63028dd2019-06-13 09:15:47 +01005132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005133 if (data_length != 0) {
5134 prf->label = mbedtls_calloc(1, data_length);
5135 if (prf->label == NULL) {
5136 return PSA_ERROR_INSUFFICIENT_MEMORY;
5137 }
Janos Follath63028dd2019-06-13 09:15:47 +01005138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005139 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005140 prf->label_length = data_length;
5141 }
Janos Follath63028dd2019-06-13 09:15:47 +01005142
Gilles Peskine43f958b2020-12-13 14:55:14 +01005143 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005145 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01005146}
5147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005148static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
5149 psa_key_derivation_step_t step,
5150 const uint8_t *data,
5151 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01005152{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005153 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01005154 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005155 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01005156 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005157 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01005158 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005159 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01005160 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005161 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01005162 }
5163}
John Durkop07cc04a2020-11-16 22:08:34 -08005164#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5165 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5166
5167#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5168static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5169 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005170 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005171 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08005172{
5173 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005174 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08005175 uint8_t *cur = pms;
5176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005177 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
5178 return PSA_ERROR_INVALID_ARGUMENT;
5179 }
John Durkop07cc04a2020-11-16 22:08:34 -08005180
5181 /* Quoting RFC 4279, Section 2:
5182 *
5183 * The premaster secret is formed as follows: if the PSK is N octets
5184 * long, concatenate a uint16 with the value N, N zero octets, a second
5185 * uint16 with the value N, and the PSK itself.
5186 */
5187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005188 *cur++ = MBEDTLS_BYTE_1(data_length);
5189 *cur++ = MBEDTLS_BYTE_0(data_length);
5190 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005191 cur += data_length;
5192 *cur++ = pms[0];
5193 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005194 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005195 cur += data_length;
5196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005197 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08005198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005199 mbedtls_platform_zeroize(pms, sizeof(pms));
5200 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08005201}
Janos Follath6660f0e2019-06-17 08:44:03 +01005202
5203static psa_status_t psa_tls12_prf_psk_to_ms_input(
5204 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005205 psa_key_derivation_step_t step,
5206 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005207 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01005208{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005209 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
5210 return psa_tls12_prf_psk_to_ms_set_key(prf,
5211 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01005212 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005214 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01005215}
John Durkop07cc04a2020-11-16 22:08:34 -08005216#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005217
Gilles Peskineb8965192019-09-24 16:21:10 +02005218/** Check whether the given key type is acceptable for the given
5219 * input step of a key derivation.
5220 *
5221 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5222 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5223 * Both secret and non-secret inputs can alternatively have the type
5224 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5225 * that the input was passed as a buffer rather than via a key object.
5226 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005227static int psa_key_derivation_check_input_type(
5228 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005229 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02005230{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005231 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005232 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005233 if (key_type == PSA_KEY_TYPE_DERIVE) {
5234 return PSA_SUCCESS;
5235 }
5236 if (key_type == PSA_KEY_TYPE_NONE) {
5237 return PSA_SUCCESS;
5238 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005239 break;
5240 case PSA_KEY_DERIVATION_INPUT_LABEL:
5241 case PSA_KEY_DERIVATION_INPUT_SALT:
5242 case PSA_KEY_DERIVATION_INPUT_INFO:
5243 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005244 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
5245 return PSA_SUCCESS;
5246 }
5247 if (key_type == PSA_KEY_TYPE_NONE) {
5248 return PSA_SUCCESS;
5249 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005250 break;
5251 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005252 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005253}
5254
Janos Follathb80a94e2019-06-12 15:54:46 +01005255static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005256 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005257 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005258 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005259 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005260 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005261{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005262 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005263 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005264
Waleed Elmelegy254cada2025-03-11 12:27:34 +00005265 if (kdf_alg == PSA_ALG_NONE) {
Waleed Elmelegye0148872025-03-03 15:01:38 +00005266 /* This is a blank or aborted operation. */
Waleed Elmelegyc8c89ed2025-03-03 12:35:28 +00005267 status = PSA_ERROR_BAD_STATE;
5268 goto exit;
5269 }
5270
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005271 status = psa_key_derivation_check_input_type(step, key_type);
5272 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005273 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005274 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005275
John Durkopd0321952020-10-29 21:37:36 -07005276#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005277 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5278 status = psa_hkdf_input(&operation->ctx.hkdf,
5279 PSA_ALG_HKDF_GET_HASH(kdf_alg),
5280 step, data, data_length);
5281 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005282#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5283#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005284 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5285 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
5286 step, data, data_length);
5287 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005288#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5289#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005290 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5291 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
5292 step, data, data_length);
5293 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005294#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005295 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005296 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005297 (void) data;
5298 (void) data_length;
5299 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005300 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005301 }
5302
Gilles Peskine224b0d62019-09-23 18:13:17 +02005303exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005304 if (status != PSA_SUCCESS) {
5305 psa_key_derivation_abort(operation);
5306 }
5307 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005308}
5309
Janos Follath51f4a0f2019-06-14 11:35:55 +01005310psa_status_t psa_key_derivation_input_bytes(
5311 psa_key_derivation_operation_t *operation,
5312 psa_key_derivation_step_t step,
Ryan Everett6f682062024-02-09 16:18:39 +00005313 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005314 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005315{
Ryan Everett6f682062024-02-09 16:18:39 +00005316 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5317 LOCAL_INPUT_DECLARE(data_external, data);
5318
5319 LOCAL_INPUT_ALLOC(data_external, data_length, data);
5320
5321 status = psa_key_derivation_input_internal(operation, step,
5322 PSA_KEY_TYPE_NONE,
5323 data, data_length);
David Horstmann42015332024-03-13 15:42:31 +00005324#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everett6f682062024-02-09 16:18:39 +00005325exit:
Ryan Everettd0d12fb2024-02-12 09:19:29 +00005326#endif
Ryan Everett6f682062024-02-09 16:18:39 +00005327 LOCAL_INPUT_FREE(data_external, data);
5328 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005329}
5330
Janos Follath51f4a0f2019-06-14 11:35:55 +01005331psa_status_t psa_key_derivation_input_key(
5332 psa_key_derivation_operation_t *operation,
5333 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005334 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005335{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005336 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005337 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005338 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005339
Ronald Cron5c522922020-11-14 16:35:34 +01005340 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005341 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5342 if (status != PSA_SUCCESS) {
5343 psa_key_derivation_abort(operation);
5344 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02005345 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005346
5347 /* Passing a key object as a SECRET input unlocks the permission
5348 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005349 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005350 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005351 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005353 status = psa_key_derivation_input_internal(operation,
5354 step, slot->attr.type,
5355 slot->key.data,
5356 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005358 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005360 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005361}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005362
5363
5364
5365/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005366/* Key agreement */
5367/****************************************************************/
5368
John Durkop6ba40d12020-11-10 08:50:04 -08005369#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005370static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
5371 size_t peer_key_length,
5372 const mbedtls_ecp_keypair *our_key,
5373 uint8_t *shared_secret,
5374 size_t shared_secret_size,
5375 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005376{
Steven Cooremand4867872020-08-05 16:31:39 +02005377 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005378 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005379 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005380 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005381 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
5382 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005383
Ronald Cron90857082020-11-25 14:58:33 +01005384 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005385 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5386 bits,
5387 peer_key,
5388 peer_key_length,
5389 &their_key);
5390 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005391 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005392 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005393
Jaeden Amero97271b32019-01-10 19:38:51 +00005394 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005395 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
5396 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005397 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005398 }
Jaeden Amero97271b32019-01-10 19:38:51 +00005399 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005400 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
5401 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005402 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005403 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005404
Jaeden Amero97271b32019-01-10 19:38:51 +00005405 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005406 mbedtls_ecdh_calc_secret(&ecdh,
5407 shared_secret_length,
5408 shared_secret, shared_secret_size,
5409 mbedtls_psa_get_random,
5410 MBEDTLS_PSA_RANDOM_STATE));
5411 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005412 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005413 }
5414 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005415 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005416 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005417
5418exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005419 if (status != PSA_SUCCESS) {
5420 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
5421 }
5422 mbedtls_ecdh_free(&ecdh);
5423 mbedtls_ecp_keypair_free(their_key);
5424 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02005425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005426 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005427}
John Durkop6ba40d12020-11-10 08:50:04 -08005428#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005429
Gilles Peskine01d718c2018-09-18 12:01:02 +02005430#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005432static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
5433 psa_key_slot_t *private_key,
5434 const uint8_t *peer_key,
5435 size_t peer_key_length,
5436 uint8_t *shared_secret,
5437 size_t shared_secret_size,
5438 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005439{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005440 mbedtls_ecp_keypair *ecp = NULL;
5441 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01005442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005443 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08005444#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005445 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005446 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
5447 return PSA_ERROR_INVALID_ARGUMENT;
5448 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005449 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005450 private_key->attr.type,
5451 private_key->attr.bits,
5452 private_key->key.data,
5453 private_key->key.bytes,
5454 &ecp);
5455 if (status != PSA_SUCCESS) {
5456 return status;
5457 }
5458 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
5459 ecp,
5460 shared_secret, shared_secret_size,
5461 shared_secret_length);
5462 mbedtls_ecp_keypair_free(ecp);
5463 mbedtls_free(ecp);
5464 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08005465#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005466 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005467 (void) ecp;
5468 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01005469 (void) private_key;
5470 (void) peer_key;
5471 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005472 (void) shared_secret;
5473 (void) shared_secret_size;
5474 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005475 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005476 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005477}
5478
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005479/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005480 * to potentially free embedded data structures and wipe confidential data.
5481 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005482static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5483 psa_key_derivation_step_t step,
5484 psa_key_slot_t *private_key,
5485 const uint8_t *peer_key,
5486 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005487{
5488 psa_status_t status;
5489 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5490 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005491 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005492
5493 /* Step 1: run the secret agreement algorithm to generate the shared
5494 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005495 status = psa_key_agreement_raw_internal(ka_alg,
5496 private_key,
5497 peer_key, peer_key_length,
5498 shared_secret,
5499 sizeof(shared_secret),
5500 &shared_secret_length);
5501 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005502 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005503 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005504
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005505 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005506 * the shared secret. A shared secret is permitted wherever a key
5507 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005508 status = psa_key_derivation_input_internal(operation, step,
5509 PSA_KEY_TYPE_DERIVE,
5510 shared_secret,
5511 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005512exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005513 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5514 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005515}
5516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005517psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5518 psa_key_derivation_step_t step,
5519 mbedtls_svc_key_id_t private_key,
Thomas Daubney9da359f2024-02-15 13:15:47 +00005520 const uint8_t *peer_key_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005521 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005522{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005523 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005524 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005525 psa_key_slot_t *slot;
Thomas Daubney9da359f2024-02-15 13:15:47 +00005526 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005528 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5529 return PSA_ERROR_INVALID_ARGUMENT;
5530 }
Ronald Cron5c522922020-11-14 16:35:34 +01005531 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005532 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5533 if (status != PSA_SUCCESS) {
5534 return status;
5535 }
Thomas Daubney9da359f2024-02-15 13:15:47 +00005536
Thomas Daubneyca928312024-03-12 17:53:30 +00005537 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005538 status = psa_key_agreement_internal(operation, step,
5539 slot,
5540 peer_key, peer_key_length);
Thomas Daubney9da359f2024-02-15 13:15:47 +00005541
David Horstmann42015332024-03-13 15:42:31 +00005542#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney9da359f2024-02-15 13:15:47 +00005543exit:
Thomas Daubney3c0c6b12024-02-15 14:25:08 +00005544#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005545 if (status != PSA_SUCCESS) {
5546 psa_key_derivation_abort(operation);
5547 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005548 /* If a private key has been added as SECRET, we allow the derived
5549 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005550 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005551 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005552 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005553 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005555 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005556
Thomas Daubney9da359f2024-02-15 13:15:47 +00005557 LOCAL_INPUT_FREE(peer_key_external, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005558 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005559}
5560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005561psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5562 mbedtls_svc_key_id_t private_key,
Thomas Daubney43042762024-02-15 12:57:26 +00005563 const uint8_t *peer_key_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005564 size_t peer_key_length,
Thomas Daubney43042762024-02-15 12:57:26 +00005565 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005566 size_t output_size,
5567 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005568{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005569 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005570 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005571 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005572 size_t expected_length;
Thomas Daubney43042762024-02-15 12:57:26 +00005573 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
5574 LOCAL_OUTPUT_DECLARE(output_external, output);
Thomas Daubney0736df32024-02-21 12:28:20 +00005575 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005577 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005578 status = PSA_ERROR_INVALID_ARGUMENT;
5579 goto exit;
5580 }
Ronald Cron5c522922020-11-14 16:35:34 +01005581 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005582 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5583 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005584 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005585 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005586
Gilles Peskine42313fb2022-04-14 00:17:15 +02005587 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5588 * for the output size. The PSA specification only guarantees that this
5589 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5590 * but it might be nice to allow smaller buffers if the output fits.
5591 * At the time of writing this comment, with only ECDH implemented,
5592 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5593 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5594 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005595 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005596 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5597 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005598 status = PSA_ERROR_BUFFER_TOO_SMALL;
5599 goto exit;
5600 }
5601
Thomas Daubney43042762024-02-15 12:57:26 +00005602 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005603 status = psa_key_agreement_raw_internal(alg, slot,
5604 peer_key, peer_key_length,
5605 output, output_size,
5606 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005607
5608exit:
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005609 /* Check for successful allocation of output,
5610 * with an unsuccessful status. */
Thomas Daubney2ea8d8f2024-02-21 15:16:01 +00005611 if (output != NULL && status != PSA_SUCCESS) {
5612 /* If an error happens and is not handled properly, the output
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005613 * may be used as a key to protect sensitive data. Arrange for such
5614 * a key to be random, which is likely to result in decryption or
5615 * verification errors. This is better than filling the buffer with
5616 * some constant data such as zeros, which would result in the data
5617 * being protected with a reproducible, easily knowable key.
5618 */
David Horstmann65bf12c2024-02-06 15:27:49 +00005619 psa_generate_random_internal(output, output_size);
Thomas Daubney2ea8d8f2024-02-21 15:16:01 +00005620 *output_length = output_size;
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005621 }
5622
5623 if (output == NULL) {
Thomas Daubney0736df32024-02-21 12:28:20 +00005624 /* output allocation failed. */
5625 *output_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005626 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005627
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005628 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005629
Thomas Daubney43042762024-02-15 12:57:26 +00005630 LOCAL_INPUT_FREE(peer_key_external, peer_key);
5631 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005632 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005633}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005634
5635
5636/****************************************************************/
5637/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005638/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005639
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005640#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5641#include "mbedtls/entropy_poll.h"
5642#endif
5643
Gilles Peskine30524eb2020-11-13 17:02:26 +01005644/** Initialize the PSA random generator.
5645 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005646static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005647{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005648#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005649 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005650#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5651
Gilles Peskine30524eb2020-11-13 17:02:26 +01005652 /* Set default configuration if
5653 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005654 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005655 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005656 }
5657 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005658 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005659 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005661 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005662#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5663 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5664 /* The PSA entropy injection feature depends on using NV seed as an entropy
5665 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005666 mbedtls_entropy_add_source(&rng->entropy,
5667 mbedtls_nv_seed_poll, NULL,
5668 MBEDTLS_ENTROPY_BLOCK_SIZE,
5669 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005670#endif
5671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005672 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005673#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005674}
5675
5676/** Deinitialize the PSA random generator.
5677 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005678static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005679{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005680#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005681 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005682#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005683 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5684 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005685#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005686}
5687
5688/** Seed the PSA random generator.
5689 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005690static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005691{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005692#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5693 /* Do nothing: the external RNG seeds itself. */
5694 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005695 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005696#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005697 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005698 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5699 drbg_seed, sizeof(drbg_seed) - 1);
5700 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005701#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005702}
5703
David Horstmann65bf12c2024-02-06 15:27:49 +00005704psa_status_t psa_generate_random(uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005705 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005706{
David Horstmann65bf12c2024-02-06 15:27:49 +00005707 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005708
David Horstmann65bf12c2024-02-06 15:27:49 +00005709 LOCAL_OUTPUT_DECLARE(output_external, output);
5710 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005711
David Horstmann65bf12c2024-02-06 15:27:49 +00005712 status = psa_generate_random_internal(output, output_size);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005713
David Horstmann42015332024-03-13 15:42:31 +00005714#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann65bf12c2024-02-06 15:27:49 +00005715exit:
David Horstmann10e44f32024-02-28 14:17:10 +00005716#endif
David Horstmann65bf12c2024-02-06 15:27:49 +00005717 LOCAL_OUTPUT_FREE(output_external, output);
5718 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005719}
5720
Gilles Peskine8814fc42020-12-14 15:33:44 +01005721/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005722 *
5723 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5724 * `psa_generate_random(...)`. The state parameter is ignored since the
5725 * PSA API doesn't support passing an explicit state.
5726 *
5727 * In the non-external case, psa_generate_random() calls an
5728 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5729 * and semantics as mbedtls_psa_get_random(). As an optimization,
5730 * instead of doing this back-and-forth between the PSA API and the
5731 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5732 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005733 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005734#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5735int mbedtls_psa_get_random(void *p_rng,
5736 unsigned char *output,
5737 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005738{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005739 /* This function takes a pointer to the RNG state because that's what
5740 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5741 * its own state internally and doesn't let the caller access that state.
5742 * So we just ignore the state parameter, and in practice we'll pass
5743 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005744 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005745 psa_status_t status = psa_generate_random(output, output_size);
5746 if (status == PSA_SUCCESS) {
5747 return 0;
5748 } else {
5749 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5750 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005751}
5752#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5753
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005754#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005755psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5756 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005757{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005758 if (global_data.initialized) {
5759 return PSA_ERROR_NOT_PERMITTED;
5760 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005761
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005762 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5763 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5764 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5765 return PSA_ERROR_INVALID_ARGUMENT;
5766 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005767
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005768 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005769}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005770#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005771
Ronald Cron3772afe2021-02-08 16:10:05 +01005772/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005773 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005774 * \param type The key type
5775 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005776 *
5777 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005778 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005779 * \retval #PSA_ERROR_INVALID_ARGUMENT
5780 * The size in bits of the key is not valid.
5781 * \retval #PSA_ERROR_NOT_SUPPORTED
5782 * The type and/or the size in bits of the key or the combination of
5783 * the two is not supported.
5784 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005785static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005786 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005787{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005788 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005790 if (key_type_is_raw_bytes(type)) {
5791 status = validate_unstructured_key_bit_size(type, bits);
5792 if (status != PSA_SUCCESS) {
5793 return status;
5794 }
5795 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005796#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005797 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5798 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5799 return PSA_ERROR_NOT_SUPPORTED;
5800 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005801
Ronald Cron01b2aba2020-10-05 09:42:02 +02005802 /* Accept only byte-aligned keys, for the same reasons as
5803 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005804 if (bits % 8 != 0) {
5805 return PSA_ERROR_NOT_SUPPORTED;
5806 }
5807 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005808#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005809
Ronald Cron5c4d3862020-12-07 11:07:24 +01005810#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005811 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005812 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005813 return PSA_SUCCESS;
5814 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005815#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005816 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005817 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005818 }
5819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005820 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005821}
5822
Ronald Cron55ed0592020-10-05 10:30:40 +02005823psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005824 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005825 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005826{
5827 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005828 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005830 if ((attributes->domain_parameters == NULL) &&
5831 (attributes->domain_parameters_size != 0)) {
5832 return PSA_ERROR_INVALID_ARGUMENT;
5833 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005835 if (key_type_is_raw_bytes(type)) {
5836 status = psa_generate_random(key_buffer, key_buffer_size);
5837 if (status != PSA_SUCCESS) {
5838 return status;
5839 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005840
David Brown12ca5032021-02-11 11:02:00 -07005841#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005842 if (type == PSA_KEY_TYPE_DES) {
5843 psa_des_set_key_parity(key_buffer, key_buffer_size);
5844 }
David Brown8107e312021-02-12 08:08:46 -07005845#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005846 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005847
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005848#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5849 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005850 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5851 return mbedtls_psa_rsa_generate_key(attributes,
5852 key_buffer,
5853 key_buffer_size,
5854 key_buffer_length);
5855 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005856#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5857 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005858
John Durkop9814fa22020-11-04 12:28:15 -08005859#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005860 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5861 return mbedtls_psa_ecp_generate_key(attributes,
5862 key_buffer,
5863 key_buffer_size,
5864 key_buffer_length);
5865 } else
John Durkop9814fa22020-11-04 12:28:15 -08005866#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005867 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005868 (void) key_buffer_length;
5869 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005870 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005871
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005872 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005873}
Darryl Green0c6575a2018-11-07 16:05:30 +00005874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005875psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5876 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005877{
5878 psa_status_t status;
5879 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005880 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005881 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005882
Ronald Cron81709fc2020-11-14 12:10:32 +01005883 *key = MBEDTLS_SVC_KEY_ID_INIT;
5884
Gilles Peskine0f84d622019-09-12 19:03:13 +02005885 /* Reject any attempt to create a zero-length key so that we don't
5886 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005887 if (psa_get_key_bits(attributes) == 0) {
5888 return PSA_ERROR_INVALID_ARGUMENT;
5889 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005890
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005891 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005892 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5893 return PSA_ERROR_INVALID_ARGUMENT;
5894 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005896 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5897 &slot, &driver);
5898 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005899 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005900 }
Gilles Peskine11792082019-08-06 18:36:36 +02005901
Ronald Cron977c2472020-10-13 08:32:21 +02005902 /* In the case of a transparent key or an opaque key stored in local
5903 * storage (thus not in the case of generating a key in a secure element
5904 * or cryptoprocessor with storage), we have to allocate a buffer to
5905 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005906 if (slot->key.data == NULL) {
5907 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5908 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005909 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005910 attributes->core.type, attributes->core.bits);
5911 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005912 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005913 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005914
5915 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005916 attributes->core.type,
5917 attributes->core.bits);
5918 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005919 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005920 attributes, &key_buffer_size);
5921 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005922 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005923 }
Ronald Cron977c2472020-10-13 08:32:21 +02005924 }
Ronald Cron977c2472020-10-13 08:32:21 +02005925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005926 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5927 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005928 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005929 }
Ronald Cron977c2472020-10-13 08:32:21 +02005930 }
5931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005932 status = psa_driver_wrapper_generate_key(attributes,
5933 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005934
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005935 if (status != PSA_SUCCESS) {
5936 psa_remove_key_data_from_memory(slot);
5937 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005938
Gilles Peskine11792082019-08-06 18:36:36 +02005939exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005940 if (status == PSA_SUCCESS) {
5941 status = psa_finish_key_creation(slot, driver, key);
5942 }
5943 if (status != PSA_SUCCESS) {
5944 psa_fail_key_creation(slot, driver);
5945 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005947 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005948}
5949
Gilles Peskinee59236f2018-01-27 23:32:46 +01005950/****************************************************************/
5951/* Module setup */
5952/****************************************************************/
5953
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005954#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005955psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005956 void (* entropy_init)(mbedtls_entropy_context *ctx),
5957 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005958{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005959 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5960 return PSA_ERROR_BAD_STATE;
5961 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005962 global_data.rng.entropy_init = entropy_init;
5963 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005964 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005965}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005966#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005968void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005969{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005970 psa_wipe_all_key_slots();
5971 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5972 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005973 }
5974 /* Wipe all remaining data, including configuration.
5975 * In particular, this sets all state indicator to the value
5976 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005977 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005978
5979 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005980 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005981}
5982
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005983#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5984/** Recover a transaction that was interrupted by a power failure.
5985 *
5986 * This function is called during initialization, before psa_crypto_init()
5987 * returns. If this function returns a failure status, the initialization
5988 * fails.
5989 */
5990static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005991 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005992{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005993 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005994 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5995 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005996 /* TODO - fall through to the failure case until this
5997 * is implemented.
5998 * https://github.com/ARMmbed/mbed-crypto/issues/218
5999 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006000 default:
6001 /* We found an unsupported transaction in the storage.
6002 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006003 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006004 }
6005}
6006#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6007
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006008psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006009{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006010 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006011
Gilles Peskinec6b69072018-11-20 21:42:52 +01006012 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006013 if (global_data.initialized != 0) {
6014 return PSA_SUCCESS;
6015 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006016
Gilles Peskine30524eb2020-11-13 17:02:26 +01006017 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006018 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01006019 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006020 status = mbedtls_psa_random_seed(&global_data.rng);
6021 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006022 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006023 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01006024 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006026 status = psa_initialize_key_slots();
6027 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01006028 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006029 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01006030
Ronald Cron088d5d02021-04-10 16:57:30 +02006031 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006032 status = psa_driver_wrapper_init();
6033 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02006034 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006035 }
Gilles Peskined9348f22019-10-01 15:22:29 +02006036
Gilles Peskine4b734222019-07-24 15:56:31 +02006037#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006038 status = psa_crypto_load_transaction();
6039 if (status == PSA_SUCCESS) {
6040 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
6041 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006042 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006043 }
6044 status = psa_crypto_stop_transaction();
6045 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02006046 /* There's no transaction to complete. It's all good. */
6047 status = PSA_SUCCESS;
6048 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006049#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006050
Gilles Peskinec6b69072018-11-20 21:42:52 +01006051 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006052 global_data.initialized = 1;
6053
6054exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006055 if (status != PSA_SUCCESS) {
6056 mbedtls_psa_crypto_free();
6057 }
6058 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006059}
6060
David Horstmann114d8242023-12-07 18:39:17 +00006061/* Memory copying test hooks. These are called before input copy, after input
6062 * copy, before output copy and after output copy, respectively.
6063 * They are used by memory-poisoning tests to temporarily unpoison buffers
6064 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00006065#if defined(MBEDTLS_TEST_HOOKS)
6066void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
6067void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
6068void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
6069void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
6070#endif
6071
David Horstmanndf493552023-11-15 15:18:30 +00006072/** Copy from an input buffer to a local copy.
6073 *
6074 * \param[in] input Pointer to input buffer.
6075 * \param[in] input_len Length of the input buffer.
6076 * \param[out] input_copy Pointer to a local copy in which to store the input data.
6077 * \param[out] input_copy_len Length of the local copy buffer.
6078 * \return #PSA_SUCCESS, if the buffer was successfully
6079 * copied.
6080 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
6081 * copy is too small to hold contents of the
6082 * input buffer.
6083 */
6084MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00006085psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
6086 uint8_t *input_copy, size_t input_copy_len)
6087{
6088 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00006089 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00006090 }
6091
David Horstmann0760b152023-11-24 16:21:04 +00006092#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006093 if (psa_input_pre_copy_hook != NULL) {
6094 psa_input_pre_copy_hook(input, input_len);
6095 }
David Horstmann0760b152023-11-24 16:21:04 +00006096#endif
6097
David Horstmann660027f2023-11-15 17:33:47 +00006098 if (input_len > 0) {
6099 memcpy(input_copy, input, input_len);
6100 }
David Horstmann957f9802023-10-30 20:29:43 +00006101
David Horstmann0760b152023-11-24 16:21:04 +00006102#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006103 if (psa_input_post_copy_hook != NULL) {
6104 psa_input_post_copy_hook(input, input_len);
6105 }
David Horstmann0760b152023-11-24 16:21:04 +00006106#endif
6107
David Horstmann957f9802023-10-30 20:29:43 +00006108 return PSA_SUCCESS;
6109}
6110
David Horstmanndf493552023-11-15 15:18:30 +00006111/** Copy from a local output buffer into a user-supplied one.
6112 *
6113 * \param[in] output_copy Pointer to a local buffer containing the output.
6114 * \param[in] output_copy_len Length of the local buffer.
6115 * \param[out] output Pointer to user-supplied output buffer.
6116 * \param[out] output_len Length of the user-supplied output buffer.
6117 * \return #PSA_SUCCESS, if the buffer was successfully
6118 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00006119 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00006120 * user-supplied output buffer is too small to
6121 * hold the contents of the local buffer.
6122 */
6123MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00006124psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
6125 uint8_t *output, size_t output_len)
6126{
6127 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00006128 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00006129 }
David Horstmann660027f2023-11-15 17:33:47 +00006130
David Horstmann0760b152023-11-24 16:21:04 +00006131#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006132 if (psa_output_pre_copy_hook != NULL) {
6133 psa_output_pre_copy_hook(output, output_len);
6134 }
David Horstmann0760b152023-11-24 16:21:04 +00006135#endif
6136
David Horstmann660027f2023-11-15 17:33:47 +00006137 if (output_copy_len > 0) {
6138 memcpy(output, output_copy, output_copy_len);
6139 }
6140
David Horstmann0760b152023-11-24 16:21:04 +00006141#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006142 if (psa_output_post_copy_hook != NULL) {
6143 psa_output_post_copy_hook(output, output_len);
6144 }
David Horstmann0760b152023-11-24 16:21:04 +00006145#endif
6146
David Horstmann2bd296e2023-10-30 20:37:12 +00006147 return PSA_SUCCESS;
6148}
6149
David Horstmann81a0d572023-11-20 17:15:32 +00006150psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
6151 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006152{
6153 psa_status_t status;
6154
David Horstmann0d52c712023-11-23 15:50:37 +00006155 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006156
David Horstmann726bf052023-11-15 18:11:26 +00006157 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006158 return PSA_SUCCESS;
6159 }
6160
David Horstmann81a0d572023-11-20 17:15:32 +00006161 local_input->buffer = mbedtls_calloc(input_len, 1);
6162 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006163 /* Since we dealt with the zero-length case above, we know that
6164 * a NULL return value means a failure of allocation. */
6165 return PSA_ERROR_INSUFFICIENT_MEMORY;
6166 }
David Horstmann81a0d572023-11-20 17:15:32 +00006167 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006168
David Horstmann81a0d572023-11-20 17:15:32 +00006169 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006170
6171 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00006172 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006173 if (status != PSA_SUCCESS) {
6174 goto error;
6175 }
6176
6177 return PSA_SUCCESS;
6178
6179error:
Gilles Peskine466ebe82025-03-06 12:41:39 +01006180 if (local_input->buffer != NULL) {
6181 mbedtls_platform_zeroize(local_input->buffer, local_input->length);
6182 mbedtls_free(local_input->buffer);
6183 local_input->buffer = NULL;
6184 }
David Horstmann81a0d572023-11-20 17:15:32 +00006185 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006186 return status;
6187}
6188
David Horstmann81a0d572023-11-20 17:15:32 +00006189void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00006190{
Gilles Peskine466ebe82025-03-06 12:41:39 +01006191 if (local_input->buffer != NULL) {
6192 mbedtls_platform_zeroize(local_input->buffer, local_input->length);
6193 mbedtls_free(local_input->buffer);
6194 local_input->buffer = NULL;
6195 }
David Horstmann81a0d572023-11-20 17:15:32 +00006196 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00006197}
6198
David Horstmann1a76ab12023-11-20 12:54:09 +00006199psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
6200 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00006201{
David Horstmann0d52c712023-11-23 15:50:37 +00006202 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00006203
David Horstmann726bf052023-11-15 18:11:26 +00006204 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006205 return PSA_SUCCESS;
6206 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006207 local_output->buffer = mbedtls_calloc(output_len, 1);
6208 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006209 /* Since we dealt with the zero-length case above, we know that
6210 * a NULL return value means a failure of allocation. */
6211 return PSA_ERROR_INSUFFICIENT_MEMORY;
6212 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006213 local_output->length = output_len;
6214 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00006215
6216 return PSA_SUCCESS;
6217}
6218
David Horstmann1a76ab12023-11-20 12:54:09 +00006219psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00006220{
David Horstmann761761f2023-11-15 15:57:32 +00006221 psa_status_t status;
6222
David Horstmann1a76ab12023-11-20 12:54:09 +00006223 if (local_output->buffer == NULL) {
6224 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006225 return PSA_SUCCESS;
6226 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006227 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00006228 /* We have an internal copy but nothing to copy back to. */
6229 return PSA_ERROR_CORRUPTION_DETECTED;
6230 }
6231
David Horstmann1a76ab12023-11-20 12:54:09 +00006232 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
6233 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00006234 if (status != PSA_SUCCESS) {
6235 return status;
6236 }
David Horstmannaeeb2742023-11-08 17:44:18 +00006237
Gilles Peskine466ebe82025-03-06 12:41:39 +01006238 if (local_output->buffer != NULL) {
6239 mbedtls_platform_zeroize(local_output->buffer, local_output->length);
6240 mbedtls_free(local_output->buffer);
6241 local_output->buffer = NULL;
6242 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006243 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006244
6245 return PSA_SUCCESS;
6246}
6247
Gilles Peskinee59236f2018-01-27 23:32:46 +01006248#endif /* MBEDTLS_PSA_CRYPTO_C */