blob: b3f8644aad393d61192be561c816f56ddfcec2f9 [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));
Darryl Green0c6575a2018-11-07 16:05:30 +00001834 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001836 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001837 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001838 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001839 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001840#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001842 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001843}
Gilles Peskine4747d192019-04-17 15:05:45 +02001844
1845/** Finalize the creation of a key once its key material has been set.
1846 *
1847 * This entails writing the key to persistent storage.
1848 *
1849 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001850 * See the documentation of psa_start_key_creation() for the intended use
1851 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001852 *
Ronald Cron5c522922020-11-14 16:35:34 +01001853 * If the finalization succeeds, the function unlocks the key slot (it was
1854 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1855 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001856 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001857 * \param[in,out] slot Pointer to the slot with key material.
1858 * \param[in] driver The secure element driver for the key,
1859 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001860 * \param[out] key On success, identifier of the key. Note that the
1861 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001862 *
1863 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001864 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001865 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1866 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1867 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1868 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1869 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1870 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001871 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001872 * \return If this function fails, the key slot is an invalid state.
1873 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001874 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001875static psa_status_t psa_finish_key_creation(
1876 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001877 psa_se_drv_table_entry_t *driver,
1878 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001879{
1880 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001881 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001882 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001883
1884#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001885 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001886#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001887 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001888 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001889 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001890 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001891
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001892 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1893 sizeof(data.slot_number),
1894 "Slot number size does not match psa_se_key_data_storage_t");
1895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001896 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1897 status = psa_save_persistent_key(&slot->attr,
1898 (uint8_t *) &data,
1899 sizeof(data));
1900 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001901#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1902 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001903 /* Key material is saved in export representation in the slot, so
1904 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001905 status = psa_save_persistent_key(&slot->attr,
1906 slot->key.data,
1907 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001908 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001909 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001910#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1911
Gilles Peskinecbaff462019-07-12 23:46:04 +02001912#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001913 /* Finish the transaction for a key creation. This does not
1914 * happen when registering an existing key. Detect this case
1915 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001916 * creation of a persistent key in a secure element requires a transaction,
1917 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001918 if (driver != NULL &&
1919 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1920 status = psa_save_se_persistent_data(driver);
1921 if (status != PSA_SUCCESS) {
1922 psa_destroy_persistent_key(slot->attr.id);
1923 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001924 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001925 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001926 }
1927#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1928
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001929 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001930 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001931 status = psa_unlock_key_slot(slot);
1932 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001933 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001934 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001935 }
Ronald Cron50972942020-11-14 11:28:25 +01001936
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001937 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001938}
1939
1940/** Abort the creation of a key.
1941 *
1942 * You may call this function after calling psa_start_key_creation(),
1943 * or after psa_finish_key_creation() fails. In other circumstances, this
1944 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001945 * See the documentation of psa_start_key_creation() for the intended use
1946 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001947 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001948 * \param[in,out] slot Pointer to the slot with key material.
1949 * \param[in] driver The secure element driver for the key,
1950 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001951 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001952static void psa_fail_key_creation(psa_key_slot_t *slot,
1953 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001954{
Gilles Peskine011e4282019-06-26 18:34:38 +02001955 (void) driver;
1956
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001957 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001958 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001959 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001960
1961#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001962 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001963 * element, and the failure happened later (when saving metadata
1964 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001965 * element.
1966 * https://github.com/ARMmbed/mbed-crypto/issues/217
1967 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001968
Gilles Peskined7729582019-08-05 15:55:54 +02001969 /* Abort the ongoing transaction if any (there may not be one if
1970 * the creation process failed before starting one, or if the
1971 * key creation is a registration of a key in a secure element).
1972 * Earlier functions must already have done what it takes to undo any
1973 * partial creation. All that's left is to update the transaction data
1974 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001975 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001976#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1977
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001978 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001979}
1980
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001981/** Validate optional attributes during key creation.
1982 *
1983 * Some key attributes are optional during key creation. If they are
1984 * specified in the attributes structure, check that they are consistent
1985 * with the data in the slot.
1986 *
1987 * This function should be called near the end of key creation, after
1988 * the slot in memory is fully populated but before saving persistent data.
1989 */
1990static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001991 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001992 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001993{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001994 if (attributes->core.type != 0) {
1995 if (attributes->core.type != slot->attr.type) {
1996 return PSA_ERROR_INVALID_ARGUMENT;
1997 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001998 }
1999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002000 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07002001#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002002 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2003 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002004 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002005 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002006 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002007
Ronald Cron90857082020-11-25 14:58:33 +01002008 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002009 slot->attr.type,
2010 slot->key.data,
2011 slot->key.bytes,
2012 &rsa);
2013 if (status != PSA_SUCCESS) {
2014 return status;
2015 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002016
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002017 mbedtls_mpi_init(&actual);
2018 mbedtls_mpi_init(&required);
2019 ret = mbedtls_rsa_export(rsa,
2020 NULL, NULL, NULL, NULL, &actual);
2021 mbedtls_rsa_free(rsa);
2022 mbedtls_free(rsa);
2023 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002024 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002025 }
2026 ret = mbedtls_mpi_read_binary(&required,
2027 attributes->domain_parameters,
2028 attributes->domain_parameters_size);
2029 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002030 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002031 }
2032 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002033 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002034 }
2035rsa_exit:
2036 mbedtls_mpi_free(&actual);
2037 mbedtls_mpi_free(&required);
2038 if (ret != 0) {
2039 return mbedtls_to_psa_error(ret);
2040 }
2041 } else
John Durkop9814fa22020-11-04 12:28:15 -08002042#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2043 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002044 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002045 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002046 }
2047 }
2048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002049 if (attributes->core.bits != 0) {
2050 if (attributes->core.bits != slot->attr.bits) {
2051 return PSA_ERROR_INVALID_ARGUMENT;
2052 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002053 }
2054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002055 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002056}
2057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002058psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Ryan Everett6b970252024-01-19 16:02:59 +00002059 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002060 size_t data_length,
2061 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002062{
2063 psa_status_t status;
Ryan Everett6b970252024-01-19 16:02:59 +00002064 LOCAL_INPUT_DECLARE(data_external, data);
Gilles Peskine4747d192019-04-17 15:05:45 +02002065 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002066 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002067 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002068
Ronald Cron81709fc2020-11-14 12:10:32 +01002069 *key = MBEDTLS_SVC_KEY_ID_INIT;
2070
Gilles Peskine0f84d622019-09-12 19:03:13 +02002071 /* Reject zero-length symmetric keys (including raw data key objects).
2072 * This also rejects any key which might be encoded as an empty string,
2073 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002074 if (data_length == 0) {
2075 return PSA_ERROR_INVALID_ARGUMENT;
2076 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002077
Ryan Everett6b970252024-01-19 16:02:59 +00002078 LOCAL_INPUT_ALLOC(data_external, data_length, data);
2079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002080 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2081 &slot, &driver);
2082 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002083 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002084 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002085
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002086 /* In the case of a transparent key or an opaque key stored in local
2087 * storage (thus not in the case of generating a key in a secure element
2088 * or cryptoprocessor with storage), we have to allocate a buffer to
2089 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002090 if (slot->key.data == NULL) {
2091 status = psa_allocate_buffer_to_slot(slot, data_length);
2092 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002093 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002094 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002095 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002096
2097 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002098 status = psa_driver_wrapper_import_key(attributes,
2099 data, data_length,
2100 slot->key.data,
2101 slot->key.bytes,
2102 &slot->key.bytes, &bits);
2103 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002104 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002105 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002107 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002108 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002109 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002110 status = PSA_ERROR_INVALID_ARGUMENT;
2111 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002112 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002114 status = psa_validate_optional_attributes(slot, attributes);
2115 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002116 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002117 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002119 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002120exit:
Ryan Everett6b970252024-01-19 16:02:59 +00002121 LOCAL_INPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002122 if (status != PSA_SUCCESS) {
2123 psa_fail_key_creation(slot, driver);
2124 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002126 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002127}
2128
Gilles Peskined7729582019-08-05 15:55:54 +02002129#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2130psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002131 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002132{
2133 psa_status_t status;
2134 psa_key_slot_t *slot = NULL;
2135 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002136 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002137
2138 /* Leaving attributes unspecified is not currently supported.
2139 * It could make sense to query the key type and size from the
2140 * secure element, but not all secure elements support this
2141 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002142 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2143 return PSA_ERROR_NOT_SUPPORTED;
2144 }
2145 if (psa_get_key_bits(attributes) == 0) {
2146 return PSA_ERROR_NOT_SUPPORTED;
2147 }
Gilles Peskined7729582019-08-05 15:55:54 +02002148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002149 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2150 &slot, &driver);
2151 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002152 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002153 }
Gilles Peskined7729582019-08-05 15:55:54 +02002154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002155 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002156
2157exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002158 if (status != PSA_SUCCESS) {
2159 psa_fail_key_creation(slot, driver);
2160 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002161
Gilles Peskined7729582019-08-05 15:55:54 +02002162 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002163 psa_close_key(key);
2164 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002165}
2166#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002168static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2169 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002170{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002171 psa_status_t status = psa_copy_key_material_into_slot(target,
2172 source->key.data,
2173 source->key.bytes);
2174 if (status != PSA_SUCCESS) {
2175 return status;
2176 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002177
Steven Cooreman398aee52020-10-13 14:35:45 +02002178 target->attr.type = source->attr.type;
2179 target->attr.bits = source->attr.bits;
2180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002181 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002182}
2183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002184psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2185 const psa_key_attributes_t *specified_attributes,
2186 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002187{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002188 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002189 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002190 psa_key_slot_t *source_slot = NULL;
2191 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002192 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002193 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002194
Ronald Cron81709fc2020-11-14 12:10:32 +01002195 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2196
Ronald Cron5c522922020-11-14 16:35:34 +01002197 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002198 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2199 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002200 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002201 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002203 status = psa_validate_optional_attributes(source_slot,
2204 specified_attributes);
2205 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002206 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002207 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002209 status = psa_restrict_key_policy(source_slot->attr.type,
2210 &actual_attributes.core.policy,
2211 &source_slot->attr.policy);
2212 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002213 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002214 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002216 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2217 &target_slot, &driver);
2218 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002219 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002220 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002221
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002222#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002223 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002224 /* Copying to a secure element is not implemented yet. */
2225 status = PSA_ERROR_NOT_SUPPORTED;
2226 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002227 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002228#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002230 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002231 /*
2232 * Copying through an opaque driver is not implemented yet, consider
2233 * a lifetime with an external location as an invalid parameter for
2234 * now.
2235 */
2236 status = PSA_ERROR_INVALID_ARGUMENT;
2237 goto exit;
2238 }
2239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002240 status = psa_copy_key_material(source_slot, target_slot);
2241 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002242 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002243 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002245 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002246exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002247 if (status != PSA_SUCCESS) {
2248 psa_fail_key_creation(target_slot, driver);
2249 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002251 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002253 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002254}
2255
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002256
2257
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002258/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002259/* Message digests */
2260/****************************************************************/
2261
Gilles Peskineb6e362b2024-04-10 20:05:06 +02002262static int is_hash_supported(psa_algorithm_t alg)
2263{
2264 switch (alg) {
2265#if defined(PSA_WANT_ALG_MD2)
2266 case PSA_ALG_MD2:
2267 return 1;
2268#endif
2269#if defined(PSA_WANT_ALG_MD4)
2270 case PSA_ALG_MD4:
2271 return 1;
2272#endif
2273#if defined(PSA_WANT_ALG_MD5)
2274 case PSA_ALG_MD5:
2275 return 1;
2276#endif
2277#if defined(PSA_WANT_ALG_RIPEMD160)
2278 case PSA_ALG_RIPEMD160:
2279 return 1;
2280#endif
2281#if defined(PSA_WANT_ALG_SHA_1)
2282 case PSA_ALG_SHA_1:
2283 return 1;
2284#endif
2285#if defined(PSA_WANT_ALG_SHA_224)
2286 case PSA_ALG_SHA_224:
2287 return 1;
2288#endif
2289#if defined(PSA_WANT_ALG_SHA_256)
2290 case PSA_ALG_SHA_256:
2291 return 1;
2292#endif
2293#if defined(PSA_WANT_ALG_SHA_384)
2294 case PSA_ALG_SHA_384:
2295 return 1;
2296#endif
2297#if defined(PSA_WANT_ALG_SHA_512)
2298 case PSA_ALG_SHA_512:
2299 return 1;
2300#endif
2301 default:
2302 return 0;
2303 }
2304}
2305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002306psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002307{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002308 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002309 if (operation->id == 0) {
2310 return PSA_SUCCESS;
2311 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002313 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002314 operation->id = 0;
2315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002316 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002317}
2318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002319psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2320 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002322 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002323
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002324 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002325 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002326 status = PSA_ERROR_BAD_STATE;
2327 goto exit;
2328 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002329
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002330 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002331 status = PSA_ERROR_INVALID_ARGUMENT;
2332 goto exit;
2333 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002334
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002335 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2336 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002337 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002339 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002340
2341exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002342 if (status != PSA_SUCCESS) {
2343 psa_hash_abort(operation);
2344 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002345
2346 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347}
2348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002349psa_status_t psa_hash_update(psa_hash_operation_t *operation,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002350 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002351 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002352{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002353 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002354 LOCAL_INPUT_DECLARE(input_external, input);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002356 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002357 status = PSA_ERROR_BAD_STATE;
2358 goto exit;
2359 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002360
Steven Cooremanc8288352021-03-04 14:02:19 +01002361 /* Don't require hash implementations to behave correctly on a
2362 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002363 if (input_length == 0) {
2364 return PSA_SUCCESS;
2365 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002366
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002367 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002368 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002369
2370exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002371 if (status != PSA_SUCCESS) {
2372 psa_hash_abort(operation);
2373 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002374
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002375 LOCAL_INPUT_FREE(input_external, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002376 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002377}
2378
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002379static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
Thomas Daubneycbf09212024-01-25 17:14:29 +00002380 uint8_t *hash,
2381 size_t hash_size,
2382 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002383{
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002384 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2385
Steven Cooremanfbe09282021-03-08 18:41:12 +01002386 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002387 if (operation->id == 0) {
2388 return PSA_ERROR_BAD_STATE;
2389 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002390
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002391 status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002392 operation, hash, hash_size, hash_length);
2393 psa_hash_abort(operation);
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002394
2395 return status;
2396}
2397
2398psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2399 uint8_t *hash_external,
2400 size_t hash_size,
2401 size_t *hash_length)
2402{
2403 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2404 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2405
2406 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2407 status = psa_hash_finish_internal(operation, hash, hash_size, hash_length);
2408
David Horstmann42015332024-03-13 15:42:31 +00002409#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002410exit:
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002411#endif
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002412 LOCAL_OUTPUT_FREE(hash_external, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002413 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002414}
2415
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002416psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002417 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002418 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002419{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002420 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002421 size_t actual_hash_length;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002422 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2423 LOCAL_INPUT_DECLARE(hash_external, hash);
2424
2425 status = psa_hash_finish_internal(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002426 operation,
2427 actual_hash, sizeof(actual_hash),
2428 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002430 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002431 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002432 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002434 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002435 status = PSA_ERROR_INVALID_SIGNATURE;
2436 goto exit;
2437 }
2438
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002439 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002440 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002441 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002442 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002443
2444exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002445 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2446 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002447 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002448 }
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002449 LOCAL_INPUT_FREE(hash_external, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002450 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002451}
2452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002453psa_status_t psa_hash_compute(psa_algorithm_t alg,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002454 const uint8_t *input_external, size_t input_length,
2455 uint8_t *hash_external, size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002456 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002457{
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002458 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2459 LOCAL_INPUT_DECLARE(input_external, input);
2460 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2461
Steven Cooremanfbe09282021-03-08 18:41:12 +01002462 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002463 if (!PSA_ALG_IS_HASH(alg)) {
2464 return PSA_ERROR_INVALID_ARGUMENT;
2465 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002466
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002467 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2468 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2469 status = psa_driver_wrapper_hash_compute(alg, input, input_length,
Thomas Daubneycbf09212024-01-25 17:14:29 +00002470 hash, hash_size, hash_length);
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002471
David Horstmann42015332024-03-13 15:42:31 +00002472#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002473exit:
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002474#endif
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002475 LOCAL_INPUT_FREE(input_external, input);
2476 LOCAL_OUTPUT_FREE(hash_external, hash);
2477 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002478}
2479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002480psa_status_t psa_hash_compare(psa_algorithm_t alg,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002481 const uint8_t *input_external, size_t input_length,
2482 const uint8_t *hash_external, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002483{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002484 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002485 size_t actual_hash_length;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002486 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2487
2488 LOCAL_INPUT_DECLARE(input_external, input);
2489 LOCAL_INPUT_DECLARE(hash_external, hash);
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002491 if (!PSA_ALG_IS_HASH(alg)) {
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002492 status = PSA_ERROR_INVALID_ARGUMENT;
2493 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002494 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002495
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002496 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2497 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002498 alg, input, input_length,
2499 actual_hash, sizeof(actual_hash),
2500 &actual_hash_length);
2501 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002502 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002503 }
2504 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002505 status = PSA_ERROR_INVALID_SIGNATURE;
2506 goto exit;
2507 }
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002508
2509 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002510 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002511 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002512 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002513
2514exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002515 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002516
2517 LOCAL_INPUT_FREE(input_external, input);
2518 LOCAL_INPUT_FREE(hash_external, hash);
2519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002520 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002521}
2522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002523psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2524 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002525{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002526 if (source_operation->id == 0 ||
2527 target_operation->id != 0) {
2528 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002529 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002531 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2532 target_operation);
2533 if (status != PSA_SUCCESS) {
2534 psa_hash_abort(target_operation);
2535 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002537 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002538}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002539
2540
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002541/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542/* MAC */
2543/****************************************************************/
2544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002545psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002546{
Steven Cooreman07897832021-03-19 18:28:56 +01002547 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002548 if (operation->id == 0) {
2549 return PSA_SUCCESS;
2550 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002552 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002553 operation->mac_size = 0;
2554 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002555 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002557 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002558}
2559
Ronald Cron1c650a12021-06-17 16:33:22 +02002560static psa_status_t psa_mac_finalize_alg_and_key_validation(
2561 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002562 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002563 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002564{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002565 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002566 psa_key_type_t key_type = psa_get_key_type(attributes);
2567 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002569 if (!PSA_ALG_IS_MAC(alg)) {
2570 return PSA_ERROR_INVALID_ARGUMENT;
2571 }
Steven Cooreman07897832021-03-19 18:28:56 +01002572
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002573 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002574 status = psa_mac_key_can_do(alg, key_type);
2575 if (status != PSA_SUCCESS) {
2576 return status;
2577 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002578
Steven Cooremana6474de2021-05-10 11:13:41 +02002579 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002580 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002582 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002583 /* A very short MAC is too short for security since it can be
2584 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2585 * so we make this our minimum, even though 32 bits is still
2586 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002587 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002588 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002590 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2591 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002592 /* It's impossible to "truncate" to a larger length than the full length
2593 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002594 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002595 }
2596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002597 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002598 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2599 * that is disabled in the compile-time configuration. The result can
2600 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2601 * configuration into account. In this case, force a return of
2602 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2603 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2604 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2605 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2606 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002607 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002608 }
2609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002610 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002611}
2612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002613static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2614 mbedtls_svc_key_id_t key,
2615 psa_algorithm_t alg,
2616 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002617{
2618 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2619 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002620 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002621 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002622
2623 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002624 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002625 status = PSA_ERROR_BAD_STATE;
2626 goto exit;
2627 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002628
2629 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002630 key,
2631 &slot,
2632 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2633 alg);
2634 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002635 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002636 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002637
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002638 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002639 .core = slot->attr
2640 };
2641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002642 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2643 &operation->mac_size);
2644 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002645 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002646 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002647
2648 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002649 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002650 if (is_sign) {
2651 status = psa_driver_wrapper_mac_sign_setup(operation,
2652 &attributes,
2653 slot->key.data,
2654 slot->key.bytes,
2655 alg);
2656 } else {
2657 status = psa_driver_wrapper_mac_verify_setup(operation,
2658 &attributes,
2659 slot->key.data,
2660 slot->key.bytes,
2661 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002662 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002663
Gilles Peskinefbfac682018-07-08 20:51:54 +02002664exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002665 if (status != PSA_SUCCESS) {
2666 psa_mac_abort(operation);
2667 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002669 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002670
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002671 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002672}
2673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002674psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2675 mbedtls_svc_key_id_t key,
2676 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002677{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002678 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002679}
2680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002681psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2682 mbedtls_svc_key_id_t key,
2683 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002684{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002685 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002686}
2687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002688psa_status_t psa_mac_update(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002689 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002690 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002691{
Thomas Daubney324f7de2024-01-18 13:18:58 +00002692 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2693 LOCAL_INPUT_DECLARE(input_external, input);
2694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002695 if (operation->id == 0) {
Thomas Daubney324f7de2024-01-18 13:18:58 +00002696 status = PSA_ERROR_BAD_STATE;
2697 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002698 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002699
Steven Cooreman11743f92021-03-19 18:38:46 +01002700 /* Don't require hash implementations to behave correctly on a
2701 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002702 if (input_length == 0) {
Thomas Daubney324f7de2024-01-18 13:18:58 +00002703 status = PSA_SUCCESS;
2704 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002705 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002706
Thomas Daubney324f7de2024-01-18 13:18:58 +00002707 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2708 status = psa_driver_wrapper_mac_update(operation, input, input_length);
2709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002710 if (status != PSA_SUCCESS) {
2711 psa_mac_abort(operation);
2712 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002713
David Horstmann42015332024-03-13 15:42:31 +00002714#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney324f7de2024-01-18 13:18:58 +00002715exit:
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002716#endif
Thomas Daubney324f7de2024-01-18 13:18:58 +00002717 LOCAL_INPUT_FREE(input_external, input);
2718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002719 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002720}
2721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002722psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002723 uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002724 size_t mac_size,
2725 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002726{
Steven Cooreman87885df2021-03-19 19:04:39 +01002727 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2728 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002729 LOCAL_OUTPUT_DECLARE(mac_external, mac);
Thomas Daubney301491d2024-02-01 14:32:59 +00002730 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002732 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002733 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002734 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002735 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002737 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002738 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002739 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002740 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002741
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002742 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2743 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002744 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002745 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002746 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002747 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002749 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002750 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002751 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002752 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002754 status = psa_driver_wrapper_mac_sign_finish(operation,
2755 mac, operation->mac_size,
2756 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002757
Dave Rodgman478ab542021-06-25 09:09:02 +01002758exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002759 /* In case of success, set the potential excess room in the output buffer
2760 * to an invalid value, to avoid potentially leaking a longer MAC.
2761 * In case of error, set the output length and content to a safe default,
2762 * such that in case the caller misses an error check, the output would be
2763 * an unachievable MAC.
2764 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002765 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002766 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002767 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002768 }
mohammad16036df908f2018-04-02 08:34:15 -07002769
Thomas Daubney480347d2024-02-01 19:00:26 +00002770 if ((mac != NULL) && (mac_size > operation->mac_size)) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002771 memset(&mac[operation->mac_size], '!',
2772 mac_size - operation->mac_size);
2773 }
Ronald Cron882eb782021-06-18 13:05:48 +02002774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002775 abort_status = psa_mac_abort(operation);
Thomas Daubney324f7de2024-01-18 13:18:58 +00002776 LOCAL_OUTPUT_FREE(mac_external, mac);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002778 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002779}
2780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002781psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002782 const uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002783 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002784{
Steven Cooreman87885df2021-03-19 19:04:39 +01002785 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2786 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002787 LOCAL_INPUT_DECLARE(mac_external, mac);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002788
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002789 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002790 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002791 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002792 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002794 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002795 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002796 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002797 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002799 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002800 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002801 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002802 }
2803
Thomas Daubney324f7de2024-01-18 13:18:58 +00002804 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002805 status = psa_driver_wrapper_mac_verify_finish(operation,
2806 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002807
Dave Rodgman478ab542021-06-25 09:09:02 +01002808exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002809 abort_status = psa_mac_abort(operation);
Thomas Daubney324f7de2024-01-18 13:18:58 +00002810 LOCAL_INPUT_FREE(mac_external, mac);
mohammad16036df908f2018-04-02 08:34:15 -07002811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002812 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002813}
2814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002815static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2816 psa_algorithm_t alg,
2817 const uint8_t *input,
2818 size_t input_length,
2819 uint8_t *mac,
2820 size_t mac_size,
2821 size_t *mac_length,
2822 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002823{
2824 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002825 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002826 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002827 psa_key_slot_t *slot;
2828 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002829
Ronald Crondbb86462021-06-17 17:17:20 +02002830 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002831 key,
2832 &slot,
2833 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2834 alg);
2835 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002836 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002837 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002838
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002839 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002840 .core = slot->attr
2841 };
2842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002843 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2844 &operation_mac_size);
2845 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002846 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002847 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002849 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002850 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002851 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002852 }
2853
2854 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002855 &attributes,
2856 slot->key.data, slot->key.bytes,
2857 alg,
2858 input, input_length,
2859 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002860
2861exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002862 /* In case of success, set the potential excess room in the output buffer
2863 * to an invalid value, to avoid potentially leaking a longer MAC.
2864 * In case of error, set the output length and content to a safe default,
2865 * such that in case the caller misses an error check, the output would be
2866 * an unachievable MAC.
2867 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002868 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002869 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002870 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002871 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002872 if (mac_size > operation_mac_size) {
2873 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2874 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002876 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002878 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002879}
2880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002881psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002882 psa_algorithm_t alg,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002883 const uint8_t *input_external,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002884 size_t input_length,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002885 uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002886 size_t mac_size,
2887 size_t *mac_length)
2888{
Thomas Daubney324f7de2024-01-18 13:18:58 +00002889 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2890 LOCAL_INPUT_DECLARE(input_external, input);
2891 LOCAL_OUTPUT_DECLARE(mac_external, mac);
2892
2893 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2894 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
2895 status = psa_mac_compute_internal(key, alg,
Thomas Daubneyf298f652024-01-30 12:15:36 +00002896 input, input_length,
2897 mac, mac_size, mac_length, 1);
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002898
David Horstmann42015332024-03-13 15:42:31 +00002899#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney324f7de2024-01-18 13:18:58 +00002900exit:
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002901#endif
Thomas Daubney324f7de2024-01-18 13:18:58 +00002902 LOCAL_INPUT_FREE(input_external, input);
2903 LOCAL_OUTPUT_FREE(mac_external, mac);
2904
2905 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002906}
2907
2908psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2909 psa_algorithm_t alg,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002910 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002911 size_t input_length,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002912 const uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002913 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002914{
2915 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002916 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2917 size_t actual_mac_length;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002918 LOCAL_INPUT_DECLARE(input_external, input);
2919 LOCAL_INPUT_DECLARE(mac_external, mac);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002920
Thomas Daubney324f7de2024-01-18 13:18:58 +00002921 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 status = psa_mac_compute_internal(key, alg,
2923 input, input_length,
2924 actual_mac, sizeof(actual_mac),
2925 &actual_mac_length, 0);
2926 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002927 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002928 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002930 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002931 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002932 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002933 }
Thomas Daubney324f7de2024-01-18 13:18:58 +00002934
2935 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002936 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002937 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002938 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002939 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002940
2941exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002942 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Thomas Daubney324f7de2024-01-18 13:18:58 +00002943 LOCAL_INPUT_FREE(input_external, input);
2944 LOCAL_INPUT_FREE(mac_external, mac);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002946 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002947}
Gilles Peskine20035e32018-02-03 22:44:14 +01002948
Gilles Peskine20035e32018-02-03 22:44:14 +01002949/****************************************************************/
2950/* Asymmetric cryptography */
2951/****************************************************************/
2952
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002953static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2954 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002955{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002956 if (input_is_message) {
2957 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2958 return PSA_ERROR_INVALID_ARGUMENT;
2959 }
Gilles Peskineb6e362b2024-04-10 20:05:06 +02002960 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002961
Gilles Peskineb6e362b2024-04-10 20:05:06 +02002962 psa_algorithm_t hash_alg = 0;
2963 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2964 hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
2965 }
2966
2967 /* Now hash_alg==0 if alg by itself doesn't need a hash.
2968 * This is good enough for sign-hash, but a guaranteed failure for
2969 * sign-message which needs to hash first for all algorithms
2970 * supported at the moment. */
2971
2972 if (hash_alg == 0 && input_is_message) {
2973 return PSA_ERROR_INVALID_ARGUMENT;
2974 }
2975 if (hash_alg == PSA_ALG_ANY_HASH) {
2976 return PSA_ERROR_INVALID_ARGUMENT;
2977 }
2978 /* Give up immediately if the hash is not supported. This has
2979 * several advantages:
2980 * - For mechanisms that don't use the hash at all (e.g.
2981 * ECDSA verification, randomized ECDSA signature), without
2982 * this check, the operation would succeed even though it has
2983 * been given an invalid argument. This would not be insecure
2984 * since the hash was not necessary, but it would be weird.
2985 * - For mechanisms that do use the hash, we avoid an error
2986 * deep inside the execution. In principle this doesn't matter,
2987 * but there is a little more risk of a bug in error handling
2988 * deep inside than in this preliminary check.
2989 * - When calling a driver, the driver might be capable of using
2990 * a hash that the core doesn't support. This could potentially
2991 * result in a buffer overflow if the hash is larger than the
2992 * maximum hash size assumed by the core.
2993 * - Returning a consistent error makes it possible to test
2994 * not-supported hashes in a consistent way.
2995 */
2996 if (hash_alg != 0 && !is_hash_supported(hash_alg)) {
2997 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002998 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003000 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02003001}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003003static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
3004 int input_is_message,
3005 psa_algorithm_t alg,
3006 const uint8_t *input,
3007 size_t input_length,
3008 uint8_t *signature,
3009 size_t signature_size,
3010 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003011{
3012 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3013 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003014 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003015 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003016
3017 *signature_length = 0;
3018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003019 status = psa_sign_verify_check_alg(input_is_message, alg);
3020 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02003021 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003022 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003023
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003024 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02003025 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003026 * buffer can in principle be empty since it doesn't actually have
3027 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003028 if (signature_size == 0) {
3029 return PSA_ERROR_BUFFER_TOO_SMALL;
3030 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003031
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003032 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003033 key, &slot,
3034 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
3035 PSA_KEY_USAGE_SIGN_HASH,
3036 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003037
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003038 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003039 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003040 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003041
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003042 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003043 status = PSA_ERROR_INVALID_ARGUMENT;
3044 goto exit;
3045 }
3046
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003047 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003048 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003049 };
3050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003051 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003052 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003053 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003054 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003055 signature, signature_size, signature_length);
3056 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003057
3058 status = psa_driver_wrapper_sign_hash(
3059 &attributes, slot->key.data, slot->key.bytes,
3060 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003061 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003062 }
3063
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003064
3065exit:
3066 /* Fill the unused part of the output buffer (the whole buffer on error,
3067 * the trailing part on success) with something that isn't a valid signature
3068 * (barring an attack on the signature and deliberately-crafted input),
3069 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003070 if (status == PSA_SUCCESS) {
3071 memset(signature + *signature_length, '!',
3072 signature_size - *signature_length);
3073 } else {
3074 memset(signature, '!', signature_size);
3075 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003076 /* If signature_size is 0 then we have nothing to do. We must not call
3077 * memset because signature may be NULL in this case. */
3078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003079 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003081 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003082}
3083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003084static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3085 int input_is_message,
3086 psa_algorithm_t alg,
3087 const uint8_t *input,
3088 size_t input_length,
3089 const uint8_t *signature,
3090 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003091{
3092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3093 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3094 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003096 status = psa_sign_verify_check_alg(input_is_message, alg);
3097 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02003098 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003099 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003100
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003101 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003102 key, &slot,
3103 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3104 PSA_KEY_USAGE_VERIFY_HASH,
3105 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003107 if (status != PSA_SUCCESS) {
3108 return status;
3109 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003110
3111 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003112 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003113 };
3114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003115 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003116 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003117 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003118 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003119 signature, signature_length);
3120 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003121 status = psa_driver_wrapper_verify_hash(
3122 &attributes, slot->key.data, slot->key.bytes,
3123 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003124 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003125 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003127 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003129 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003130
3131}
3132
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003133psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003134 const psa_key_attributes_t *attributes,
3135 const uint8_t *key_buffer,
3136 size_t key_buffer_size,
3137 psa_algorithm_t alg,
3138 const uint8_t *input,
3139 size_t input_length,
3140 uint8_t *signature,
3141 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003142 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003143{
3144 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003146 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003147 size_t hash_length;
3148 uint8_t hash[PSA_HASH_MAX_SIZE];
3149
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003150 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003151 PSA_ALG_SIGN_GET_HASH(alg),
3152 input, input_length,
3153 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003155 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003156 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003157 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003158
3159 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003160 attributes, key_buffer, key_buffer_size,
3161 alg, hash, hash_length,
3162 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003163 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003165 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003166}
3167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003168psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3169 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003170 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003171 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003172 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003173 size_t signature_size,
3174 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003175{
Thomas Daubney290aac42024-01-18 17:23:02 +00003176 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3177 LOCAL_INPUT_DECLARE(input_external, input);
3178 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3179
3180 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3181 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3182 status = psa_sign_internal(key, 1, alg, input, input_length, signature,
3183 signature_size, signature_length);
3184
David Horstmann42015332024-03-13 15:42:31 +00003185#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003186exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003187#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003188 LOCAL_INPUT_FREE(input_external, input);
3189 LOCAL_OUTPUT_FREE(signature_external, signature);
3190 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003191}
3192
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003193psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003194 const psa_key_attributes_t *attributes,
3195 const uint8_t *key_buffer,
3196 size_t key_buffer_size,
3197 psa_algorithm_t alg,
3198 const uint8_t *input,
3199 size_t input_length,
3200 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003201 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003202{
3203 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003205 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003206 size_t hash_length;
3207 uint8_t hash[PSA_HASH_MAX_SIZE];
3208
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003209 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003210 PSA_ALG_SIGN_GET_HASH(alg),
3211 input, input_length,
3212 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003214 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003215 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003216 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003217
3218 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003219 attributes, key_buffer, key_buffer_size,
3220 alg, hash, hash_length,
3221 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003222 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003223
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003224 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003225}
3226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003227psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3228 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003229 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003230 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003231 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003232 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003233{
Thomas Daubney290aac42024-01-18 17:23:02 +00003234 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3235 LOCAL_INPUT_DECLARE(input_external, input);
3236 LOCAL_INPUT_DECLARE(signature_external, signature);
3237
3238 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3239 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3240 status = psa_verify_internal(key, 1, alg, input, input_length, signature,
3241 signature_length);
3242
David Horstmann42015332024-03-13 15:42:31 +00003243#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003244exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003245#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003246 LOCAL_INPUT_FREE(input_external, input);
3247 LOCAL_INPUT_FREE(signature_external, signature);
3248
3249 return status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003250}
3251
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003252psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003253 const psa_key_attributes_t *attributes,
3254 const uint8_t *key_buffer, size_t key_buffer_size,
3255 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003256 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01003257{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003258 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3259 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3260 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003261#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003262 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3263 return mbedtls_psa_rsa_sign_hash(
3264 attributes,
3265 key_buffer, key_buffer_size,
3266 alg, hash, hash_length,
3267 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003268#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3269 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003270 } else {
3271 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003272 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003273 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3274 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003275#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003276 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3277 return mbedtls_psa_ecdsa_sign_hash(
3278 attributes,
3279 key_buffer, key_buffer_size,
3280 alg, hash, hash_length,
3281 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003282#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3283 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003284 } else {
3285 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003286 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003287 }
Ronald Cron4501c982021-03-19 20:09:32 +01003288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003289 (void) key_buffer;
3290 (void) key_buffer_size;
3291 (void) hash;
3292 (void) hash_length;
3293 (void) signature;
3294 (void) signature_size;
3295 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003297 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003298}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003300psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3301 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003302 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003303 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003304 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003305 size_t signature_size,
3306 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003307{
Thomas Daubney290aac42024-01-18 17:23:02 +00003308 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3309 LOCAL_INPUT_DECLARE(hash_external, hash);
3310 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3311
3312 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3313 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3314 status = psa_sign_internal(key, 0, alg, hash, hash_length, signature,
3315 signature_size, signature_length);
3316
David Horstmann42015332024-03-13 15:42:31 +00003317#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003318exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003319#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003320 LOCAL_INPUT_FREE(hash_external, hash);
3321 LOCAL_OUTPUT_FREE(signature_external, signature);
3322
3323 return status;
itayzafrir5c753392018-05-08 11:18:38 +03003324}
3325
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003326psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003327 const psa_key_attributes_t *attributes,
3328 const uint8_t *key_buffer, size_t key_buffer_size,
3329 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003330 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003331{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003332 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3333 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3334 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003335#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003336 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3337 return mbedtls_psa_rsa_verify_hash(
3338 attributes,
3339 key_buffer, key_buffer_size,
3340 alg, hash, hash_length,
3341 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003342#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3343 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003344 } else {
3345 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003346 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003347 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3348 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003349#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003350 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3351 return mbedtls_psa_ecdsa_verify_hash(
3352 attributes,
3353 key_buffer, key_buffer_size,
3354 alg, hash, hash_length,
3355 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003356#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3357 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003358 } else {
3359 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003360 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003361 }
Ronald Cron4501c982021-03-19 20:09:32 +01003362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003363 (void) key_buffer;
3364 (void) key_buffer_size;
3365 (void) hash;
3366 (void) hash_length;
3367 (void) signature;
3368 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003370 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003371}
3372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003373psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3374 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003375 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003376 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003377 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003378 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003379{
Thomas Daubney290aac42024-01-18 17:23:02 +00003380 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3381 LOCAL_INPUT_DECLARE(hash_external, hash);
3382 LOCAL_INPUT_DECLARE(signature_external, signature);
3383
3384 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3385 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3386 status = psa_verify_internal(key, 0, alg, hash, hash_length, signature,
3387 signature_length);
3388
David Horstmann42015332024-03-13 15:42:31 +00003389#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003390exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003391#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003392 LOCAL_INPUT_FREE(hash_external, hash);
3393 LOCAL_INPUT_FREE(signature_external, signature);
3394
3395 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003396}
3397
John Durkop0e005192020-10-31 22:06:54 -07003398#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003399static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3400 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003401{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003402 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3403 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3404 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3405 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003406}
John Durkop0e005192020-10-31 22:06:54 -07003407#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003409psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3410 psa_algorithm_t alg,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003411 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003412 size_t input_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003413 const uint8_t *salt_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003414 size_t salt_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003415 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003416 size_t output_size,
3417 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003418{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003419 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003420 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003421 psa_key_slot_t *slot;
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003422 LOCAL_INPUT_DECLARE(input_external, input);
3423 LOCAL_INPUT_DECLARE(salt_external, salt);
3424 LOCAL_OUTPUT_DECLARE(output_external, output);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003425
Darryl Green5cc689a2018-07-24 15:34:10 +01003426 (void) input;
3427 (void) input_length;
3428 (void) salt;
3429 (void) output;
3430 (void) output_size;
3431
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003432 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003434 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3435 return PSA_ERROR_INVALID_ARGUMENT;
3436 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003437
Ronald Cron5c522922020-11-14 16:35:34 +01003438 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003439 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3440 if (status != PSA_SUCCESS) {
3441 return status;
3442 }
3443 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3444 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003445 status = PSA_ERROR_INVALID_ARGUMENT;
3446 goto exit;
3447 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003449 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003450#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003451 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003452 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003453 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3454 slot->key.data,
3455 slot->key.bytes,
3456 &rsa);
3457 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003458 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003459 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003461 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003462 status = PSA_ERROR_BUFFER_TOO_SMALL;
3463 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003464 }
Ronald Cron7207d572021-09-08 14:28:35 +02003465#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3466 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003467 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3468 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3469 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003470 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003471#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003472 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003473 mbedtls_rsa_pkcs1_encrypt(rsa,
3474 mbedtls_psa_get_random,
3475 MBEDTLS_PSA_RANDOM_STATE,
3476 MBEDTLS_RSA_PUBLIC,
3477 input_length,
3478 input,
3479 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003480#else
3481 status = PSA_ERROR_NOT_SUPPORTED;
3482#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003483 } else
3484 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003485#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003486 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003487 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003488 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3489 mbedtls_psa_get_random,
3490 MBEDTLS_PSA_RANDOM_STATE,
3491 MBEDTLS_RSA_PUBLIC,
3492 salt, salt_length,
3493 input_length,
3494 input,
3495 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003496#else
3497 status = PSA_ERROR_NOT_SUPPORTED;
3498#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003499 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003500 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003501 }
Ronald Cron7207d572021-09-08 14:28:35 +02003502#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003503 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003504rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003505 if (status == PSA_SUCCESS) {
3506 *output_length = mbedtls_rsa_get_len(rsa);
3507 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003509 mbedtls_rsa_free(rsa);
3510 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003511#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003512 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003513 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003514 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003515 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003516
3517exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003518 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003519
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003520 LOCAL_INPUT_FREE(input_external, input);
3521 LOCAL_INPUT_FREE(salt_external, salt);
3522 LOCAL_OUTPUT_FREE(output_external, output);
3523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003524 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003525}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003527psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3528 psa_algorithm_t alg,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003529 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003530 size_t input_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003531 const uint8_t *salt_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003532 size_t salt_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003533 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003534 size_t output_size,
3535 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003536{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003537 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003538 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003539 psa_key_slot_t *slot;
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003540 LOCAL_INPUT_DECLARE(input_external, input);
3541 LOCAL_INPUT_DECLARE(salt_external, salt);
3542 LOCAL_OUTPUT_DECLARE(output_external, output);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003543
Darryl Green5cc689a2018-07-24 15:34:10 +01003544 (void) input;
3545 (void) input_length;
3546 (void) salt;
3547 (void) output;
3548 (void) output_size;
3549
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003550 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003552 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3553 return PSA_ERROR_INVALID_ARGUMENT;
3554 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003555
Ronald Cron5c522922020-11-14 16:35:34 +01003556 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003557 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3558 if (status != PSA_SUCCESS) {
3559 return status;
3560 }
3561 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003562 status = PSA_ERROR_INVALID_ARGUMENT;
3563 goto exit;
3564 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003566 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003567#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003568 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003569 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003570 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3571 slot->key.data,
3572 slot->key.bytes,
3573 &rsa);
3574 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003575 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003576 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003577
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003578 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003579 status = PSA_ERROR_INVALID_ARGUMENT;
3580 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003581 }
Ronald Cron7207d572021-09-08 14:28:35 +02003582#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3583 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003584 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3585 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3586 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003587 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003588#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003589 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003590 mbedtls_rsa_pkcs1_decrypt(rsa,
3591 mbedtls_psa_get_random,
3592 MBEDTLS_PSA_RANDOM_STATE,
3593 MBEDTLS_RSA_PRIVATE,
3594 output_length,
3595 input,
3596 output,
3597 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003598#else
3599 status = PSA_ERROR_NOT_SUPPORTED;
3600#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003601 } else
3602 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003603#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003604 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003605 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003606 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3607 mbedtls_psa_get_random,
3608 MBEDTLS_PSA_RANDOM_STATE,
3609 MBEDTLS_RSA_PRIVATE,
3610 salt, salt_length,
3611 output_length,
3612 input,
3613 output,
3614 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003615#else
3616 status = PSA_ERROR_NOT_SUPPORTED;
3617#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003618 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003619 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003620 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003621
Ronald Cron7207d572021-09-08 14:28:35 +02003622#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003623 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003624rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003625 mbedtls_rsa_free(rsa);
3626 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003627#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3628 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003629 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003630 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003631 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003632
3633exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003634 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003635
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003636 LOCAL_INPUT_FREE(input_external, input);
3637 LOCAL_INPUT_FREE(salt_external, salt);
3638 LOCAL_OUTPUT_FREE(output_external, output);
3639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003640 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003641}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003642
David Horstmann65bf12c2024-02-06 15:27:49 +00003643static psa_status_t psa_generate_random_internal(uint8_t *output,
3644 size_t output_size)
3645{
3646 GUARD_MODULE_INITIALIZED;
3647
3648 psa_status_t status;
3649
3650#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
3651
3652 size_t output_length = 0;
3653 status = mbedtls_psa_external_get_random(&global_data.rng,
3654 output, output_size,
3655 &output_length);
3656 if (status != PSA_SUCCESS) {
3657 goto exit;
3658 }
3659 /* Breaking up a request into smaller chunks is currently not supported
3660 * for the external RNG interface. */
3661 if (output_length != output_size) {
3662 status = PSA_ERROR_INSUFFICIENT_ENTROPY;
3663 goto exit;
3664 }
3665 status = PSA_SUCCESS;
3666
3667#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
3668
3669 while (output_size > 0) {
3670 size_t request_size =
3671 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
3672 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
3673 output_size);
3674 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
3675 output, request_size);
3676 if (ret != 0) {
3677 status = mbedtls_to_psa_error(ret);
3678 goto exit;
3679 }
3680 output_size -= request_size;
3681 output += request_size;
3682 }
3683 status = PSA_SUCCESS;
3684#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
3685
3686exit:
3687 return status;
3688}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003689
3690
mohammad1603503973b2018-03-12 15:59:30 +02003691/****************************************************************/
3692/* Symmetric cryptography */
3693/****************************************************************/
3694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003695static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3696 mbedtls_svc_key_id_t key,
3697 psa_algorithm_t alg,
3698 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003699{
3700 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3701 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003702 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003703 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003704 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3705 PSA_KEY_USAGE_ENCRYPT :
3706 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003707
3708 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003709 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003710 status = PSA_ERROR_BAD_STATE;
3711 goto exit;
3712 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003715 status = PSA_ERROR_INVALID_ARGUMENT;
3716 goto exit;
3717 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003719 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3720 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003721 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003722 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003723
Ronald Cron49fafa92021-03-10 08:34:23 +01003724 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003725 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003726 * so we only set it (in the driver wrapper) after resources have been
3727 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003728 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003729 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003730 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003731 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003732 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003733 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003734 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003735 }
3736 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003737
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003738 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003739 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003740 };
3741
Ronald Cronab99ac22020-10-01 16:38:28 +02003742 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003743 if (cipher_operation == MBEDTLS_ENCRYPT) {
3744 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3745 &attributes,
3746 slot->key.data,
3747 slot->key.bytes,
3748 alg);
3749 } else {
3750 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3751 &attributes,
3752 slot->key.data,
3753 slot->key.bytes,
3754 alg);
3755 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003756
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003757exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003758 if (status != PSA_SUCCESS) {
3759 psa_cipher_abort(operation);
3760 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003761
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003762 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003764 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003765}
3766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3768 mbedtls_svc_key_id_t key,
3769 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003770{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003771 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003772}
3773
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003774psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3775 mbedtls_svc_key_id_t key,
3776 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003777{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003778 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003779}
3780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003781psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Gabor Mezei8677edd2024-02-07 18:10:13 +01003782 uint8_t *iv_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003783 size_t iv_size,
3784 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003785{
Ronald Cron6d051732020-10-01 14:10:20 +02003786 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Sergey04eb7c02023-03-06 15:37:23 -07003787 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003788
Gabor Mezei8677edd2024-02-07 18:10:13 +01003789 LOCAL_OUTPUT_DECLARE(iv_external, iv);
3790
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003791 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003792 status = PSA_ERROR_BAD_STATE;
3793 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003794 }
3795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003796 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003797 status = PSA_ERROR_BAD_STATE;
3798 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003799 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003800
Ronald Cronc423acb2021-07-05 12:31:44 +02003801 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003802 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003803 status = PSA_ERROR_BUFFER_TOO_SMALL;
3804 goto exit;
3805 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003807 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003808 status = PSA_ERROR_GENERIC_ERROR;
3809 goto exit;
3810 }
3811
Gabor Mezei8677edd2024-02-07 18:10:13 +01003812 LOCAL_OUTPUT_ALLOC(iv_external, default_iv_length, iv);
3813
Gabor Mezeif3c35042024-03-04 17:15:08 +01003814 status = psa_generate_random_internal(iv, default_iv_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003815 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003816 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003817 }
Ronald Cron5618a392021-03-26 09:52:26 +01003818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003819 status = psa_driver_wrapper_cipher_set_iv(operation,
Gabor Mezei8677edd2024-02-07 18:10:13 +01003820 iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003821
3822exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003823 if (status == PSA_SUCCESS) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003824 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003825 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003826 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003827 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003828 psa_cipher_abort(operation);
Gabor Mezei8677edd2024-02-07 18:10:13 +01003829 if (iv != NULL) {
3830 mbedtls_platform_zeroize(iv, default_iv_length);
3831 }
Ronald Cronc423acb2021-07-05 12:31:44 +02003832 }
Ronald Cron6d051732020-10-01 14:10:20 +02003833
Gabor Mezei8677edd2024-02-07 18:10:13 +01003834 LOCAL_OUTPUT_FREE(iv_external, iv);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003835 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003836}
3837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003838psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Gabor Mezei282bb532024-02-01 10:39:26 +01003839 const uint8_t *iv_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003840 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003841{
Ronald Cron6d051732020-10-01 14:10:20 +02003842 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003843
Gabor Mezei282bb532024-02-01 10:39:26 +01003844 LOCAL_INPUT_DECLARE(iv_external, iv);
3845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003846 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003847 status = PSA_ERROR_BAD_STATE;
3848 goto exit;
3849 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003851 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003852 status = PSA_ERROR_BAD_STATE;
3853 goto exit;
3854 }
Ronald Crona0d68172021-03-26 10:15:08 +01003855
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003856 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003857 status = PSA_ERROR_INVALID_ARGUMENT;
3858 goto exit;
3859 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003860
Gabor Mezei282bb532024-02-01 10:39:26 +01003861 LOCAL_INPUT_ALLOC(iv_external, iv_length, iv);
3862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003863 status = psa_driver_wrapper_cipher_set_iv(operation,
3864 iv,
3865 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003866
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003867exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003868 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003869 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003870 } else {
3871 psa_cipher_abort(operation);
3872 }
Gabor Mezei282bb532024-02-01 10:39:26 +01003873
3874 LOCAL_INPUT_FREE(iv_external, iv);
3875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003876 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003877}
3878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003879psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003880 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003881 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003882 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003883 size_t output_size,
3884 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003885{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003886 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003887
Gabor Mezei13a15c22024-01-24 16:56:00 +01003888 LOCAL_INPUT_DECLARE(input_external, input);
3889 LOCAL_OUTPUT_DECLARE(output_external, output);
3890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003891 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003892 status = PSA_ERROR_BAD_STATE;
3893 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003894 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003896 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003897 status = PSA_ERROR_BAD_STATE;
3898 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003899 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003900
Gabor Mezeied96d682024-01-31 17:45:29 +01003901 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gabor Mezeiff783e02024-02-29 10:08:16 +00003902 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezeied96d682024-01-31 17:45:29 +01003903
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003904 status = psa_driver_wrapper_cipher_update(operation,
3905 input,
3906 input_length,
3907 output,
3908 output_size,
3909 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003910
3911exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003912 if (status != PSA_SUCCESS) {
3913 psa_cipher_abort(operation);
3914 }
Ronald Cron6d051732020-10-01 14:10:20 +02003915
Gabor Mezei13a15c22024-01-24 16:56:00 +01003916 LOCAL_INPUT_FREE(input_external, input);
3917 LOCAL_OUTPUT_FREE(output_external, output);
3918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003919 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003920}
3921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003922psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003923 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003924 size_t output_size,
3925 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003926{
David Saadab4ecc272019-02-14 13:48:10 +02003927 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003928
Gabor Mezei13a15c22024-01-24 16:56:00 +01003929 LOCAL_OUTPUT_DECLARE(output_external, output);
3930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003931 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003932 status = PSA_ERROR_BAD_STATE;
3933 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003934 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003936 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003937 status = PSA_ERROR_BAD_STATE;
3938 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003939 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003940
Gabor Mezeiff783e02024-02-29 10:08:16 +00003941 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezeied96d682024-01-31 17:45:29 +01003942
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003943 status = psa_driver_wrapper_cipher_finish(operation,
3944 output,
3945 output_size,
3946 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003947
3948exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003949 if (status == PSA_SUCCESS) {
Gabor Mezei13a15c22024-01-24 16:56:00 +01003950 status = psa_cipher_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003951 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003952 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003953 (void) psa_cipher_abort(operation);
Steven Cooremanef8575e2020-09-11 11:44:50 +02003954 }
Gabor Mezei13a15c22024-01-24 16:56:00 +01003955
3956 LOCAL_OUTPUT_FREE(output_external, output);
3957
3958 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003959}
3960
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003961psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003962{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003963 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003964 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003965 * in use. It's ok to call abort on such an object, and there's
3966 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003967 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003968 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003969
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003970 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003971
Ronald Cron49fafa92021-03-10 08:34:23 +01003972 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003973 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003974 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003975
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003976 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003977}
3978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003979psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3980 psa_algorithm_t alg,
David Horstmann926193a2023-12-13 15:55:25 +00003981 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003982 size_t input_length,
David Horstmann926193a2023-12-13 15:55:25 +00003983 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003984 size_t output_size,
3985 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003986{
3987 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003988 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003989 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003990 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003991 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3992 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003993
David Horstmannb80e35a2023-12-14 18:12:47 +00003994 LOCAL_INPUT_DECLARE(input_external, input);
3995 LOCAL_OUTPUT_DECLARE(output_external, output);
3996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003997 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003998 status = PSA_ERROR_INVALID_ARGUMENT;
3999 goto exit;
4000 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004002 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4003 PSA_KEY_USAGE_ENCRYPT,
4004 alg);
4005 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004006 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004007 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004008
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004009 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004010 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004011 };
4012
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004013 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4014 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02004015 status = PSA_ERROR_GENERIC_ERROR;
4016 goto exit;
4017 }
4018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004019 if (default_iv_length > 0) {
4020 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004021 status = PSA_ERROR_BUFFER_TOO_SMALL;
4022 goto exit;
4023 }
4024
David Horstmann65bf12c2024-02-06 15:27:49 +00004025 status = psa_generate_random_internal(local_iv, default_iv_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004026 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004027 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004028 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004029 }
4030
Gabor Mezeied96d682024-01-31 17:45:29 +01004031 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4032 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4033
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004034 status = psa_driver_wrapper_cipher_encrypt(
4035 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02004036 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004037 mbedtls_buffer_offset(output, default_iv_length),
4038 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004039
4040exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004041 unlock_status = psa_unlock_key_slot(slot);
4042 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004043 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02004044 }
Ronald Cron16377072021-07-08 19:00:07 +02004045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004046 if (status == PSA_SUCCESS) {
4047 if (default_iv_length > 0) {
4048 memcpy(output, local_iv, default_iv_length);
4049 }
4050 *output_length += default_iv_length;
4051 } else {
4052 *output_length = 0;
4053 }
4054
David Horstmannb80e35a2023-12-14 18:12:47 +00004055 LOCAL_INPUT_FREE(input_external, input);
4056 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00004057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004058 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004059}
4060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004061psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4062 psa_algorithm_t alg,
Gabor Mezei13a15c22024-01-24 16:56:00 +01004063 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004064 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01004065 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004066 size_t output_size,
4067 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004068{
4069 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004070 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004071 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02004072 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02004073
Gabor Mezei13a15c22024-01-24 16:56:00 +01004074 LOCAL_INPUT_DECLARE(input_external, input);
4075 LOCAL_OUTPUT_DECLARE(output_external, output);
4076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004077 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02004078 status = PSA_ERROR_INVALID_ARGUMENT;
4079 goto exit;
4080 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004082 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4083 PSA_KEY_USAGE_DECRYPT,
4084 alg);
4085 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004086 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004087 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004088
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004089 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004090 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004091 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004093 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02004094 status = PSA_ERROR_INVALID_ARGUMENT;
4095 goto exit;
4096 }
4097
Gabor Mezeied96d682024-01-31 17:45:29 +01004098 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4099 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4100
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004101 status = psa_driver_wrapper_cipher_decrypt(
4102 &attributes, slot->key.data, slot->key.bytes,
4103 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004104 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004105
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02004106exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004107 unlock_status = psa_unlock_key_slot(slot);
4108 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004109 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004110 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004112 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004113 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004114 }
Ronald Cron16377072021-07-08 19:00:07 +02004115
Gabor Mezei13a15c22024-01-24 16:56:00 +01004116 LOCAL_INPUT_FREE(input_external, input);
4117 LOCAL_OUTPUT_FREE(output_external, output);
4118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004119 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004120}
4121
4122
mohammad16035955c982018-04-26 00:53:03 +03004123/****************************************************************/
4124/* AEAD */
4125/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004127psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4128 psa_algorithm_t alg,
David Horstmann21c1a942023-11-28 19:25:00 +00004129 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004130 size_t nonce_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004131 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004132 size_t additional_data_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004133 const uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004134 size_t plaintext_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004135 uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004136 size_t ciphertext_size,
4137 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004138{
Ronald Cron215633c2021-03-16 17:15:37 +01004139 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4140 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004141
David Horstmann21c1a942023-11-28 19:25:00 +00004142 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4143 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4144 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4145 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4146
mohammad1603f08a5502018-06-03 15:05:47 +03004147 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004149 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4150 return PSA_ERROR_NOT_SUPPORTED;
4151 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004152
Ronald Cron9a986162021-03-26 12:40:07 +01004153 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004154 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4155 if (status != PSA_SUCCESS) {
4156 return status;
4157 }
mohammad16035955c982018-04-26 00:53:03 +03004158
Ronald Cron215633c2021-03-16 17:15:37 +01004159 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004160 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004161 };
mohammad16035955c982018-04-26 00:53:03 +03004162
David Horstmann21c1a942023-11-28 19:25:00 +00004163 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4164 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4165 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4166 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4167
Ronald Cronde822812021-03-17 16:08:20 +01004168 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004169 &attributes, slot->key.data, slot->key.bytes,
4170 alg,
4171 nonce, nonce_length,
4172 additional_data, additional_data_length,
4173 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004174 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004176 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4177 memset(ciphertext, 0, ciphertext_size);
4178 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004179
David Horstmann21c1a942023-11-28 19:25:00 +00004180/* Exit label is only used for buffer copying, prevent unused warnings. */
David Horstmann42015332024-03-13 15:42:31 +00004181#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann21c1a942023-11-28 19:25:00 +00004182exit:
4183#endif
4184 LOCAL_INPUT_FREE(nonce_external, nonce);
4185 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4186 LOCAL_INPUT_FREE(plaintext_external, plaintext);
4187 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
4188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004189 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004191 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004192}
4193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004194psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4195 psa_algorithm_t alg,
David Horstmann6baf6e92023-11-28 19:43:53 +00004196 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004197 size_t nonce_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004198 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004199 size_t additional_data_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004200 const uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004201 size_t ciphertext_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004202 uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004203 size_t plaintext_size,
4204 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004205{
Ronald Cron215633c2021-03-16 17:15:37 +01004206 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4207 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004208
David Horstmann6baf6e92023-11-28 19:43:53 +00004209 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4210 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4211 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
4212 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
4213
Gilles Peskineee652a32018-06-01 19:23:52 +02004214 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004216 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4217 return PSA_ERROR_NOT_SUPPORTED;
4218 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004219
Ronald Cron9a986162021-03-26 12:40:07 +01004220 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004221 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4222 if (status != PSA_SUCCESS) {
4223 return status;
4224 }
mohammad16035955c982018-04-26 00:53:03 +03004225
Ronald Cron215633c2021-03-16 17:15:37 +01004226 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004227 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004228 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004229
David Horstmann6baf6e92023-11-28 19:43:53 +00004230 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4231 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
4232 additional_data);
4233 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
4234 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
4235
Ronald Cronde822812021-03-17 16:08:20 +01004236 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004237 &attributes, slot->key.data, slot->key.bytes,
4238 alg,
4239 nonce, nonce_length,
4240 additional_data, additional_data_length,
4241 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004242 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004244 if (status != PSA_SUCCESS && plaintext_size != 0) {
4245 memset(plaintext, 0, plaintext_size);
4246 }
mohammad160360a64d02018-06-03 17:20:42 +03004247
David Horstmann6baf6e92023-11-28 19:43:53 +00004248/* Exit label is only used for buffer copying, prevent unused warnings. */
David Horstmann42015332024-03-13 15:42:31 +00004249#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann6baf6e92023-11-28 19:43:53 +00004250exit:
4251#endif
4252 LOCAL_INPUT_FREE(nonce_external, nonce);
4253 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4254 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
4255 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
4256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004257 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004259 return status;
mohammad16035955c982018-04-26 00:53:03 +03004260}
4261
Gilles Peskinee59236f2018-01-27 23:32:46 +01004262/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004263/* Generators */
4264/****************************************************************/
4265
John Durkop07cc04a2020-11-16 22:08:34 -08004266#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4267 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4268 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4269#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02004270#endif /* At least one builtin KDF */
4271
4272#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4273 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4274 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4275static psa_status_t psa_key_derivation_start_hmac(
4276 psa_mac_operation_t *operation,
4277 psa_algorithm_t hash_alg,
4278 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004279 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02004280{
4281 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004283 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4284 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4285 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02004286
Steven Cooreman15f0d922021-05-07 14:14:37 +02004287 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004288 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02004289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004290 status = psa_driver_wrapper_mac_sign_setup(operation,
4291 &attributes,
4292 hmac_key, hmac_key_length,
4293 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02004294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004295 psa_reset_key_attributes(&attributes);
4296 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02004297}
4298#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004299
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004300#define HKDF_STATE_INIT 0 /* no input yet */
4301#define HKDF_STATE_STARTED 1 /* got salt */
4302#define HKDF_STATE_KEYED 2 /* got key */
4303#define HKDF_STATE_OUTPUT 3 /* output started */
4304
Gilles Peskinecbe66502019-05-16 16:59:18 +02004305static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004306 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004307{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004308 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4309 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4310 } else {
4311 return operation->alg;
4312 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004313}
4314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004315psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004316{
4317 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004318 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4319 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004320 /* The object has (apparently) been initialized but it is not
4321 * in use. It's ok to call abort on such an object, and there's
4322 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004323 } else
John Durkopd0321952020-10-29 21:37:36 -07004324#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004325 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4326 mbedtls_free(operation->ctx.hkdf.info);
4327 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4328 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004329#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4330#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4331 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004332 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4333 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4334 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4335 if (operation->ctx.tls12_prf.secret != NULL) {
4336 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
4337 operation->ctx.tls12_prf.secret_length);
4338 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004339 }
4340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004341 if (operation->ctx.tls12_prf.seed != NULL) {
4342 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
4343 operation->ctx.tls12_prf.seed_length);
4344 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01004345 }
4346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004347 if (operation->ctx.tls12_prf.label != NULL) {
4348 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
4349 operation->ctx.tls12_prf.label_length);
4350 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01004351 }
4352
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004353 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004354
4355 /* We leave the fields Ai and output_block to be erased safely by the
4356 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004357 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004358#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4359 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004360 {
4361 status = PSA_ERROR_BAD_STATE;
4362 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004363 mbedtls_platform_zeroize(operation, sizeof(*operation));
4364 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004365}
4366
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004367psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004368 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004369{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004370 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004371 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004372 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004373 }
4374
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004375 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004376 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004377}
4378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004379psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4380 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004381{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004382 if (operation->alg == 0) {
4383 return PSA_ERROR_BAD_STATE;
4384 }
4385 if (capacity > operation->capacity) {
4386 return PSA_ERROR_INVALID_ARGUMENT;
4387 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004388 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004389 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004390}
4391
John Durkopd0321952020-10-29 21:37:36 -07004392#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004393/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004394 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004395static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
4396 psa_algorithm_t hash_alg,
4397 uint8_t *output,
4398 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004399{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004400 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02004401 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004402 psa_status_t status;
4403
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004404 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
4405 return PSA_ERROR_BAD_STATE;
4406 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004407 hkdf->state = HKDF_STATE_OUTPUT;
4408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004409 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004410 /* Copy what remains of the current block */
4411 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004412 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004413 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004414 }
4415 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004416 output += n;
4417 output_length -= n;
4418 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004419 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004420 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004421 }
Gilles Peskined54931c2018-07-17 21:06:59 +02004422 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004423 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004425 * object was corrupted or if this function is called directly
4426 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004427 if (hkdf->block_number == 0xff) {
4428 return PSA_ERROR_BAD_STATE;
4429 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004430
4431 /* We need a new block */
4432 ++hkdf->block_number;
4433 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004435 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4436 hash_alg,
4437 hkdf->prk,
4438 hash_length);
4439 if (status != PSA_SUCCESS) {
4440 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004441 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004442
4443 if (hkdf->block_number != 1) {
4444 status = psa_mac_update(&hkdf->hmac,
4445 hkdf->output_block,
4446 hash_length);
4447 if (status != PSA_SUCCESS) {
4448 return status;
4449 }
4450 }
4451 status = psa_mac_update(&hkdf->hmac,
4452 hkdf->info,
4453 hkdf->info_length);
4454 if (status != PSA_SUCCESS) {
4455 return status;
4456 }
4457 status = psa_mac_update(&hkdf->hmac,
4458 &hkdf->block_number, 1);
4459 if (status != PSA_SUCCESS) {
4460 return status;
4461 }
4462 status = psa_mac_sign_finish(&hkdf->hmac,
4463 hkdf->output_block,
4464 sizeof(hkdf->output_block),
4465 &hmac_output_length);
4466 if (status != PSA_SUCCESS) {
4467 return status;
4468 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004469 }
4470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004471 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004472}
John Durkop07cc04a2020-11-16 22:08:34 -08004473#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004474
John Durkop07cc04a2020-11-16 22:08:34 -08004475#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4476 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004477static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4478 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004479 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004480{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004481 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4482 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004483 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4484 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004485 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004486
Janos Follath7742fee2019-06-17 12:58:10 +01004487 /* We can't be wanting more output after block 0xff, otherwise
4488 * the capacity check in psa_key_derivation_output_bytes() would have
4489 * prevented this call. It could happen only if the operation
4490 * object was corrupted or if this function is called directly
4491 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004492 if (tls12_prf->block_number == 0xff) {
4493 return PSA_ERROR_CORRUPTION_DETECTED;
4494 }
Janos Follath7742fee2019-06-17 12:58:10 +01004495
4496 /* We need a new block */
4497 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004498 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004499
4500 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4501 *
4502 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4503 *
4504 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4505 * HMAC_hash(secret, A(2) + seed) +
4506 * HMAC_hash(secret, A(3) + seed) + ...
4507 *
4508 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004509 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004510 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004511 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004512 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004513 * is currently extracted as `output_block` and where i is
4514 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004515 */
4516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004517 status = psa_key_derivation_start_hmac(&hmac,
4518 hash_alg,
4519 tls12_prf->secret,
4520 tls12_prf->secret_length);
4521 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004522 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004523 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004524
4525 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004526 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004527 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4528 * the variable seed and in this instance means it in the context of the
4529 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004530 status = psa_mac_update(&hmac,
4531 tls12_prf->label,
4532 tls12_prf->label_length);
4533 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004534 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004535 }
4536 status = psa_mac_update(&hmac,
4537 tls12_prf->seed,
4538 tls12_prf->seed_length);
4539 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004540 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004541 }
4542 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004543 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004544 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4545 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004546 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004547 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004548 }
4549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004550 status = psa_mac_sign_finish(&hmac,
4551 tls12_prf->Ai, hash_length,
4552 &hmac_output_length);
4553 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004554 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004555 }
4556 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004557 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004558 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004559
4560 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004561 status = psa_key_derivation_start_hmac(&hmac,
4562 hash_alg,
4563 tls12_prf->secret,
4564 tls12_prf->secret_length);
4565 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004566 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004567 }
4568 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4569 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004570 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004571 }
4572 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4573 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004574 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004575 }
4576 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4577 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004578 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004579 }
4580 status = psa_mac_sign_finish(&hmac,
4581 tls12_prf->output_block, hash_length,
4582 &hmac_output_length);
4583 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004584 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004585 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004586
Janos Follath7742fee2019-06-17 12:58:10 +01004587
4588cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004589 cleanup_status = psa_mac_abort(&hmac);
4590 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004591 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004592 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004594 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004595}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004596
Janos Follath844eb0e2019-06-19 12:10:49 +01004597static psa_status_t psa_key_derivation_tls12_prf_read(
4598 psa_tls12_prf_key_derivation_t *tls12_prf,
4599 psa_algorithm_t alg,
4600 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004601 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004602{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004603 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4604 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004605 psa_status_t status;
4606 uint8_t offset, length;
4607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004608 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004609 case PSA_TLS12_PRF_STATE_LABEL_SET:
4610 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4611 break;
4612 case PSA_TLS12_PRF_STATE_OUTPUT:
4613 break;
4614 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004615 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004616 }
4617
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004618 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004619 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004620 if (tls12_prf->left_in_block == 0) {
4621 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4622 alg);
4623 if (status != PSA_SUCCESS) {
4624 return status;
4625 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004626
4627 continue;
4628 }
4629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004630 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004631 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004632 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004633 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004634 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004635
4636 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004637 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004638 output += length;
4639 output_length -= length;
4640 tls12_prf->left_in_block -= length;
4641 }
4642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004643 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004644}
John Durkop07cc04a2020-11-16 22:08:34 -08004645#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4646 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004647
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004648psa_status_t psa_key_derivation_output_bytes(
4649 psa_key_derivation_operation_t *operation,
Ryan Everett08bd2462024-02-09 16:15:32 +00004650 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004651 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004652{
4653 psa_status_t status;
Ryan Everett08bd2462024-02-09 16:15:32 +00004654 LOCAL_OUTPUT_DECLARE(output_external, output);
4655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004656 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004658 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004660 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004661 }
4662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004663 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004664 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004665 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004666 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4667 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004668 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004669 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004670 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004671 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004672
4673 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
4674 if (output_length > operation->capacity) {
4675 operation->capacity = 0;
4676 /* Go through the error path to wipe all confidential data now
4677 * that the operation object is useless. */
4678 status = PSA_ERROR_INSUFFICIENT_DATA;
4679 goto exit;
4680 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004681 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004682
John Durkopd0321952020-10-29 21:37:36 -07004683#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004684 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4685 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4686 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4687 output, output_length);
4688 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004689#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4690#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4691 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004692 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4693 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4694 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4695 kdf_alg, output,
4696 output_length);
4697 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004698#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4699 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004700 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004701 (void) kdf_alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004702 status = PSA_ERROR_BAD_STATE;
4703 LOCAL_OUTPUT_FREE(output_external, output);
4704
4705 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004706 }
4707
4708exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004709 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004710 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004711 * This allows us to differentiate between exhausted operations and
4712 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4713 * operations. */
4714 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004715 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004716 operation->alg = alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004717 if (output != NULL) {
4718 memset(output, '!', output_length);
4719 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004720 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004721
4722 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004723 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004724}
4725
David Brown7807bf72021-02-09 16:28:23 -07004726#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004727static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004728{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004729 if (data_size >= 8) {
4730 mbedtls_des_key_set_parity(data);
4731 }
4732 if (data_size >= 16) {
4733 mbedtls_des_key_set_parity(data + 8);
4734 }
4735 if (data_size >= 24) {
4736 mbedtls_des_key_set_parity(data + 16);
4737 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004738}
David Brown7807bf72021-02-09 16:28:23 -07004739#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004740
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004741static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004742 psa_key_slot_t *slot,
4743 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004744 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004745{
4746 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004747 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004748 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004749 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004751 if (!key_type_is_raw_bytes(slot->attr.type)) {
4752 return PSA_ERROR_INVALID_ARGUMENT;
4753 }
4754 if (bits % 8 != 0) {
4755 return PSA_ERROR_INVALID_ARGUMENT;
4756 }
4757 data = mbedtls_calloc(1, bytes);
4758 if (data == NULL) {
4759 return PSA_ERROR_INSUFFICIENT_MEMORY;
4760 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004761
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004762 status = psa_key_derivation_output_bytes(operation, data, bytes);
4763 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004764 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004765 }
David Brown12ca5032021-02-11 11:02:00 -07004766#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004767 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4768 psa_des_set_key_parity(data, bytes);
4769 }
David Brown8107e312021-02-12 08:08:46 -07004770#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004772 status = psa_allocate_buffer_to_slot(slot, bytes);
4773 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004774 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004775 }
Ronald Crondd04d422020-11-28 15:14:42 +01004776
Ronald Cronbf33c932020-11-28 18:06:53 +01004777 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004778 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004779 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004780 };
4781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004782 status = psa_driver_wrapper_import_key(&attributes,
4783 data, bytes,
4784 slot->key.data,
4785 slot->key.bytes,
4786 &slot->key.bytes, &bits);
4787 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004788 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004789 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004790
4791exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004792 mbedtls_free(data);
4793 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004794}
4795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004796psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4797 psa_key_derivation_operation_t *operation,
4798 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004799{
4800 psa_status_t status;
4801 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004802 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004803
Ronald Cron81709fc2020-11-14 12:10:32 +01004804 *key = MBEDTLS_SVC_KEY_ID_INIT;
4805
Gilles Peskine0f84d622019-09-12 19:03:13 +02004806 /* Reject any attempt to create a zero-length key so that we don't
4807 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004808 if (psa_get_key_bits(attributes) == 0) {
4809 return PSA_ERROR_INVALID_ARGUMENT;
4810 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004812 if (operation->alg == PSA_ALG_NONE) {
4813 return PSA_ERROR_BAD_STATE;
4814 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004816 if (!operation->can_output_key) {
4817 return PSA_ERROR_NOT_PERMITTED;
4818 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004820 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4821 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004822#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004823 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004824 /* Deriving a key in a secure element is not implemented yet. */
4825 status = PSA_ERROR_NOT_SUPPORTED;
4826 }
4827#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004828 if (status == PSA_SUCCESS) {
4829 status = psa_generate_derived_key_internal(slot,
4830 attributes->core.bits,
4831 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004832 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004833 if (status == PSA_SUCCESS) {
4834 status = psa_finish_key_creation(slot, driver, key);
4835 }
4836 if (status != PSA_SUCCESS) {
4837 psa_fail_key_creation(slot, driver);
4838 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004840 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004841}
4842
Gilles Peskineeab56e42018-07-12 17:12:33 +02004843
4844
4845/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004846/* Key derivation */
4847/****************************************************************/
4848
Steven Cooreman932ffb72021-02-15 12:14:32 +01004849#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004850static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004851{
4852#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004853 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4854 return 1;
4855 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004856#endif
4857#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004858 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4859 return 1;
4860 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004861#endif
4862#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004863 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4864 return 1;
4865 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004866#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004867 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004868}
4869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004870static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004871{
4872 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004873 psa_status_t status = psa_hash_setup(&operation, alg);
4874 psa_hash_abort(&operation);
4875 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004876}
4877
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304878static psa_status_t psa_key_derivation_set_maximum_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004879 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004880 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004881{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004882 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4883 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4884 if (hash_size == 0) {
4885 return PSA_ERROR_NOT_SUPPORTED;
4886 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004887
4888 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4889 * we might fail later, which is somewhat unfriendly and potentially
4890 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004891 psa_status_t status = psa_hash_try_support(hash_alg);
4892 if (status != PSA_SUCCESS) {
4893 return status;
4894 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004895
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304896#if defined(PSA_WANT_ALG_HKDF)
4897 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4898 operation->capacity = 255 * hash_size;
4899 } else
4900#endif
4901#if defined(PSA_WANT_ALG_TLS12_PRF)
4902 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
4903 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4904 operation->capacity = SIZE_MAX;
4905 } else
4906#endif
4907#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
4908 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
4909 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4910 /* Master Secret is always 48 bytes
4911 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
4912 operation->capacity = 48U;
4913 } else
4914#endif
4915 {
4916 (void) hash_size;
4917 status = PSA_ERROR_NOT_SUPPORTED;
4918 }
4919 return status;
4920}
4921
4922
4923static psa_status_t psa_key_derivation_setup_kdf(
4924 psa_key_derivation_operation_t *operation,
4925 psa_algorithm_t kdf_alg)
4926{
4927 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4928 * initialisers for this union leave some bytes unspecified.) */
4929 memset(&operation->ctx, 0, sizeof(operation->ctx));
4930 /* Make sure that kdf_alg is a supported key derivation algorithm. */
4931 if (!is_kdf_alg_supported(kdf_alg)) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004932 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004933 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004934
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304935 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
4936 kdf_alg);
4937 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004938}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004940static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004941{
4942#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004943 if (alg == PSA_ALG_ECDH) {
4944 return PSA_SUCCESS;
4945 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004946#endif
4947 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004948 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004949}
John Durkop07cc04a2020-11-16 22:08:34 -08004950#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004952psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4953 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004954{
4955 psa_status_t status;
4956
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004957 if (operation->alg != 0) {
4958 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004959 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004960
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004961 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4962 return PSA_ERROR_INVALID_ARGUMENT;
4963 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4964#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4965 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4966 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4967 status = psa_key_agreement_try_support(ka_alg);
4968 if (status != PSA_SUCCESS) {
4969 return status;
4970 }
4971 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4972#else
4973 return PSA_ERROR_NOT_SUPPORTED;
4974#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4975 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4976#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4977 status = psa_key_derivation_setup_kdf(operation, alg);
4978#else
4979 return PSA_ERROR_NOT_SUPPORTED;
4980#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4981 } else {
4982 return PSA_ERROR_INVALID_ARGUMENT;
4983 }
4984
4985 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004986 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004987 }
4988 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004989}
4990
John Durkopd0321952020-10-29 21:37:36 -07004991#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004992static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4993 psa_algorithm_t hash_alg,
4994 psa_key_derivation_step_t step,
4995 const uint8_t *data,
4996 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004997{
4998 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004999 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02005000 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005001 if (hkdf->state != HKDF_STATE_INIT) {
5002 return PSA_ERROR_BAD_STATE;
5003 } else {
5004 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5005 hash_alg,
5006 data, data_length);
5007 if (status != PSA_SUCCESS) {
5008 return status;
5009 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02005010 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005011 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02005012 }
Gilles Peskine03410b52019-05-16 16:05:19 +02005013 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005014 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005015 if (hkdf->state == HKDF_STATE_INIT) {
5016 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5017 hash_alg,
5018 NULL, 0);
5019 if (status != PSA_SUCCESS) {
5020 return status;
5021 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005022 hkdf->state = HKDF_STATE_STARTED;
5023 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005024 if (hkdf->state != HKDF_STATE_STARTED) {
5025 return PSA_ERROR_BAD_STATE;
5026 }
5027 status = psa_mac_update(&hkdf->hmac,
5028 data, data_length);
5029 if (status != PSA_SUCCESS) {
5030 return status;
5031 }
5032 status = psa_mac_sign_finish(&hkdf->hmac,
5033 hkdf->prk,
5034 sizeof(hkdf->prk),
5035 &data_length);
5036 if (status != PSA_SUCCESS) {
5037 return status;
5038 }
5039 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02005040 hkdf->block_number = 0;
5041 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005042 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02005043 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005044 if (hkdf->state == HKDF_STATE_OUTPUT) {
5045 return PSA_ERROR_BAD_STATE;
5046 }
5047 if (hkdf->info_set) {
5048 return PSA_ERROR_BAD_STATE;
5049 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005050 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005051 if (data_length != 0) {
5052 hkdf->info = mbedtls_calloc(1, data_length);
5053 if (hkdf->info == NULL) {
5054 return PSA_ERROR_INSUFFICIENT_MEMORY;
5055 }
5056 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005057 }
5058 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005059 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005060 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005061 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005062 }
5063}
John Durkop07cc04a2020-11-16 22:08:34 -08005064#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005065
John Durkop07cc04a2020-11-16 22:08:34 -08005066#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5067 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005068static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
5069 const uint8_t *data,
5070 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01005071{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005072 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
5073 return PSA_ERROR_BAD_STATE;
5074 }
Janos Follathf08e2652019-06-13 09:05:41 +01005075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005076 if (data_length != 0) {
5077 prf->seed = mbedtls_calloc(1, data_length);
5078 if (prf->seed == NULL) {
5079 return PSA_ERROR_INSUFFICIENT_MEMORY;
5080 }
Janos Follathf08e2652019-06-13 09:05:41 +01005081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005082 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005083 prf->seed_length = data_length;
5084 }
Janos Follathf08e2652019-06-13 09:05:41 +01005085
Gilles Peskine43f958b2020-12-13 14:55:14 +01005086 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005088 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01005089}
5090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005091static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
5092 const uint8_t *data,
5093 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01005094{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005095 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
5096 return PSA_ERROR_BAD_STATE;
5097 }
Janos Follath81550542019-06-13 14:26:34 +01005098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005099 if (data_length != 0) {
5100 prf->secret = mbedtls_calloc(1, data_length);
5101 if (prf->secret == NULL) {
5102 return PSA_ERROR_INSUFFICIENT_MEMORY;
5103 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02005104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005105 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02005106 prf->secret_length = data_length;
5107 }
Janos Follath81550542019-06-13 14:26:34 +01005108
Gilles Peskine43f958b2020-12-13 14:55:14 +01005109 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005111 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01005112}
5113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005114static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
5115 const uint8_t *data,
5116 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01005117{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005118 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
5119 return PSA_ERROR_BAD_STATE;
5120 }
Janos Follath63028dd2019-06-13 09:15:47 +01005121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005122 if (data_length != 0) {
5123 prf->label = mbedtls_calloc(1, data_length);
5124 if (prf->label == NULL) {
5125 return PSA_ERROR_INSUFFICIENT_MEMORY;
5126 }
Janos Follath63028dd2019-06-13 09:15:47 +01005127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005128 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005129 prf->label_length = data_length;
5130 }
Janos Follath63028dd2019-06-13 09:15:47 +01005131
Gilles Peskine43f958b2020-12-13 14:55:14 +01005132 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005134 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01005135}
5136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005137static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
5138 psa_key_derivation_step_t step,
5139 const uint8_t *data,
5140 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01005141{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005142 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01005143 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005144 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01005145 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005146 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01005147 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005148 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01005149 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005150 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01005151 }
5152}
John Durkop07cc04a2020-11-16 22:08:34 -08005153#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5154 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5155
5156#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5157static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5158 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005159 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005160 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08005161{
5162 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005163 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08005164 uint8_t *cur = pms;
5165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005166 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
5167 return PSA_ERROR_INVALID_ARGUMENT;
5168 }
John Durkop07cc04a2020-11-16 22:08:34 -08005169
5170 /* Quoting RFC 4279, Section 2:
5171 *
5172 * The premaster secret is formed as follows: if the PSK is N octets
5173 * long, concatenate a uint16 with the value N, N zero octets, a second
5174 * uint16 with the value N, and the PSK itself.
5175 */
5176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005177 *cur++ = MBEDTLS_BYTE_1(data_length);
5178 *cur++ = MBEDTLS_BYTE_0(data_length);
5179 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005180 cur += data_length;
5181 *cur++ = pms[0];
5182 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005183 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005184 cur += data_length;
5185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005186 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08005187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005188 mbedtls_platform_zeroize(pms, sizeof(pms));
5189 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08005190}
Janos Follath6660f0e2019-06-17 08:44:03 +01005191
5192static psa_status_t psa_tls12_prf_psk_to_ms_input(
5193 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005194 psa_key_derivation_step_t step,
5195 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005196 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01005197{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005198 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
5199 return psa_tls12_prf_psk_to_ms_set_key(prf,
5200 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01005201 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005203 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01005204}
John Durkop07cc04a2020-11-16 22:08:34 -08005205#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005206
Gilles Peskineb8965192019-09-24 16:21:10 +02005207/** Check whether the given key type is acceptable for the given
5208 * input step of a key derivation.
5209 *
5210 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5211 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5212 * Both secret and non-secret inputs can alternatively have the type
5213 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5214 * that the input was passed as a buffer rather than via a key object.
5215 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005216static int psa_key_derivation_check_input_type(
5217 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005218 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02005219{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005220 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005221 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005222 if (key_type == PSA_KEY_TYPE_DERIVE) {
5223 return PSA_SUCCESS;
5224 }
5225 if (key_type == PSA_KEY_TYPE_NONE) {
5226 return PSA_SUCCESS;
5227 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005228 break;
5229 case PSA_KEY_DERIVATION_INPUT_LABEL:
5230 case PSA_KEY_DERIVATION_INPUT_SALT:
5231 case PSA_KEY_DERIVATION_INPUT_INFO:
5232 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005233 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
5234 return PSA_SUCCESS;
5235 }
5236 if (key_type == PSA_KEY_TYPE_NONE) {
5237 return PSA_SUCCESS;
5238 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005239 break;
5240 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005241 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005242}
5243
Janos Follathb80a94e2019-06-12 15:54:46 +01005244static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005245 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005246 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005247 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005248 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005249 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005250{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005251 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005252 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005254 status = psa_key_derivation_check_input_type(step, key_type);
5255 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005256 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005257 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005258
John Durkopd0321952020-10-29 21:37:36 -07005259#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005260 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5261 status = psa_hkdf_input(&operation->ctx.hkdf,
5262 PSA_ALG_HKDF_GET_HASH(kdf_alg),
5263 step, data, data_length);
5264 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005265#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5266#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005267 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5268 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
5269 step, data, data_length);
5270 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005271#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5272#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005273 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5274 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
5275 step, data, data_length);
5276 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005277#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005278 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005279 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005280 (void) data;
5281 (void) data_length;
5282 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005283 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005284 }
5285
Gilles Peskine224b0d62019-09-23 18:13:17 +02005286exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005287 if (status != PSA_SUCCESS) {
5288 psa_key_derivation_abort(operation);
5289 }
5290 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005291}
5292
Janos Follath51f4a0f2019-06-14 11:35:55 +01005293psa_status_t psa_key_derivation_input_bytes(
5294 psa_key_derivation_operation_t *operation,
5295 psa_key_derivation_step_t step,
Ryan Everett6f682062024-02-09 16:18:39 +00005296 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005297 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005298{
Ryan Everett6f682062024-02-09 16:18:39 +00005299 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5300 LOCAL_INPUT_DECLARE(data_external, data);
5301
5302 LOCAL_INPUT_ALLOC(data_external, data_length, data);
5303
5304 status = psa_key_derivation_input_internal(operation, step,
5305 PSA_KEY_TYPE_NONE,
5306 data, data_length);
David Horstmann42015332024-03-13 15:42:31 +00005307#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everett6f682062024-02-09 16:18:39 +00005308exit:
Ryan Everettd0d12fb2024-02-12 09:19:29 +00005309#endif
Ryan Everett6f682062024-02-09 16:18:39 +00005310 LOCAL_INPUT_FREE(data_external, data);
5311 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005312}
5313
Janos Follath51f4a0f2019-06-14 11:35:55 +01005314psa_status_t psa_key_derivation_input_key(
5315 psa_key_derivation_operation_t *operation,
5316 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005317 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005318{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005319 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005320 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005321 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005322
Ronald Cron5c522922020-11-14 16:35:34 +01005323 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005324 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5325 if (status != PSA_SUCCESS) {
5326 psa_key_derivation_abort(operation);
5327 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02005328 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005329
5330 /* Passing a key object as a SECRET input unlocks the permission
5331 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005332 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005333 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005334 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005336 status = psa_key_derivation_input_internal(operation,
5337 step, slot->attr.type,
5338 slot->key.data,
5339 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005341 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005343 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005344}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005345
5346
5347
5348/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005349/* Key agreement */
5350/****************************************************************/
5351
John Durkop6ba40d12020-11-10 08:50:04 -08005352#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005353static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
5354 size_t peer_key_length,
5355 const mbedtls_ecp_keypair *our_key,
5356 uint8_t *shared_secret,
5357 size_t shared_secret_size,
5358 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005359{
Steven Cooremand4867872020-08-05 16:31:39 +02005360 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005361 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005362 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005363 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005364 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
5365 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005366
Ronald Cron90857082020-11-25 14:58:33 +01005367 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005368 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5369 bits,
5370 peer_key,
5371 peer_key_length,
5372 &their_key);
5373 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005374 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005375 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005376
Jaeden Amero97271b32019-01-10 19:38:51 +00005377 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005378 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
5379 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005380 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005381 }
Jaeden Amero97271b32019-01-10 19:38:51 +00005382 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005383 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
5384 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005385 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005386 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005387
Jaeden Amero97271b32019-01-10 19:38:51 +00005388 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005389 mbedtls_ecdh_calc_secret(&ecdh,
5390 shared_secret_length,
5391 shared_secret, shared_secret_size,
5392 mbedtls_psa_get_random,
5393 MBEDTLS_PSA_RANDOM_STATE));
5394 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005395 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005396 }
5397 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005398 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005399 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005400
5401exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005402 if (status != PSA_SUCCESS) {
5403 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
5404 }
5405 mbedtls_ecdh_free(&ecdh);
5406 mbedtls_ecp_keypair_free(their_key);
5407 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02005408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005409 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005410}
John Durkop6ba40d12020-11-10 08:50:04 -08005411#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005412
Gilles Peskine01d718c2018-09-18 12:01:02 +02005413#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005415static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
5416 psa_key_slot_t *private_key,
5417 const uint8_t *peer_key,
5418 size_t peer_key_length,
5419 uint8_t *shared_secret,
5420 size_t shared_secret_size,
5421 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005422{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005423 mbedtls_ecp_keypair *ecp = NULL;
5424 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01005425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005426 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08005427#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005428 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005429 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
5430 return PSA_ERROR_INVALID_ARGUMENT;
5431 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005432 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005433 private_key->attr.type,
5434 private_key->attr.bits,
5435 private_key->key.data,
5436 private_key->key.bytes,
5437 &ecp);
5438 if (status != PSA_SUCCESS) {
5439 return status;
5440 }
5441 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
5442 ecp,
5443 shared_secret, shared_secret_size,
5444 shared_secret_length);
5445 mbedtls_ecp_keypair_free(ecp);
5446 mbedtls_free(ecp);
5447 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08005448#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005449 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005450 (void) ecp;
5451 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01005452 (void) private_key;
5453 (void) peer_key;
5454 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005455 (void) shared_secret;
5456 (void) shared_secret_size;
5457 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005458 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005459 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005460}
5461
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005462/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005463 * to potentially free embedded data structures and wipe confidential data.
5464 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005465static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5466 psa_key_derivation_step_t step,
5467 psa_key_slot_t *private_key,
5468 const uint8_t *peer_key,
5469 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005470{
5471 psa_status_t status;
5472 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5473 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005474 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005475
5476 /* Step 1: run the secret agreement algorithm to generate the shared
5477 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005478 status = psa_key_agreement_raw_internal(ka_alg,
5479 private_key,
5480 peer_key, peer_key_length,
5481 shared_secret,
5482 sizeof(shared_secret),
5483 &shared_secret_length);
5484 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005485 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005486 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005487
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005488 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005489 * the shared secret. A shared secret is permitted wherever a key
5490 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005491 status = psa_key_derivation_input_internal(operation, step,
5492 PSA_KEY_TYPE_DERIVE,
5493 shared_secret,
5494 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005495exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005496 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5497 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005498}
5499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005500psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5501 psa_key_derivation_step_t step,
5502 mbedtls_svc_key_id_t private_key,
Thomas Daubney9da359f2024-02-15 13:15:47 +00005503 const uint8_t *peer_key_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005504 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005505{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005506 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005507 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005508 psa_key_slot_t *slot;
Thomas Daubney9da359f2024-02-15 13:15:47 +00005509 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005511 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5512 return PSA_ERROR_INVALID_ARGUMENT;
5513 }
Ronald Cron5c522922020-11-14 16:35:34 +01005514 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005515 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5516 if (status != PSA_SUCCESS) {
5517 return status;
5518 }
Thomas Daubney9da359f2024-02-15 13:15:47 +00005519
Thomas Daubneyca928312024-03-12 17:53:30 +00005520 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005521 status = psa_key_agreement_internal(operation, step,
5522 slot,
5523 peer_key, peer_key_length);
Thomas Daubney9da359f2024-02-15 13:15:47 +00005524
David Horstmann42015332024-03-13 15:42:31 +00005525#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney9da359f2024-02-15 13:15:47 +00005526exit:
Thomas Daubney3c0c6b12024-02-15 14:25:08 +00005527#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005528 if (status != PSA_SUCCESS) {
5529 psa_key_derivation_abort(operation);
5530 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005531 /* If a private key has been added as SECRET, we allow the derived
5532 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005533 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005534 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005535 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005536 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005538 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005539
Thomas Daubney9da359f2024-02-15 13:15:47 +00005540 LOCAL_INPUT_FREE(peer_key_external, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005541 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005542}
5543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005544psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5545 mbedtls_svc_key_id_t private_key,
Thomas Daubney43042762024-02-15 12:57:26 +00005546 const uint8_t *peer_key_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005547 size_t peer_key_length,
Thomas Daubney43042762024-02-15 12:57:26 +00005548 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005549 size_t output_size,
5550 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005551{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005552 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005553 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005554 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005555 size_t expected_length;
Thomas Daubney43042762024-02-15 12:57:26 +00005556 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
5557 LOCAL_OUTPUT_DECLARE(output_external, output);
Thomas Daubney0736df32024-02-21 12:28:20 +00005558 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005560 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005561 status = PSA_ERROR_INVALID_ARGUMENT;
5562 goto exit;
5563 }
Ronald Cron5c522922020-11-14 16:35:34 +01005564 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005565 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5566 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005567 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005568 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005569
Gilles Peskine42313fb2022-04-14 00:17:15 +02005570 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5571 * for the output size. The PSA specification only guarantees that this
5572 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5573 * but it might be nice to allow smaller buffers if the output fits.
5574 * At the time of writing this comment, with only ECDH implemented,
5575 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5576 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5577 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005578 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005579 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5580 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005581 status = PSA_ERROR_BUFFER_TOO_SMALL;
5582 goto exit;
5583 }
5584
Thomas Daubney43042762024-02-15 12:57:26 +00005585 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005586 status = psa_key_agreement_raw_internal(alg, slot,
5587 peer_key, peer_key_length,
5588 output, output_size,
5589 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005590
5591exit:
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005592 /* Check for successful allocation of output,
5593 * with an unsuccessful status. */
Thomas Daubney2ea8d8f2024-02-21 15:16:01 +00005594 if (output != NULL && status != PSA_SUCCESS) {
5595 /* If an error happens and is not handled properly, the output
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005596 * may be used as a key to protect sensitive data. Arrange for such
5597 * a key to be random, which is likely to result in decryption or
5598 * verification errors. This is better than filling the buffer with
5599 * some constant data such as zeros, which would result in the data
5600 * being protected with a reproducible, easily knowable key.
5601 */
David Horstmann65bf12c2024-02-06 15:27:49 +00005602 psa_generate_random_internal(output, output_size);
Thomas Daubney2ea8d8f2024-02-21 15:16:01 +00005603 *output_length = output_size;
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005604 }
5605
5606 if (output == NULL) {
Thomas Daubney0736df32024-02-21 12:28:20 +00005607 /* output allocation failed. */
5608 *output_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005609 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005611 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005612
Thomas Daubney43042762024-02-15 12:57:26 +00005613 LOCAL_INPUT_FREE(peer_key_external, peer_key);
5614 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005615 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005616}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005617
5618
5619/****************************************************************/
5620/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005621/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005622
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005623#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5624#include "mbedtls/entropy_poll.h"
5625#endif
5626
Gilles Peskine30524eb2020-11-13 17:02:26 +01005627/** Initialize the PSA random generator.
5628 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005629static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005630{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005631#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005632 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005633#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5634
Gilles Peskine30524eb2020-11-13 17:02:26 +01005635 /* Set default configuration if
5636 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005637 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005638 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005639 }
5640 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005641 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005642 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005644 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005645#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5646 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5647 /* The PSA entropy injection feature depends on using NV seed as an entropy
5648 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005649 mbedtls_entropy_add_source(&rng->entropy,
5650 mbedtls_nv_seed_poll, NULL,
5651 MBEDTLS_ENTROPY_BLOCK_SIZE,
5652 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005653#endif
5654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005655 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005656#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005657}
5658
5659/** Deinitialize the PSA random generator.
5660 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005661static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005662{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005663#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005664 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005665#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005666 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5667 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005668#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005669}
5670
5671/** Seed the PSA random generator.
5672 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005673static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005674{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005675#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5676 /* Do nothing: the external RNG seeds itself. */
5677 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005678 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005679#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005680 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005681 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5682 drbg_seed, sizeof(drbg_seed) - 1);
5683 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005684#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005685}
5686
David Horstmann65bf12c2024-02-06 15:27:49 +00005687psa_status_t psa_generate_random(uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005688 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005689{
David Horstmann65bf12c2024-02-06 15:27:49 +00005690 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005691
David Horstmann65bf12c2024-02-06 15:27:49 +00005692 LOCAL_OUTPUT_DECLARE(output_external, output);
5693 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005694
David Horstmann65bf12c2024-02-06 15:27:49 +00005695 status = psa_generate_random_internal(output, output_size);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005696
David Horstmann42015332024-03-13 15:42:31 +00005697#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann65bf12c2024-02-06 15:27:49 +00005698exit:
David Horstmann10e44f32024-02-28 14:17:10 +00005699#endif
David Horstmann65bf12c2024-02-06 15:27:49 +00005700 LOCAL_OUTPUT_FREE(output_external, output);
5701 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005702}
5703
Gilles Peskine8814fc42020-12-14 15:33:44 +01005704/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005705 *
5706 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5707 * `psa_generate_random(...)`. The state parameter is ignored since the
5708 * PSA API doesn't support passing an explicit state.
5709 *
5710 * In the non-external case, psa_generate_random() calls an
5711 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5712 * and semantics as mbedtls_psa_get_random(). As an optimization,
5713 * instead of doing this back-and-forth between the PSA API and the
5714 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5715 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005716 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005717#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5718int mbedtls_psa_get_random(void *p_rng,
5719 unsigned char *output,
5720 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005721{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005722 /* This function takes a pointer to the RNG state because that's what
5723 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5724 * its own state internally and doesn't let the caller access that state.
5725 * So we just ignore the state parameter, and in practice we'll pass
5726 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005727 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005728 psa_status_t status = psa_generate_random(output, output_size);
5729 if (status == PSA_SUCCESS) {
5730 return 0;
5731 } else {
5732 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5733 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005734}
5735#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5736
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005737#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005738psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5739 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005740{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005741 if (global_data.initialized) {
5742 return PSA_ERROR_NOT_PERMITTED;
5743 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005745 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5746 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5747 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5748 return PSA_ERROR_INVALID_ARGUMENT;
5749 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005751 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005752}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005753#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005754
Ronald Cron3772afe2021-02-08 16:10:05 +01005755/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005756 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005757 * \param type The key type
5758 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005759 *
5760 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005761 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005762 * \retval #PSA_ERROR_INVALID_ARGUMENT
5763 * The size in bits of the key is not valid.
5764 * \retval #PSA_ERROR_NOT_SUPPORTED
5765 * The type and/or the size in bits of the key or the combination of
5766 * the two is not supported.
5767 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005768static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005769 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005770{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005771 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005773 if (key_type_is_raw_bytes(type)) {
5774 status = validate_unstructured_key_bit_size(type, bits);
5775 if (status != PSA_SUCCESS) {
5776 return status;
5777 }
5778 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005779#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005780 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5781 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5782 return PSA_ERROR_NOT_SUPPORTED;
5783 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005784
Ronald Cron01b2aba2020-10-05 09:42:02 +02005785 /* Accept only byte-aligned keys, for the same reasons as
5786 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005787 if (bits % 8 != 0) {
5788 return PSA_ERROR_NOT_SUPPORTED;
5789 }
5790 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005791#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005792
Ronald Cron5c4d3862020-12-07 11:07:24 +01005793#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005794 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005795 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005796 return PSA_SUCCESS;
5797 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005798#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005799 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005800 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005801 }
5802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005803 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005804}
5805
Ronald Cron55ed0592020-10-05 10:30:40 +02005806psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005807 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005808 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005809{
5810 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005811 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005813 if ((attributes->domain_parameters == NULL) &&
5814 (attributes->domain_parameters_size != 0)) {
5815 return PSA_ERROR_INVALID_ARGUMENT;
5816 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005818 if (key_type_is_raw_bytes(type)) {
5819 status = psa_generate_random(key_buffer, key_buffer_size);
5820 if (status != PSA_SUCCESS) {
5821 return status;
5822 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005823
David Brown12ca5032021-02-11 11:02:00 -07005824#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005825 if (type == PSA_KEY_TYPE_DES) {
5826 psa_des_set_key_parity(key_buffer, key_buffer_size);
5827 }
David Brown8107e312021-02-12 08:08:46 -07005828#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005829 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005830
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005831#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5832 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005833 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5834 return mbedtls_psa_rsa_generate_key(attributes,
5835 key_buffer,
5836 key_buffer_size,
5837 key_buffer_length);
5838 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005839#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5840 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005841
John Durkop9814fa22020-11-04 12:28:15 -08005842#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005843 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5844 return mbedtls_psa_ecp_generate_key(attributes,
5845 key_buffer,
5846 key_buffer_size,
5847 key_buffer_length);
5848 } else
John Durkop9814fa22020-11-04 12:28:15 -08005849#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005850 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005851 (void) key_buffer_length;
5852 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005853 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005855 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005856}
Darryl Green0c6575a2018-11-07 16:05:30 +00005857
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005858psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5859 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005860{
5861 psa_status_t status;
5862 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005863 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005864 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005865
Ronald Cron81709fc2020-11-14 12:10:32 +01005866 *key = MBEDTLS_SVC_KEY_ID_INIT;
5867
Gilles Peskine0f84d622019-09-12 19:03:13 +02005868 /* Reject any attempt to create a zero-length key so that we don't
5869 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005870 if (psa_get_key_bits(attributes) == 0) {
5871 return PSA_ERROR_INVALID_ARGUMENT;
5872 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005873
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005874 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005875 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5876 return PSA_ERROR_INVALID_ARGUMENT;
5877 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005879 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5880 &slot, &driver);
5881 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005882 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005883 }
Gilles Peskine11792082019-08-06 18:36:36 +02005884
Ronald Cron977c2472020-10-13 08:32:21 +02005885 /* In the case of a transparent key or an opaque key stored in local
5886 * storage (thus not in the case of generating a key in a secure element
5887 * or cryptoprocessor with storage), we have to allocate a buffer to
5888 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005889 if (slot->key.data == NULL) {
5890 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5891 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005892 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005893 attributes->core.type, attributes->core.bits);
5894 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005895 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005896 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005897
5898 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005899 attributes->core.type,
5900 attributes->core.bits);
5901 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005902 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005903 attributes, &key_buffer_size);
5904 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005905 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005906 }
Ronald Cron977c2472020-10-13 08:32:21 +02005907 }
Ronald Cron977c2472020-10-13 08:32:21 +02005908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005909 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5910 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005911 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005912 }
Ronald Cron977c2472020-10-13 08:32:21 +02005913 }
5914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005915 status = psa_driver_wrapper_generate_key(attributes,
5916 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005918 if (status != PSA_SUCCESS) {
5919 psa_remove_key_data_from_memory(slot);
5920 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005921
Gilles Peskine11792082019-08-06 18:36:36 +02005922exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005923 if (status == PSA_SUCCESS) {
5924 status = psa_finish_key_creation(slot, driver, key);
5925 }
5926 if (status != PSA_SUCCESS) {
5927 psa_fail_key_creation(slot, driver);
5928 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005930 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005931}
5932
Gilles Peskinee59236f2018-01-27 23:32:46 +01005933/****************************************************************/
5934/* Module setup */
5935/****************************************************************/
5936
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005937#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005938psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005939 void (* entropy_init)(mbedtls_entropy_context *ctx),
5940 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005941{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005942 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5943 return PSA_ERROR_BAD_STATE;
5944 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005945 global_data.rng.entropy_init = entropy_init;
5946 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005947 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005948}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005949#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005951void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005952{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005953 psa_wipe_all_key_slots();
5954 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5955 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005956 }
5957 /* Wipe all remaining data, including configuration.
5958 * In particular, this sets all state indicator to the value
5959 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005960 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005961
5962 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005963 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005964}
5965
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005966#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5967/** Recover a transaction that was interrupted by a power failure.
5968 *
5969 * This function is called during initialization, before psa_crypto_init()
5970 * returns. If this function returns a failure status, the initialization
5971 * fails.
5972 */
5973static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005974 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005975{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005976 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005977 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5978 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005979 /* TODO - fall through to the failure case until this
5980 * is implemented.
5981 * https://github.com/ARMmbed/mbed-crypto/issues/218
5982 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005983 default:
5984 /* We found an unsupported transaction in the storage.
5985 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005986 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005987 }
5988}
5989#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5990
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005991psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005992{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005993 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005994
Gilles Peskinec6b69072018-11-20 21:42:52 +01005995 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005996 if (global_data.initialized != 0) {
5997 return PSA_SUCCESS;
5998 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005999
Gilles Peskine30524eb2020-11-13 17:02:26 +01006000 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006001 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01006002 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006003 status = mbedtls_psa_random_seed(&global_data.rng);
6004 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006005 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006006 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01006007 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006008
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006009 status = psa_initialize_key_slots();
6010 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01006011 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006012 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01006013
Ronald Cron088d5d02021-04-10 16:57:30 +02006014 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006015 status = psa_driver_wrapper_init();
6016 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02006017 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006018 }
Gilles Peskined9348f22019-10-01 15:22:29 +02006019
Gilles Peskine4b734222019-07-24 15:56:31 +02006020#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006021 status = psa_crypto_load_transaction();
6022 if (status == PSA_SUCCESS) {
6023 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
6024 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006025 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006026 }
6027 status = psa_crypto_stop_transaction();
6028 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02006029 /* There's no transaction to complete. It's all good. */
6030 status = PSA_SUCCESS;
6031 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006032#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006033
Gilles Peskinec6b69072018-11-20 21:42:52 +01006034 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006035 global_data.initialized = 1;
6036
6037exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006038 if (status != PSA_SUCCESS) {
6039 mbedtls_psa_crypto_free();
6040 }
6041 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006042}
6043
David Horstmann114d8242023-12-07 18:39:17 +00006044/* Memory copying test hooks. These are called before input copy, after input
6045 * copy, before output copy and after output copy, respectively.
6046 * They are used by memory-poisoning tests to temporarily unpoison buffers
6047 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00006048#if defined(MBEDTLS_TEST_HOOKS)
6049void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
6050void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
6051void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
6052void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
6053#endif
6054
David Horstmanndf493552023-11-15 15:18:30 +00006055/** Copy from an input buffer to a local copy.
6056 *
6057 * \param[in] input Pointer to input buffer.
6058 * \param[in] input_len Length of the input buffer.
6059 * \param[out] input_copy Pointer to a local copy in which to store the input data.
6060 * \param[out] input_copy_len Length of the local copy buffer.
6061 * \return #PSA_SUCCESS, if the buffer was successfully
6062 * copied.
6063 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
6064 * copy is too small to hold contents of the
6065 * input buffer.
6066 */
6067MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00006068psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
6069 uint8_t *input_copy, size_t input_copy_len)
6070{
6071 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00006072 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00006073 }
6074
David Horstmann0760b152023-11-24 16:21:04 +00006075#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006076 if (psa_input_pre_copy_hook != NULL) {
6077 psa_input_pre_copy_hook(input, input_len);
6078 }
David Horstmann0760b152023-11-24 16:21:04 +00006079#endif
6080
David Horstmann660027f2023-11-15 17:33:47 +00006081 if (input_len > 0) {
6082 memcpy(input_copy, input, input_len);
6083 }
David Horstmann957f9802023-10-30 20:29:43 +00006084
David Horstmann0760b152023-11-24 16:21:04 +00006085#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006086 if (psa_input_post_copy_hook != NULL) {
6087 psa_input_post_copy_hook(input, input_len);
6088 }
David Horstmann0760b152023-11-24 16:21:04 +00006089#endif
6090
David Horstmann957f9802023-10-30 20:29:43 +00006091 return PSA_SUCCESS;
6092}
6093
David Horstmanndf493552023-11-15 15:18:30 +00006094/** Copy from a local output buffer into a user-supplied one.
6095 *
6096 * \param[in] output_copy Pointer to a local buffer containing the output.
6097 * \param[in] output_copy_len Length of the local buffer.
6098 * \param[out] output Pointer to user-supplied output buffer.
6099 * \param[out] output_len Length of the user-supplied output buffer.
6100 * \return #PSA_SUCCESS, if the buffer was successfully
6101 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00006102 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00006103 * user-supplied output buffer is too small to
6104 * hold the contents of the local buffer.
6105 */
6106MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00006107psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
6108 uint8_t *output, size_t output_len)
6109{
6110 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00006111 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00006112 }
David Horstmann660027f2023-11-15 17:33:47 +00006113
David Horstmann0760b152023-11-24 16:21:04 +00006114#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006115 if (psa_output_pre_copy_hook != NULL) {
6116 psa_output_pre_copy_hook(output, output_len);
6117 }
David Horstmann0760b152023-11-24 16:21:04 +00006118#endif
6119
David Horstmann660027f2023-11-15 17:33:47 +00006120 if (output_copy_len > 0) {
6121 memcpy(output, output_copy, output_copy_len);
6122 }
6123
David Horstmann0760b152023-11-24 16:21:04 +00006124#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006125 if (psa_output_post_copy_hook != NULL) {
6126 psa_output_post_copy_hook(output, output_len);
6127 }
David Horstmann0760b152023-11-24 16:21:04 +00006128#endif
6129
David Horstmann2bd296e2023-10-30 20:37:12 +00006130 return PSA_SUCCESS;
6131}
6132
David Horstmann81a0d572023-11-20 17:15:32 +00006133psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
6134 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006135{
6136 psa_status_t status;
6137
David Horstmann0d52c712023-11-23 15:50:37 +00006138 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006139
David Horstmann726bf052023-11-15 18:11:26 +00006140 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006141 return PSA_SUCCESS;
6142 }
6143
David Horstmann81a0d572023-11-20 17:15:32 +00006144 local_input->buffer = mbedtls_calloc(input_len, 1);
6145 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006146 /* Since we dealt with the zero-length case above, we know that
6147 * a NULL return value means a failure of allocation. */
6148 return PSA_ERROR_INSUFFICIENT_MEMORY;
6149 }
David Horstmann81a0d572023-11-20 17:15:32 +00006150 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006151
David Horstmann81a0d572023-11-20 17:15:32 +00006152 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006153
6154 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00006155 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006156 if (status != PSA_SUCCESS) {
6157 goto error;
6158 }
6159
6160 return PSA_SUCCESS;
6161
6162error:
David Horstmann81a0d572023-11-20 17:15:32 +00006163 mbedtls_free(local_input->buffer);
6164 local_input->buffer = NULL;
6165 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006166 return status;
6167}
6168
David Horstmann81a0d572023-11-20 17:15:32 +00006169void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00006170{
David Horstmann81a0d572023-11-20 17:15:32 +00006171 mbedtls_free(local_input->buffer);
6172 local_input->buffer = NULL;
6173 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00006174}
6175
David Horstmann1a76ab12023-11-20 12:54:09 +00006176psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
6177 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00006178{
David Horstmann0d52c712023-11-23 15:50:37 +00006179 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00006180
David Horstmann726bf052023-11-15 18:11:26 +00006181 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006182 return PSA_SUCCESS;
6183 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006184 local_output->buffer = mbedtls_calloc(output_len, 1);
6185 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006186 /* Since we dealt with the zero-length case above, we know that
6187 * a NULL return value means a failure of allocation. */
6188 return PSA_ERROR_INSUFFICIENT_MEMORY;
6189 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006190 local_output->length = output_len;
6191 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00006192
6193 return PSA_SUCCESS;
6194}
6195
David Horstmann1a76ab12023-11-20 12:54:09 +00006196psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00006197{
David Horstmann761761f2023-11-15 15:57:32 +00006198 psa_status_t status;
6199
David Horstmann1a76ab12023-11-20 12:54:09 +00006200 if (local_output->buffer == NULL) {
6201 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006202 return PSA_SUCCESS;
6203 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006204 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00006205 /* We have an internal copy but nothing to copy back to. */
6206 return PSA_ERROR_CORRUPTION_DETECTED;
6207 }
6208
David Horstmann1a76ab12023-11-20 12:54:09 +00006209 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
6210 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00006211 if (status != PSA_SUCCESS) {
6212 return status;
6213 }
David Horstmannaeeb2742023-11-08 17:44:18 +00006214
David Horstmann1a76ab12023-11-20 12:54:09 +00006215 mbedtls_free(local_output->buffer);
6216 local_output->buffer = NULL;
6217 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006218
6219 return PSA_SUCCESS;
6220}
6221
Gilles Peskinee59236f2018-01-27 23:32:46 +01006222#endif /* MBEDTLS_PSA_CRYPTO_C */