blob: a58033395ec397aa6762efe435d2f421d49698cd [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,
1625 "One or more key attribute flag is listed as both external-only and dual-use")
1626MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1627 "One or more key attribute flag is listed as both internal-only and dual-use")
1628MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1629 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001630
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001631/** Validate that a key policy is internally well-formed.
1632 *
1633 * This function only rejects invalid policies. It does not validate the
1634 * consistency of the policy with respect to other attributes of the key
1635 * such as the key type.
1636 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001637static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001638{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001639 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1640 PSA_KEY_USAGE_COPY |
1641 PSA_KEY_USAGE_ENCRYPT |
1642 PSA_KEY_USAGE_DECRYPT |
1643 PSA_KEY_USAGE_SIGN_MESSAGE |
1644 PSA_KEY_USAGE_VERIFY_MESSAGE |
1645 PSA_KEY_USAGE_SIGN_HASH |
1646 PSA_KEY_USAGE_VERIFY_HASH |
1647 PSA_KEY_USAGE_DERIVE)) != 0) {
1648 return PSA_ERROR_INVALID_ARGUMENT;
1649 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001651 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001652}
1653
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001654/** Validate the internal consistency of key attributes.
1655 *
1656 * This function only rejects invalid attribute values. If does not
1657 * validate the consistency of the attributes with any key data that may
1658 * be involved in the creation of the key.
1659 *
1660 * Call this function early in the key creation process.
1661 *
1662 * \param[in] attributes Key attributes for the new key.
1663 * \param[out] p_drv On any return, the driver for the key, if any.
1664 * NULL for a transparent key.
1665 *
1666 */
1667static psa_status_t psa_validate_key_attributes(
1668 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001669 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001670{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001671 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001672 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1673 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001675 status = psa_validate_key_location(lifetime, p_drv);
1676 if (status != PSA_SUCCESS) {
1677 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001678 }
1679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001680 status = psa_validate_key_persistence(lifetime);
1681 if (status != PSA_SUCCESS) {
1682 return status;
1683 }
1684
1685 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1686 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1687 return PSA_ERROR_INVALID_ARGUMENT;
1688 }
1689 } else {
1690 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1691 return PSA_ERROR_INVALID_ARGUMENT;
1692 }
1693 }
1694
1695 status = psa_validate_key_policy(&attributes->core.policy);
1696 if (status != PSA_SUCCESS) {
1697 return status;
1698 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001699
1700 /* Refuse to create overly large keys.
1701 * Note that this doesn't trigger on import if the attributes don't
1702 * explicitly specify a size (so psa_get_key_bits returns 0), so
1703 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001704 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1705 return PSA_ERROR_NOT_SUPPORTED;
1706 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001707
Gilles Peskine91e8c332019-08-02 19:19:39 +02001708 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001709 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1710 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1711 return PSA_ERROR_INVALID_ARGUMENT;
1712 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001714 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001715}
1716
Gilles Peskine4747d192019-04-17 15:05:45 +02001717/** Prepare a key slot to receive key material.
1718 *
1719 * This function allocates a key slot and sets its metadata.
1720 *
1721 * If this function fails, call psa_fail_key_creation().
1722 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001723 * This function is intended to be used as follows:
1724 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001725 * it with the specified attributes, and in case of a volatile key assign it
1726 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001727 * -# Populate the slot with the key material.
1728 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1729 * In case of failure at any step, stop the sequence and call
1730 * psa_fail_key_creation().
1731 *
Ronald Cron5c522922020-11-14 16:35:34 +01001732 * On success, the key slot is locked. It is the responsibility of the caller
1733 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001734 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001735 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001736 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001737 * \param[out] p_slot On success, a pointer to the prepared slot.
1738 * \param[out] p_drv On any return, the driver for the key, if any.
1739 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001740 *
1741 * \retval #PSA_SUCCESS
1742 * The key slot is ready to receive key material.
1743 * \return If this function fails, the key slot is an invalid state.
1744 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001745 */
1746static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001747 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001748 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001749 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001750 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001751{
1752 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001753 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001754 psa_key_slot_t *slot;
1755
Gilles Peskinedf179142019-07-15 22:02:14 +02001756 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001757 *p_drv = NULL;
1758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001759 status = psa_validate_key_attributes(attributes, p_drv);
1760 if (status != PSA_SUCCESS) {
1761 return status;
1762 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001764 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1765 if (status != PSA_SUCCESS) {
1766 return status;
1767 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001768 slot = *p_slot;
1769
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001770 /* We're storing the declared bit-size of the key. It's up to each
1771 * creation mechanism to verify that this information is correct.
1772 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001773 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001774 * is optional (import, copy). In case of a volatile key, assign it the
1775 * volatile key identifier associated to the slot returned to contain its
1776 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001777
1778 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001779 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001780#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1781 slot->attr.id = volatile_key_id;
1782#else
1783 slot->attr.id.key_id = volatile_key_id;
1784#endif
1785 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001786
Gilles Peskine91e8c332019-08-02 19:19:39 +02001787 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001788 * external-only flags, query `attributes`. Thanks to the check
1789 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001790 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001791 * may have set. */
1792 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001793
Gilles Peskinecbaff462019-07-12 23:46:04 +02001794#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001795 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001796 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001797 * create the key file in internal storage, create the
1798 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001799 * persistent data. This is done by starting a transaction that will
1800 * encompass these three actions.
1801 * For registering a volatile key, we just need to find an appropriate
1802 * slot number inside the SE. Since the key is designated volatile, creating
1803 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001804 /* The first thing to do is to find a slot number for the new key.
1805 * We save the slot number in persistent storage as part of the
1806 * transaction data. It will be needed to recover if the power
1807 * fails during the key creation process, to clean up on the secure
1808 * element side after restarting. Obtaining a slot number from the
1809 * secure element driver updates its persistent state, but we do not yet
1810 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001811 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001812 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001813 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001814 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1815 &slot_number);
1816 if (status != PSA_SUCCESS) {
1817 return status;
1818 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001820 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1821 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001822 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001823 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001824 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001825 status = psa_crypto_save_transaction();
1826 if (status != PSA_SUCCESS) {
1827 (void) psa_crypto_stop_transaction();
1828 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001829 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001830 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001831
1832 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001833 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Ryan Everett68a85e22023-11-16 16:44:13 +00001834 if (status != PSA_SUCCESS) {
Ryan Everett167ecba2023-11-20 12:16:18 +00001835 return status;
Ryan Everett68a85e22023-11-16 16:44:13 +00001836 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001837 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001839 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001840 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001841 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001842 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001843#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001845 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001846}
Gilles Peskine4747d192019-04-17 15:05:45 +02001847
1848/** Finalize the creation of a key once its key material has been set.
1849 *
1850 * This entails writing the key to persistent storage.
1851 *
1852 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001853 * See the documentation of psa_start_key_creation() for the intended use
1854 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001855 *
Ronald Cron5c522922020-11-14 16:35:34 +01001856 * If the finalization succeeds, the function unlocks the key slot (it was
1857 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1858 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001859 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001860 * \param[in,out] slot Pointer to the slot with key material.
1861 * \param[in] driver The secure element driver for the key,
1862 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001863 * \param[out] key On success, identifier of the key. Note that the
1864 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001865 *
1866 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001867 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001868 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1869 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1870 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1871 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1872 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1873 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001874 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001875 * \return If this function fails, the key slot is an invalid state.
1876 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001877 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001878static psa_status_t psa_finish_key_creation(
1879 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001880 psa_se_drv_table_entry_t *driver,
1881 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001882{
1883 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001884 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001885 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001886
1887#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001888 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001889#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001890 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001891 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001892 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001893 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001894
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001895 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1896 sizeof(data.slot_number),
1897 "Slot number size does not match psa_se_key_data_storage_t");
1898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001899 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1900 status = psa_save_persistent_key(&slot->attr,
1901 (uint8_t *) &data,
1902 sizeof(data));
1903 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001904#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1905 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001906 /* Key material is saved in export representation in the slot, so
1907 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001908 status = psa_save_persistent_key(&slot->attr,
1909 slot->key.data,
1910 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001911 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001912 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001913#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1914
Gilles Peskinecbaff462019-07-12 23:46:04 +02001915#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001916 /* Finish the transaction for a key creation. This does not
1917 * happen when registering an existing key. Detect this case
1918 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001919 * creation of a persistent key in a secure element requires a transaction,
1920 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001921 if (driver != NULL &&
1922 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1923 status = psa_save_se_persistent_data(driver);
1924 if (status != PSA_SUCCESS) {
1925 psa_destroy_persistent_key(slot->attr.id);
1926 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001927 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001928 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001929 }
1930#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001932 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001933 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001934 status = psa_unlock_key_slot(slot);
1935 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001936 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001937 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001938 }
Ronald Cron50972942020-11-14 11:28:25 +01001939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001940 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001941}
1942
1943/** Abort the creation of a key.
1944 *
1945 * You may call this function after calling psa_start_key_creation(),
1946 * or after psa_finish_key_creation() fails. In other circumstances, this
1947 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001948 * See the documentation of psa_start_key_creation() for the intended use
1949 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001950 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001951 * \param[in,out] slot Pointer to the slot with key material.
1952 * \param[in] driver The secure element driver for the key,
1953 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001954 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001955static void psa_fail_key_creation(psa_key_slot_t *slot,
1956 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001957{
Gilles Peskine011e4282019-06-26 18:34:38 +02001958 (void) driver;
1959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001960 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001961 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001962 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001963
1964#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001965 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001966 * element, and the failure happened later (when saving metadata
1967 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001968 * element.
1969 * https://github.com/ARMmbed/mbed-crypto/issues/217
1970 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001971
Gilles Peskined7729582019-08-05 15:55:54 +02001972 /* Abort the ongoing transaction if any (there may not be one if
1973 * the creation process failed before starting one, or if the
1974 * key creation is a registration of a key in a secure element).
1975 * Earlier functions must already have done what it takes to undo any
1976 * partial creation. All that's left is to update the transaction data
1977 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001978 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001979#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001981 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001982}
1983
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001984/** Validate optional attributes during key creation.
1985 *
1986 * Some key attributes are optional during key creation. If they are
1987 * specified in the attributes structure, check that they are consistent
1988 * with the data in the slot.
1989 *
1990 * This function should be called near the end of key creation, after
1991 * the slot in memory is fully populated but before saving persistent data.
1992 */
1993static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001994 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001995 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001996{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001997 if (attributes->core.type != 0) {
1998 if (attributes->core.type != slot->attr.type) {
1999 return PSA_ERROR_INVALID_ARGUMENT;
2000 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002001 }
2002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002003 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07002004#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002005 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2006 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002007 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002008 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002009 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002010
Ronald Cron90857082020-11-25 14:58:33 +01002011 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002012 slot->attr.type,
2013 slot->key.data,
2014 slot->key.bytes,
2015 &rsa);
2016 if (status != PSA_SUCCESS) {
2017 return status;
2018 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002020 mbedtls_mpi_init(&actual);
2021 mbedtls_mpi_init(&required);
2022 ret = mbedtls_rsa_export(rsa,
2023 NULL, NULL, NULL, NULL, &actual);
2024 mbedtls_rsa_free(rsa);
2025 mbedtls_free(rsa);
2026 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002027 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028 }
2029 ret = mbedtls_mpi_read_binary(&required,
2030 attributes->domain_parameters,
2031 attributes->domain_parameters_size);
2032 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002033 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002034 }
2035 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002036 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002037 }
2038rsa_exit:
2039 mbedtls_mpi_free(&actual);
2040 mbedtls_mpi_free(&required);
2041 if (ret != 0) {
2042 return mbedtls_to_psa_error(ret);
2043 }
2044 } else
John Durkop9814fa22020-11-04 12:28:15 -08002045#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2046 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002047 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002048 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002049 }
2050 }
2051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002052 if (attributes->core.bits != 0) {
2053 if (attributes->core.bits != slot->attr.bits) {
2054 return PSA_ERROR_INVALID_ARGUMENT;
2055 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002056 }
2057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002058 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002059}
2060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002061psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Ryan Everett6b970252024-01-19 16:02:59 +00002062 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002063 size_t data_length,
2064 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002065{
2066 psa_status_t status;
Ryan Everett6b970252024-01-19 16:02:59 +00002067 LOCAL_INPUT_DECLARE(data_external, data);
Gilles Peskine4747d192019-04-17 15:05:45 +02002068 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002069 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002070 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002071
Ronald Cron81709fc2020-11-14 12:10:32 +01002072 *key = MBEDTLS_SVC_KEY_ID_INIT;
2073
Gilles Peskine0f84d622019-09-12 19:03:13 +02002074 /* Reject zero-length symmetric keys (including raw data key objects).
2075 * This also rejects any key which might be encoded as an empty string,
2076 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002077 if (data_length == 0) {
2078 return PSA_ERROR_INVALID_ARGUMENT;
2079 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002080
Ryan Everett6b970252024-01-19 16:02:59 +00002081 LOCAL_INPUT_ALLOC(data_external, data_length, data);
2082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002083 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2084 &slot, &driver);
2085 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002086 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002087 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002088
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002089 /* In the case of a transparent key or an opaque key stored in local
2090 * storage (thus not in the case of generating a key in a secure element
2091 * or cryptoprocessor with storage), we have to allocate a buffer to
2092 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002093 if (slot->key.data == NULL) {
2094 status = psa_allocate_buffer_to_slot(slot, data_length);
2095 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002096 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002097 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002098 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002099
2100 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 status = psa_driver_wrapper_import_key(attributes,
2102 data, data_length,
2103 slot->key.data,
2104 slot->key.bytes,
2105 &slot->key.bytes, &bits);
2106 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002107 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002108 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002110 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002111 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002112 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002113 status = PSA_ERROR_INVALID_ARGUMENT;
2114 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002115 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002117 status = psa_validate_optional_attributes(slot, attributes);
2118 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002119 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002120 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002122 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002123exit:
Ryan Everett6b970252024-01-19 16:02:59 +00002124 LOCAL_INPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002125 if (status != PSA_SUCCESS) {
2126 psa_fail_key_creation(slot, driver);
2127 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002129 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002130}
2131
Gilles Peskined7729582019-08-05 15:55:54 +02002132#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2133psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002134 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002135{
2136 psa_status_t status;
2137 psa_key_slot_t *slot = NULL;
2138 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002139 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002140
2141 /* Leaving attributes unspecified is not currently supported.
2142 * It could make sense to query the key type and size from the
2143 * secure element, but not all secure elements support this
2144 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002145 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2146 return PSA_ERROR_NOT_SUPPORTED;
2147 }
2148 if (psa_get_key_bits(attributes) == 0) {
2149 return PSA_ERROR_NOT_SUPPORTED;
2150 }
Gilles Peskined7729582019-08-05 15:55:54 +02002151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2153 &slot, &driver);
2154 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002155 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002156 }
Gilles Peskined7729582019-08-05 15:55:54 +02002157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002158 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002159
2160exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002161 if (status != PSA_SUCCESS) {
2162 psa_fail_key_creation(slot, driver);
2163 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002164
Gilles Peskined7729582019-08-05 15:55:54 +02002165 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002166 psa_close_key(key);
2167 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002168}
2169#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002171static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2172 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002173{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002174 psa_status_t status = psa_copy_key_material_into_slot(target,
2175 source->key.data,
2176 source->key.bytes);
2177 if (status != PSA_SUCCESS) {
2178 return status;
2179 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002180
Steven Cooreman398aee52020-10-13 14:35:45 +02002181 target->attr.type = source->attr.type;
2182 target->attr.bits = source->attr.bits;
2183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002184 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002185}
2186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002187psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2188 const psa_key_attributes_t *specified_attributes,
2189 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002190{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002192 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002193 psa_key_slot_t *source_slot = NULL;
2194 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002195 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002196 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002197
Ronald Cron81709fc2020-11-14 12:10:32 +01002198 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2199
Ronald Cron5c522922020-11-14 16:35:34 +01002200 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002201 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2202 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002203 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002204 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002206 status = psa_validate_optional_attributes(source_slot,
2207 specified_attributes);
2208 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002209 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002210 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002212 status = psa_restrict_key_policy(source_slot->attr.type,
2213 &actual_attributes.core.policy,
2214 &source_slot->attr.policy);
2215 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002216 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002217 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002219 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2220 &target_slot, &driver);
2221 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002222 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002223 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002224
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002225#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002226 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002227 /* Copying to a secure element is not implemented yet. */
2228 status = PSA_ERROR_NOT_SUPPORTED;
2229 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002230 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002231#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002233 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002234 /*
2235 * Copying through an opaque driver is not implemented yet, consider
2236 * a lifetime with an external location as an invalid parameter for
2237 * now.
2238 */
2239 status = PSA_ERROR_INVALID_ARGUMENT;
2240 goto exit;
2241 }
2242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002243 status = psa_copy_key_material(source_slot, target_slot);
2244 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002245 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002246 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002248 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002249exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002250 if (status != PSA_SUCCESS) {
2251 psa_fail_key_creation(target_slot, driver);
2252 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002254 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002256 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002257}
2258
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002259
2260
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002261/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002262/* Message digests */
2263/****************************************************************/
2264
Gilles Peskineb6e362b2024-04-10 20:05:06 +02002265static int is_hash_supported(psa_algorithm_t alg)
2266{
2267 switch (alg) {
2268#if defined(PSA_WANT_ALG_MD2)
2269 case PSA_ALG_MD2:
2270 return 1;
2271#endif
2272#if defined(PSA_WANT_ALG_MD4)
2273 case PSA_ALG_MD4:
2274 return 1;
2275#endif
2276#if defined(PSA_WANT_ALG_MD5)
2277 case PSA_ALG_MD5:
2278 return 1;
2279#endif
2280#if defined(PSA_WANT_ALG_RIPEMD160)
2281 case PSA_ALG_RIPEMD160:
2282 return 1;
2283#endif
2284#if defined(PSA_WANT_ALG_SHA_1)
2285 case PSA_ALG_SHA_1:
2286 return 1;
2287#endif
2288#if defined(PSA_WANT_ALG_SHA_224)
2289 case PSA_ALG_SHA_224:
2290 return 1;
2291#endif
2292#if defined(PSA_WANT_ALG_SHA_256)
2293 case PSA_ALG_SHA_256:
2294 return 1;
2295#endif
2296#if defined(PSA_WANT_ALG_SHA_384)
2297 case PSA_ALG_SHA_384:
2298 return 1;
2299#endif
2300#if defined(PSA_WANT_ALG_SHA_512)
2301 case PSA_ALG_SHA_512:
2302 return 1;
2303#endif
2304 default:
2305 return 0;
2306 }
2307}
2308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002309psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002310{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002311 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002312 if (operation->id == 0) {
2313 return PSA_SUCCESS;
2314 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002316 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002317 operation->id = 0;
2318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002319 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002320}
2321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002322psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2323 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002325 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002326
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002327 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002328 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002329 status = PSA_ERROR_BAD_STATE;
2330 goto exit;
2331 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002333 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002334 status = PSA_ERROR_INVALID_ARGUMENT;
2335 goto exit;
2336 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002337
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002338 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2339 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002340 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002342 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002343
2344exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002345 if (status != PSA_SUCCESS) {
2346 psa_hash_abort(operation);
2347 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002348
2349 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002350}
2351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002352psa_status_t psa_hash_update(psa_hash_operation_t *operation,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002353 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002354 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002355{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002357 LOCAL_INPUT_DECLARE(input_external, input);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002359 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002360 status = PSA_ERROR_BAD_STATE;
2361 goto exit;
2362 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002363
Steven Cooremanc8288352021-03-04 14:02:19 +01002364 /* Don't require hash implementations to behave correctly on a
2365 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002366 if (input_length == 0) {
2367 return PSA_SUCCESS;
2368 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002369
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002370 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002371 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002372
2373exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002374 if (status != PSA_SUCCESS) {
2375 psa_hash_abort(operation);
2376 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002377
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002378 LOCAL_INPUT_FREE(input_external, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002379 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002380}
2381
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002382static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
Thomas Daubneycbf09212024-01-25 17:14:29 +00002383 uint8_t *hash,
2384 size_t hash_size,
2385 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386{
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002387 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2388
Steven Cooremanfbe09282021-03-08 18:41:12 +01002389 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002390 if (operation->id == 0) {
2391 return PSA_ERROR_BAD_STATE;
2392 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002393
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002394 status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002395 operation, hash, hash_size, hash_length);
2396 psa_hash_abort(operation);
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002397
2398 return status;
2399}
2400
2401psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2402 uint8_t *hash_external,
2403 size_t hash_size,
2404 size_t *hash_length)
2405{
2406 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2407 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2408
2409 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2410 status = psa_hash_finish_internal(operation, hash, hash_size, hash_length);
2411
David Horstmann42015332024-03-13 15:42:31 +00002412#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002413exit:
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002414#endif
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002415 LOCAL_OUTPUT_FREE(hash_external, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002416 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002417}
2418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002419psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002420 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002421 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002422{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002423 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002424 size_t actual_hash_length;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002425 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2426 LOCAL_INPUT_DECLARE(hash_external, hash);
2427
2428 status = psa_hash_finish_internal(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002429 operation,
2430 actual_hash, sizeof(actual_hash),
2431 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002433 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002434 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002435 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002437 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002438 status = PSA_ERROR_INVALID_SIGNATURE;
2439 goto exit;
2440 }
2441
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002442 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002443 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002444 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002445 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002446
2447exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002448 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2449 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002450 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002451 }
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002452 LOCAL_INPUT_FREE(hash_external, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002453 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002454}
2455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002456psa_status_t psa_hash_compute(psa_algorithm_t alg,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002457 const uint8_t *input_external, size_t input_length,
2458 uint8_t *hash_external, size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002459 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002460{
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002461 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2462 LOCAL_INPUT_DECLARE(input_external, input);
2463 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2464
Steven Cooremanfbe09282021-03-08 18:41:12 +01002465 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002466 if (!PSA_ALG_IS_HASH(alg)) {
2467 return PSA_ERROR_INVALID_ARGUMENT;
2468 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002469
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002470 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2471 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2472 status = psa_driver_wrapper_hash_compute(alg, input, input_length,
Thomas Daubneycbf09212024-01-25 17:14:29 +00002473 hash, hash_size, hash_length);
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002474
David Horstmann42015332024-03-13 15:42:31 +00002475#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002476exit:
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002477#endif
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002478 LOCAL_INPUT_FREE(input_external, input);
2479 LOCAL_OUTPUT_FREE(hash_external, hash);
2480 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002481}
2482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002483psa_status_t psa_hash_compare(psa_algorithm_t alg,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002484 const uint8_t *input_external, size_t input_length,
2485 const uint8_t *hash_external, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002486{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002487 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002488 size_t actual_hash_length;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002489 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2490
2491 LOCAL_INPUT_DECLARE(input_external, input);
2492 LOCAL_INPUT_DECLARE(hash_external, hash);
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002494 if (!PSA_ALG_IS_HASH(alg)) {
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002495 status = PSA_ERROR_INVALID_ARGUMENT;
2496 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002497 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002498
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002499 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2500 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002501 alg, input, input_length,
2502 actual_hash, sizeof(actual_hash),
2503 &actual_hash_length);
2504 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002505 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002506 }
2507 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002508 status = PSA_ERROR_INVALID_SIGNATURE;
2509 goto exit;
2510 }
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002511
2512 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002513 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002514 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002515 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002516
2517exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002518 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002519
2520 LOCAL_INPUT_FREE(input_external, input);
2521 LOCAL_INPUT_FREE(hash_external, hash);
2522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002523 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002524}
2525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002526psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2527 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002528{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002529 if (source_operation->id == 0 ||
2530 target_operation->id != 0) {
2531 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002532 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002534 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2535 target_operation);
2536 if (status != PSA_SUCCESS) {
2537 psa_hash_abort(target_operation);
2538 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002540 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002541}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002542
2543
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002544/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545/* MAC */
2546/****************************************************************/
2547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002548psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002549{
Steven Cooreman07897832021-03-19 18:28:56 +01002550 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002551 if (operation->id == 0) {
2552 return PSA_SUCCESS;
2553 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002555 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002556 operation->mac_size = 0;
2557 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002558 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002560 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002561}
2562
Ronald Cron1c650a12021-06-17 16:33:22 +02002563static psa_status_t psa_mac_finalize_alg_and_key_validation(
2564 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002565 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002566 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002567{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002568 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002569 psa_key_type_t key_type = psa_get_key_type(attributes);
2570 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002572 if (!PSA_ALG_IS_MAC(alg)) {
2573 return PSA_ERROR_INVALID_ARGUMENT;
2574 }
Steven Cooreman07897832021-03-19 18:28:56 +01002575
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002576 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002577 status = psa_mac_key_can_do(alg, key_type);
2578 if (status != PSA_SUCCESS) {
2579 return status;
2580 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002581
Steven Cooremana6474de2021-05-10 11:13:41 +02002582 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002583 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002585 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002586 /* A very short MAC is too short for security since it can be
2587 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2588 * so we make this our minimum, even though 32 bits is still
2589 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002590 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002591 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002593 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2594 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002595 /* It's impossible to "truncate" to a larger length than the full length
2596 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002597 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002598 }
2599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002600 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002601 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2602 * that is disabled in the compile-time configuration. The result can
2603 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2604 * configuration into account. In this case, force a return of
2605 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2606 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2607 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2608 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2609 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002610 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002611 }
2612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002613 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002614}
2615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002616static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2617 mbedtls_svc_key_id_t key,
2618 psa_algorithm_t alg,
2619 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002620{
2621 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2622 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002623 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002624 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002625
2626 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002627 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002628 status = PSA_ERROR_BAD_STATE;
2629 goto exit;
2630 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002631
2632 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002633 key,
2634 &slot,
2635 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2636 alg);
2637 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002638 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002639 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002640
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002641 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002642 .core = slot->attr
2643 };
2644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002645 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2646 &operation->mac_size);
2647 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002648 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002649 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002650
2651 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002652 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002653 if (is_sign) {
2654 status = psa_driver_wrapper_mac_sign_setup(operation,
2655 &attributes,
2656 slot->key.data,
2657 slot->key.bytes,
2658 alg);
2659 } else {
2660 status = psa_driver_wrapper_mac_verify_setup(operation,
2661 &attributes,
2662 slot->key.data,
2663 slot->key.bytes,
2664 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002665 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002666
Gilles Peskinefbfac682018-07-08 20:51:54 +02002667exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002668 if (status != PSA_SUCCESS) {
2669 psa_mac_abort(operation);
2670 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002672 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002674 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002675}
2676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002677psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2678 mbedtls_svc_key_id_t key,
2679 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002680{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002681 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002682}
2683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002684psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2685 mbedtls_svc_key_id_t key,
2686 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002687{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002688 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002689}
2690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002691psa_status_t psa_mac_update(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002692 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002693 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694{
Thomas Daubney324f7de2024-01-18 13:18:58 +00002695 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2696 LOCAL_INPUT_DECLARE(input_external, input);
2697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002698 if (operation->id == 0) {
Thomas Daubney324f7de2024-01-18 13:18:58 +00002699 status = PSA_ERROR_BAD_STATE;
2700 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002701 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002702
Steven Cooreman11743f92021-03-19 18:38:46 +01002703 /* Don't require hash implementations to behave correctly on a
2704 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002705 if (input_length == 0) {
Thomas Daubney324f7de2024-01-18 13:18:58 +00002706 status = PSA_SUCCESS;
2707 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002708 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002709
Thomas Daubney324f7de2024-01-18 13:18:58 +00002710 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2711 status = psa_driver_wrapper_mac_update(operation, input, input_length);
2712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002713 if (status != PSA_SUCCESS) {
2714 psa_mac_abort(operation);
2715 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002716
David Horstmann42015332024-03-13 15:42:31 +00002717#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney324f7de2024-01-18 13:18:58 +00002718exit:
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002719#endif
Thomas Daubney324f7de2024-01-18 13:18:58 +00002720 LOCAL_INPUT_FREE(input_external, input);
2721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002722 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002723}
2724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002725psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002726 uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002727 size_t mac_size,
2728 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002729{
Steven Cooreman87885df2021-03-19 19:04:39 +01002730 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2731 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002732 LOCAL_OUTPUT_DECLARE(mac_external, mac);
Thomas Daubney301491d2024-02-01 14:32:59 +00002733 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002735 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002736 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002737 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002738 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002739
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002740 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002741 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002742 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002743 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002744
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002745 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2746 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002747 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002748 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002749 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002750 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002752 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002753 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002754 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002755 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002757 status = psa_driver_wrapper_mac_sign_finish(operation,
2758 mac, operation->mac_size,
2759 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002760
Dave Rodgman478ab542021-06-25 09:09:02 +01002761exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002762 /* In case of success, set the potential excess room in the output buffer
2763 * to an invalid value, to avoid potentially leaking a longer MAC.
2764 * In case of error, set the output length and content to a safe default,
2765 * such that in case the caller misses an error check, the output would be
2766 * an unachievable MAC.
2767 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002768 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002769 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002770 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002771 }
mohammad16036df908f2018-04-02 08:34:15 -07002772
Thomas Daubney480347d2024-02-01 19:00:26 +00002773 if ((mac != NULL) && (mac_size > operation->mac_size)) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002774 memset(&mac[operation->mac_size], '!',
2775 mac_size - operation->mac_size);
2776 }
Ronald Cron882eb782021-06-18 13:05:48 +02002777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002778 abort_status = psa_mac_abort(operation);
Thomas Daubney324f7de2024-01-18 13:18:58 +00002779 LOCAL_OUTPUT_FREE(mac_external, mac);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002781 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002782}
2783
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002784psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002785 const uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002786 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002787{
Steven Cooreman87885df2021-03-19 19:04:39 +01002788 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2789 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002790 LOCAL_INPUT_DECLARE(mac_external, mac);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002792 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002793 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002794 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002795 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002796
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002797 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002798 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002799 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002800 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002802 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002803 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002804 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002805 }
2806
Thomas Daubney324f7de2024-01-18 13:18:58 +00002807 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002808 status = psa_driver_wrapper_mac_verify_finish(operation,
2809 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002810
Dave Rodgman478ab542021-06-25 09:09:02 +01002811exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002812 abort_status = psa_mac_abort(operation);
Thomas Daubney324f7de2024-01-18 13:18:58 +00002813 LOCAL_INPUT_FREE(mac_external, mac);
mohammad16036df908f2018-04-02 08:34:15 -07002814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002815 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002816}
2817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002818static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2819 psa_algorithm_t alg,
2820 const uint8_t *input,
2821 size_t input_length,
2822 uint8_t *mac,
2823 size_t mac_size,
2824 size_t *mac_length,
2825 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002826{
2827 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002828 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002829 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002830 psa_key_slot_t *slot;
2831 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002832
Ronald Crondbb86462021-06-17 17:17:20 +02002833 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002834 key,
2835 &slot,
2836 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2837 alg);
2838 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002839 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002840 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002841
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002842 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002843 .core = slot->attr
2844 };
2845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002846 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2847 &operation_mac_size);
2848 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002849 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002850 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002851
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002852 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002853 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002854 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002855 }
2856
2857 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002858 &attributes,
2859 slot->key.data, slot->key.bytes,
2860 alg,
2861 input, input_length,
2862 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002863
2864exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002865 /* In case of success, set the potential excess room in the output buffer
2866 * to an invalid value, to avoid potentially leaking a longer MAC.
2867 * In case of error, set the output length and content to a safe default,
2868 * such that in case the caller misses an error check, the output would be
2869 * an unachievable MAC.
2870 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002871 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002872 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002873 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002874 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002875 if (mac_size > operation_mac_size) {
2876 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2877 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002879 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002881 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002882}
2883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002884psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002885 psa_algorithm_t alg,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002886 const uint8_t *input_external,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002887 size_t input_length,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002888 uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002889 size_t mac_size,
2890 size_t *mac_length)
2891{
Thomas Daubney324f7de2024-01-18 13:18:58 +00002892 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2893 LOCAL_INPUT_DECLARE(input_external, input);
2894 LOCAL_OUTPUT_DECLARE(mac_external, mac);
2895
2896 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2897 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
2898 status = psa_mac_compute_internal(key, alg,
Thomas Daubneyf298f652024-01-30 12:15:36 +00002899 input, input_length,
2900 mac, mac_size, mac_length, 1);
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002901
David Horstmann42015332024-03-13 15:42:31 +00002902#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney324f7de2024-01-18 13:18:58 +00002903exit:
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002904#endif
Thomas Daubney324f7de2024-01-18 13:18:58 +00002905 LOCAL_INPUT_FREE(input_external, input);
2906 LOCAL_OUTPUT_FREE(mac_external, mac);
2907
2908 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002909}
2910
2911psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2912 psa_algorithm_t alg,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002913 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002914 size_t input_length,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002915 const uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002916 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002917{
2918 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002919 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2920 size_t actual_mac_length;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002921 LOCAL_INPUT_DECLARE(input_external, input);
2922 LOCAL_INPUT_DECLARE(mac_external, mac);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002923
Thomas Daubney324f7de2024-01-18 13:18:58 +00002924 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002925 status = psa_mac_compute_internal(key, alg,
2926 input, input_length,
2927 actual_mac, sizeof(actual_mac),
2928 &actual_mac_length, 0);
2929 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002930 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002931 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002933 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002934 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002935 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002936 }
Thomas Daubney324f7de2024-01-18 13:18:58 +00002937
2938 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002939 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002940 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002941 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002942 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002943
2944exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002945 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Thomas Daubney324f7de2024-01-18 13:18:58 +00002946 LOCAL_INPUT_FREE(input_external, input);
2947 LOCAL_INPUT_FREE(mac_external, mac);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002949 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002950}
Gilles Peskine20035e32018-02-03 22:44:14 +01002951
Gilles Peskine20035e32018-02-03 22:44:14 +01002952/****************************************************************/
2953/* Asymmetric cryptography */
2954/****************************************************************/
2955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002956static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2957 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002958{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002959 if (input_is_message) {
2960 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2961 return PSA_ERROR_INVALID_ARGUMENT;
2962 }
Gilles Peskineb6e362b2024-04-10 20:05:06 +02002963 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002964
Gilles Peskineb6e362b2024-04-10 20:05:06 +02002965 psa_algorithm_t hash_alg = 0;
2966 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2967 hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
2968 }
2969
2970 /* Now hash_alg==0 if alg by itself doesn't need a hash.
2971 * This is good enough for sign-hash, but a guaranteed failure for
2972 * sign-message which needs to hash first for all algorithms
2973 * supported at the moment. */
2974
2975 if (hash_alg == 0 && input_is_message) {
2976 return PSA_ERROR_INVALID_ARGUMENT;
2977 }
2978 if (hash_alg == PSA_ALG_ANY_HASH) {
2979 return PSA_ERROR_INVALID_ARGUMENT;
2980 }
2981 /* Give up immediately if the hash is not supported. This has
2982 * several advantages:
2983 * - For mechanisms that don't use the hash at all (e.g.
2984 * ECDSA verification, randomized ECDSA signature), without
2985 * this check, the operation would succeed even though it has
2986 * been given an invalid argument. This would not be insecure
2987 * since the hash was not necessary, but it would be weird.
2988 * - For mechanisms that do use the hash, we avoid an error
2989 * deep inside the execution. In principle this doesn't matter,
2990 * but there is a little more risk of a bug in error handling
2991 * deep inside than in this preliminary check.
2992 * - When calling a driver, the driver might be capable of using
2993 * a hash that the core doesn't support. This could potentially
2994 * result in a buffer overflow if the hash is larger than the
2995 * maximum hash size assumed by the core.
2996 * - Returning a consistent error makes it possible to test
2997 * not-supported hashes in a consistent way.
2998 */
2999 if (hash_alg != 0 && !is_hash_supported(hash_alg)) {
3000 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm56980482021-05-05 13:56:27 +02003001 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02003002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003003 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02003004}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003006static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
3007 int input_is_message,
3008 psa_algorithm_t alg,
3009 const uint8_t *input,
3010 size_t input_length,
3011 uint8_t *signature,
3012 size_t signature_size,
3013 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003014{
3015 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3016 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003017 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003018 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003019
3020 *signature_length = 0;
3021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003022 status = psa_sign_verify_check_alg(input_is_message, alg);
3023 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02003024 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003025 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003026
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003027 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02003028 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003029 * buffer can in principle be empty since it doesn't actually have
3030 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003031 if (signature_size == 0) {
3032 return PSA_ERROR_BUFFER_TOO_SMALL;
3033 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003034
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003035 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003036 key, &slot,
3037 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
3038 PSA_KEY_USAGE_SIGN_HASH,
3039 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003041 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003042 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003043 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003045 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003046 status = PSA_ERROR_INVALID_ARGUMENT;
3047 goto exit;
3048 }
3049
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003050 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003051 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003052 };
3053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003054 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003055 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003056 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003057 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003058 signature, signature_size, signature_length);
3059 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003060
3061 status = psa_driver_wrapper_sign_hash(
3062 &attributes, slot->key.data, slot->key.bytes,
3063 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003064 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003065 }
3066
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003067
3068exit:
3069 /* Fill the unused part of the output buffer (the whole buffer on error,
3070 * the trailing part on success) with something that isn't a valid signature
3071 * (barring an attack on the signature and deliberately-crafted input),
3072 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003073 if (status == PSA_SUCCESS) {
3074 memset(signature + *signature_length, '!',
3075 signature_size - *signature_length);
3076 } else {
3077 memset(signature, '!', signature_size);
3078 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003079 /* If signature_size is 0 then we have nothing to do. We must not call
3080 * memset because signature may be NULL in this case. */
3081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003084 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003085}
3086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003087static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3088 int input_is_message,
3089 psa_algorithm_t alg,
3090 const uint8_t *input,
3091 size_t input_length,
3092 const uint8_t *signature,
3093 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003094{
3095 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3096 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3097 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003099 status = psa_sign_verify_check_alg(input_is_message, alg);
3100 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02003101 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003102 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003103
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003104 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003105 key, &slot,
3106 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3107 PSA_KEY_USAGE_VERIFY_HASH,
3108 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003110 if (status != PSA_SUCCESS) {
3111 return status;
3112 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003113
3114 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003115 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003116 };
3117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003118 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003119 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003120 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003121 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003122 signature, signature_length);
3123 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003124 status = psa_driver_wrapper_verify_hash(
3125 &attributes, slot->key.data, slot->key.bytes,
3126 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003127 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003128 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003130 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003132 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003133
3134}
3135
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003136psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003137 const psa_key_attributes_t *attributes,
3138 const uint8_t *key_buffer,
3139 size_t key_buffer_size,
3140 psa_algorithm_t alg,
3141 const uint8_t *input,
3142 size_t input_length,
3143 uint8_t *signature,
3144 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003145 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003146{
3147 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003149 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003150 size_t hash_length;
3151 uint8_t hash[PSA_HASH_MAX_SIZE];
3152
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003153 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003154 PSA_ALG_SIGN_GET_HASH(alg),
3155 input, input_length,
3156 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003158 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003159 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003160 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003161
3162 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003163 attributes, key_buffer, key_buffer_size,
3164 alg, hash, hash_length,
3165 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003166 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003168 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003169}
3170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003171psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3172 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003173 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003174 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003175 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003176 size_t signature_size,
3177 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003178{
Thomas Daubney290aac42024-01-18 17:23:02 +00003179 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3180 LOCAL_INPUT_DECLARE(input_external, input);
3181 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3182
3183 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3184 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3185 status = psa_sign_internal(key, 1, alg, input, input_length, signature,
3186 signature_size, signature_length);
3187
David Horstmann42015332024-03-13 15:42:31 +00003188#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003189exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003190#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003191 LOCAL_INPUT_FREE(input_external, input);
3192 LOCAL_OUTPUT_FREE(signature_external, signature);
3193 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003194}
3195
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003196psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003197 const psa_key_attributes_t *attributes,
3198 const uint8_t *key_buffer,
3199 size_t key_buffer_size,
3200 psa_algorithm_t alg,
3201 const uint8_t *input,
3202 size_t input_length,
3203 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003204 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003205{
3206 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003208 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003209 size_t hash_length;
3210 uint8_t hash[PSA_HASH_MAX_SIZE];
3211
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003212 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003213 PSA_ALG_SIGN_GET_HASH(alg),
3214 input, input_length,
3215 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003217 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003218 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003219 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003220
3221 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003222 attributes, key_buffer, key_buffer_size,
3223 alg, hash, hash_length,
3224 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003225 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003227 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003228}
3229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003230psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3231 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003232 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003233 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003234 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003235 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003236{
Thomas Daubney290aac42024-01-18 17:23:02 +00003237 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3238 LOCAL_INPUT_DECLARE(input_external, input);
3239 LOCAL_INPUT_DECLARE(signature_external, signature);
3240
3241 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3242 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3243 status = psa_verify_internal(key, 1, alg, input, input_length, signature,
3244 signature_length);
3245
David Horstmann42015332024-03-13 15:42:31 +00003246#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003247exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003248#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003249 LOCAL_INPUT_FREE(input_external, input);
3250 LOCAL_INPUT_FREE(signature_external, signature);
3251
3252 return status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003253}
3254
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003255psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003256 const psa_key_attributes_t *attributes,
3257 const uint8_t *key_buffer, size_t key_buffer_size,
3258 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003259 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01003260{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003261 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3262 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3263 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003264#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003265 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3266 return mbedtls_psa_rsa_sign_hash(
3267 attributes,
3268 key_buffer, key_buffer_size,
3269 alg, hash, hash_length,
3270 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003271#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3272 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003273 } else {
3274 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003275 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003276 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3277 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003278#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003279 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3280 return mbedtls_psa_ecdsa_sign_hash(
3281 attributes,
3282 key_buffer, key_buffer_size,
3283 alg, hash, hash_length,
3284 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003285#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3286 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003287 } else {
3288 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003289 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003290 }
Ronald Cron4501c982021-03-19 20:09:32 +01003291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003292 (void) key_buffer;
3293 (void) key_buffer_size;
3294 (void) hash;
3295 (void) hash_length;
3296 (void) signature;
3297 (void) signature_size;
3298 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003300 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003301}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003303psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3304 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003305 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003306 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003307 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003308 size_t signature_size,
3309 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003310{
Thomas Daubney290aac42024-01-18 17:23:02 +00003311 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3312 LOCAL_INPUT_DECLARE(hash_external, hash);
3313 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3314
3315 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3316 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3317 status = psa_sign_internal(key, 0, alg, hash, hash_length, signature,
3318 signature_size, signature_length);
3319
David Horstmann42015332024-03-13 15:42:31 +00003320#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003321exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003322#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003323 LOCAL_INPUT_FREE(hash_external, hash);
3324 LOCAL_OUTPUT_FREE(signature_external, signature);
3325
3326 return status;
itayzafrir5c753392018-05-08 11:18:38 +03003327}
3328
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003329psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003330 const psa_key_attributes_t *attributes,
3331 const uint8_t *key_buffer, size_t key_buffer_size,
3332 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003333 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003334{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003335 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3336 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3337 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003338#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003339 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3340 return mbedtls_psa_rsa_verify_hash(
3341 attributes,
3342 key_buffer, key_buffer_size,
3343 alg, hash, hash_length,
3344 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003345#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3346 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003347 } else {
3348 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003349 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003350 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3351 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003352#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003353 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3354 return mbedtls_psa_ecdsa_verify_hash(
3355 attributes,
3356 key_buffer, key_buffer_size,
3357 alg, hash, hash_length,
3358 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003359#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3360 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003361 } else {
3362 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003363 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003364 }
Ronald Cron4501c982021-03-19 20:09:32 +01003365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003366 (void) key_buffer;
3367 (void) key_buffer_size;
3368 (void) hash;
3369 (void) hash_length;
3370 (void) signature;
3371 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003373 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003374}
3375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003376psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3377 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003378 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003379 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003380 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003381 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003382{
Thomas Daubney290aac42024-01-18 17:23:02 +00003383 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3384 LOCAL_INPUT_DECLARE(hash_external, hash);
3385 LOCAL_INPUT_DECLARE(signature_external, signature);
3386
3387 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3388 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3389 status = psa_verify_internal(key, 0, alg, hash, hash_length, signature,
3390 signature_length);
3391
David Horstmann42015332024-03-13 15:42:31 +00003392#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003393exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003394#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003395 LOCAL_INPUT_FREE(hash_external, hash);
3396 LOCAL_INPUT_FREE(signature_external, signature);
3397
3398 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003399}
3400
John Durkop0e005192020-10-31 22:06:54 -07003401#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003402static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3403 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003404{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003405 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3406 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3407 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3408 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003409}
John Durkop0e005192020-10-31 22:06:54 -07003410#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003412psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3413 psa_algorithm_t alg,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003414 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003415 size_t input_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003416 const uint8_t *salt_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003417 size_t salt_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003418 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003419 size_t output_size,
3420 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003421{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003422 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003423 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003424 psa_key_slot_t *slot;
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003425 LOCAL_INPUT_DECLARE(input_external, input);
3426 LOCAL_INPUT_DECLARE(salt_external, salt);
3427 LOCAL_OUTPUT_DECLARE(output_external, output);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003428
Darryl Green5cc689a2018-07-24 15:34:10 +01003429 (void) input;
3430 (void) input_length;
3431 (void) salt;
3432 (void) output;
3433 (void) output_size;
3434
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003435 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003437 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3438 return PSA_ERROR_INVALID_ARGUMENT;
3439 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003440
Ronald Cron5c522922020-11-14 16:35:34 +01003441 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003442 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3443 if (status != PSA_SUCCESS) {
3444 return status;
3445 }
3446 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3447 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003448 status = PSA_ERROR_INVALID_ARGUMENT;
3449 goto exit;
3450 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003452 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003453#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003454 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003455 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003456 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3457 slot->key.data,
3458 slot->key.bytes,
3459 &rsa);
3460 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003461 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003462 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003464 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003465 status = PSA_ERROR_BUFFER_TOO_SMALL;
3466 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003467 }
Ronald Cron7207d572021-09-08 14:28:35 +02003468#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3469 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003470 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3471 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3472 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003473 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003474#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003475 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003476 mbedtls_rsa_pkcs1_encrypt(rsa,
3477 mbedtls_psa_get_random,
3478 MBEDTLS_PSA_RANDOM_STATE,
3479 MBEDTLS_RSA_PUBLIC,
3480 input_length,
3481 input,
3482 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003483#else
3484 status = PSA_ERROR_NOT_SUPPORTED;
3485#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003486 } else
3487 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003488#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003489 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003490 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003491 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3492 mbedtls_psa_get_random,
3493 MBEDTLS_PSA_RANDOM_STATE,
3494 MBEDTLS_RSA_PUBLIC,
3495 salt, salt_length,
3496 input_length,
3497 input,
3498 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003499#else
3500 status = PSA_ERROR_NOT_SUPPORTED;
3501#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003502 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003503 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003504 }
Ronald Cron7207d572021-09-08 14:28:35 +02003505#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003506 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003507rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003508 if (status == PSA_SUCCESS) {
3509 *output_length = mbedtls_rsa_get_len(rsa);
3510 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003512 mbedtls_rsa_free(rsa);
3513 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003514#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003515 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003516 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003517 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003518 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003519
3520exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003521 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003522
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003523 LOCAL_INPUT_FREE(input_external, input);
3524 LOCAL_INPUT_FREE(salt_external, salt);
3525 LOCAL_OUTPUT_FREE(output_external, output);
3526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003527 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003528}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003530psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3531 psa_algorithm_t alg,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003532 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003533 size_t input_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003534 const uint8_t *salt_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003535 size_t salt_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003536 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003537 size_t output_size,
3538 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003539{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003540 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003541 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003542 psa_key_slot_t *slot;
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003543 LOCAL_INPUT_DECLARE(input_external, input);
3544 LOCAL_INPUT_DECLARE(salt_external, salt);
3545 LOCAL_OUTPUT_DECLARE(output_external, output);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003546
Darryl Green5cc689a2018-07-24 15:34:10 +01003547 (void) input;
3548 (void) input_length;
3549 (void) salt;
3550 (void) output;
3551 (void) output_size;
3552
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003553 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003555 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3556 return PSA_ERROR_INVALID_ARGUMENT;
3557 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003558
Ronald Cron5c522922020-11-14 16:35:34 +01003559 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003560 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3561 if (status != PSA_SUCCESS) {
3562 return status;
3563 }
3564 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003565 status = PSA_ERROR_INVALID_ARGUMENT;
3566 goto exit;
3567 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003569 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003570#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003571 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003572 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003573 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3574 slot->key.data,
3575 slot->key.bytes,
3576 &rsa);
3577 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003578 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003579 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003581 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003582 status = PSA_ERROR_INVALID_ARGUMENT;
3583 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003584 }
Ronald Cron7207d572021-09-08 14:28:35 +02003585#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3586 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003587 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3588 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3589 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003590 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003591#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003592 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003593 mbedtls_rsa_pkcs1_decrypt(rsa,
3594 mbedtls_psa_get_random,
3595 MBEDTLS_PSA_RANDOM_STATE,
3596 MBEDTLS_RSA_PRIVATE,
3597 output_length,
3598 input,
3599 output,
3600 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003601#else
3602 status = PSA_ERROR_NOT_SUPPORTED;
3603#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003604 } else
3605 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003606#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003607 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003608 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003609 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3610 mbedtls_psa_get_random,
3611 MBEDTLS_PSA_RANDOM_STATE,
3612 MBEDTLS_RSA_PRIVATE,
3613 salt, salt_length,
3614 output_length,
3615 input,
3616 output,
3617 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003618#else
3619 status = PSA_ERROR_NOT_SUPPORTED;
3620#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003621 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003622 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003623 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003624
Ronald Cron7207d572021-09-08 14:28:35 +02003625#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003626 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003627rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003628 mbedtls_rsa_free(rsa);
3629 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003630#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3631 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003632 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003633 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003634 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003635
3636exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003637 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003638
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003639 LOCAL_INPUT_FREE(input_external, input);
3640 LOCAL_INPUT_FREE(salt_external, salt);
3641 LOCAL_OUTPUT_FREE(output_external, output);
3642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003643 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003644}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003645
David Horstmann65bf12c2024-02-06 15:27:49 +00003646static psa_status_t psa_generate_random_internal(uint8_t *output,
3647 size_t output_size)
3648{
3649 GUARD_MODULE_INITIALIZED;
3650
3651 psa_status_t status;
3652
3653#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
3654
3655 size_t output_length = 0;
3656 status = mbedtls_psa_external_get_random(&global_data.rng,
3657 output, output_size,
3658 &output_length);
3659 if (status != PSA_SUCCESS) {
3660 goto exit;
3661 }
3662 /* Breaking up a request into smaller chunks is currently not supported
3663 * for the external RNG interface. */
3664 if (output_length != output_size) {
3665 status = PSA_ERROR_INSUFFICIENT_ENTROPY;
3666 goto exit;
3667 }
3668 status = PSA_SUCCESS;
3669
3670#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
3671
3672 while (output_size > 0) {
3673 size_t request_size =
3674 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
3675 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
3676 output_size);
3677 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
3678 output, request_size);
3679 if (ret != 0) {
3680 status = mbedtls_to_psa_error(ret);
3681 goto exit;
3682 }
3683 output_size -= request_size;
3684 output += request_size;
3685 }
3686 status = PSA_SUCCESS;
3687#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
3688
3689exit:
3690 return status;
3691}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003692
3693
mohammad1603503973b2018-03-12 15:59:30 +02003694/****************************************************************/
3695/* Symmetric cryptography */
3696/****************************************************************/
3697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003698static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3699 mbedtls_svc_key_id_t key,
3700 psa_algorithm_t alg,
3701 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003702{
3703 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3704 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003705 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003706 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003707 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3708 PSA_KEY_USAGE_ENCRYPT :
3709 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003710
3711 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003712 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003713 status = PSA_ERROR_BAD_STATE;
3714 goto exit;
3715 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003717 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003718 status = PSA_ERROR_INVALID_ARGUMENT;
3719 goto exit;
3720 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003722 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3723 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003724 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003725 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003726
Ronald Cron49fafa92021-03-10 08:34:23 +01003727 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003728 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003729 * so we only set it (in the driver wrapper) after resources have been
3730 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003731 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003732 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003733 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003734 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003735 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003736 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003737 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003738 }
3739 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003740
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003741 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003742 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003743 };
3744
Ronald Cronab99ac22020-10-01 16:38:28 +02003745 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003746 if (cipher_operation == MBEDTLS_ENCRYPT) {
3747 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3748 &attributes,
3749 slot->key.data,
3750 slot->key.bytes,
3751 alg);
3752 } else {
3753 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3754 &attributes,
3755 slot->key.data,
3756 slot->key.bytes,
3757 alg);
3758 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003759
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003760exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003761 if (status != PSA_SUCCESS) {
3762 psa_cipher_abort(operation);
3763 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003765 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003768}
3769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003770psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3771 mbedtls_svc_key_id_t key,
3772 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003773{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003774 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003775}
3776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003777psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3778 mbedtls_svc_key_id_t key,
3779 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003780{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003781 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003782}
3783
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003784psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Gabor Mezei8677edd2024-02-07 18:10:13 +01003785 uint8_t *iv_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003786 size_t iv_size,
3787 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003788{
Ronald Cron6d051732020-10-01 14:10:20 +02003789 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Sergey04eb7c02023-03-06 15:37:23 -07003790 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003791
Gabor Mezei8677edd2024-02-07 18:10:13 +01003792 LOCAL_OUTPUT_DECLARE(iv_external, iv);
3793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003794 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003795 status = PSA_ERROR_BAD_STATE;
3796 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003797 }
3798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003799 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003800 status = PSA_ERROR_BAD_STATE;
3801 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003802 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003803
Ronald Cronc423acb2021-07-05 12:31:44 +02003804 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003805 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003806 status = PSA_ERROR_BUFFER_TOO_SMALL;
3807 goto exit;
3808 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003810 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003811 status = PSA_ERROR_GENERIC_ERROR;
3812 goto exit;
3813 }
3814
Gabor Mezei8677edd2024-02-07 18:10:13 +01003815 LOCAL_OUTPUT_ALLOC(iv_external, default_iv_length, iv);
3816
Gabor Mezeif3c35042024-03-04 17:15:08 +01003817 status = psa_generate_random_internal(iv, default_iv_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003818 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003819 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003820 }
Ronald Cron5618a392021-03-26 09:52:26 +01003821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003822 status = psa_driver_wrapper_cipher_set_iv(operation,
Gabor Mezei8677edd2024-02-07 18:10:13 +01003823 iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003824
3825exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003826 if (status == PSA_SUCCESS) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003827 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003828 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003829 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003830 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003831 psa_cipher_abort(operation);
Gabor Mezei8677edd2024-02-07 18:10:13 +01003832 if (iv != NULL) {
3833 mbedtls_platform_zeroize(iv, default_iv_length);
3834 }
Ronald Cronc423acb2021-07-05 12:31:44 +02003835 }
Ronald Cron6d051732020-10-01 14:10:20 +02003836
Gabor Mezei8677edd2024-02-07 18:10:13 +01003837 LOCAL_OUTPUT_FREE(iv_external, iv);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003838 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003839}
3840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003841psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Gabor Mezei282bb532024-02-01 10:39:26 +01003842 const uint8_t *iv_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003843 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003844{
Ronald Cron6d051732020-10-01 14:10:20 +02003845 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003846
Gabor Mezei282bb532024-02-01 10:39:26 +01003847 LOCAL_INPUT_DECLARE(iv_external, iv);
3848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003849 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003850 status = PSA_ERROR_BAD_STATE;
3851 goto exit;
3852 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003854 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003855 status = PSA_ERROR_BAD_STATE;
3856 goto exit;
3857 }
Ronald Crona0d68172021-03-26 10:15:08 +01003858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003859 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003860 status = PSA_ERROR_INVALID_ARGUMENT;
3861 goto exit;
3862 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003863
Gabor Mezei282bb532024-02-01 10:39:26 +01003864 LOCAL_INPUT_ALLOC(iv_external, iv_length, iv);
3865
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003866 status = psa_driver_wrapper_cipher_set_iv(operation,
3867 iv,
3868 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003869
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003870exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003871 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003872 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003873 } else {
3874 psa_cipher_abort(operation);
3875 }
Gabor Mezei282bb532024-02-01 10:39:26 +01003876
3877 LOCAL_INPUT_FREE(iv_external, iv);
3878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003879 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003880}
3881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003882psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003883 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003884 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003885 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003886 size_t output_size,
3887 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003888{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003889 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003890
Gabor Mezei13a15c22024-01-24 16:56:00 +01003891 LOCAL_INPUT_DECLARE(input_external, input);
3892 LOCAL_OUTPUT_DECLARE(output_external, output);
3893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003894 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003895 status = PSA_ERROR_BAD_STATE;
3896 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003897 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003899 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003900 status = PSA_ERROR_BAD_STATE;
3901 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003902 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003903
Gabor Mezeied96d682024-01-31 17:45:29 +01003904 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gabor Mezeiff783e02024-02-29 10:08:16 +00003905 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezeied96d682024-01-31 17:45:29 +01003906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003907 status = psa_driver_wrapper_cipher_update(operation,
3908 input,
3909 input_length,
3910 output,
3911 output_size,
3912 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003913
3914exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003915 if (status != PSA_SUCCESS) {
3916 psa_cipher_abort(operation);
3917 }
Ronald Cron6d051732020-10-01 14:10:20 +02003918
Gabor Mezei13a15c22024-01-24 16:56:00 +01003919 LOCAL_INPUT_FREE(input_external, input);
3920 LOCAL_OUTPUT_FREE(output_external, output);
3921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003922 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003923}
3924
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003925psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003926 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003927 size_t output_size,
3928 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003929{
David Saadab4ecc272019-02-14 13:48:10 +02003930 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003931
Gabor Mezei13a15c22024-01-24 16:56:00 +01003932 LOCAL_OUTPUT_DECLARE(output_external, output);
3933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003934 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003935 status = PSA_ERROR_BAD_STATE;
3936 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003937 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003939 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003940 status = PSA_ERROR_BAD_STATE;
3941 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003942 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003943
Gabor Mezeiff783e02024-02-29 10:08:16 +00003944 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezeied96d682024-01-31 17:45:29 +01003945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003946 status = psa_driver_wrapper_cipher_finish(operation,
3947 output,
3948 output_size,
3949 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003950
3951exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003952 if (status == PSA_SUCCESS) {
Gabor Mezei13a15c22024-01-24 16:56:00 +01003953 status = psa_cipher_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003954 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003955 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003956 (void) psa_cipher_abort(operation);
Steven Cooremanef8575e2020-09-11 11:44:50 +02003957 }
Gabor Mezei13a15c22024-01-24 16:56:00 +01003958
3959 LOCAL_OUTPUT_FREE(output_external, output);
3960
3961 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003962}
3963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003964psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003965{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003966 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003967 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003968 * in use. It's ok to call abort on such an object, and there's
3969 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003970 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003971 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003972
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003973 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003974
Ronald Cron49fafa92021-03-10 08:34:23 +01003975 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003976 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003977 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003979 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003980}
3981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003982psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3983 psa_algorithm_t alg,
David Horstmann926193a2023-12-13 15:55:25 +00003984 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003985 size_t input_length,
David Horstmann926193a2023-12-13 15:55:25 +00003986 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003987 size_t output_size,
3988 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003989{
3990 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003991 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003992 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003993 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003994 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3995 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003996
David Horstmannb80e35a2023-12-14 18:12:47 +00003997 LOCAL_INPUT_DECLARE(input_external, input);
3998 LOCAL_OUTPUT_DECLARE(output_external, output);
3999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004000 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02004001 status = PSA_ERROR_INVALID_ARGUMENT;
4002 goto exit;
4003 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004005 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4006 PSA_KEY_USAGE_ENCRYPT,
4007 alg);
4008 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004009 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004010 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004011
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004012 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004013 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004014 };
4015
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004016 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4017 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02004018 status = PSA_ERROR_GENERIC_ERROR;
4019 goto exit;
4020 }
4021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004022 if (default_iv_length > 0) {
4023 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004024 status = PSA_ERROR_BUFFER_TOO_SMALL;
4025 goto exit;
4026 }
4027
David Horstmann65bf12c2024-02-06 15:27:49 +00004028 status = psa_generate_random_internal(local_iv, default_iv_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004029 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004030 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004031 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004032 }
4033
Gabor Mezeied96d682024-01-31 17:45:29 +01004034 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4035 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4036
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004037 status = psa_driver_wrapper_cipher_encrypt(
4038 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02004039 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004040 mbedtls_buffer_offset(output, default_iv_length),
4041 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004042
4043exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004044 unlock_status = psa_unlock_key_slot(slot);
4045 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004046 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02004047 }
Ronald Cron16377072021-07-08 19:00:07 +02004048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004049 if (status == PSA_SUCCESS) {
4050 if (default_iv_length > 0) {
4051 memcpy(output, local_iv, default_iv_length);
4052 }
4053 *output_length += default_iv_length;
4054 } else {
4055 *output_length = 0;
4056 }
4057
David Horstmannb80e35a2023-12-14 18:12:47 +00004058 LOCAL_INPUT_FREE(input_external, input);
4059 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00004060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004061 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004062}
4063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004064psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4065 psa_algorithm_t alg,
Gabor Mezei13a15c22024-01-24 16:56:00 +01004066 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004067 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01004068 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004069 size_t output_size,
4070 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004071{
4072 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004073 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004074 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02004075 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02004076
Gabor Mezei13a15c22024-01-24 16:56:00 +01004077 LOCAL_INPUT_DECLARE(input_external, input);
4078 LOCAL_OUTPUT_DECLARE(output_external, output);
4079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004080 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02004081 status = PSA_ERROR_INVALID_ARGUMENT;
4082 goto exit;
4083 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004085 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4086 PSA_KEY_USAGE_DECRYPT,
4087 alg);
4088 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004089 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004090 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004091
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004092 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004093 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004094 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004096 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02004097 status = PSA_ERROR_INVALID_ARGUMENT;
4098 goto exit;
4099 }
4100
Gabor Mezeied96d682024-01-31 17:45:29 +01004101 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4102 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4103
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004104 status = psa_driver_wrapper_cipher_decrypt(
4105 &attributes, slot->key.data, slot->key.bytes,
4106 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004107 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004108
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02004109exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004110 unlock_status = psa_unlock_key_slot(slot);
4111 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004112 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004113 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004115 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004116 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004117 }
Ronald Cron16377072021-07-08 19:00:07 +02004118
Gabor Mezei13a15c22024-01-24 16:56:00 +01004119 LOCAL_INPUT_FREE(input_external, input);
4120 LOCAL_OUTPUT_FREE(output_external, output);
4121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004122 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004123}
4124
4125
mohammad16035955c982018-04-26 00:53:03 +03004126/****************************************************************/
4127/* AEAD */
4128/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004130psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4131 psa_algorithm_t alg,
David Horstmann21c1a942023-11-28 19:25:00 +00004132 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004133 size_t nonce_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004134 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004135 size_t additional_data_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004136 const uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004137 size_t plaintext_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004138 uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004139 size_t ciphertext_size,
4140 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004141{
Ronald Cron215633c2021-03-16 17:15:37 +01004142 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4143 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004144
David Horstmann21c1a942023-11-28 19:25:00 +00004145 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4146 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4147 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4148 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4149
mohammad1603f08a5502018-06-03 15:05:47 +03004150 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004152 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4153 return PSA_ERROR_NOT_SUPPORTED;
4154 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004155
Ronald Cron9a986162021-03-26 12:40:07 +01004156 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004157 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4158 if (status != PSA_SUCCESS) {
4159 return status;
4160 }
mohammad16035955c982018-04-26 00:53:03 +03004161
Ronald Cron215633c2021-03-16 17:15:37 +01004162 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004163 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004164 };
mohammad16035955c982018-04-26 00:53:03 +03004165
David Horstmann21c1a942023-11-28 19:25:00 +00004166 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4167 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4168 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4169 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4170
Ronald Cronde822812021-03-17 16:08:20 +01004171 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004172 &attributes, slot->key.data, slot->key.bytes,
4173 alg,
4174 nonce, nonce_length,
4175 additional_data, additional_data_length,
4176 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004177 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004179 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4180 memset(ciphertext, 0, ciphertext_size);
4181 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004182
David Horstmann21c1a942023-11-28 19:25:00 +00004183/* Exit label is only used for buffer copying, prevent unused warnings. */
David Horstmann42015332024-03-13 15:42:31 +00004184#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann21c1a942023-11-28 19:25:00 +00004185exit:
4186#endif
4187 LOCAL_INPUT_FREE(nonce_external, nonce);
4188 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4189 LOCAL_INPUT_FREE(plaintext_external, plaintext);
4190 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
4191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004192 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004194 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004195}
4196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004197psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4198 psa_algorithm_t alg,
David Horstmann6baf6e92023-11-28 19:43:53 +00004199 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004200 size_t nonce_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004201 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004202 size_t additional_data_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004203 const uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004204 size_t ciphertext_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004205 uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004206 size_t plaintext_size,
4207 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004208{
Ronald Cron215633c2021-03-16 17:15:37 +01004209 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4210 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004211
David Horstmann6baf6e92023-11-28 19:43:53 +00004212 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4213 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4214 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
4215 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
4216
Gilles Peskineee652a32018-06-01 19:23:52 +02004217 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004219 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4220 return PSA_ERROR_NOT_SUPPORTED;
4221 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004222
Ronald Cron9a986162021-03-26 12:40:07 +01004223 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004224 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4225 if (status != PSA_SUCCESS) {
4226 return status;
4227 }
mohammad16035955c982018-04-26 00:53:03 +03004228
Ronald Cron215633c2021-03-16 17:15:37 +01004229 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004230 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004231 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004232
David Horstmann6baf6e92023-11-28 19:43:53 +00004233 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4234 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
4235 additional_data);
4236 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
4237 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
4238
Ronald Cronde822812021-03-17 16:08:20 +01004239 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004240 &attributes, slot->key.data, slot->key.bytes,
4241 alg,
4242 nonce, nonce_length,
4243 additional_data, additional_data_length,
4244 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004245 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004247 if (status != PSA_SUCCESS && plaintext_size != 0) {
4248 memset(plaintext, 0, plaintext_size);
4249 }
mohammad160360a64d02018-06-03 17:20:42 +03004250
David Horstmann6baf6e92023-11-28 19:43:53 +00004251/* Exit label is only used for buffer copying, prevent unused warnings. */
David Horstmann42015332024-03-13 15:42:31 +00004252#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann6baf6e92023-11-28 19:43:53 +00004253exit:
4254#endif
4255 LOCAL_INPUT_FREE(nonce_external, nonce);
4256 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4257 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
4258 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
4259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004260 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004262 return status;
mohammad16035955c982018-04-26 00:53:03 +03004263}
4264
Gilles Peskinee59236f2018-01-27 23:32:46 +01004265/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004266/* Generators */
4267/****************************************************************/
4268
John Durkop07cc04a2020-11-16 22:08:34 -08004269#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4270 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4271 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4272#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02004273#endif /* At least one builtin KDF */
4274
4275#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4276 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4277 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4278static psa_status_t psa_key_derivation_start_hmac(
4279 psa_mac_operation_t *operation,
4280 psa_algorithm_t hash_alg,
4281 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004282 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02004283{
4284 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4285 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004286 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4287 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4288 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02004289
Steven Cooreman15f0d922021-05-07 14:14:37 +02004290 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004291 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02004292
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004293 status = psa_driver_wrapper_mac_sign_setup(operation,
4294 &attributes,
4295 hmac_key, hmac_key_length,
4296 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02004297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004298 psa_reset_key_attributes(&attributes);
4299 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02004300}
4301#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004302
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004303#define HKDF_STATE_INIT 0 /* no input yet */
4304#define HKDF_STATE_STARTED 1 /* got salt */
4305#define HKDF_STATE_KEYED 2 /* got key */
4306#define HKDF_STATE_OUTPUT 3 /* output started */
4307
Gilles Peskinecbe66502019-05-16 16:59:18 +02004308static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004309 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004310{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004311 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4312 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4313 } else {
4314 return operation->alg;
4315 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004316}
4317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004318psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004319{
4320 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004321 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4322 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004323 /* The object has (apparently) been initialized but it is not
4324 * in use. It's ok to call abort on such an object, and there's
4325 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004326 } else
John Durkopd0321952020-10-29 21:37:36 -07004327#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004328 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4329 mbedtls_free(operation->ctx.hkdf.info);
4330 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4331 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004332#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4333#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4334 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004335 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4336 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4337 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4338 if (operation->ctx.tls12_prf.secret != NULL) {
4339 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
4340 operation->ctx.tls12_prf.secret_length);
4341 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004342 }
4343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004344 if (operation->ctx.tls12_prf.seed != NULL) {
4345 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
4346 operation->ctx.tls12_prf.seed_length);
4347 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01004348 }
4349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004350 if (operation->ctx.tls12_prf.label != NULL) {
4351 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
4352 operation->ctx.tls12_prf.label_length);
4353 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01004354 }
4355
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004356 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004357
4358 /* We leave the fields Ai and output_block to be erased safely by the
4359 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004360 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004361#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4362 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004363 {
4364 status = PSA_ERROR_BAD_STATE;
4365 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004366 mbedtls_platform_zeroize(operation, sizeof(*operation));
4367 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004368}
4369
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004370psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004371 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004372{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004373 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004374 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004375 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004376 }
4377
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004378 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004379 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004380}
4381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004382psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4383 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004384{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004385 if (operation->alg == 0) {
4386 return PSA_ERROR_BAD_STATE;
4387 }
4388 if (capacity > operation->capacity) {
4389 return PSA_ERROR_INVALID_ARGUMENT;
4390 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004391 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004392 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004393}
4394
John Durkopd0321952020-10-29 21:37:36 -07004395#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004396/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004397 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004398static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
4399 psa_algorithm_t hash_alg,
4400 uint8_t *output,
4401 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004402{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004403 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02004404 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004405 psa_status_t status;
4406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004407 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
4408 return PSA_ERROR_BAD_STATE;
4409 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004410 hkdf->state = HKDF_STATE_OUTPUT;
4411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004412 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004413 /* Copy what remains of the current block */
4414 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004415 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004416 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004417 }
4418 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004419 output += n;
4420 output_length -= n;
4421 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004422 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004423 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004424 }
Gilles Peskined54931c2018-07-17 21:06:59 +02004425 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004426 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004427 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004428 * object was corrupted or if this function is called directly
4429 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004430 if (hkdf->block_number == 0xff) {
4431 return PSA_ERROR_BAD_STATE;
4432 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004433
4434 /* We need a new block */
4435 ++hkdf->block_number;
4436 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004438 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4439 hash_alg,
4440 hkdf->prk,
4441 hash_length);
4442 if (status != PSA_SUCCESS) {
4443 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004444 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004445
4446 if (hkdf->block_number != 1) {
4447 status = psa_mac_update(&hkdf->hmac,
4448 hkdf->output_block,
4449 hash_length);
4450 if (status != PSA_SUCCESS) {
4451 return status;
4452 }
4453 }
4454 status = psa_mac_update(&hkdf->hmac,
4455 hkdf->info,
4456 hkdf->info_length);
4457 if (status != PSA_SUCCESS) {
4458 return status;
4459 }
4460 status = psa_mac_update(&hkdf->hmac,
4461 &hkdf->block_number, 1);
4462 if (status != PSA_SUCCESS) {
4463 return status;
4464 }
4465 status = psa_mac_sign_finish(&hkdf->hmac,
4466 hkdf->output_block,
4467 sizeof(hkdf->output_block),
4468 &hmac_output_length);
4469 if (status != PSA_SUCCESS) {
4470 return status;
4471 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004472 }
4473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004474 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004475}
John Durkop07cc04a2020-11-16 22:08:34 -08004476#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004477
John Durkop07cc04a2020-11-16 22:08:34 -08004478#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4479 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004480static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4481 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004482 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004483{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004484 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4485 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004486 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4487 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004488 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004489
Janos Follath7742fee2019-06-17 12:58:10 +01004490 /* We can't be wanting more output after block 0xff, otherwise
4491 * the capacity check in psa_key_derivation_output_bytes() would have
4492 * prevented this call. It could happen only if the operation
4493 * object was corrupted or if this function is called directly
4494 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004495 if (tls12_prf->block_number == 0xff) {
4496 return PSA_ERROR_CORRUPTION_DETECTED;
4497 }
Janos Follath7742fee2019-06-17 12:58:10 +01004498
4499 /* We need a new block */
4500 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004501 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004502
4503 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4504 *
4505 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4506 *
4507 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4508 * HMAC_hash(secret, A(2) + seed) +
4509 * HMAC_hash(secret, A(3) + seed) + ...
4510 *
4511 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004512 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004513 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004514 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004515 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004516 * is currently extracted as `output_block` and where i is
4517 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004518 */
4519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004520 status = psa_key_derivation_start_hmac(&hmac,
4521 hash_alg,
4522 tls12_prf->secret,
4523 tls12_prf->secret_length);
4524 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004525 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004526 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004527
4528 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004529 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004530 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4531 * the variable seed and in this instance means it in the context of the
4532 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004533 status = psa_mac_update(&hmac,
4534 tls12_prf->label,
4535 tls12_prf->label_length);
4536 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004537 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004538 }
4539 status = psa_mac_update(&hmac,
4540 tls12_prf->seed,
4541 tls12_prf->seed_length);
4542 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004543 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004544 }
4545 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004546 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004547 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4548 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004549 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004550 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004551 }
4552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004553 status = psa_mac_sign_finish(&hmac,
4554 tls12_prf->Ai, hash_length,
4555 &hmac_output_length);
4556 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004557 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004558 }
4559 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004560 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004561 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004562
4563 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004564 status = psa_key_derivation_start_hmac(&hmac,
4565 hash_alg,
4566 tls12_prf->secret,
4567 tls12_prf->secret_length);
4568 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004569 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004570 }
4571 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4572 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004573 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004574 }
4575 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_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->seed, tls12_prf->seed_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_sign_finish(&hmac,
4584 tls12_prf->output_block, hash_length,
4585 &hmac_output_length);
4586 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004587 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004588 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004589
Janos Follath7742fee2019-06-17 12:58:10 +01004590
4591cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004592 cleanup_status = psa_mac_abort(&hmac);
4593 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004594 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004595 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004597 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004598}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004599
Janos Follath844eb0e2019-06-19 12:10:49 +01004600static psa_status_t psa_key_derivation_tls12_prf_read(
4601 psa_tls12_prf_key_derivation_t *tls12_prf,
4602 psa_algorithm_t alg,
4603 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004604 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004605{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004606 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4607 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004608 psa_status_t status;
4609 uint8_t offset, length;
4610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004611 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004612 case PSA_TLS12_PRF_STATE_LABEL_SET:
4613 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4614 break;
4615 case PSA_TLS12_PRF_STATE_OUTPUT:
4616 break;
4617 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004618 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004619 }
4620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004621 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004622 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623 if (tls12_prf->left_in_block == 0) {
4624 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4625 alg);
4626 if (status != PSA_SUCCESS) {
4627 return status;
4628 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004629
4630 continue;
4631 }
4632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004633 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004634 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004635 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004636 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004637 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004638
4639 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004640 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004641 output += length;
4642 output_length -= length;
4643 tls12_prf->left_in_block -= length;
4644 }
4645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004646 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004647}
John Durkop07cc04a2020-11-16 22:08:34 -08004648#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4649 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004650
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004651psa_status_t psa_key_derivation_output_bytes(
4652 psa_key_derivation_operation_t *operation,
Ryan Everett08bd2462024-02-09 16:15:32 +00004653 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004654 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004655{
4656 psa_status_t status;
Ryan Everett08bd2462024-02-09 16:15:32 +00004657 LOCAL_OUTPUT_DECLARE(output_external, output);
4658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004659 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004661 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004663 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004664 }
4665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004666 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004667 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004668 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004669 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4670 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004671 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004672 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004673 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004674 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004675
4676 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
4677 if (output_length > operation->capacity) {
4678 operation->capacity = 0;
4679 /* Go through the error path to wipe all confidential data now
4680 * that the operation object is useless. */
4681 status = PSA_ERROR_INSUFFICIENT_DATA;
4682 goto exit;
4683 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004684 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004685
John Durkopd0321952020-10-29 21:37:36 -07004686#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004687 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4688 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4689 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4690 output, output_length);
4691 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004692#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4693#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4694 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004695 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4696 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4697 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4698 kdf_alg, output,
4699 output_length);
4700 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004701#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4702 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004703 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004704 (void) kdf_alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004705 status = PSA_ERROR_BAD_STATE;
4706 LOCAL_OUTPUT_FREE(output_external, output);
4707
4708 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004709 }
4710
4711exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004712 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004713 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004714 * This allows us to differentiate between exhausted operations and
4715 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4716 * operations. */
4717 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004718 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004719 operation->alg = alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004720 if (output != NULL) {
4721 memset(output, '!', output_length);
4722 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004723 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004724
4725 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004726 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004727}
4728
David Brown7807bf72021-02-09 16:28:23 -07004729#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004730static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004731{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004732 if (data_size >= 8) {
4733 mbedtls_des_key_set_parity(data);
4734 }
4735 if (data_size >= 16) {
4736 mbedtls_des_key_set_parity(data + 8);
4737 }
4738 if (data_size >= 24) {
4739 mbedtls_des_key_set_parity(data + 16);
4740 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004741}
David Brown7807bf72021-02-09 16:28:23 -07004742#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004743
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004744static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004745 psa_key_slot_t *slot,
4746 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004747 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004748{
4749 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004750 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004751 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004752 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004754 if (!key_type_is_raw_bytes(slot->attr.type)) {
4755 return PSA_ERROR_INVALID_ARGUMENT;
4756 }
4757 if (bits % 8 != 0) {
4758 return PSA_ERROR_INVALID_ARGUMENT;
4759 }
4760 data = mbedtls_calloc(1, bytes);
4761 if (data == NULL) {
4762 return PSA_ERROR_INSUFFICIENT_MEMORY;
4763 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004765 status = psa_key_derivation_output_bytes(operation, data, bytes);
4766 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004767 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004768 }
David Brown12ca5032021-02-11 11:02:00 -07004769#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004770 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4771 psa_des_set_key_parity(data, bytes);
4772 }
David Brown8107e312021-02-12 08:08:46 -07004773#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004775 status = psa_allocate_buffer_to_slot(slot, bytes);
4776 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004777 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004778 }
Ronald Crondd04d422020-11-28 15:14:42 +01004779
Ronald Cronbf33c932020-11-28 18:06:53 +01004780 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004781 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004782 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004783 };
4784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004785 status = psa_driver_wrapper_import_key(&attributes,
4786 data, bytes,
4787 slot->key.data,
4788 slot->key.bytes,
4789 &slot->key.bytes, &bits);
4790 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004791 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004792 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004793
4794exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004795 mbedtls_free(data);
4796 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004797}
4798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004799psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4800 psa_key_derivation_operation_t *operation,
4801 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004802{
4803 psa_status_t status;
4804 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004805 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004806
Ronald Cron81709fc2020-11-14 12:10:32 +01004807 *key = MBEDTLS_SVC_KEY_ID_INIT;
4808
Gilles Peskine0f84d622019-09-12 19:03:13 +02004809 /* Reject any attempt to create a zero-length key so that we don't
4810 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004811 if (psa_get_key_bits(attributes) == 0) {
4812 return PSA_ERROR_INVALID_ARGUMENT;
4813 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004815 if (operation->alg == PSA_ALG_NONE) {
4816 return PSA_ERROR_BAD_STATE;
4817 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004819 if (!operation->can_output_key) {
4820 return PSA_ERROR_NOT_PERMITTED;
4821 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004822
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004823 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4824 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004825#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004826 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004827 /* Deriving a key in a secure element is not implemented yet. */
4828 status = PSA_ERROR_NOT_SUPPORTED;
4829 }
4830#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004831 if (status == PSA_SUCCESS) {
4832 status = psa_generate_derived_key_internal(slot,
4833 attributes->core.bits,
4834 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004835 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004836 if (status == PSA_SUCCESS) {
4837 status = psa_finish_key_creation(slot, driver, key);
4838 }
4839 if (status != PSA_SUCCESS) {
4840 psa_fail_key_creation(slot, driver);
4841 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004843 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004844}
4845
Gilles Peskineeab56e42018-07-12 17:12:33 +02004846
4847
4848/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004849/* Key derivation */
4850/****************************************************************/
4851
Steven Cooreman932ffb72021-02-15 12:14:32 +01004852#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004853static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004854{
4855#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004856 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4857 return 1;
4858 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004859#endif
4860#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004861 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4862 return 1;
4863 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004864#endif
4865#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004866 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4867 return 1;
4868 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004869#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004870 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004871}
4872
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004873static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004874{
4875 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004876 psa_status_t status = psa_hash_setup(&operation, alg);
4877 psa_hash_abort(&operation);
4878 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004879}
4880
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304881static psa_status_t psa_key_derivation_set_maximum_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004882 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004883 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004884{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004885 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4886 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4887 if (hash_size == 0) {
4888 return PSA_ERROR_NOT_SUPPORTED;
4889 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004890
4891 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4892 * we might fail later, which is somewhat unfriendly and potentially
4893 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004894 psa_status_t status = psa_hash_try_support(hash_alg);
4895 if (status != PSA_SUCCESS) {
4896 return status;
4897 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004898
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304899#if defined(PSA_WANT_ALG_HKDF)
4900 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4901 operation->capacity = 255 * hash_size;
4902 } else
4903#endif
4904#if defined(PSA_WANT_ALG_TLS12_PRF)
4905 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
4906 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4907 operation->capacity = SIZE_MAX;
4908 } else
4909#endif
4910#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
4911 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
4912 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4913 /* Master Secret is always 48 bytes
4914 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
4915 operation->capacity = 48U;
4916 } else
4917#endif
4918 {
4919 (void) hash_size;
4920 status = PSA_ERROR_NOT_SUPPORTED;
4921 }
4922 return status;
4923}
4924
4925
4926static psa_status_t psa_key_derivation_setup_kdf(
4927 psa_key_derivation_operation_t *operation,
4928 psa_algorithm_t kdf_alg)
4929{
4930 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4931 * initialisers for this union leave some bytes unspecified.) */
4932 memset(&operation->ctx, 0, sizeof(operation->ctx));
4933 /* Make sure that kdf_alg is a supported key derivation algorithm. */
4934 if (!is_kdf_alg_supported(kdf_alg)) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004935 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004936 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004937
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304938 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
4939 kdf_alg);
4940 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004941}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004942
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004943static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004944{
4945#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004946 if (alg == PSA_ALG_ECDH) {
4947 return PSA_SUCCESS;
4948 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004949#endif
4950 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004951 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004952}
John Durkop07cc04a2020-11-16 22:08:34 -08004953#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004954
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004955psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4956 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004957{
4958 psa_status_t status;
4959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004960 if (operation->alg != 0) {
4961 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004962 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004964 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4965 return PSA_ERROR_INVALID_ARGUMENT;
4966 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4967#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4968 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4969 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4970 status = psa_key_agreement_try_support(ka_alg);
4971 if (status != PSA_SUCCESS) {
4972 return status;
4973 }
4974 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4975#else
4976 return PSA_ERROR_NOT_SUPPORTED;
4977#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4978 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4979#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4980 status = psa_key_derivation_setup_kdf(operation, alg);
4981#else
4982 return PSA_ERROR_NOT_SUPPORTED;
4983#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4984 } else {
4985 return PSA_ERROR_INVALID_ARGUMENT;
4986 }
4987
4988 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004989 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004990 }
4991 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004992}
4993
John Durkopd0321952020-10-29 21:37:36 -07004994#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004995static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4996 psa_algorithm_t hash_alg,
4997 psa_key_derivation_step_t step,
4998 const uint8_t *data,
4999 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005000{
5001 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005002 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02005003 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005004 if (hkdf->state != HKDF_STATE_INIT) {
5005 return PSA_ERROR_BAD_STATE;
5006 } else {
5007 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5008 hash_alg,
5009 data, data_length);
5010 if (status != PSA_SUCCESS) {
5011 return status;
5012 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02005013 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005014 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02005015 }
Gilles Peskine03410b52019-05-16 16:05:19 +02005016 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005017 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005018 if (hkdf->state == HKDF_STATE_INIT) {
5019 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5020 hash_alg,
5021 NULL, 0);
5022 if (status != PSA_SUCCESS) {
5023 return status;
5024 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005025 hkdf->state = HKDF_STATE_STARTED;
5026 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005027 if (hkdf->state != HKDF_STATE_STARTED) {
5028 return PSA_ERROR_BAD_STATE;
5029 }
5030 status = psa_mac_update(&hkdf->hmac,
5031 data, data_length);
5032 if (status != PSA_SUCCESS) {
5033 return status;
5034 }
5035 status = psa_mac_sign_finish(&hkdf->hmac,
5036 hkdf->prk,
5037 sizeof(hkdf->prk),
5038 &data_length);
5039 if (status != PSA_SUCCESS) {
5040 return status;
5041 }
5042 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02005043 hkdf->block_number = 0;
5044 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005045 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02005046 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005047 if (hkdf->state == HKDF_STATE_OUTPUT) {
5048 return PSA_ERROR_BAD_STATE;
5049 }
5050 if (hkdf->info_set) {
5051 return PSA_ERROR_BAD_STATE;
5052 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005053 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005054 if (data_length != 0) {
5055 hkdf->info = mbedtls_calloc(1, data_length);
5056 if (hkdf->info == NULL) {
5057 return PSA_ERROR_INSUFFICIENT_MEMORY;
5058 }
5059 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005060 }
5061 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005062 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005063 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005064 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005065 }
5066}
John Durkop07cc04a2020-11-16 22:08:34 -08005067#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005068
John Durkop07cc04a2020-11-16 22:08:34 -08005069#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5070 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005071static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
5072 const uint8_t *data,
5073 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01005074{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005075 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
5076 return PSA_ERROR_BAD_STATE;
5077 }
Janos Follathf08e2652019-06-13 09:05:41 +01005078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005079 if (data_length != 0) {
5080 prf->seed = mbedtls_calloc(1, data_length);
5081 if (prf->seed == NULL) {
5082 return PSA_ERROR_INSUFFICIENT_MEMORY;
5083 }
Janos Follathf08e2652019-06-13 09:05:41 +01005084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005085 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005086 prf->seed_length = data_length;
5087 }
Janos Follathf08e2652019-06-13 09:05:41 +01005088
Gilles Peskine43f958b2020-12-13 14:55:14 +01005089 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005091 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01005092}
5093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005094static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
5095 const uint8_t *data,
5096 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01005097{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005098 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
5099 return PSA_ERROR_BAD_STATE;
5100 }
Janos Follath81550542019-06-13 14:26:34 +01005101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005102 if (data_length != 0) {
5103 prf->secret = mbedtls_calloc(1, data_length);
5104 if (prf->secret == NULL) {
5105 return PSA_ERROR_INSUFFICIENT_MEMORY;
5106 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02005107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005108 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02005109 prf->secret_length = data_length;
5110 }
Janos Follath81550542019-06-13 14:26:34 +01005111
Gilles Peskine43f958b2020-12-13 14:55:14 +01005112 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005114 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01005115}
5116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005117static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
5118 const uint8_t *data,
5119 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01005120{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005121 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
5122 return PSA_ERROR_BAD_STATE;
5123 }
Janos Follath63028dd2019-06-13 09:15:47 +01005124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005125 if (data_length != 0) {
5126 prf->label = mbedtls_calloc(1, data_length);
5127 if (prf->label == NULL) {
5128 return PSA_ERROR_INSUFFICIENT_MEMORY;
5129 }
Janos Follath63028dd2019-06-13 09:15:47 +01005130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005131 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005132 prf->label_length = data_length;
5133 }
Janos Follath63028dd2019-06-13 09:15:47 +01005134
Gilles Peskine43f958b2020-12-13 14:55:14 +01005135 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005137 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01005138}
5139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005140static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
5141 psa_key_derivation_step_t step,
5142 const uint8_t *data,
5143 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01005144{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005145 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01005146 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005147 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01005148 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005149 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01005150 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005151 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01005152 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005153 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01005154 }
5155}
John Durkop07cc04a2020-11-16 22:08:34 -08005156#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5157 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5158
5159#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5160static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5161 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005162 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005163 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08005164{
5165 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005166 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08005167 uint8_t *cur = pms;
5168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005169 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
5170 return PSA_ERROR_INVALID_ARGUMENT;
5171 }
John Durkop07cc04a2020-11-16 22:08:34 -08005172
5173 /* Quoting RFC 4279, Section 2:
5174 *
5175 * The premaster secret is formed as follows: if the PSK is N octets
5176 * long, concatenate a uint16 with the value N, N zero octets, a second
5177 * uint16 with the value N, and the PSK itself.
5178 */
5179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005180 *cur++ = MBEDTLS_BYTE_1(data_length);
5181 *cur++ = MBEDTLS_BYTE_0(data_length);
5182 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005183 cur += data_length;
5184 *cur++ = pms[0];
5185 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005186 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005187 cur += data_length;
5188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005189 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08005190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005191 mbedtls_platform_zeroize(pms, sizeof(pms));
5192 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08005193}
Janos Follath6660f0e2019-06-17 08:44:03 +01005194
5195static psa_status_t psa_tls12_prf_psk_to_ms_input(
5196 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005197 psa_key_derivation_step_t step,
5198 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005199 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01005200{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005201 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
5202 return psa_tls12_prf_psk_to_ms_set_key(prf,
5203 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01005204 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005206 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01005207}
John Durkop07cc04a2020-11-16 22:08:34 -08005208#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005209
Gilles Peskineb8965192019-09-24 16:21:10 +02005210/** Check whether the given key type is acceptable for the given
5211 * input step of a key derivation.
5212 *
5213 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5214 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5215 * Both secret and non-secret inputs can alternatively have the type
5216 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5217 * that the input was passed as a buffer rather than via a key object.
5218 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005219static int psa_key_derivation_check_input_type(
5220 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005221 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02005222{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005223 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005224 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005225 if (key_type == PSA_KEY_TYPE_DERIVE) {
5226 return PSA_SUCCESS;
5227 }
5228 if (key_type == PSA_KEY_TYPE_NONE) {
5229 return PSA_SUCCESS;
5230 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005231 break;
5232 case PSA_KEY_DERIVATION_INPUT_LABEL:
5233 case PSA_KEY_DERIVATION_INPUT_SALT:
5234 case PSA_KEY_DERIVATION_INPUT_INFO:
5235 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005236 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
5237 return PSA_SUCCESS;
5238 }
5239 if (key_type == PSA_KEY_TYPE_NONE) {
5240 return PSA_SUCCESS;
5241 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005242 break;
5243 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005244 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005245}
5246
Janos Follathb80a94e2019-06-12 15:54:46 +01005247static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005248 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005249 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005250 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005251 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005252 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005253{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005254 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005255 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005257 status = psa_key_derivation_check_input_type(step, key_type);
5258 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005259 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005260 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005261
John Durkopd0321952020-10-29 21:37:36 -07005262#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005263 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5264 status = psa_hkdf_input(&operation->ctx.hkdf,
5265 PSA_ALG_HKDF_GET_HASH(kdf_alg),
5266 step, data, data_length);
5267 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005268#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5269#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005270 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5271 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
5272 step, data, data_length);
5273 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005274#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5275#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005276 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5277 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
5278 step, data, data_length);
5279 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005280#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005281 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005282 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005283 (void) data;
5284 (void) data_length;
5285 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005286 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005287 }
5288
Gilles Peskine224b0d62019-09-23 18:13:17 +02005289exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005290 if (status != PSA_SUCCESS) {
5291 psa_key_derivation_abort(operation);
5292 }
5293 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005294}
5295
Janos Follath51f4a0f2019-06-14 11:35:55 +01005296psa_status_t psa_key_derivation_input_bytes(
5297 psa_key_derivation_operation_t *operation,
5298 psa_key_derivation_step_t step,
Ryan Everett6f682062024-02-09 16:18:39 +00005299 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005300 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005301{
Ryan Everett6f682062024-02-09 16:18:39 +00005302 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5303 LOCAL_INPUT_DECLARE(data_external, data);
5304
5305 LOCAL_INPUT_ALLOC(data_external, data_length, data);
5306
5307 status = psa_key_derivation_input_internal(operation, step,
5308 PSA_KEY_TYPE_NONE,
5309 data, data_length);
David Horstmann42015332024-03-13 15:42:31 +00005310#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everett6f682062024-02-09 16:18:39 +00005311exit:
Ryan Everettd0d12fb2024-02-12 09:19:29 +00005312#endif
Ryan Everett6f682062024-02-09 16:18:39 +00005313 LOCAL_INPUT_FREE(data_external, data);
5314 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005315}
5316
Janos Follath51f4a0f2019-06-14 11:35:55 +01005317psa_status_t psa_key_derivation_input_key(
5318 psa_key_derivation_operation_t *operation,
5319 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005320 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005321{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005322 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005323 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005324 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005325
Ronald Cron5c522922020-11-14 16:35:34 +01005326 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005327 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5328 if (status != PSA_SUCCESS) {
5329 psa_key_derivation_abort(operation);
5330 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02005331 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005332
5333 /* Passing a key object as a SECRET input unlocks the permission
5334 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005335 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005336 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005337 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005339 status = psa_key_derivation_input_internal(operation,
5340 step, slot->attr.type,
5341 slot->key.data,
5342 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005344 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005346 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005347}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005348
5349
5350
5351/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005352/* Key agreement */
5353/****************************************************************/
5354
John Durkop6ba40d12020-11-10 08:50:04 -08005355#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005356static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
5357 size_t peer_key_length,
5358 const mbedtls_ecp_keypair *our_key,
5359 uint8_t *shared_secret,
5360 size_t shared_secret_size,
5361 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005362{
Steven Cooremand4867872020-08-05 16:31:39 +02005363 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005364 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005365 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005366 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005367 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
5368 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005369
Ronald Cron90857082020-11-25 14:58:33 +01005370 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005371 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5372 bits,
5373 peer_key,
5374 peer_key_length,
5375 &their_key);
5376 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005377 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005378 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005379
Jaeden Amero97271b32019-01-10 19:38:51 +00005380 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005381 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
5382 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005383 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005384 }
Jaeden Amero97271b32019-01-10 19:38:51 +00005385 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005386 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
5387 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005388 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005389 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005390
Jaeden Amero97271b32019-01-10 19:38:51 +00005391 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005392 mbedtls_ecdh_calc_secret(&ecdh,
5393 shared_secret_length,
5394 shared_secret, shared_secret_size,
5395 mbedtls_psa_get_random,
5396 MBEDTLS_PSA_RANDOM_STATE));
5397 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005398 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005399 }
5400 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005401 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005402 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005403
5404exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005405 if (status != PSA_SUCCESS) {
5406 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
5407 }
5408 mbedtls_ecdh_free(&ecdh);
5409 mbedtls_ecp_keypair_free(their_key);
5410 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02005411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005412 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005413}
John Durkop6ba40d12020-11-10 08:50:04 -08005414#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005415
Gilles Peskine01d718c2018-09-18 12:01:02 +02005416#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005418static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
5419 psa_key_slot_t *private_key,
5420 const uint8_t *peer_key,
5421 size_t peer_key_length,
5422 uint8_t *shared_secret,
5423 size_t shared_secret_size,
5424 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005425{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005426 mbedtls_ecp_keypair *ecp = NULL;
5427 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01005428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005429 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08005430#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005431 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005432 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
5433 return PSA_ERROR_INVALID_ARGUMENT;
5434 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005435 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005436 private_key->attr.type,
5437 private_key->attr.bits,
5438 private_key->key.data,
5439 private_key->key.bytes,
5440 &ecp);
5441 if (status != PSA_SUCCESS) {
5442 return status;
5443 }
5444 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
5445 ecp,
5446 shared_secret, shared_secret_size,
5447 shared_secret_length);
5448 mbedtls_ecp_keypair_free(ecp);
5449 mbedtls_free(ecp);
5450 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08005451#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005452 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005453 (void) ecp;
5454 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01005455 (void) private_key;
5456 (void) peer_key;
5457 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005458 (void) shared_secret;
5459 (void) shared_secret_size;
5460 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005461 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005462 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005463}
5464
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005465/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005466 * to potentially free embedded data structures and wipe confidential data.
5467 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005468static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5469 psa_key_derivation_step_t step,
5470 psa_key_slot_t *private_key,
5471 const uint8_t *peer_key,
5472 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005473{
5474 psa_status_t status;
5475 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5476 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005477 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005478
5479 /* Step 1: run the secret agreement algorithm to generate the shared
5480 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005481 status = psa_key_agreement_raw_internal(ka_alg,
5482 private_key,
5483 peer_key, peer_key_length,
5484 shared_secret,
5485 sizeof(shared_secret),
5486 &shared_secret_length);
5487 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005488 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005489 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005490
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005491 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005492 * the shared secret. A shared secret is permitted wherever a key
5493 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005494 status = psa_key_derivation_input_internal(operation, step,
5495 PSA_KEY_TYPE_DERIVE,
5496 shared_secret,
5497 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005498exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005499 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5500 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005501}
5502
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005503psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5504 psa_key_derivation_step_t step,
5505 mbedtls_svc_key_id_t private_key,
Thomas Daubney9da359f2024-02-15 13:15:47 +00005506 const uint8_t *peer_key_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005507 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005508{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005509 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005510 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005511 psa_key_slot_t *slot;
Thomas Daubney9da359f2024-02-15 13:15:47 +00005512 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005514 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5515 return PSA_ERROR_INVALID_ARGUMENT;
5516 }
Ronald Cron5c522922020-11-14 16:35:34 +01005517 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005518 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5519 if (status != PSA_SUCCESS) {
5520 return status;
5521 }
Thomas Daubney9da359f2024-02-15 13:15:47 +00005522
Thomas Daubneyca928312024-03-12 17:53:30 +00005523 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005524 status = psa_key_agreement_internal(operation, step,
5525 slot,
5526 peer_key, peer_key_length);
Thomas Daubney9da359f2024-02-15 13:15:47 +00005527
David Horstmann42015332024-03-13 15:42:31 +00005528#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney9da359f2024-02-15 13:15:47 +00005529exit:
Thomas Daubney3c0c6b12024-02-15 14:25:08 +00005530#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005531 if (status != PSA_SUCCESS) {
5532 psa_key_derivation_abort(operation);
5533 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005534 /* If a private key has been added as SECRET, we allow the derived
5535 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005536 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005537 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005538 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005539 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005541 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005542
Thomas Daubney9da359f2024-02-15 13:15:47 +00005543 LOCAL_INPUT_FREE(peer_key_external, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005544 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005545}
5546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005547psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5548 mbedtls_svc_key_id_t private_key,
Thomas Daubney43042762024-02-15 12:57:26 +00005549 const uint8_t *peer_key_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005550 size_t peer_key_length,
Thomas Daubney43042762024-02-15 12:57:26 +00005551 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005552 size_t output_size,
5553 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005554{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005555 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005556 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005557 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005558 size_t expected_length;
Thomas Daubney43042762024-02-15 12:57:26 +00005559 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
5560 LOCAL_OUTPUT_DECLARE(output_external, output);
Thomas Daubney0736df32024-02-21 12:28:20 +00005561 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005563 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005564 status = PSA_ERROR_INVALID_ARGUMENT;
5565 goto exit;
5566 }
Ronald Cron5c522922020-11-14 16:35:34 +01005567 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005568 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5569 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005570 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005571 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005572
Gilles Peskine42313fb2022-04-14 00:17:15 +02005573 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5574 * for the output size. The PSA specification only guarantees that this
5575 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5576 * but it might be nice to allow smaller buffers if the output fits.
5577 * At the time of writing this comment, with only ECDH implemented,
5578 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5579 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5580 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005581 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005582 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5583 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005584 status = PSA_ERROR_BUFFER_TOO_SMALL;
5585 goto exit;
5586 }
5587
Thomas Daubney43042762024-02-15 12:57:26 +00005588 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005589 status = psa_key_agreement_raw_internal(alg, slot,
5590 peer_key, peer_key_length,
5591 output, output_size,
5592 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005593
5594exit:
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005595 /* Check for successful allocation of output,
5596 * with an unsuccessful status. */
Thomas Daubney2ea8d8f2024-02-21 15:16:01 +00005597 if (output != NULL && status != PSA_SUCCESS) {
5598 /* If an error happens and is not handled properly, the output
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005599 * may be used as a key to protect sensitive data. Arrange for such
5600 * a key to be random, which is likely to result in decryption or
5601 * verification errors. This is better than filling the buffer with
5602 * some constant data such as zeros, which would result in the data
5603 * being protected with a reproducible, easily knowable key.
5604 */
David Horstmann65bf12c2024-02-06 15:27:49 +00005605 psa_generate_random_internal(output, output_size);
Thomas Daubney2ea8d8f2024-02-21 15:16:01 +00005606 *output_length = output_size;
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005607 }
5608
5609 if (output == NULL) {
Thomas Daubney0736df32024-02-21 12:28:20 +00005610 /* output allocation failed. */
5611 *output_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005612 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005614 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005615
Thomas Daubney43042762024-02-15 12:57:26 +00005616 LOCAL_INPUT_FREE(peer_key_external, peer_key);
5617 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005618 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005619}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005620
5621
5622/****************************************************************/
5623/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005624/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005625
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005626#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5627#include "mbedtls/entropy_poll.h"
5628#endif
5629
Gilles Peskine30524eb2020-11-13 17:02:26 +01005630/** Initialize the PSA random generator.
5631 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005632static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005633{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005634#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005635 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005636#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5637
Gilles Peskine30524eb2020-11-13 17:02:26 +01005638 /* Set default configuration if
5639 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005640 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005641 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005642 }
5643 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005644 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005645 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005647 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005648#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5649 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5650 /* The PSA entropy injection feature depends on using NV seed as an entropy
5651 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005652 mbedtls_entropy_add_source(&rng->entropy,
5653 mbedtls_nv_seed_poll, NULL,
5654 MBEDTLS_ENTROPY_BLOCK_SIZE,
5655 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005656#endif
5657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005658 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005659#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005660}
5661
5662/** Deinitialize the PSA random generator.
5663 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005664static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005665{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005666#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005667 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005668#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005669 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5670 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005671#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005672}
5673
5674/** Seed the PSA random generator.
5675 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005676static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005677{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005678#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5679 /* Do nothing: the external RNG seeds itself. */
5680 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005681 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005682#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005683 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005684 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5685 drbg_seed, sizeof(drbg_seed) - 1);
5686 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005687#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005688}
5689
David Horstmann65bf12c2024-02-06 15:27:49 +00005690psa_status_t psa_generate_random(uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005691 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005692{
David Horstmann65bf12c2024-02-06 15:27:49 +00005693 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005694
David Horstmann65bf12c2024-02-06 15:27:49 +00005695 LOCAL_OUTPUT_DECLARE(output_external, output);
5696 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005697
David Horstmann65bf12c2024-02-06 15:27:49 +00005698 status = psa_generate_random_internal(output, output_size);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005699
David Horstmann42015332024-03-13 15:42:31 +00005700#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann65bf12c2024-02-06 15:27:49 +00005701exit:
David Horstmann10e44f32024-02-28 14:17:10 +00005702#endif
David Horstmann65bf12c2024-02-06 15:27:49 +00005703 LOCAL_OUTPUT_FREE(output_external, output);
5704 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005705}
5706
Gilles Peskine8814fc42020-12-14 15:33:44 +01005707/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005708 *
5709 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5710 * `psa_generate_random(...)`. The state parameter is ignored since the
5711 * PSA API doesn't support passing an explicit state.
5712 *
5713 * In the non-external case, psa_generate_random() calls an
5714 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5715 * and semantics as mbedtls_psa_get_random(). As an optimization,
5716 * instead of doing this back-and-forth between the PSA API and the
5717 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5718 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005719 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005720#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5721int mbedtls_psa_get_random(void *p_rng,
5722 unsigned char *output,
5723 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005724{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005725 /* This function takes a pointer to the RNG state because that's what
5726 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5727 * its own state internally and doesn't let the caller access that state.
5728 * So we just ignore the state parameter, and in practice we'll pass
5729 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005730 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005731 psa_status_t status = psa_generate_random(output, output_size);
5732 if (status == PSA_SUCCESS) {
5733 return 0;
5734 } else {
5735 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5736 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005737}
5738#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5739
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005740#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005741psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5742 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005743{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005744 if (global_data.initialized) {
5745 return PSA_ERROR_NOT_PERMITTED;
5746 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005747
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005748 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5749 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5750 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5751 return PSA_ERROR_INVALID_ARGUMENT;
5752 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005754 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005755}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005756#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005757
Ronald Cron3772afe2021-02-08 16:10:05 +01005758/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005759 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005760 * \param type The key type
5761 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005762 *
5763 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005764 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005765 * \retval #PSA_ERROR_INVALID_ARGUMENT
5766 * The size in bits of the key is not valid.
5767 * \retval #PSA_ERROR_NOT_SUPPORTED
5768 * The type and/or the size in bits of the key or the combination of
5769 * the two is not supported.
5770 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005771static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005772 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005773{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005774 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005775
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005776 if (key_type_is_raw_bytes(type)) {
5777 status = validate_unstructured_key_bit_size(type, bits);
5778 if (status != PSA_SUCCESS) {
5779 return status;
5780 }
5781 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005782#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005783 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5784 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5785 return PSA_ERROR_NOT_SUPPORTED;
5786 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005787
Ronald Cron01b2aba2020-10-05 09:42:02 +02005788 /* Accept only byte-aligned keys, for the same reasons as
5789 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005790 if (bits % 8 != 0) {
5791 return PSA_ERROR_NOT_SUPPORTED;
5792 }
5793 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005794#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005795
Ronald Cron5c4d3862020-12-07 11:07:24 +01005796#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005797 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005798 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005799 return PSA_SUCCESS;
5800 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005801#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005802 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005803 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005804 }
5805
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005806 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005807}
5808
Ronald Cron55ed0592020-10-05 10:30:40 +02005809psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005810 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005811 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005812{
5813 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005814 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005816 if ((attributes->domain_parameters == NULL) &&
5817 (attributes->domain_parameters_size != 0)) {
5818 return PSA_ERROR_INVALID_ARGUMENT;
5819 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005821 if (key_type_is_raw_bytes(type)) {
5822 status = psa_generate_random(key_buffer, key_buffer_size);
5823 if (status != PSA_SUCCESS) {
5824 return status;
5825 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005826
David Brown12ca5032021-02-11 11:02:00 -07005827#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005828 if (type == PSA_KEY_TYPE_DES) {
5829 psa_des_set_key_parity(key_buffer, key_buffer_size);
5830 }
David Brown8107e312021-02-12 08:08:46 -07005831#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005832 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005833
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005834#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5835 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005836 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5837 return mbedtls_psa_rsa_generate_key(attributes,
5838 key_buffer,
5839 key_buffer_size,
5840 key_buffer_length);
5841 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005842#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5843 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005844
John Durkop9814fa22020-11-04 12:28:15 -08005845#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005846 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5847 return mbedtls_psa_ecp_generate_key(attributes,
5848 key_buffer,
5849 key_buffer_size,
5850 key_buffer_length);
5851 } else
John Durkop9814fa22020-11-04 12:28:15 -08005852#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005853 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005854 (void) key_buffer_length;
5855 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005856 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005857
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005858 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005859}
Darryl Green0c6575a2018-11-07 16:05:30 +00005860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005861psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5862 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005863{
5864 psa_status_t status;
5865 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005866 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005867 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005868
Ronald Cron81709fc2020-11-14 12:10:32 +01005869 *key = MBEDTLS_SVC_KEY_ID_INIT;
5870
Gilles Peskine0f84d622019-09-12 19:03:13 +02005871 /* Reject any attempt to create a zero-length key so that we don't
5872 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005873 if (psa_get_key_bits(attributes) == 0) {
5874 return PSA_ERROR_INVALID_ARGUMENT;
5875 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005876
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005877 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005878 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5879 return PSA_ERROR_INVALID_ARGUMENT;
5880 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005882 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5883 &slot, &driver);
5884 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005885 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005886 }
Gilles Peskine11792082019-08-06 18:36:36 +02005887
Ronald Cron977c2472020-10-13 08:32:21 +02005888 /* In the case of a transparent key or an opaque key stored in local
5889 * storage (thus not in the case of generating a key in a secure element
5890 * or cryptoprocessor with storage), we have to allocate a buffer to
5891 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005892 if (slot->key.data == NULL) {
5893 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5894 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005895 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005896 attributes->core.type, attributes->core.bits);
5897 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005898 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005899 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005900
5901 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005902 attributes->core.type,
5903 attributes->core.bits);
5904 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005905 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005906 attributes, &key_buffer_size);
5907 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005908 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005909 }
Ronald Cron977c2472020-10-13 08:32:21 +02005910 }
Ronald Cron977c2472020-10-13 08:32:21 +02005911
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005912 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5913 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005914 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005915 }
Ronald Cron977c2472020-10-13 08:32:21 +02005916 }
5917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005918 status = psa_driver_wrapper_generate_key(attributes,
5919 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005921 if (status != PSA_SUCCESS) {
5922 psa_remove_key_data_from_memory(slot);
5923 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005924
Gilles Peskine11792082019-08-06 18:36:36 +02005925exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005926 if (status == PSA_SUCCESS) {
5927 status = psa_finish_key_creation(slot, driver, key);
5928 }
5929 if (status != PSA_SUCCESS) {
5930 psa_fail_key_creation(slot, driver);
5931 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005933 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005934}
5935
Gilles Peskinee59236f2018-01-27 23:32:46 +01005936/****************************************************************/
5937/* Module setup */
5938/****************************************************************/
5939
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005940#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005941psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005942 void (* entropy_init)(mbedtls_entropy_context *ctx),
5943 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005944{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005945 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5946 return PSA_ERROR_BAD_STATE;
5947 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005948 global_data.rng.entropy_init = entropy_init;
5949 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005950 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005951}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005952#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005954void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005955{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005956 psa_wipe_all_key_slots();
5957 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5958 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005959 }
5960 /* Wipe all remaining data, including configuration.
5961 * In particular, this sets all state indicator to the value
5962 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005963 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005964
5965 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005966 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005967}
5968
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005969#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5970/** Recover a transaction that was interrupted by a power failure.
5971 *
5972 * This function is called during initialization, before psa_crypto_init()
5973 * returns. If this function returns a failure status, the initialization
5974 * fails.
5975 */
5976static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005977 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005978{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005979 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005980 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5981 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005982 /* TODO - fall through to the failure case until this
5983 * is implemented.
5984 * https://github.com/ARMmbed/mbed-crypto/issues/218
5985 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005986 default:
5987 /* We found an unsupported transaction in the storage.
5988 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005989 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005990 }
5991}
5992#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005994psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005995{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005996 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005997
Gilles Peskinec6b69072018-11-20 21:42:52 +01005998 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005999 if (global_data.initialized != 0) {
6000 return PSA_SUCCESS;
6001 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006002
Gilles Peskine30524eb2020-11-13 17:02:26 +01006003 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006004 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01006005 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006006 status = mbedtls_psa_random_seed(&global_data.rng);
6007 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006008 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006009 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01006010 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006012 status = psa_initialize_key_slots();
6013 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01006014 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006015 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01006016
Ronald Cron088d5d02021-04-10 16:57:30 +02006017 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006018 status = psa_driver_wrapper_init();
6019 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02006020 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006021 }
Gilles Peskined9348f22019-10-01 15:22:29 +02006022
Gilles Peskine4b734222019-07-24 15:56:31 +02006023#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006024 status = psa_crypto_load_transaction();
6025 if (status == PSA_SUCCESS) {
6026 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
6027 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006028 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006029 }
6030 status = psa_crypto_stop_transaction();
6031 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02006032 /* There's no transaction to complete. It's all good. */
6033 status = PSA_SUCCESS;
6034 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006035#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006036
Gilles Peskinec6b69072018-11-20 21:42:52 +01006037 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006038 global_data.initialized = 1;
6039
6040exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006041 if (status != PSA_SUCCESS) {
6042 mbedtls_psa_crypto_free();
6043 }
6044 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006045}
6046
David Horstmann114d8242023-12-07 18:39:17 +00006047/* Memory copying test hooks. These are called before input copy, after input
6048 * copy, before output copy and after output copy, respectively.
6049 * They are used by memory-poisoning tests to temporarily unpoison buffers
6050 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00006051#if defined(MBEDTLS_TEST_HOOKS)
6052void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
6053void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
6054void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
6055void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
6056#endif
6057
David Horstmanndf493552023-11-15 15:18:30 +00006058/** Copy from an input buffer to a local copy.
6059 *
6060 * \param[in] input Pointer to input buffer.
6061 * \param[in] input_len Length of the input buffer.
6062 * \param[out] input_copy Pointer to a local copy in which to store the input data.
6063 * \param[out] input_copy_len Length of the local copy buffer.
6064 * \return #PSA_SUCCESS, if the buffer was successfully
6065 * copied.
6066 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
6067 * copy is too small to hold contents of the
6068 * input buffer.
6069 */
6070MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00006071psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
6072 uint8_t *input_copy, size_t input_copy_len)
6073{
6074 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00006075 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00006076 }
6077
David Horstmann0760b152023-11-24 16:21:04 +00006078#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006079 if (psa_input_pre_copy_hook != NULL) {
6080 psa_input_pre_copy_hook(input, input_len);
6081 }
David Horstmann0760b152023-11-24 16:21:04 +00006082#endif
6083
David Horstmann660027f2023-11-15 17:33:47 +00006084 if (input_len > 0) {
6085 memcpy(input_copy, input, input_len);
6086 }
David Horstmann957f9802023-10-30 20:29:43 +00006087
David Horstmann0760b152023-11-24 16:21:04 +00006088#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006089 if (psa_input_post_copy_hook != NULL) {
6090 psa_input_post_copy_hook(input, input_len);
6091 }
David Horstmann0760b152023-11-24 16:21:04 +00006092#endif
6093
David Horstmann957f9802023-10-30 20:29:43 +00006094 return PSA_SUCCESS;
6095}
6096
David Horstmanndf493552023-11-15 15:18:30 +00006097/** Copy from a local output buffer into a user-supplied one.
6098 *
6099 * \param[in] output_copy Pointer to a local buffer containing the output.
6100 * \param[in] output_copy_len Length of the local buffer.
6101 * \param[out] output Pointer to user-supplied output buffer.
6102 * \param[out] output_len Length of the user-supplied output buffer.
6103 * \return #PSA_SUCCESS, if the buffer was successfully
6104 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00006105 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00006106 * user-supplied output buffer is too small to
6107 * hold the contents of the local buffer.
6108 */
6109MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00006110psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
6111 uint8_t *output, size_t output_len)
6112{
6113 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00006114 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00006115 }
David Horstmann660027f2023-11-15 17:33:47 +00006116
David Horstmann0760b152023-11-24 16:21:04 +00006117#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006118 if (psa_output_pre_copy_hook != NULL) {
6119 psa_output_pre_copy_hook(output, output_len);
6120 }
David Horstmann0760b152023-11-24 16:21:04 +00006121#endif
6122
David Horstmann660027f2023-11-15 17:33:47 +00006123 if (output_copy_len > 0) {
6124 memcpy(output, output_copy, output_copy_len);
6125 }
6126
David Horstmann0760b152023-11-24 16:21:04 +00006127#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006128 if (psa_output_post_copy_hook != NULL) {
6129 psa_output_post_copy_hook(output, output_len);
6130 }
David Horstmann0760b152023-11-24 16:21:04 +00006131#endif
6132
David Horstmann2bd296e2023-10-30 20:37:12 +00006133 return PSA_SUCCESS;
6134}
6135
David Horstmann81a0d572023-11-20 17:15:32 +00006136psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
6137 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006138{
6139 psa_status_t status;
6140
David Horstmann0d52c712023-11-23 15:50:37 +00006141 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006142
David Horstmann726bf052023-11-15 18:11:26 +00006143 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006144 return PSA_SUCCESS;
6145 }
6146
David Horstmann81a0d572023-11-20 17:15:32 +00006147 local_input->buffer = mbedtls_calloc(input_len, 1);
6148 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006149 /* Since we dealt with the zero-length case above, we know that
6150 * a NULL return value means a failure of allocation. */
6151 return PSA_ERROR_INSUFFICIENT_MEMORY;
6152 }
David Horstmann81a0d572023-11-20 17:15:32 +00006153 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006154
David Horstmann81a0d572023-11-20 17:15:32 +00006155 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006156
6157 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00006158 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006159 if (status != PSA_SUCCESS) {
6160 goto error;
6161 }
6162
6163 return PSA_SUCCESS;
6164
6165error:
David Horstmann81a0d572023-11-20 17:15:32 +00006166 mbedtls_free(local_input->buffer);
6167 local_input->buffer = NULL;
6168 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006169 return status;
6170}
6171
David Horstmann81a0d572023-11-20 17:15:32 +00006172void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00006173{
David Horstmann81a0d572023-11-20 17:15:32 +00006174 mbedtls_free(local_input->buffer);
6175 local_input->buffer = NULL;
6176 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00006177}
6178
David Horstmann1a76ab12023-11-20 12:54:09 +00006179psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
6180 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00006181{
David Horstmann0d52c712023-11-23 15:50:37 +00006182 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00006183
David Horstmann726bf052023-11-15 18:11:26 +00006184 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006185 return PSA_SUCCESS;
6186 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006187 local_output->buffer = mbedtls_calloc(output_len, 1);
6188 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006189 /* Since we dealt with the zero-length case above, we know that
6190 * a NULL return value means a failure of allocation. */
6191 return PSA_ERROR_INSUFFICIENT_MEMORY;
6192 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006193 local_output->length = output_len;
6194 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00006195
6196 return PSA_SUCCESS;
6197}
6198
David Horstmann1a76ab12023-11-20 12:54:09 +00006199psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00006200{
David Horstmann761761f2023-11-15 15:57:32 +00006201 psa_status_t status;
6202
David Horstmann1a76ab12023-11-20 12:54:09 +00006203 if (local_output->buffer == NULL) {
6204 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006205 return PSA_SUCCESS;
6206 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006207 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00006208 /* We have an internal copy but nothing to copy back to. */
6209 return PSA_ERROR_CORRUPTION_DETECTED;
6210 }
6211
David Horstmann1a76ab12023-11-20 12:54:09 +00006212 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
6213 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00006214 if (status != PSA_SUCCESS) {
6215 return status;
6216 }
David Horstmannaeeb2742023-11-08 17:44:18 +00006217
David Horstmann1a76ab12023-11-20 12:54:09 +00006218 mbedtls_free(local_output->buffer);
6219 local_output->buffer = NULL;
6220 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006221
6222 return PSA_SUCCESS;
6223}
6224
Gilles Peskinee59236f2018-01-27 23:32:46 +01006225#endif /* MBEDTLS_PSA_CRYPTO_C */