blob: 502ddc274b488c92882a67a0269d53024239bd0f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinee59236f2018-01-27 23:32:46 +01007 */
8
Gilles Peskinedb09ef62020-06-03 01:43:33 +02009#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010010
11#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030012
John Durkop07cc04a2020-11-16 22:08:34 -080013#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
14#include "check_crypto_config.h"
15#endif
16
Gilles Peskinee59236f2018-01-27 23:32:46 +010017#include "psa/crypto.h"
18
Ronald Crond6d28882020-12-14 14:56:02 +010019#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010020#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010021#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020022#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010023#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010024#include "psa_crypto_hash.h"
Steven Cooreman32d56942021-03-19 17:39:17 +010025#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010027#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020028#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020029#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020030#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010031#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010032/* Include internal declarations that are useful for implementing persistently
33 * stored keys. */
34#include "psa_crypto_storage.h"
35
Gilles Peskineb2b64d32020-12-14 16:43:58 +010036#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010037
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010038#include <stdlib.h>
39#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010041
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010042#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010043#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020044#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000045#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020046#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010047#include "mbedtls/blowfish.h"
48#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020049#include "mbedtls/chacha20.h"
50#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/cipher.h"
52#include "mbedtls/ccm.h"
53#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010054#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020055#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010056#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010057#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/error.h"
59#include "mbedtls/gcm.h"
60#include "mbedtls/md2.h"
61#include "mbedtls/md4.h"
62#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010063#include "mbedtls/md.h"
64#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010065#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010067#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000068#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/sha1.h"
72#include "mbedtls/sha256.h"
73#include "mbedtls/sha512.h"
74#include "mbedtls/xtea.h"
75
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020077
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010078/****************************************************************/
79/* Global data, support functions and library management */
80/****************************************************************/
81
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020083{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020085}
86
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010087/* Values for psa_global_data_t::rng_state */
88#define RNG_NOT_INITIALIZED 0
89#define RNG_INITIALIZED 1
90#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +010093 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010094 unsigned rng_state : 2;
Gilles Peskineb8006a62021-11-08 22:20:03 +010095 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +010096} psa_global_data_t;
97
98static psa_global_data_t global_data;
99
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100100#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
101mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
102 &global_data.rng.drbg;
103#endif
104
itayzafrir0adf0fc2018-09-06 16:24:41 +0300105#define GUARD_MODULE_INITIALIZED \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 if (global_data.initialized == 0) \
107 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300108
David Horstmann42015332024-03-13 15:42:31 +0000109#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann926193a2023-12-13 15:55:25 +0000110
David Horstmannb80e35a2023-12-14 18:12:47 +0000111/* Declare a local copy of an input buffer and a variable that will be used
112 * to store a pointer to the start of the buffer.
David Horstmann926193a2023-12-13 15:55:25 +0000113 *
114 * Note: This macro must be called before any operations which may jump to
115 * the exit label, so that the local input copy object is safe to be freed.
116 *
117 * Assumptions:
118 * - input is the name of a pointer to the buffer to be copied
119 * - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope
David Horstmannb80e35a2023-12-14 18:12:47 +0000120 * - input_copy_name is a name that is unused in the current scope
David Horstmann926193a2023-12-13 15:55:25 +0000121 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000122#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
123 psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT; \
124 const uint8_t *input_copy_name = NULL;
David Horstmann926193a2023-12-13 15:55:25 +0000125
David Horstmannb80e35a2023-12-14 18:12:47 +0000126/* Allocate a copy of the buffer input and set the pointer input_copy to
127 * point to the start of the copy.
David Horstmann926193a2023-12-13 15:55:25 +0000128 *
David Horstmann34980bd2023-11-29 17:07:13 +0000129 * Assumptions:
130 * - psa_status_t status exists
131 * - An exit label is declared
David Horstmann926193a2023-12-13 15:55:25 +0000132 * - input is the name of a pointer to the buffer to be copied
David Horstmannb80e35a2023-12-14 18:12:47 +0000133 * - LOCAL_INPUT_DECLARE(input, input_copy) has previously been called
David Horstmann34980bd2023-11-29 17:07:13 +0000134 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000135#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
David Horstmann926193a2023-12-13 15:55:25 +0000136 status = psa_crypto_local_input_alloc(input, length, \
137 &LOCAL_INPUT_COPY_OF_##input); \
David Horstmann34980bd2023-11-29 17:07:13 +0000138 if (status != PSA_SUCCESS) { \
139 goto exit; \
140 } \
David Horstmannb80e35a2023-12-14 18:12:47 +0000141 input_copy = LOCAL_INPUT_COPY_OF_##input.buffer;
David Horstmann34980bd2023-11-29 17:07:13 +0000142
David Horstmann926193a2023-12-13 15:55:25 +0000143/* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC()
144 *
David Horstmann1f532132023-12-08 14:08:18 +0000145 * Assumptions:
David Horstmannb80e35a2023-12-14 18:12:47 +0000146 * - input_copy is the name of the input copy pointer set by LOCAL_INPUT_ALLOC()
David Horstmann926193a2023-12-13 15:55:25 +0000147 * - input is the name of the original buffer that was copied
David Horstmann1f532132023-12-08 14:08:18 +0000148 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000149#define LOCAL_INPUT_FREE(input, input_copy) \
150 input_copy = NULL; \
David Horstmann926193a2023-12-13 15:55:25 +0000151 psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input);
David Horstmann34980bd2023-11-29 17:07:13 +0000152
David Horstmannb80e35a2023-12-14 18:12:47 +0000153/* Declare a local copy of an output buffer and a variable that will be used
154 * to store a pointer to the start of the buffer.
David Horstmann926193a2023-12-13 15:55:25 +0000155 *
156 * Note: This macro must be called before any operations which may jump to
157 * the exit label, so that the local output copy object is safe to be freed.
158 *
159 * Assumptions:
160 * - output is the name of a pointer to the buffer to be copied
161 * - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope
David Horstmannb80e35a2023-12-14 18:12:47 +0000162 * - output_copy_name is a name that is unused in the current scope
David Horstmann926193a2023-12-13 15:55:25 +0000163 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000164#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
165 psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
166 uint8_t *output_copy_name = NULL;
David Horstmann926193a2023-12-13 15:55:25 +0000167
David Horstmannb80e35a2023-12-14 18:12:47 +0000168/* Allocate a copy of the buffer output and set the pointer output_copy to
169 * point to the start of the copy.
David Horstmann926193a2023-12-13 15:55:25 +0000170 *
David Horstmann34980bd2023-11-29 17:07:13 +0000171 * Assumptions:
172 * - psa_status_t status exists
173 * - An exit label is declared
David Horstmann926193a2023-12-13 15:55:25 +0000174 * - output is the name of a pointer to the buffer to be copied
David Horstmannb80e35a2023-12-14 18:12:47 +0000175 * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called
David Horstmann34980bd2023-11-29 17:07:13 +0000176 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000177#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
David Horstmann926193a2023-12-13 15:55:25 +0000178 status = psa_crypto_local_output_alloc(output, length, \
179 &LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmann34980bd2023-11-29 17:07:13 +0000180 if (status != PSA_SUCCESS) { \
181 goto exit; \
182 } \
David Horstmannb80e35a2023-12-14 18:12:47 +0000183 output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer;
David Horstmann34980bd2023-11-29 17:07:13 +0000184
David Horstmannb80e35a2023-12-14 18:12:47 +0000185/* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC()
David Horstmann1f532132023-12-08 14:08:18 +0000186 * after first copying back its contents to the original buffer.
David Horstmann926193a2023-12-13 15:55:25 +0000187 *
David Horstmann1f532132023-12-08 14:08:18 +0000188 * Assumptions:
David Horstmann1f532132023-12-08 14:08:18 +0000189 * - psa_status_t status exists
David Horstmannb80e35a2023-12-14 18:12:47 +0000190 * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC()
191 * - output is the name of the original buffer that was copied
David Horstmann1f532132023-12-08 14:08:18 +0000192 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000193#define LOCAL_OUTPUT_FREE(output, output_copy) \
194 output_copy = NULL; \
David Horstmann2b70a662023-12-13 14:09:08 +0000195 do { \
196 psa_status_t local_output_status; \
David Horstmann926193a2023-12-13 15:55:25 +0000197 local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmann2b70a662023-12-13 14:09:08 +0000198 if (local_output_status != PSA_SUCCESS) { \
199 /* Since this error case is an internal error, it's more serious than \
200 * any existing error code and so it's fine to overwrite the existing \
201 * status. */ \
202 status = local_output_status; \
203 } \
204 } while (0)
David Horstmann42015332024-03-13 15:42:31 +0000205#else /* !MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS */
David Horstmannb80e35a2023-12-14 18:12:47 +0000206#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
207 const uint8_t *input_copy_name = NULL;
208#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
209 input_copy = input;
210#define LOCAL_INPUT_FREE(input, input_copy) \
211 input_copy = NULL;
212#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
213 uint8_t *output_copy_name = NULL;
214#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
215 output_copy = output;
216#define LOCAL_OUTPUT_FREE(output, output_copy) \
217 output_copy = NULL;
David Horstmann42015332024-03-13 15:42:31 +0000218#endif /* !MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS */
David Horstmann34980bd2023-11-29 17:07:13 +0000219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100221{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100222 /* Mbed TLS error codes can combine a high-level error code and a
223 * low-level error code. The low-level error usually reflects the
224 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 int low_level_ret = -(-ret & 0x007f);
226 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100227 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
230 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
231 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
232 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100236
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200237 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200239
Gilles Peskine9a944802018-06-21 09:35:35 +0200240 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
241 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
242 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
243 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
244 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200246 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200248 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200250
Jaeden Amero93e21112019-02-20 13:57:28 +0000251#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000252 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000253#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
254 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
255#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100256 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100258 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100260
Jaeden Amero93e21112019-02-20 13:57:28 +0000261#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000262 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000263#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
264 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
265#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100268 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100270
271 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100273 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100275 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100277
Gilles Peskine26869f22019-05-06 15:25:00 +0200278 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200280
281 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200283 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200285
Gilles Peskinea5905292018-02-07 20:59:33 +0100286 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100290 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100300 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100302
303 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
307 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100308 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
309 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100312 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
313 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100317#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100318
319 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100321 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100323
Gilles Peskinee59236f2018-01-27 23:32:46 +0100324 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
325 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
326 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100328
329 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100331 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100333 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100335
Gilles Peskine82e57d12020-11-13 21:31:17 +0100336#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100338 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
339 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100340 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100342 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
343 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100345 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100347#endif
348
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200349 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
350 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
351 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200353
Gilles Peskinea5905292018-02-07 20:59:33 +0100354 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100356 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100358 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100360 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200362 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100364
Gilles Peskinef76aa772018-10-29 19:24:33 +0100365 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100367 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100369 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100371 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100373 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100375 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100377 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100379 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100381
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100382 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100384 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
385 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100387 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100389 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
390 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100392 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100394 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
395 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100396 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100397 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100399 case MBEDTLS_ERR_PK_INVALID_ALG:
400 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
401 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100403 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100405 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100407
Gilles Peskineff2d2002019-05-06 15:26:23 +0200408 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200410 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200412
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200413 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200415
Gilles Peskinea5905292018-02-07 20:59:33 +0100416 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100418 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100420 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100422 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100424 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
425 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100427 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100429 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100431 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100433 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100435 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100437
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200438 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
439 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
440 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200442
Gilles Peskinea5905292018-02-07 20:59:33 +0100443 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100445 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100447
itayzafrir5c753392018-05-08 11:18:38 +0300448 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300449 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300451 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300453 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300455 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
456 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300458 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100460 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300462 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100464
Janos Follatha13b9052019-11-22 12:48:59 +0000465 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100466 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300467
Gilles Peskinee59236f2018-01-27 23:32:46 +0100468 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100470 }
471}
472
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200473
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200474
475
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100476/****************************************************************/
477/* Key management */
478/****************************************************************/
479
John Durkop9814fa22020-11-04 12:28:15 -0800480#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800481 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100482 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
483 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
484 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
486 size_t bits,
487 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200488{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100490 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100492#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100493 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100494 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100495#endif
496#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100497 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100498 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100499#endif
500#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100501 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100503#endif
504#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100505 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100507#endif
508#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100509 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100511 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100512 if (bits_is_sloppy) {
513 return MBEDTLS_ECP_DP_SECP521R1;
514 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100515 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100516#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100517 }
518 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100519
Paul Elliott8ff510a2020-06-02 17:19:28 +0100520 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100521 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100522#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100523 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100525#endif
526#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100527 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100529#endif
530#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100531 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100533#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100534 }
535 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100536
Paul Elliott8ff510a2020-06-02 17:19:28 +0100537 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100539#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100540 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100542 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100543 if (bits_is_sloppy) {
544 return MBEDTLS_ECP_DP_CURVE25519;
545 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100546 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100547#endif
548#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100549 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100551#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100552 }
553 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100554
Paul Elliott8ff510a2020-06-02 17:19:28 +0100555 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100557#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100558 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100559 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100560#endif
561#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100562 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100564#endif
565#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100566 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100568#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100569 }
570 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200571 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100572
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100573 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200575}
John Durkop9814fa22020-11-04 12:28:15 -0800576#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100577 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
578 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
579 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
580 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
583 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200584{
585 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200587 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200588 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200589 case PSA_KEY_TYPE_DERIVE:
590 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100591#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200592 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 if (bits != 128 && bits != 192 && bits != 256) {
594 return PSA_ERROR_INVALID_ARGUMENT;
595 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200596 break;
597#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200598#if defined(PSA_WANT_KEY_TYPE_ARIA)
599 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100600 if (bits != 128 && bits != 192 && bits != 256) {
601 return PSA_ERROR_INVALID_ARGUMENT;
602 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200603 break;
604#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100605#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200606 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100607 if (bits != 128 && bits != 192 && bits != 256) {
608 return PSA_ERROR_INVALID_ARGUMENT;
609 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200610 break;
611#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100612#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200613 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100614 if (bits != 64 && bits != 128 && bits != 192) {
615 return PSA_ERROR_INVALID_ARGUMENT;
616 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200617 break;
618#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100619#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200620 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100621 if (bits < 8 || bits > 2048) {
622 return PSA_ERROR_INVALID_ARGUMENT;
623 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200624 break;
625#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100626#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200627 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100628 if (bits != 256) {
629 return PSA_ERROR_INVALID_ARGUMENT;
630 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200631 break;
632#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200633 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100634 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200635 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 if (bits % 8 != 0) {
637 return PSA_ERROR_INVALID_ARGUMENT;
638 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200641}
642
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100643/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100644 *
Steven Cooremancd640932021-03-08 12:00:27 +0100645 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
646 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100647 *
648 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100649 * \param[in] key_type The key type of the key to be used with the
650 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100651 *
652 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100653 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100654 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100655 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100656 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100657MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100658 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100660{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100661 if (PSA_ALG_IS_HMAC(algorithm)) {
662 if (key_type == PSA_KEY_TYPE_HMAC) {
663 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100664 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100665 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100667 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
668 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
669 * key. */
670 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
671 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
672 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
673 * the block length (larger than 1) for block ciphers. */
674 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
675 return PSA_SUCCESS;
676 }
677 }
678 }
679
680 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100681}
682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
684 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200685{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100686 if (slot->key.data != NULL) {
687 return PSA_ERROR_ALREADY_EXISTS;
688 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100690 slot->key.data = mbedtls_calloc(1, buffer_length);
691 if (slot->key.data == NULL) {
692 return PSA_ERROR_INSUFFICIENT_MEMORY;
693 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200694
Ronald Cronea0f8a62020-11-25 17:52:23 +0100695 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100696 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200697}
698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100699psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
700 const uint8_t *data,
701 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200702{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100703 psa_status_t status = psa_allocate_buffer_to_slot(slot,
704 data_length);
705 if (status != PSA_SUCCESS) {
706 return status;
707 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709 memcpy(slot->key.data, data, data_length);
710 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200711}
712
Ronald Crona0fe59f2020-11-29 11:14:53 +0100713psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100714 const psa_key_attributes_t *attributes,
715 const uint8_t *data, size_t data_length,
716 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100717 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100718{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100719 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
720 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100721
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200722 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100723 if (data_length == 0) {
724 return PSA_ERROR_NOT_SUPPORTED;
725 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100727 if (key_type_is_raw_bytes(type)) {
728 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200729
Steven Cooreman75b74362020-07-28 14:30:13 +0200730 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100731 if (data_length > SIZE_MAX / 8) {
732 return PSA_ERROR_NOT_SUPPORTED;
733 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200734
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200735 /* Enforce a size limit, and in particular ensure that the bit
736 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100737 if ((*bits) > PSA_MAX_KEY_BITS) {
738 return PSA_ERROR_NOT_SUPPORTED;
739 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200740
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100741 status = validate_unstructured_key_bit_size(type, *bits);
742 if (status != PSA_SUCCESS) {
743 return status;
744 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200745
Ronald Crondd04d422020-11-28 15:14:42 +0100746 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100747 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100748 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100749 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100751 return PSA_SUCCESS;
752 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800753#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100754 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
755 if (PSA_KEY_TYPE_IS_ECC(type)) {
756 return mbedtls_psa_ecp_import_key(attributes,
757 data, data_length,
758 key_buffer, key_buffer_size,
759 key_buffer_length,
760 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100761 }
John Durkop9814fa22020-11-04 12:28:15 -0800762#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
763 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700764#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100765 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
766 if (PSA_KEY_TYPE_IS_RSA(type)) {
767 return mbedtls_psa_rsa_import_key(attributes,
768 data, data_length,
769 key_buffer, key_buffer_size,
770 key_buffer_length,
771 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200772 }
John Durkop9814fa22020-11-04 12:28:15 -0800773#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
774 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100775 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100777 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100778}
779
Gilles Peskinef603c712019-01-19 13:40:11 +0100780/** Calculate the intersection of two algorithm usage policies.
781 *
782 * Return 0 (which allows no operation) on incompatibility.
783 */
784static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100785 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100786 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100787 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100788{
Gilles Peskine549ea862019-05-22 11:45:59 +0200789 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790 if (alg1 == alg2) {
791 return alg1;
792 }
Steven Cooreman03488022021-02-18 12:07:20 +0100793 /* If the policies are from the same hash-and-sign family, check
794 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100795 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
796 PSA_ALG_IS_SIGN_HASH(alg2) &&
797 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
798 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
799 return alg2;
800 }
801 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
802 return alg1;
803 }
Steven Cooreman03488022021-02-18 12:07:20 +0100804 }
805 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100806 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100807 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100808 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
809 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
810 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
811 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
812 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100813 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100814
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100815 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100816 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
817 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
818 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
819 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100820 }
821 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100822 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
823 (alg1_len <= alg2_len)) {
824 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100825 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100826 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
827 (alg2_len <= alg1_len)) {
828 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100829 }
830 }
831 /* If the policies are from the same MAC family, check whether one
832 * of them is a minimum-MAC-length policy. Calculate the most
833 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100834 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
835 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
836 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100837 /* Validate the combination of key type and algorithm. Since the base
838 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
840 return 0;
841 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100842
Steven Cooreman31a876d2021-03-03 20:47:40 +0100843 /* Get the (exact or at-least) output lengths for both sides of the
844 * requested intersection. None of the currently supported algorithms
845 * have an output length dependent on the actual key size, so setting it
846 * to a bogus value of 0 is currently OK.
847 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100848 * Note that for at-least-this-length wildcard algorithms, the output
849 * length is set to the shortest allowed length, which allows us to
850 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100851 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
852 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100853 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100854
Steven Cooremana1d83222021-02-25 10:20:29 +0100855 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100856 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
857 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
858 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100859 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100860
861 /* If only one is an at-least-this-length policy, the intersection would
862 * be the other (fixed-length) policy as long as said fixed length is
863 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100864 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
865 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100866 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100867 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
868 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100869 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100870
Steven Cooremancd640932021-03-08 12:00:27 +0100871 /* If none of them are wildcards, check whether they define the same tag
872 * length. This is still possible here when one is default-length and
873 * the other specific-length. Ensure to always return the
874 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100875 if (alg1_len == alg2_len) {
876 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
877 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100878 }
879 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100880 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100881}
882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100883static int psa_key_algorithm_permits(psa_key_type_t key_type,
884 psa_algorithm_t policy_alg,
885 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200886{
Gilles Peskine549ea862019-05-22 11:45:59 +0200887 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100888 if (requested_alg == policy_alg) {
889 return 1;
890 }
Steven Cooreman03488022021-02-18 12:07:20 +0100891 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
892 * and requested_alg is the same hash-and-sign family with any hash,
893 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
895 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
896 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
897 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100898 }
899 /* If policy_alg is a wildcard AEAD algorithm of the same base as
900 * the requested algorithm, check the requested tag length to be
901 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100902 if (PSA_ALG_IS_AEAD(policy_alg) &&
903 PSA_ALG_IS_AEAD(requested_alg) &&
904 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
905 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
906 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
907 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
908 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100909 }
Steven Cooremancd640932021-03-08 12:00:27 +0100910 /* If policy_alg is a MAC algorithm of the same base as the requested
911 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100912 if (PSA_ALG_IS_MAC(policy_alg) &&
913 PSA_ALG_IS_MAC(requested_alg) &&
914 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
915 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100916 /* Validate the combination of key type and algorithm. Since the policy
917 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100918 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
919 return 0;
920 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100921
Steven Cooreman31a876d2021-03-03 20:47:40 +0100922 /* Get both the requested output length for the algorithm which is to be
923 * verified, and the default output length for the base algorithm.
924 * Note that none of the currently supported algorithms have an output
925 * length dependent on actual key size, so setting it to a bogus value
926 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100927 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100928 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100929 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 key_type, 0,
931 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100932
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100933 /* If the policy is default-length, only allow an algorithm with
934 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100935 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
936 return requested_output_length == default_output_length;
937 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100938
939 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100940 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100941 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
942 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
943 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100944 }
945
Steven Cooremancd640932021-03-08 12:00:27 +0100946 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
947 * check for the requested MAC length to be equal to or longer than the
948 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100949 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
950 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
951 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100952 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200953 }
Steven Cooremance48e852020-10-05 16:02:45 +0200954 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200955 * a key derivation with that key agreement should also be allowed. This
956 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100957 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
958 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
959 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
960 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200961 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100962 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100963 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200964}
965
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100966/** Test whether a policy permits an algorithm.
967 *
968 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100969 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100970 * \note This function requires providing the key type for which the policy is
971 * being validated, since some algorithm policy definitions (e.g. MAC)
972 * have different properties depending on what kind of cipher it is
973 * combined with.
974 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100975 * \retval PSA_SUCCESS When \p alg is a specific algorithm
976 * allowed by the \p policy.
977 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
978 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
979 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100980 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100981static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
982 psa_key_type_t key_type,
983 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100984{
Steven Cooremand788fab2021-02-25 11:29:17 +0100985 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100986 if (alg == 0) {
987 return PSA_ERROR_INVALID_ARGUMENT;
988 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100989
Steven Cooreman7e39f052021-02-23 14:18:32 +0100990 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100991 if (PSA_ALG_IS_WILDCARD(alg)) {
992 return PSA_ERROR_INVALID_ARGUMENT;
993 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100995 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
996 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
997 return PSA_SUCCESS;
998 } else {
999 return PSA_ERROR_NOT_PERMITTED;
1000 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001001}
1002
Gilles Peskinef603c712019-01-19 13:40:11 +01001003/** Restrict a key policy based on a constraint.
1004 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001005 * \note This function requires providing the key type for which the policy is
1006 * being restricted, since some algorithm policy definitions (e.g. MAC)
1007 * have different properties depending on what kind of cipher it is
1008 * combined with.
1009 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001010 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001011 * \param[in,out] policy The policy to restrict.
1012 * \param[in] constraint The policy constraint to apply.
1013 *
1014 * \retval #PSA_SUCCESS
1015 * \c *policy contains the intersection of the original value of
1016 * \c *policy and \c *constraint.
1017 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001018 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001019 * \c *policy is unchanged.
1020 */
1021static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001022 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001023 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001024 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001025{
1026 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001027 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1028 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001029 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001030 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1031 constraint->alg2);
1032 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1033 return PSA_ERROR_INVALID_ARGUMENT;
1034 }
1035 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1036 return PSA_ERROR_INVALID_ARGUMENT;
1037 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001038 policy->usage &= constraint->usage;
1039 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001040 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001041 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001042}
1043
Ronald Cron5c522922020-11-14 16:35:34 +01001044/** Get the description of a key given its identifier and policy constraints
1045 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001046 *
Ronald Cron5c522922020-11-14 16:35:34 +01001047 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +01001048 * nonzero, the key must allow operations with this algorithm. If \p alg is
1049 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +01001050 *
Ronald Cron5c522922020-11-14 16:35:34 +01001051 * In case of a persistent key, the function loads the description of the key
1052 * into a key slot if not already done.
1053 *
1054 * On success, the returned key slot is locked. It is the responsibility of
1055 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001056 */
Ronald Cron5c522922020-11-14 16:35:34 +01001057static psa_status_t psa_get_and_lock_key_slot_with_policy(
1058 mbedtls_svc_key_id_t key,
1059 psa_key_slot_t **p_slot,
1060 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001061 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001062{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001063 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1064 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001066 status = psa_get_and_lock_key_slot(key, p_slot);
1067 if (status != PSA_SUCCESS) {
1068 return status;
1069 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001070 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001071
1072 /* Enforce that usage policy for the key slot contains all the flags
1073 * required by the usage parameter. There is one exception: public
1074 * keys can always be exported, so we treat public key objects as
1075 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001076 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001077 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001078 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001080 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001081 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001082 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001083 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001084
Shaun Case0e7791f2021-12-20 21:14:10 -08001085 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001086 if (alg != 0) {
1087 status = psa_key_policy_permits(&slot->attr.policy,
1088 slot->attr.type,
1089 alg);
1090 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001091 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001092 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001093 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001095 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001096
1097error:
1098 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001099 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001101 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001102}
Darryl Green940d72c2018-07-13 13:18:51 +01001103
Ronald Cron5c522922020-11-14 16:35:34 +01001104/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001105 *
1106 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001107 * available, as opposed to a key in a secure element and/or to be used
1108 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001109 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001110 * This is a temporary function that may be used instead of
1111 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1112 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001113 *
Ronald Cron5c522922020-11-14 16:35:34 +01001114 * On success, the returned key slot is locked. It is the responsibility of the
1115 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001116 */
Ronald Cron5c522922020-11-14 16:35:34 +01001117static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1118 mbedtls_svc_key_id_t key,
1119 psa_key_slot_t **p_slot,
1120 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001121 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001122{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001123 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1124 usage, alg);
1125 if (status != PSA_SUCCESS) {
1126 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001127 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001129 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1130 psa_unlock_key_slot(*p_slot);
1131 *p_slot = NULL;
1132 return PSA_ERROR_NOT_SUPPORTED;
1133 }
1134
1135 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001136}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001138psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001139{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001140 /* Data pointer will always be either a valid pointer or NULL in an
1141 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001142 if (slot->key.data != NULL) {
1143 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1144 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001146 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001147 slot->key.data = NULL;
1148 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001150 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001151}
1152
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001153/** Completely wipe a slot in memory, including its policy.
1154 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001155psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001156{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001157 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001158
1159 /*
1160 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001161 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001162 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1163 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001164 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001165 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001166 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001167#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001168 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001169#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001170 status = PSA_ERROR_CORRUPTION_DETECTED;
1171 }
1172
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001173 /* Multipart operations may still be using the key. This is safe
1174 * because all multipart operation objects are independent from
1175 * the key slot: if they need to access the key after the setup
1176 * phase, they have a copy of the key. Note that this means that
1177 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001178 /* At this point, key material and other type-specific content has
1179 * been wiped. Clear remaining metadata. We can call memset and not
1180 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001181 memset(slot, 0, sizeof(*slot));
1182 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001183}
1184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001185psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001186{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001187 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001188 psa_status_t status; /* status of the last operation */
1189 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001190#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1191 psa_se_drv_table_entry_t *driver;
1192#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001194 if (mbedtls_svc_key_id_is_null(key)) {
1195 return PSA_SUCCESS;
1196 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001197
Ronald Cronf2911112020-10-29 17:51:10 +01001198 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001199 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001200 * key, this will load the key description from persistent memory if not
1201 * done yet. We cannot avoid this loading as without it we don't know if
1202 * the key is operated by an SE or not and this information is needed by
1203 * the current implementation.
1204 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001205 status = psa_get_and_lock_key_slot(key, &slot);
1206 if (status != PSA_SUCCESS) {
1207 return status;
1208 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001209
Ronald Cronf2911112020-10-29 17:51:10 +01001210 /*
1211 * If the key slot containing the key description is under access by the
1212 * library (apart from the present access), the key cannot be destroyed
1213 * yet. For the time being, just return in error. Eventually (to be
1214 * implemented), the key should be destroyed when all accesses have
1215 * stopped.
1216 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001217 if (slot->lock_count > 1) {
1218 psa_unlock_key_slot(slot);
1219 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001220 }
1221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001222 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001223 /* Refuse the destruction of a read-only key (which may or may not work
1224 * if we attempt it, depending on whether the key is merely read-only
1225 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001226 * Just do the best we can, which is to wipe the copy in memory
1227 * (done in this function's cleanup code). */
1228 overall_status = PSA_ERROR_NOT_PERMITTED;
1229 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001230 }
1231
Gilles Peskine354f7672019-07-12 23:46:38 +02001232#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001233 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1234 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001235 /* For a key in a secure element, we need to do three things:
1236 * remove the key file in internal storage, destroy the
1237 * key inside the secure element, and update the driver's
1238 * persistent data. Start a transaction that will encompass these
1239 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001240 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001241 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001242 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001243 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001244 status = psa_crypto_save_transaction();
1245 if (status != PSA_SUCCESS) {
1246 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001247 /* We should still try to destroy the key in the secure
1248 * element and the key metadata in storage. This is especially
1249 * important if the error is that the storage is full.
1250 * But how to do it exactly without risking an inconsistent
1251 * state after a reset?
1252 * https://github.com/ARMmbed/mbed-crypto/issues/215
1253 */
1254 overall_status = status;
1255 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001256 }
1257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001258 status = psa_destroy_se_key(driver,
1259 psa_key_slot_get_slot_number(slot));
1260 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001261 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001262 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001263 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001264#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1265
Darryl Greend49a4992018-06-18 17:27:26 +01001266#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001267 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1268 status = psa_destroy_persistent_key(slot->attr.id);
1269 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001270 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001271 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001272
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001273 /* TODO: other slots may have a copy of the same key. We should
1274 * invalidate them.
1275 * https://github.com/ARMmbed/mbed-crypto/issues/214
1276 */
Darryl Greend49a4992018-06-18 17:27:26 +01001277 }
1278#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001279
Gilles Peskinefc762652019-07-22 19:30:34 +02001280#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001281 if (driver != NULL) {
1282 status = psa_save_se_persistent_data(driver);
1283 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001284 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001285 }
1286 status = psa_crypto_stop_transaction();
1287 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001288 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001289 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001290 }
1291#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1292
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001293exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001294 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001295 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001296 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001297 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001298 }
1299 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001300}
1301
John Durkop07cc04a2020-11-16 22:08:34 -08001302#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001303 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001304static psa_status_t psa_get_rsa_public_exponent(
1305 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001306 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001307{
1308 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001310 uint8_t *buffer = NULL;
1311 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001312 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001314 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1315 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001316 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001317 }
1318 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001319 /* It's the default value, which is reported as an empty string,
1320 * so there's nothing to do. */
1321 goto exit;
1322 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001324 buflen = mbedtls_mpi_size(&mpi);
1325 buffer = mbedtls_calloc(1, buflen);
1326 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001327 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1328 goto exit;
1329 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001330 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1331 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001332 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001333 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001334 attributes->domain_parameters = buffer;
1335 attributes->domain_parameters_size = buflen;
1336
1337exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001338 mbedtls_mpi_free(&mpi);
1339 if (ret != 0) {
1340 mbedtls_free(buffer);
1341 }
1342 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001343}
John Durkop07cc04a2020-11-16 22:08:34 -08001344#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001345 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001346
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001347/** Retrieve all the publicly-accessible attributes of a key.
1348 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001349psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1350 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001351{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001352 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001353 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001354 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001356 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001358 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1359 if (status != PSA_SUCCESS) {
1360 return status;
1361 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001362
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001363 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001364 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1365 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001366
1367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001368 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1369 psa_set_key_slot_number(attributes,
1370 psa_key_slot_get_slot_number(slot));
1371 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001374 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001375#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001376 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001377 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001378 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001379 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001380 * is not yet implemented.
1381 * https://github.com/ARMmbed/mbed-crypto/issues/216
1382 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001383 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001384 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001385
Ronald Cron90857082020-11-25 14:58:33 +01001386 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001387 slot->attr.type,
1388 slot->key.data,
1389 slot->key.bytes,
1390 &rsa);
1391 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001392 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001393 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001395 status = psa_get_rsa_public_exponent(rsa,
1396 attributes);
1397 mbedtls_rsa_free(rsa);
1398 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001399 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001400 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001401#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001402 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001403 default:
1404 /* Nothing else to do. */
1405 break;
1406 }
1407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001408 if (status != PSA_SUCCESS) {
1409 psa_reset_key_attributes(attributes);
1410 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001412 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001414 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001415}
1416
Gilles Peskinec8000c02019-08-02 20:15:51 +02001417#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1418psa_status_t psa_get_key_slot_number(
1419 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001420 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001421{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001422 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001423 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001424 return PSA_SUCCESS;
1425 } else {
1426 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001427 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001428}
1429#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001431static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1432 size_t key_buffer_size,
1433 uint8_t *data,
1434 size_t data_size,
1435 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001436{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001437 if (key_buffer_size > data_size) {
1438 return PSA_ERROR_BUFFER_TOO_SMALL;
1439 }
1440 memcpy(data, key_buffer, key_buffer_size);
1441 memset(data + key_buffer_size, 0,
1442 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001443 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001444 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001445}
1446
Ronald Cron7285cda2020-11-26 14:46:37 +01001447psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001448 const psa_key_attributes_t *attributes,
1449 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001450 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001451{
Ronald Crond18b5f82020-11-25 16:46:05 +01001452 psa_key_type_t type = attributes->core.type;
1453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001454 if (key_type_is_raw_bytes(type) ||
1455 PSA_KEY_TYPE_IS_RSA(type) ||
1456 PSA_KEY_TYPE_IS_ECC(type)) {
1457 return psa_export_key_buffer_internal(
1458 key_buffer, key_buffer_size,
1459 data, data_size, data_length);
1460 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001461 /* This shouldn't happen in the reference implementation, but
1462 it is valid for a special-purpose implementation to omit
1463 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001464 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001465 }
1466}
1467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001468psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Ryan Everette3e760c2024-01-19 16:03:50 +00001469 uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001470 size_t data_size,
1471 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001472{
1473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1474 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1475 psa_key_slot_t *slot;
Ryan Everette3e760c2024-01-19 16:03:50 +00001476 LOCAL_OUTPUT_DECLARE(data_external, data);
Ronald Crone907e552021-01-18 13:22:38 +01001477 /* Reject a zero-length output buffer now, since this can never be a
1478 * valid key representation. This way we know that data must be a valid
1479 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001480 if (data_size == 0) {
1481 return PSA_ERROR_BUFFER_TOO_SMALL;
1482 }
Ronald Crone907e552021-01-18 13:22:38 +01001483
Ronald Cron9486f9d2020-11-26 13:36:23 +01001484 /* Set the key to empty now, so that even when there are errors, we always
1485 * set data_length to a value between 0 and data_size. On error, setting
1486 * the key to empty is a good choice because an empty key representation is
1487 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001488 *data_length = 0;
1489
Ronald Cron9486f9d2020-11-26 13:36:23 +01001490 /* Export requires the EXPORT flag. There is an exception for public keys,
1491 * which don't require any flag, but
1492 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1493 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001494 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1495 PSA_KEY_USAGE_EXPORT, 0);
1496 if (status != PSA_SUCCESS) {
1497 return status;
1498 }
mohammad160306e79202018-03-28 13:17:44 +03001499
Ryan Everette3e760c2024-01-19 16:03:50 +00001500 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1501
Ronald Crond18b5f82020-11-25 16:46:05 +01001502 psa_key_attributes_t attributes = {
1503 .core = slot->attr
1504 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001505 status = psa_driver_wrapper_export_key(&attributes,
1506 slot->key.data, slot->key.bytes,
1507 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001508
David Horstmann42015332024-03-13 15:42:31 +00001509#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everette3e760c2024-01-19 16:03:50 +00001510exit:
Ryan Everett3a4153a2024-01-25 12:04:55 +00001511#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001512 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001513
Ryan Everette3e760c2024-01-19 16:03:50 +00001514 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001515 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001516}
1517
Ronald Cron7285cda2020-11-26 14:46:37 +01001518psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001519 const psa_key_attributes_t *attributes,
1520 const uint8_t *key_buffer,
1521 size_t key_buffer_size,
1522 uint8_t *data,
1523 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001524 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001525{
Ronald Crond18b5f82020-11-25 16:46:05 +01001526 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001528 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1529 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001530 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001531 return psa_export_key_buffer_internal(
1532 key_buffer, key_buffer_size,
1533 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001534 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001536 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001537#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001538 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1539 return mbedtls_psa_rsa_export_public_key(attributes,
1540 key_buffer,
1541 key_buffer_size,
1542 data,
1543 data_size,
1544 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001545#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001546 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001547 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001548#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1549 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001550 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001551#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001552 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1553 return mbedtls_psa_ecp_export_public_key(attributes,
1554 key_buffer,
1555 key_buffer_size,
1556 data,
1557 data_size,
1558 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001559#else
1560 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001561 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001562#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1563 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001564 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001565 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001566 /* This shouldn't happen in the reference implementation, but
1567 it is valid for a special-purpose implementation to omit
1568 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001569 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001570 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001571}
Ronald Crond18b5f82020-11-25 16:46:05 +01001572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001573psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Ryan Everett30827912024-01-19 16:05:00 +00001574 uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001575 size_t data_size,
1576 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001577{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001578 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001579 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001580 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001581 psa_key_slot_t *slot;
Ryan Everett30827912024-01-19 16:05:00 +00001582 LOCAL_OUTPUT_DECLARE(data_external, data);
Darryl Greendd8fb772018-11-07 16:00:44 +00001583
Ronald Crone907e552021-01-18 13:22:38 +01001584 /* Reject a zero-length output buffer now, since this can never be a
1585 * valid key representation. This way we know that data must be a valid
1586 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001587 if (data_size == 0) {
1588 return PSA_ERROR_BUFFER_TOO_SMALL;
1589 }
Ronald Crone907e552021-01-18 13:22:38 +01001590
Darryl Greendd8fb772018-11-07 16:00:44 +00001591 /* Set the key to empty now, so that even when there are errors, we always
1592 * set data_length to a value between 0 and data_size. On error, setting
1593 * the key to empty is a good choice because an empty key representation is
1594 * unlikely to be accepted anywhere. */
1595 *data_length = 0;
1596
1597 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001598 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1599 if (status != PSA_SUCCESS) {
1600 return status;
1601 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001602
Ryan Everett30827912024-01-19 16:05:00 +00001603 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001605 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1606 status = PSA_ERROR_INVALID_ARGUMENT;
1607 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001608 }
1609
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001610 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001611 .core = slot->attr
1612 };
Ronald Cron67227982020-11-26 15:16:05 +01001613 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001614 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001615 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001616
1617exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001618 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001619
Ryan Everett30827912024-01-19 16:05:00 +00001620 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001621 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001622}
1623
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001624MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
Gilles Peskinef555a4e2024-06-12 19:13:19 +02001625 "One or more key attribute flag is listed as both external-only and dual-use");
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001626MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
Gilles Peskinef555a4e2024-06-12 19:13:19 +02001627 "One or more key attribute flag is listed as both internal-only and dual-use");
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001628MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
Gilles Peskinef555a4e2024-06-12 19:13:19 +02001629 "One or more key attribute flag is listed as both internal-only and external-only");
Gilles Peskine91e8c332019-08-02 19:19:39 +02001630
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001631/** Validate that a key policy is internally well-formed.
1632 *
1633 * This function only rejects invalid policies. It does not validate the
1634 * consistency of the policy with respect to other attributes of the key
1635 * such as the key type.
1636 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001637static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001638{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001639 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1640 PSA_KEY_USAGE_COPY |
1641 PSA_KEY_USAGE_ENCRYPT |
1642 PSA_KEY_USAGE_DECRYPT |
1643 PSA_KEY_USAGE_SIGN_MESSAGE |
1644 PSA_KEY_USAGE_VERIFY_MESSAGE |
1645 PSA_KEY_USAGE_SIGN_HASH |
1646 PSA_KEY_USAGE_VERIFY_HASH |
1647 PSA_KEY_USAGE_DERIVE)) != 0) {
1648 return PSA_ERROR_INVALID_ARGUMENT;
1649 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001651 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001652}
1653
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001654/** Validate the internal consistency of key attributes.
1655 *
1656 * This function only rejects invalid attribute values. If does not
1657 * validate the consistency of the attributes with any key data that may
1658 * be involved in the creation of the key.
1659 *
1660 * Call this function early in the key creation process.
1661 *
1662 * \param[in] attributes Key attributes for the new key.
1663 * \param[out] p_drv On any return, the driver for the key, if any.
1664 * NULL for a transparent key.
1665 *
1666 */
1667static psa_status_t psa_validate_key_attributes(
1668 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001669 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001670{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001671 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001672 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1673 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001675 status = psa_validate_key_location(lifetime, p_drv);
1676 if (status != PSA_SUCCESS) {
1677 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001678 }
1679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001680 status = psa_validate_key_persistence(lifetime);
1681 if (status != PSA_SUCCESS) {
1682 return status;
1683 }
1684
1685 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1686 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1687 return PSA_ERROR_INVALID_ARGUMENT;
1688 }
1689 } else {
1690 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1691 return PSA_ERROR_INVALID_ARGUMENT;
1692 }
1693 }
1694
1695 status = psa_validate_key_policy(&attributes->core.policy);
1696 if (status != PSA_SUCCESS) {
1697 return status;
1698 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001699
1700 /* Refuse to create overly large keys.
1701 * Note that this doesn't trigger on import if the attributes don't
1702 * explicitly specify a size (so psa_get_key_bits returns 0), so
1703 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001704 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1705 return PSA_ERROR_NOT_SUPPORTED;
1706 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001707
Gilles Peskine91e8c332019-08-02 19:19:39 +02001708 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001709 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1710 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1711 return PSA_ERROR_INVALID_ARGUMENT;
1712 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001714 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001715}
1716
Gilles Peskine4747d192019-04-17 15:05:45 +02001717/** Prepare a key slot to receive key material.
1718 *
1719 * This function allocates a key slot and sets its metadata.
1720 *
1721 * If this function fails, call psa_fail_key_creation().
1722 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001723 * This function is intended to be used as follows:
1724 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001725 * it with the specified attributes, and in case of a volatile key assign it
1726 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001727 * -# Populate the slot with the key material.
1728 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1729 * In case of failure at any step, stop the sequence and call
1730 * psa_fail_key_creation().
1731 *
Ronald Cron5c522922020-11-14 16:35:34 +01001732 * On success, the key slot is locked. It is the responsibility of the caller
1733 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001734 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001735 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001736 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001737 * \param[out] p_slot On success, a pointer to the prepared slot.
1738 * \param[out] p_drv On any return, the driver for the key, if any.
1739 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001740 *
1741 * \retval #PSA_SUCCESS
1742 * The key slot is ready to receive key material.
1743 * \return If this function fails, the key slot is an invalid state.
1744 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001745 */
1746static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001747 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001748 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001749 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001750 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001751{
1752 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001753 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001754 psa_key_slot_t *slot;
1755
Gilles Peskinedf179142019-07-15 22:02:14 +02001756 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001757 *p_drv = NULL;
1758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001759 status = psa_validate_key_attributes(attributes, p_drv);
1760 if (status != PSA_SUCCESS) {
1761 return status;
1762 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001764 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1765 if (status != PSA_SUCCESS) {
1766 return status;
1767 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001768 slot = *p_slot;
1769
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001770 /* We're storing the declared bit-size of the key. It's up to each
1771 * creation mechanism to verify that this information is correct.
1772 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001773 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001774 * is optional (import, copy). In case of a volatile key, assign it the
1775 * volatile key identifier associated to the slot returned to contain its
1776 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001777
1778 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001779 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001780#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1781 slot->attr.id = volatile_key_id;
1782#else
1783 slot->attr.id.key_id = volatile_key_id;
1784#endif
1785 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001786
Gilles Peskine91e8c332019-08-02 19:19:39 +02001787 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001788 * external-only flags, query `attributes`. Thanks to the check
1789 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001790 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001791 * may have set. */
1792 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001793
Gilles Peskinecbaff462019-07-12 23:46:04 +02001794#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001795 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001796 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001797 * create the key file in internal storage, create the
1798 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001799 * persistent data. This is done by starting a transaction that will
1800 * encompass these three actions.
1801 * For registering a volatile key, we just need to find an appropriate
1802 * slot number inside the SE. Since the key is designated volatile, creating
1803 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001804 /* The first thing to do is to find a slot number for the new key.
1805 * We save the slot number in persistent storage as part of the
1806 * transaction data. It will be needed to recover if the power
1807 * fails during the key creation process, to clean up on the secure
1808 * element side after restarting. Obtaining a slot number from the
1809 * secure element driver updates its persistent state, but we do not yet
1810 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001811 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001812 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001813 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001814 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1815 &slot_number);
1816 if (status != PSA_SUCCESS) {
1817 return status;
1818 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001820 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1821 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001822 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001823 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001824 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001825 status = psa_crypto_save_transaction();
1826 if (status != PSA_SUCCESS) {
1827 (void) psa_crypto_stop_transaction();
1828 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001829 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001830 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001831
1832 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001833 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Ryan Everett68a85e22023-11-16 16:44:13 +00001834 if (status != PSA_SUCCESS) {
Ryan Everett167ecba2023-11-20 12:16:18 +00001835 return status;
Ryan Everett68a85e22023-11-16 16:44:13 +00001836 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001837 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001839 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001840 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001841 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001842 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001843#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001845 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001846}
Gilles Peskine4747d192019-04-17 15:05:45 +02001847
1848/** Finalize the creation of a key once its key material has been set.
1849 *
1850 * This entails writing the key to persistent storage.
1851 *
1852 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001853 * See the documentation of psa_start_key_creation() for the intended use
1854 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001855 *
Ronald Cron5c522922020-11-14 16:35:34 +01001856 * If the finalization succeeds, the function unlocks the key slot (it was
1857 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1858 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001859 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001860 * \param[in,out] slot Pointer to the slot with key material.
1861 * \param[in] driver The secure element driver for the key,
1862 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001863 * \param[out] key On success, identifier of the key. Note that the
1864 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001865 *
1866 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001867 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001868 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1869 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1870 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1871 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1872 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1873 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001874 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001875 * \return If this function fails, the key slot is an invalid state.
1876 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001877 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001878static psa_status_t psa_finish_key_creation(
1879 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001880 psa_se_drv_table_entry_t *driver,
1881 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001882{
1883 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001884 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001885 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001886
1887#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001888 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001889#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001890 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001891 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001892 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001893 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001894
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001895 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1896 sizeof(data.slot_number),
1897 "Slot number size does not match psa_se_key_data_storage_t");
1898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001899 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1900 status = psa_save_persistent_key(&slot->attr,
1901 (uint8_t *) &data,
1902 sizeof(data));
1903 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001904#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1905 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001906 /* Key material is saved in export representation in the slot, so
1907 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001908 status = psa_save_persistent_key(&slot->attr,
1909 slot->key.data,
1910 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001911 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001912 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001913#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1914
Gilles Peskinecbaff462019-07-12 23:46:04 +02001915#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001916 /* Finish the transaction for a key creation. This does not
1917 * happen when registering an existing key. Detect this case
1918 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001919 * creation of a persistent key in a secure element requires a transaction,
1920 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001921 if (driver != NULL &&
1922 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1923 status = psa_save_se_persistent_data(driver);
1924 if (status != PSA_SUCCESS) {
1925 psa_destroy_persistent_key(slot->attr.id);
1926 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001927 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001928 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001929 }
1930#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001932 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001933 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001934 status = psa_unlock_key_slot(slot);
1935 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001936 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001937 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001938 }
Ronald Cron50972942020-11-14 11:28:25 +01001939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001940 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001941}
1942
1943/** Abort the creation of a key.
1944 *
1945 * You may call this function after calling psa_start_key_creation(),
1946 * or after psa_finish_key_creation() fails. In other circumstances, this
1947 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001948 * See the documentation of psa_start_key_creation() for the intended use
1949 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001950 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001951 * \param[in,out] slot Pointer to the slot with key material.
1952 * \param[in] driver The secure element driver for the key,
1953 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001954 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001955static void psa_fail_key_creation(psa_key_slot_t *slot,
1956 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001957{
Gilles Peskine011e4282019-06-26 18:34:38 +02001958 (void) driver;
1959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001960 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001961 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001962 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001963
1964#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001965 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001966 * element, and the failure happened later (when saving metadata
1967 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001968 * element.
1969 * https://github.com/ARMmbed/mbed-crypto/issues/217
1970 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001971
Gilles Peskined7729582019-08-05 15:55:54 +02001972 /* Abort the ongoing transaction if any (there may not be one if
1973 * the creation process failed before starting one, or if the
1974 * key creation is a registration of a key in a secure element).
1975 * Earlier functions must already have done what it takes to undo any
1976 * partial creation. All that's left is to update the transaction data
1977 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001978 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001979#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001981 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001982}
1983
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001984/** Validate optional attributes during key creation.
1985 *
1986 * Some key attributes are optional during key creation. If they are
1987 * specified in the attributes structure, check that they are consistent
1988 * with the data in the slot.
1989 *
1990 * This function should be called near the end of key creation, after
1991 * the slot in memory is fully populated but before saving persistent data.
1992 */
1993static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001994 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001995 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001996{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001997 if (attributes->core.type != 0) {
1998 if (attributes->core.type != slot->attr.type) {
1999 return PSA_ERROR_INVALID_ARGUMENT;
2000 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002001 }
2002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002003 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07002004#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002005 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2006 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002007 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002008 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002009 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002010
Ronald Cron90857082020-11-25 14:58:33 +01002011 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002012 slot->attr.type,
2013 slot->key.data,
2014 slot->key.bytes,
2015 &rsa);
2016 if (status != PSA_SUCCESS) {
2017 return status;
2018 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002020 mbedtls_mpi_init(&actual);
2021 mbedtls_mpi_init(&required);
2022 ret = mbedtls_rsa_export(rsa,
2023 NULL, NULL, NULL, NULL, &actual);
2024 mbedtls_rsa_free(rsa);
2025 mbedtls_free(rsa);
2026 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002027 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028 }
2029 ret = mbedtls_mpi_read_binary(&required,
2030 attributes->domain_parameters,
2031 attributes->domain_parameters_size);
2032 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002033 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002034 }
2035 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002036 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002037 }
2038rsa_exit:
2039 mbedtls_mpi_free(&actual);
2040 mbedtls_mpi_free(&required);
2041 if (ret != 0) {
2042 return mbedtls_to_psa_error(ret);
2043 }
2044 } else
John Durkop9814fa22020-11-04 12:28:15 -08002045#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2046 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002047 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002048 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002049 }
2050 }
2051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002052 if (attributes->core.bits != 0) {
2053 if (attributes->core.bits != slot->attr.bits) {
2054 return PSA_ERROR_INVALID_ARGUMENT;
2055 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002056 }
2057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002058 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002059}
2060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002061psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Ryan Everett6b970252024-01-19 16:02:59 +00002062 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002063 size_t data_length,
2064 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002065{
2066 psa_status_t status;
Ryan Everett6b970252024-01-19 16:02:59 +00002067 LOCAL_INPUT_DECLARE(data_external, data);
Gilles Peskine4747d192019-04-17 15:05:45 +02002068 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002069 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002070 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002071
Ronald Cron81709fc2020-11-14 12:10:32 +01002072 *key = MBEDTLS_SVC_KEY_ID_INIT;
2073
Gilles Peskine0f84d622019-09-12 19:03:13 +02002074 /* Reject zero-length symmetric keys (including raw data key objects).
2075 * This also rejects any key which might be encoded as an empty string,
2076 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002077 if (data_length == 0) {
2078 return PSA_ERROR_INVALID_ARGUMENT;
2079 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002080
Ryan Everett6b970252024-01-19 16:02:59 +00002081 LOCAL_INPUT_ALLOC(data_external, data_length, data);
2082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002083 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2084 &slot, &driver);
2085 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002086 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002087 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002088
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002089 /* In the case of a transparent key or an opaque key stored in local
2090 * storage (thus not in the case of generating a key in a secure element
2091 * or cryptoprocessor with storage), we have to allocate a buffer to
2092 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002093 if (slot->key.data == NULL) {
2094 status = psa_allocate_buffer_to_slot(slot, data_length);
2095 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002096 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002097 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002098 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002099
2100 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 status = psa_driver_wrapper_import_key(attributes,
2102 data, data_length,
2103 slot->key.data,
2104 slot->key.bytes,
2105 &slot->key.bytes, &bits);
2106 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002107 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002108 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002110 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002111 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002112 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002113 status = PSA_ERROR_INVALID_ARGUMENT;
2114 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002115 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002117 status = psa_validate_optional_attributes(slot, attributes);
2118 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002119 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002120 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002122 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002123exit:
Ryan Everett6b970252024-01-19 16:02:59 +00002124 LOCAL_INPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002125 if (status != PSA_SUCCESS) {
2126 psa_fail_key_creation(slot, driver);
2127 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002129 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002130}
2131
Gilles Peskined7729582019-08-05 15:55:54 +02002132#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2133psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002134 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002135{
2136 psa_status_t status;
2137 psa_key_slot_t *slot = NULL;
2138 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002139 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002140
2141 /* Leaving attributes unspecified is not currently supported.
2142 * It could make sense to query the key type and size from the
2143 * secure element, but not all secure elements support this
2144 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002145 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2146 return PSA_ERROR_NOT_SUPPORTED;
2147 }
2148 if (psa_get_key_bits(attributes) == 0) {
2149 return PSA_ERROR_NOT_SUPPORTED;
2150 }
Gilles Peskined7729582019-08-05 15:55:54 +02002151
Gilles Peskine37a4fcc2024-06-13 16:06:45 +02002152 /* Not usable with volatile keys, even with an appropriate location,
2153 * due to the API design.
2154 * https://github.com/Mbed-TLS/mbedtls/issues/9253
2155 */
2156 if (PSA_KEY_LIFETIME_IS_VOLATILE(psa_get_key_lifetime(attributes))) {
2157 return PSA_ERROR_INVALID_ARGUMENT;
2158 }
2159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002160 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2161 &slot, &driver);
2162 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002163 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002164 }
Gilles Peskined7729582019-08-05 15:55:54 +02002165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002166 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002167
2168exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002169 if (status != PSA_SUCCESS) {
2170 psa_fail_key_creation(slot, driver);
2171 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002172
Gilles Peskined7729582019-08-05 15:55:54 +02002173 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002174 psa_close_key(key);
2175 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002176}
2177#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002179static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2180 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002181{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002182 psa_status_t status = psa_copy_key_material_into_slot(target,
2183 source->key.data,
2184 source->key.bytes);
2185 if (status != PSA_SUCCESS) {
2186 return status;
2187 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002188
Steven Cooreman398aee52020-10-13 14:35:45 +02002189 target->attr.type = source->attr.type;
2190 target->attr.bits = source->attr.bits;
2191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002192 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002193}
2194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002195psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2196 const psa_key_attributes_t *specified_attributes,
2197 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002198{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002199 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002200 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002201 psa_key_slot_t *source_slot = NULL;
2202 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002203 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002204 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002205
Ronald Cron81709fc2020-11-14 12:10:32 +01002206 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2207
Ronald Cron5c522922020-11-14 16:35:34 +01002208 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002209 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2210 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002211 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002212 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002214 status = psa_validate_optional_attributes(source_slot,
2215 specified_attributes);
2216 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002217 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002218 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002220 status = psa_restrict_key_policy(source_slot->attr.type,
2221 &actual_attributes.core.policy,
2222 &source_slot->attr.policy);
2223 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002224 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002225 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002227 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2228 &target_slot, &driver);
2229 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002230 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002231 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002232
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002233#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002234 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002235 /* Copying to a secure element is not implemented yet. */
2236 status = PSA_ERROR_NOT_SUPPORTED;
2237 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002238 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002239#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002241 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002242 /*
2243 * Copying through an opaque driver is not implemented yet, consider
2244 * a lifetime with an external location as an invalid parameter for
2245 * now.
2246 */
2247 status = PSA_ERROR_INVALID_ARGUMENT;
2248 goto exit;
2249 }
2250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002251 status = psa_copy_key_material(source_slot, target_slot);
2252 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002253 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002254 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002256 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002257exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002258 if (status != PSA_SUCCESS) {
2259 psa_fail_key_creation(target_slot, driver);
2260 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002262 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002264 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002265}
2266
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002267
2268
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002269/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002270/* Message digests */
2271/****************************************************************/
2272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002273psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002274{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002275 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002276 if (operation->id == 0) {
2277 return PSA_SUCCESS;
2278 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002280 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002281 operation->id = 0;
2282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002283 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002284}
2285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002286psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2287 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002289 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002290
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002291 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002292 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002293 status = PSA_ERROR_BAD_STATE;
2294 goto exit;
2295 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002297 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002298 status = PSA_ERROR_INVALID_ARGUMENT;
2299 goto exit;
2300 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002301
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002302 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2303 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002304 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002306 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002307
2308exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002309 if (status != PSA_SUCCESS) {
2310 psa_hash_abort(operation);
2311 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002312
2313 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314}
2315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002316psa_status_t psa_hash_update(psa_hash_operation_t *operation,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002317 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002318 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002320 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002321 LOCAL_INPUT_DECLARE(input_external, input);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002322
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002323 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002324 status = PSA_ERROR_BAD_STATE;
2325 goto exit;
2326 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002327
Steven Cooremanc8288352021-03-04 14:02:19 +01002328 /* Don't require hash implementations to behave correctly on a
2329 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002330 if (input_length == 0) {
2331 return PSA_SUCCESS;
2332 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002333
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002334 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002335 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002336
2337exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002338 if (status != PSA_SUCCESS) {
2339 psa_hash_abort(operation);
2340 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002341
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002342 LOCAL_INPUT_FREE(input_external, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002343 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002344}
2345
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002346static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
Thomas Daubneycbf09212024-01-25 17:14:29 +00002347 uint8_t *hash,
2348 size_t hash_size,
2349 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002350{
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002351 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2352
Steven Cooremanfbe09282021-03-08 18:41:12 +01002353 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002354 if (operation->id == 0) {
2355 return PSA_ERROR_BAD_STATE;
2356 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002357
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002358 status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002359 operation, hash, hash_size, hash_length);
2360 psa_hash_abort(operation);
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002361
2362 return status;
2363}
2364
2365psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2366 uint8_t *hash_external,
2367 size_t hash_size,
2368 size_t *hash_length)
2369{
2370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2371 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2372
2373 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2374 status = psa_hash_finish_internal(operation, hash, hash_size, hash_length);
2375
David Horstmann42015332024-03-13 15:42:31 +00002376#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002377exit:
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002378#endif
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002379 LOCAL_OUTPUT_FREE(hash_external, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002380 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002381}
2382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002383psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002384 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002385 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002387 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002388 size_t actual_hash_length;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002389 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2390 LOCAL_INPUT_DECLARE(hash_external, hash);
2391
2392 status = psa_hash_finish_internal(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002393 operation,
2394 actual_hash, sizeof(actual_hash),
2395 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002396
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002397 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002398 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002399 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002401 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002402 status = PSA_ERROR_INVALID_SIGNATURE;
2403 goto exit;
2404 }
2405
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002406 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002407 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002408 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002409 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002410
2411exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002412 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2413 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002414 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002415 }
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002416 LOCAL_INPUT_FREE(hash_external, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002417 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002418}
2419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002420psa_status_t psa_hash_compute(psa_algorithm_t alg,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002421 const uint8_t *input_external, size_t input_length,
2422 uint8_t *hash_external, size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002423 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002424{
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002425 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2426 LOCAL_INPUT_DECLARE(input_external, input);
2427 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2428
Steven Cooremanfbe09282021-03-08 18:41:12 +01002429 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002430 if (!PSA_ALG_IS_HASH(alg)) {
2431 return PSA_ERROR_INVALID_ARGUMENT;
2432 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002433
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002434 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2435 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2436 status = psa_driver_wrapper_hash_compute(alg, input, input_length,
Thomas Daubneycbf09212024-01-25 17:14:29 +00002437 hash, hash_size, hash_length);
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002438
David Horstmann42015332024-03-13 15:42:31 +00002439#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002440exit:
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002441#endif
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002442 LOCAL_INPUT_FREE(input_external, input);
2443 LOCAL_OUTPUT_FREE(hash_external, hash);
2444 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002445}
2446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002447psa_status_t psa_hash_compare(psa_algorithm_t alg,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002448 const uint8_t *input_external, size_t input_length,
2449 const uint8_t *hash_external, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002450{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002451 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002452 size_t actual_hash_length;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002453 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2454
2455 LOCAL_INPUT_DECLARE(input_external, input);
2456 LOCAL_INPUT_DECLARE(hash_external, hash);
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002458 if (!PSA_ALG_IS_HASH(alg)) {
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002459 status = PSA_ERROR_INVALID_ARGUMENT;
2460 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002461 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002462
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002463 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2464 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002465 alg, input, input_length,
2466 actual_hash, sizeof(actual_hash),
2467 &actual_hash_length);
2468 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002469 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002470 }
2471 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002472 status = PSA_ERROR_INVALID_SIGNATURE;
2473 goto exit;
2474 }
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002475
2476 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002477 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002478 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002479 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002480
2481exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002482 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002483
2484 LOCAL_INPUT_FREE(input_external, input);
2485 LOCAL_INPUT_FREE(hash_external, hash);
2486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002487 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002488}
2489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002490psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2491 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002492{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002493 if (source_operation->id == 0 ||
2494 target_operation->id != 0) {
2495 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002496 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002498 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2499 target_operation);
2500 if (status != PSA_SUCCESS) {
2501 psa_hash_abort(target_operation);
2502 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002504 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002505}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002506
2507
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002508/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509/* MAC */
2510/****************************************************************/
2511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002512psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002513{
Steven Cooreman07897832021-03-19 18:28:56 +01002514 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002515 if (operation->id == 0) {
2516 return PSA_SUCCESS;
2517 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002519 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002520 operation->mac_size = 0;
2521 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002522 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002524 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002525}
2526
Ronald Cron1c650a12021-06-17 16:33:22 +02002527static psa_status_t psa_mac_finalize_alg_and_key_validation(
2528 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002529 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002530 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002532 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002533 psa_key_type_t key_type = psa_get_key_type(attributes);
2534 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002536 if (!PSA_ALG_IS_MAC(alg)) {
2537 return PSA_ERROR_INVALID_ARGUMENT;
2538 }
Steven Cooreman07897832021-03-19 18:28:56 +01002539
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002540 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002541 status = psa_mac_key_can_do(alg, key_type);
2542 if (status != PSA_SUCCESS) {
2543 return status;
2544 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002545
Steven Cooremana6474de2021-05-10 11:13:41 +02002546 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002547 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002549 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002550 /* A very short MAC is too short for security since it can be
2551 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2552 * so we make this our minimum, even though 32 bits is still
2553 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002554 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002555 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002557 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2558 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002559 /* It's impossible to "truncate" to a larger length than the full length
2560 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002561 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002562 }
2563
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002564 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002565 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2566 * that is disabled in the compile-time configuration. The result can
2567 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2568 * configuration into account. In this case, force a return of
2569 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2570 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2571 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2572 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2573 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002574 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002575 }
2576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002577 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002578}
2579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002580static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2581 mbedtls_svc_key_id_t key,
2582 psa_algorithm_t alg,
2583 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002584{
2585 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2586 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002587 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002588 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002589
2590 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002591 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002592 status = PSA_ERROR_BAD_STATE;
2593 goto exit;
2594 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002595
2596 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002597 key,
2598 &slot,
2599 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2600 alg);
2601 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002602 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002603 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002604
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002605 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002606 .core = slot->attr
2607 };
2608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002609 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2610 &operation->mac_size);
2611 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002612 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002613 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002614
2615 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002616 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002617 if (is_sign) {
2618 status = psa_driver_wrapper_mac_sign_setup(operation,
2619 &attributes,
2620 slot->key.data,
2621 slot->key.bytes,
2622 alg);
2623 } else {
2624 status = psa_driver_wrapper_mac_verify_setup(operation,
2625 &attributes,
2626 slot->key.data,
2627 slot->key.bytes,
2628 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002629 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002630
Gilles Peskinefbfac682018-07-08 20:51:54 +02002631exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002632 if (status != PSA_SUCCESS) {
2633 psa_mac_abort(operation);
2634 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002636 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002638 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002639}
2640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002641psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2642 mbedtls_svc_key_id_t key,
2643 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002644{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002645 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002646}
2647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002648psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2649 mbedtls_svc_key_id_t key,
2650 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002651{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002652 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002653}
2654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002655psa_status_t psa_mac_update(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002656 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002657 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658{
Thomas Daubney324f7de2024-01-18 13:18:58 +00002659 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2660 LOCAL_INPUT_DECLARE(input_external, input);
2661
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002662 if (operation->id == 0) {
Thomas Daubney324f7de2024-01-18 13:18:58 +00002663 status = PSA_ERROR_BAD_STATE;
2664 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002665 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002666
Steven Cooreman11743f92021-03-19 18:38:46 +01002667 /* Don't require hash implementations to behave correctly on a
2668 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002669 if (input_length == 0) {
Thomas Daubney324f7de2024-01-18 13:18:58 +00002670 status = PSA_SUCCESS;
2671 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002672 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002673
Thomas Daubney324f7de2024-01-18 13:18:58 +00002674 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2675 status = psa_driver_wrapper_mac_update(operation, input, input_length);
2676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002677 if (status != PSA_SUCCESS) {
2678 psa_mac_abort(operation);
2679 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002680
David Horstmann42015332024-03-13 15:42:31 +00002681#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney324f7de2024-01-18 13:18:58 +00002682exit:
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002683#endif
Thomas Daubney324f7de2024-01-18 13:18:58 +00002684 LOCAL_INPUT_FREE(input_external, input);
2685
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002686 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002687}
2688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002689psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002690 uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002691 size_t mac_size,
2692 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002693{
Steven Cooreman87885df2021-03-19 19:04:39 +01002694 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2695 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002696 LOCAL_OUTPUT_DECLARE(mac_external, mac);
Thomas Daubney301491d2024-02-01 14:32:59 +00002697 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002699 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002700 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002701 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002702 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002704 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002705 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002706 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002707 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002708
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002709 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2710 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002711 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002712 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002713 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002714 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002716 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002717 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002718 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002719 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002721 status = psa_driver_wrapper_mac_sign_finish(operation,
2722 mac, operation->mac_size,
2723 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002724
Dave Rodgman478ab542021-06-25 09:09:02 +01002725exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002726 /* In case of success, set the potential excess room in the output buffer
2727 * to an invalid value, to avoid potentially leaking a longer MAC.
2728 * In case of error, set the output length and content to a safe default,
2729 * such that in case the caller misses an error check, the output would be
2730 * an unachievable MAC.
2731 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002732 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002733 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002734 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002735 }
mohammad16036df908f2018-04-02 08:34:15 -07002736
Thomas Daubney480347d2024-02-01 19:00:26 +00002737 if ((mac != NULL) && (mac_size > operation->mac_size)) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002738 memset(&mac[operation->mac_size], '!',
2739 mac_size - operation->mac_size);
2740 }
Ronald Cron882eb782021-06-18 13:05:48 +02002741
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002742 abort_status = psa_mac_abort(operation);
Thomas Daubney324f7de2024-01-18 13:18:58 +00002743 LOCAL_OUTPUT_FREE(mac_external, mac);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002745 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002746}
2747
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002748psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002749 const uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002750 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002751{
Steven Cooreman87885df2021-03-19 19:04:39 +01002752 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2753 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002754 LOCAL_INPUT_DECLARE(mac_external, mac);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002755
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002756 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002757 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002758 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002759 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002760
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002761 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002762 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002763 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002764 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002765
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002766 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002767 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002768 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002769 }
2770
Thomas Daubney324f7de2024-01-18 13:18:58 +00002771 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002772 status = psa_driver_wrapper_mac_verify_finish(operation,
2773 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002774
Dave Rodgman478ab542021-06-25 09:09:02 +01002775exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002776 abort_status = psa_mac_abort(operation);
Thomas Daubney324f7de2024-01-18 13:18:58 +00002777 LOCAL_INPUT_FREE(mac_external, mac);
mohammad16036df908f2018-04-02 08:34:15 -07002778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002779 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002780}
2781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002782static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2783 psa_algorithm_t alg,
2784 const uint8_t *input,
2785 size_t input_length,
2786 uint8_t *mac,
2787 size_t mac_size,
2788 size_t *mac_length,
2789 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002790{
2791 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002792 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002793 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002794 psa_key_slot_t *slot;
2795 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002796
Ronald Crondbb86462021-06-17 17:17:20 +02002797 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002798 key,
2799 &slot,
2800 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2801 alg);
2802 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002803 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002804 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002805
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002806 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002807 .core = slot->attr
2808 };
2809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002810 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2811 &operation_mac_size);
2812 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002813 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002814 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002816 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002817 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002818 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002819 }
2820
2821 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002822 &attributes,
2823 slot->key.data, slot->key.bytes,
2824 alg,
2825 input, input_length,
2826 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002827
2828exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002829 /* In case of success, set the potential excess room in the output buffer
2830 * to an invalid value, to avoid potentially leaking a longer MAC.
2831 * In case of error, set the output length and content to a safe default,
2832 * such that in case the caller misses an error check, the output would be
2833 * an unachievable MAC.
2834 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002835 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002836 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002837 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002838 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002839 if (mac_size > operation_mac_size) {
2840 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2841 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002843 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002845 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002846}
2847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002848psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002849 psa_algorithm_t alg,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002850 const uint8_t *input_external,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002851 size_t input_length,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002852 uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002853 size_t mac_size,
2854 size_t *mac_length)
2855{
Thomas Daubney324f7de2024-01-18 13:18:58 +00002856 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2857 LOCAL_INPUT_DECLARE(input_external, input);
2858 LOCAL_OUTPUT_DECLARE(mac_external, mac);
2859
2860 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2861 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
2862 status = psa_mac_compute_internal(key, alg,
Thomas Daubneyf298f652024-01-30 12:15:36 +00002863 input, input_length,
2864 mac, mac_size, mac_length, 1);
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002865
David Horstmann42015332024-03-13 15:42:31 +00002866#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney324f7de2024-01-18 13:18:58 +00002867exit:
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002868#endif
Thomas Daubney324f7de2024-01-18 13:18:58 +00002869 LOCAL_INPUT_FREE(input_external, input);
2870 LOCAL_OUTPUT_FREE(mac_external, mac);
2871
2872 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002873}
2874
2875psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2876 psa_algorithm_t alg,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002877 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002878 size_t input_length,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002879 const uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002880 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002881{
2882 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002883 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2884 size_t actual_mac_length;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002885 LOCAL_INPUT_DECLARE(input_external, input);
2886 LOCAL_INPUT_DECLARE(mac_external, mac);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002887
Thomas Daubney324f7de2024-01-18 13:18:58 +00002888 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002889 status = psa_mac_compute_internal(key, alg,
2890 input, input_length,
2891 actual_mac, sizeof(actual_mac),
2892 &actual_mac_length, 0);
2893 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002894 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002895 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002896
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002897 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002898 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002899 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002900 }
Thomas Daubney324f7de2024-01-18 13:18:58 +00002901
2902 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002903 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002904 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002905 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002906 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002907
2908exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002909 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Thomas Daubney324f7de2024-01-18 13:18:58 +00002910 LOCAL_INPUT_FREE(input_external, input);
2911 LOCAL_INPUT_FREE(mac_external, mac);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002912
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002913 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002914}
Gilles Peskine20035e32018-02-03 22:44:14 +01002915
Gilles Peskine20035e32018-02-03 22:44:14 +01002916/****************************************************************/
2917/* Asymmetric cryptography */
2918/****************************************************************/
2919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002920static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2921 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002922{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002923 if (input_is_message) {
2924 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2925 return PSA_ERROR_INVALID_ARGUMENT;
2926 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002927
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002928 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2929 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2930 return PSA_ERROR_INVALID_ARGUMENT;
2931 }
2932 }
2933 } else {
2934 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2935 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002936 }
2937 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002939 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002940}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002942static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2943 int input_is_message,
2944 psa_algorithm_t alg,
2945 const uint8_t *input,
2946 size_t input_length,
2947 uint8_t *signature,
2948 size_t signature_size,
2949 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002950{
2951 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2952 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002953 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002954 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002955
2956 *signature_length = 0;
2957
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002958 status = psa_sign_verify_check_alg(input_is_message, alg);
2959 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002960 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002961 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002962
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002963 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002964 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002965 * buffer can in principle be empty since it doesn't actually have
2966 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002967 if (signature_size == 0) {
2968 return PSA_ERROR_BUFFER_TOO_SMALL;
2969 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002970
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002971 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002972 key, &slot,
2973 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2974 PSA_KEY_USAGE_SIGN_HASH,
2975 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002976
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002977 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002978 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002979 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002981 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002982 status = PSA_ERROR_INVALID_ARGUMENT;
2983 goto exit;
2984 }
2985
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002986 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002987 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002988 };
2989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002990 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002991 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002992 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002993 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002994 signature, signature_size, signature_length);
2995 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002996
2997 status = psa_driver_wrapper_sign_hash(
2998 &attributes, slot->key.data, slot->key.bytes,
2999 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003000 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003001 }
3002
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003003
3004exit:
3005 /* Fill the unused part of the output buffer (the whole buffer on error,
3006 * the trailing part on success) with something that isn't a valid signature
3007 * (barring an attack on the signature and deliberately-crafted input),
3008 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003009 if (status == PSA_SUCCESS) {
3010 memset(signature + *signature_length, '!',
3011 signature_size - *signature_length);
3012 } else {
3013 memset(signature, '!', signature_size);
3014 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003015 /* If signature_size is 0 then we have nothing to do. We must not call
3016 * memset because signature may be NULL in this case. */
3017
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003018 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003020 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003021}
3022
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003023static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3024 int input_is_message,
3025 psa_algorithm_t alg,
3026 const uint8_t *input,
3027 size_t input_length,
3028 const uint8_t *signature,
3029 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003030{
3031 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3032 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3033 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003035 status = psa_sign_verify_check_alg(input_is_message, alg);
3036 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02003037 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003038 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003039
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003040 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003041 key, &slot,
3042 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3043 PSA_KEY_USAGE_VERIFY_HASH,
3044 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003046 if (status != PSA_SUCCESS) {
3047 return status;
3048 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003049
3050 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003051 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003052 };
3053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003054 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003055 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003056 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003057 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003058 signature, signature_length);
3059 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003060 status = psa_driver_wrapper_verify_hash(
3061 &attributes, slot->key.data, slot->key.bytes,
3062 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003063 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003064 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003066 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003068 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003069
3070}
3071
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003072psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003073 const psa_key_attributes_t *attributes,
3074 const uint8_t *key_buffer,
3075 size_t key_buffer_size,
3076 psa_algorithm_t alg,
3077 const uint8_t *input,
3078 size_t input_length,
3079 uint8_t *signature,
3080 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003081 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003082{
3083 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003085 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003086 size_t hash_length;
3087 uint8_t hash[PSA_HASH_MAX_SIZE];
3088
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003089 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003090 PSA_ALG_SIGN_GET_HASH(alg),
3091 input, input_length,
3092 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003094 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003095 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003096 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003097
3098 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003099 attributes, key_buffer, key_buffer_size,
3100 alg, hash, hash_length,
3101 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003102 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003104 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003105}
3106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003107psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3108 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003109 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003110 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003111 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003112 size_t signature_size,
3113 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003114{
Thomas Daubney290aac42024-01-18 17:23:02 +00003115 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3116 LOCAL_INPUT_DECLARE(input_external, input);
3117 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3118
3119 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3120 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3121 status = psa_sign_internal(key, 1, alg, input, input_length, signature,
3122 signature_size, signature_length);
3123
David Horstmann42015332024-03-13 15:42:31 +00003124#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003125exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003126#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003127 LOCAL_INPUT_FREE(input_external, input);
3128 LOCAL_OUTPUT_FREE(signature_external, signature);
3129 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003130}
3131
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003132psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003133 const psa_key_attributes_t *attributes,
3134 const uint8_t *key_buffer,
3135 size_t key_buffer_size,
3136 psa_algorithm_t alg,
3137 const uint8_t *input,
3138 size_t input_length,
3139 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003140 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003141{
3142 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003144 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003145 size_t hash_length;
3146 uint8_t hash[PSA_HASH_MAX_SIZE];
3147
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003148 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003149 PSA_ALG_SIGN_GET_HASH(alg),
3150 input, input_length,
3151 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003153 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003154 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003155 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003156
3157 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003158 attributes, key_buffer, key_buffer_size,
3159 alg, hash, hash_length,
3160 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003161 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003163 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003164}
3165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003166psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3167 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003168 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003169 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003170 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003171 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003172{
Thomas Daubney290aac42024-01-18 17:23:02 +00003173 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3174 LOCAL_INPUT_DECLARE(input_external, input);
3175 LOCAL_INPUT_DECLARE(signature_external, signature);
3176
3177 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3178 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3179 status = psa_verify_internal(key, 1, alg, input, input_length, signature,
3180 signature_length);
3181
David Horstmann42015332024-03-13 15:42:31 +00003182#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003183exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003184#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003185 LOCAL_INPUT_FREE(input_external, input);
3186 LOCAL_INPUT_FREE(signature_external, signature);
3187
3188 return status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003189}
3190
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003191psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003192 const psa_key_attributes_t *attributes,
3193 const uint8_t *key_buffer, size_t key_buffer_size,
3194 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003195 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01003196{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003197 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3198 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3199 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003200#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003201 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3202 return mbedtls_psa_rsa_sign_hash(
3203 attributes,
3204 key_buffer, key_buffer_size,
3205 alg, hash, hash_length,
3206 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003207#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3208 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003209 } else {
3210 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003211 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003212 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3213 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003214#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003215 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3216 return mbedtls_psa_ecdsa_sign_hash(
3217 attributes,
3218 key_buffer, key_buffer_size,
3219 alg, hash, hash_length,
3220 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003221#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3222 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003223 } else {
3224 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003225 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003226 }
Ronald Cron4501c982021-03-19 20:09:32 +01003227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003228 (void) key_buffer;
3229 (void) key_buffer_size;
3230 (void) hash;
3231 (void) hash_length;
3232 (void) signature;
3233 (void) signature_size;
3234 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003236 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003237}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003239psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3240 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003241 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003242 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003243 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003244 size_t signature_size,
3245 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003246{
Thomas Daubney290aac42024-01-18 17:23:02 +00003247 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3248 LOCAL_INPUT_DECLARE(hash_external, hash);
3249 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3250
3251 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3252 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3253 status = psa_sign_internal(key, 0, alg, hash, hash_length, signature,
3254 signature_size, signature_length);
3255
David Horstmann42015332024-03-13 15:42:31 +00003256#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003257exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003258#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003259 LOCAL_INPUT_FREE(hash_external, hash);
3260 LOCAL_OUTPUT_FREE(signature_external, signature);
3261
3262 return status;
itayzafrir5c753392018-05-08 11:18:38 +03003263}
3264
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003265psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003266 const psa_key_attributes_t *attributes,
3267 const uint8_t *key_buffer, size_t key_buffer_size,
3268 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003269 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003270{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003271 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3272 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3273 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003274#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003275 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3276 return mbedtls_psa_rsa_verify_hash(
3277 attributes,
3278 key_buffer, key_buffer_size,
3279 alg, hash, hash_length,
3280 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003281#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3282 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003283 } else {
3284 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003285 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003286 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3287 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003288#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003289 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3290 return mbedtls_psa_ecdsa_verify_hash(
3291 attributes,
3292 key_buffer, key_buffer_size,
3293 alg, hash, hash_length,
3294 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003295#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3296 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003297 } else {
3298 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003299 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003300 }
Ronald Cron4501c982021-03-19 20:09:32 +01003301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003302 (void) key_buffer;
3303 (void) key_buffer_size;
3304 (void) hash;
3305 (void) hash_length;
3306 (void) signature;
3307 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003309 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003310}
3311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003312psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3313 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003314 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003315 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003316 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003317 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003318{
Thomas Daubney290aac42024-01-18 17:23:02 +00003319 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3320 LOCAL_INPUT_DECLARE(hash_external, hash);
3321 LOCAL_INPUT_DECLARE(signature_external, signature);
3322
3323 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3324 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3325 status = psa_verify_internal(key, 0, alg, hash, hash_length, signature,
3326 signature_length);
3327
David Horstmann42015332024-03-13 15:42:31 +00003328#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003329exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003330#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003331 LOCAL_INPUT_FREE(hash_external, hash);
3332 LOCAL_INPUT_FREE(signature_external, signature);
3333
3334 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003335}
3336
John Durkop0e005192020-10-31 22:06:54 -07003337#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003338static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3339 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003340{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003341 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3342 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3343 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3344 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003345}
John Durkop0e005192020-10-31 22:06:54 -07003346#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003348psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3349 psa_algorithm_t alg,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003350 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003351 size_t input_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003352 const uint8_t *salt_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003353 size_t salt_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003354 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003355 size_t output_size,
3356 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003357{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003358 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003359 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003360 psa_key_slot_t *slot;
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003361 LOCAL_INPUT_DECLARE(input_external, input);
3362 LOCAL_INPUT_DECLARE(salt_external, salt);
3363 LOCAL_OUTPUT_DECLARE(output_external, output);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003364
Darryl Green5cc689a2018-07-24 15:34:10 +01003365 (void) input;
3366 (void) input_length;
3367 (void) salt;
3368 (void) output;
3369 (void) output_size;
3370
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003371 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003373 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3374 return PSA_ERROR_INVALID_ARGUMENT;
3375 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003376
Ronald Cron5c522922020-11-14 16:35:34 +01003377 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003378 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3379 if (status != PSA_SUCCESS) {
3380 return status;
3381 }
3382 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3383 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003384 status = PSA_ERROR_INVALID_ARGUMENT;
3385 goto exit;
3386 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003388 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003389#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003390 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003391 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003392 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3393 slot->key.data,
3394 slot->key.bytes,
3395 &rsa);
3396 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003397 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003398 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003400 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003401 status = PSA_ERROR_BUFFER_TOO_SMALL;
3402 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003403 }
Ronald Cron7207d572021-09-08 14:28:35 +02003404#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3405 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003406 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3407 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3408 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003409 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003410#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003411 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003412 mbedtls_rsa_pkcs1_encrypt(rsa,
3413 mbedtls_psa_get_random,
3414 MBEDTLS_PSA_RANDOM_STATE,
3415 MBEDTLS_RSA_PUBLIC,
3416 input_length,
3417 input,
3418 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003419#else
3420 status = PSA_ERROR_NOT_SUPPORTED;
3421#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003422 } else
3423 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003424#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003425 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003426 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003427 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3428 mbedtls_psa_get_random,
3429 MBEDTLS_PSA_RANDOM_STATE,
3430 MBEDTLS_RSA_PUBLIC,
3431 salt, salt_length,
3432 input_length,
3433 input,
3434 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003435#else
3436 status = PSA_ERROR_NOT_SUPPORTED;
3437#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003438 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003439 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003440 }
Ronald Cron7207d572021-09-08 14:28:35 +02003441#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003442 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003443rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003444 if (status == PSA_SUCCESS) {
3445 *output_length = mbedtls_rsa_get_len(rsa);
3446 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003448 mbedtls_rsa_free(rsa);
3449 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003450#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003451 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003452 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003453 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003454 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003455
3456exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003457 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003458
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003459 LOCAL_INPUT_FREE(input_external, input);
3460 LOCAL_INPUT_FREE(salt_external, salt);
3461 LOCAL_OUTPUT_FREE(output_external, output);
3462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003463 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003464}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003466psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3467 psa_algorithm_t alg,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003468 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003469 size_t input_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003470 const uint8_t *salt_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003471 size_t salt_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003472 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003473 size_t output_size,
3474 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003475{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003476 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003477 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003478 psa_key_slot_t *slot;
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003479 LOCAL_INPUT_DECLARE(input_external, input);
3480 LOCAL_INPUT_DECLARE(salt_external, salt);
3481 LOCAL_OUTPUT_DECLARE(output_external, output);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003482
Darryl Green5cc689a2018-07-24 15:34:10 +01003483 (void) input;
3484 (void) input_length;
3485 (void) salt;
3486 (void) output;
3487 (void) output_size;
3488
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003489 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003491 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3492 return PSA_ERROR_INVALID_ARGUMENT;
3493 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003494
Ronald Cron5c522922020-11-14 16:35:34 +01003495 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003496 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3497 if (status != PSA_SUCCESS) {
3498 return status;
3499 }
3500 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003501 status = PSA_ERROR_INVALID_ARGUMENT;
3502 goto exit;
3503 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003504
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003505 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003506#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003507 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003508 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003509 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3510 slot->key.data,
3511 slot->key.bytes,
3512 &rsa);
3513 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003514 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003515 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003517 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003518 status = PSA_ERROR_INVALID_ARGUMENT;
3519 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003520 }
Ronald Cron7207d572021-09-08 14:28:35 +02003521#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3522 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003523 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3524 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3525 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003526 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003527#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003528 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003529 mbedtls_rsa_pkcs1_decrypt(rsa,
3530 mbedtls_psa_get_random,
3531 MBEDTLS_PSA_RANDOM_STATE,
3532 MBEDTLS_RSA_PRIVATE,
3533 output_length,
3534 input,
3535 output,
3536 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003537#else
3538 status = PSA_ERROR_NOT_SUPPORTED;
3539#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003540 } else
3541 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003542#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003543 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003544 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003545 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3546 mbedtls_psa_get_random,
3547 MBEDTLS_PSA_RANDOM_STATE,
3548 MBEDTLS_RSA_PRIVATE,
3549 salt, salt_length,
3550 output_length,
3551 input,
3552 output,
3553 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003554#else
3555 status = PSA_ERROR_NOT_SUPPORTED;
3556#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003557 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003558 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003559 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003560
Ronald Cron7207d572021-09-08 14:28:35 +02003561#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003562 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003563rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003564 mbedtls_rsa_free(rsa);
3565 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003566#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3567 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003568 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003569 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003570 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003571
3572exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003573 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003574
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003575 LOCAL_INPUT_FREE(input_external, input);
3576 LOCAL_INPUT_FREE(salt_external, salt);
3577 LOCAL_OUTPUT_FREE(output_external, output);
3578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003579 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003580}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003581
David Horstmann65bf12c2024-02-06 15:27:49 +00003582static psa_status_t psa_generate_random_internal(uint8_t *output,
3583 size_t output_size)
3584{
3585 GUARD_MODULE_INITIALIZED;
3586
3587 psa_status_t status;
3588
3589#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
3590
3591 size_t output_length = 0;
3592 status = mbedtls_psa_external_get_random(&global_data.rng,
3593 output, output_size,
3594 &output_length);
3595 if (status != PSA_SUCCESS) {
3596 goto exit;
3597 }
3598 /* Breaking up a request into smaller chunks is currently not supported
3599 * for the external RNG interface. */
3600 if (output_length != output_size) {
3601 status = PSA_ERROR_INSUFFICIENT_ENTROPY;
3602 goto exit;
3603 }
3604 status = PSA_SUCCESS;
3605
3606#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
3607
3608 while (output_size > 0) {
3609 size_t request_size =
3610 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
3611 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
3612 output_size);
3613 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
3614 output, request_size);
3615 if (ret != 0) {
3616 status = mbedtls_to_psa_error(ret);
3617 goto exit;
3618 }
3619 output_size -= request_size;
3620 output += request_size;
3621 }
3622 status = PSA_SUCCESS;
3623#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
3624
3625exit:
3626 return status;
3627}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003628
3629
mohammad1603503973b2018-03-12 15:59:30 +02003630/****************************************************************/
3631/* Symmetric cryptography */
3632/****************************************************************/
3633
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003634static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3635 mbedtls_svc_key_id_t key,
3636 psa_algorithm_t alg,
3637 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003638{
3639 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3640 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003641 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003642 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003643 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3644 PSA_KEY_USAGE_ENCRYPT :
3645 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003646
3647 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003648 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003649 status = PSA_ERROR_BAD_STATE;
3650 goto exit;
3651 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003653 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003654 status = PSA_ERROR_INVALID_ARGUMENT;
3655 goto exit;
3656 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003658 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3659 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003660 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003661 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003662
Ronald Cron49fafa92021-03-10 08:34:23 +01003663 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003664 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003665 * so we only set it (in the driver wrapper) after resources have been
3666 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003667 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003668 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003669 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003670 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003671 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003672 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003673 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003674 }
3675 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003676
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003677 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003678 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003679 };
3680
Ronald Cronab99ac22020-10-01 16:38:28 +02003681 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003682 if (cipher_operation == MBEDTLS_ENCRYPT) {
3683 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3684 &attributes,
3685 slot->key.data,
3686 slot->key.bytes,
3687 alg);
3688 } else {
3689 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3690 &attributes,
3691 slot->key.data,
3692 slot->key.bytes,
3693 alg);
3694 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003695
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003696exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003697 if (status != PSA_SUCCESS) {
3698 psa_cipher_abort(operation);
3699 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003701 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003703 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003704}
3705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003706psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3707 mbedtls_svc_key_id_t key,
3708 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003709{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003710 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003711}
3712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003713psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3714 mbedtls_svc_key_id_t key,
3715 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003716{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003717 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003718}
3719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003720psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Gabor Mezei8677edd2024-02-07 18:10:13 +01003721 uint8_t *iv_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003722 size_t iv_size,
3723 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003724{
Ronald Cron6d051732020-10-01 14:10:20 +02003725 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Sergey04eb7c02023-03-06 15:37:23 -07003726 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003727
Gabor Mezei8677edd2024-02-07 18:10:13 +01003728 LOCAL_OUTPUT_DECLARE(iv_external, iv);
3729
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003730 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003731 status = PSA_ERROR_BAD_STATE;
3732 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003733 }
3734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003735 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003736 status = PSA_ERROR_BAD_STATE;
3737 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003738 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003739
Ronald Cronc423acb2021-07-05 12:31:44 +02003740 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003741 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003742 status = PSA_ERROR_BUFFER_TOO_SMALL;
3743 goto exit;
3744 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003745
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003746 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003747 status = PSA_ERROR_GENERIC_ERROR;
3748 goto exit;
3749 }
3750
Gabor Mezei8677edd2024-02-07 18:10:13 +01003751 LOCAL_OUTPUT_ALLOC(iv_external, default_iv_length, iv);
3752
Gabor Mezeif3c35042024-03-04 17:15:08 +01003753 status = psa_generate_random_internal(iv, default_iv_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003754 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003755 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003756 }
Ronald Cron5618a392021-03-26 09:52:26 +01003757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003758 status = psa_driver_wrapper_cipher_set_iv(operation,
Gabor Mezei8677edd2024-02-07 18:10:13 +01003759 iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003760
3761exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003762 if (status == PSA_SUCCESS) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003763 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003764 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003765 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003766 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767 psa_cipher_abort(operation);
Gabor Mezei8677edd2024-02-07 18:10:13 +01003768 if (iv != NULL) {
3769 mbedtls_platform_zeroize(iv, default_iv_length);
3770 }
Ronald Cronc423acb2021-07-05 12:31:44 +02003771 }
Ronald Cron6d051732020-10-01 14:10:20 +02003772
Gabor Mezei8677edd2024-02-07 18:10:13 +01003773 LOCAL_OUTPUT_FREE(iv_external, iv);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003774 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003775}
3776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003777psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Gabor Mezei282bb532024-02-01 10:39:26 +01003778 const uint8_t *iv_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003779 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003780{
Ronald Cron6d051732020-10-01 14:10:20 +02003781 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003782
Gabor Mezei282bb532024-02-01 10:39:26 +01003783 LOCAL_INPUT_DECLARE(iv_external, iv);
3784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003785 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003786 status = PSA_ERROR_BAD_STATE;
3787 goto exit;
3788 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003790 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003791 status = PSA_ERROR_BAD_STATE;
3792 goto exit;
3793 }
Ronald Crona0d68172021-03-26 10:15:08 +01003794
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003795 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003796 status = PSA_ERROR_INVALID_ARGUMENT;
3797 goto exit;
3798 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003799
Gabor Mezei282bb532024-02-01 10:39:26 +01003800 LOCAL_INPUT_ALLOC(iv_external, iv_length, iv);
3801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003802 status = psa_driver_wrapper_cipher_set_iv(operation,
3803 iv,
3804 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003805
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003806exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003807 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003808 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003809 } else {
3810 psa_cipher_abort(operation);
3811 }
Gabor Mezei282bb532024-02-01 10:39:26 +01003812
3813 LOCAL_INPUT_FREE(iv_external, iv);
3814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003815 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003816}
3817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003818psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003819 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003820 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003821 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003822 size_t output_size,
3823 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003824{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003825 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003826
Gabor Mezei13a15c22024-01-24 16:56:00 +01003827 LOCAL_INPUT_DECLARE(input_external, input);
3828 LOCAL_OUTPUT_DECLARE(output_external, output);
3829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003830 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003831 status = PSA_ERROR_BAD_STATE;
3832 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003833 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003835 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003836 status = PSA_ERROR_BAD_STATE;
3837 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003838 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003839
Gabor Mezeied96d682024-01-31 17:45:29 +01003840 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gabor Mezeiff783e02024-02-29 10:08:16 +00003841 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezeied96d682024-01-31 17:45:29 +01003842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003843 status = psa_driver_wrapper_cipher_update(operation,
3844 input,
3845 input_length,
3846 output,
3847 output_size,
3848 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003849
3850exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003851 if (status != PSA_SUCCESS) {
3852 psa_cipher_abort(operation);
3853 }
Ronald Cron6d051732020-10-01 14:10:20 +02003854
Gabor Mezei13a15c22024-01-24 16:56:00 +01003855 LOCAL_INPUT_FREE(input_external, input);
3856 LOCAL_OUTPUT_FREE(output_external, output);
3857
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003858 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003859}
3860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003861psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003862 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003863 size_t output_size,
3864 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003865{
David Saadab4ecc272019-02-14 13:48:10 +02003866 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003867
Gabor Mezei13a15c22024-01-24 16:56:00 +01003868 LOCAL_OUTPUT_DECLARE(output_external, output);
3869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003870 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003871 status = PSA_ERROR_BAD_STATE;
3872 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003873 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003875 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003876 status = PSA_ERROR_BAD_STATE;
3877 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003878 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003879
Gabor Mezeiff783e02024-02-29 10:08:16 +00003880 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezeied96d682024-01-31 17:45:29 +01003881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003882 status = psa_driver_wrapper_cipher_finish(operation,
3883 output,
3884 output_size,
3885 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003886
3887exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003888 if (status == PSA_SUCCESS) {
Gabor Mezei13a15c22024-01-24 16:56:00 +01003889 status = psa_cipher_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003890 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003891 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003892 (void) psa_cipher_abort(operation);
Steven Cooremanef8575e2020-09-11 11:44:50 +02003893 }
Gabor Mezei13a15c22024-01-24 16:56:00 +01003894
3895 LOCAL_OUTPUT_FREE(output_external, output);
3896
3897 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003898}
3899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003900psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003901{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003902 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003903 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003904 * in use. It's ok to call abort on such an object, and there's
3905 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003906 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003907 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003909 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003910
Ronald Cron49fafa92021-03-10 08:34:23 +01003911 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003912 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003913 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003915 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003916}
3917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003918psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3919 psa_algorithm_t alg,
David Horstmann926193a2023-12-13 15:55:25 +00003920 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003921 size_t input_length,
David Horstmann926193a2023-12-13 15:55:25 +00003922 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003923 size_t output_size,
3924 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003925{
3926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003927 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003928 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003929 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003930 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3931 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003932
David Horstmannb80e35a2023-12-14 18:12:47 +00003933 LOCAL_INPUT_DECLARE(input_external, input);
3934 LOCAL_OUTPUT_DECLARE(output_external, output);
3935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003936 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003937 status = PSA_ERROR_INVALID_ARGUMENT;
3938 goto exit;
3939 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003941 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3942 PSA_KEY_USAGE_ENCRYPT,
3943 alg);
3944 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003945 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003946 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003947
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003948 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003949 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003950 };
3951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003952 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3953 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003954 status = PSA_ERROR_GENERIC_ERROR;
3955 goto exit;
3956 }
3957
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003958 if (default_iv_length > 0) {
3959 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003960 status = PSA_ERROR_BUFFER_TOO_SMALL;
3961 goto exit;
3962 }
3963
David Horstmann65bf12c2024-02-06 15:27:49 +00003964 status = psa_generate_random_internal(local_iv, default_iv_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003965 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003966 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003967 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003968 }
3969
Gabor Mezeied96d682024-01-31 17:45:29 +01003970 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3971 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
3972
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003973 status = psa_driver_wrapper_cipher_encrypt(
3974 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003975 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003976 mbedtls_buffer_offset(output, default_iv_length),
3977 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003978
3979exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003980 unlock_status = psa_unlock_key_slot(slot);
3981 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003982 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003983 }
Ronald Cron16377072021-07-08 19:00:07 +02003984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003985 if (status == PSA_SUCCESS) {
3986 if (default_iv_length > 0) {
3987 memcpy(output, local_iv, default_iv_length);
3988 }
3989 *output_length += default_iv_length;
3990 } else {
3991 *output_length = 0;
3992 }
3993
David Horstmannb80e35a2023-12-14 18:12:47 +00003994 LOCAL_INPUT_FREE(input_external, input);
3995 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003997 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003998}
3999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004000psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4001 psa_algorithm_t alg,
Gabor Mezei13a15c22024-01-24 16:56:00 +01004002 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004003 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01004004 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004005 size_t output_size,
4006 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004007{
4008 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004009 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004010 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02004011 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02004012
Gabor Mezei13a15c22024-01-24 16:56:00 +01004013 LOCAL_INPUT_DECLARE(input_external, input);
4014 LOCAL_OUTPUT_DECLARE(output_external, output);
4015
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004016 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02004017 status = PSA_ERROR_INVALID_ARGUMENT;
4018 goto exit;
4019 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004021 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4022 PSA_KEY_USAGE_DECRYPT,
4023 alg);
4024 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004025 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004026 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004027
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004028 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004029 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004030 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004031
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004032 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02004033 status = PSA_ERROR_INVALID_ARGUMENT;
4034 goto exit;
4035 }
4036
Gabor Mezeied96d682024-01-31 17:45:29 +01004037 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4038 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4039
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004040 status = psa_driver_wrapper_cipher_decrypt(
4041 &attributes, slot->key.data, slot->key.bytes,
4042 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004043 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004044
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02004045exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004046 unlock_status = psa_unlock_key_slot(slot);
4047 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004048 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004049 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004051 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004052 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004053 }
Ronald Cron16377072021-07-08 19:00:07 +02004054
Gabor Mezei13a15c22024-01-24 16:56:00 +01004055 LOCAL_INPUT_FREE(input_external, input);
4056 LOCAL_OUTPUT_FREE(output_external, output);
4057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004058 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004059}
4060
4061
mohammad16035955c982018-04-26 00:53:03 +03004062/****************************************************************/
4063/* AEAD */
4064/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004066psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4067 psa_algorithm_t alg,
David Horstmann21c1a942023-11-28 19:25:00 +00004068 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004069 size_t nonce_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004070 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004071 size_t additional_data_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004072 const uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004073 size_t plaintext_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004074 uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004075 size_t ciphertext_size,
4076 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004077{
Ronald Cron215633c2021-03-16 17:15:37 +01004078 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4079 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004080
David Horstmann21c1a942023-11-28 19:25:00 +00004081 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4082 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4083 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4084 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4085
mohammad1603f08a5502018-06-03 15:05:47 +03004086 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004088 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4089 return PSA_ERROR_NOT_SUPPORTED;
4090 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004091
Ronald Cron9a986162021-03-26 12:40:07 +01004092 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004093 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4094 if (status != PSA_SUCCESS) {
4095 return status;
4096 }
mohammad16035955c982018-04-26 00:53:03 +03004097
Ronald Cron215633c2021-03-16 17:15:37 +01004098 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004099 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004100 };
mohammad16035955c982018-04-26 00:53:03 +03004101
David Horstmann21c1a942023-11-28 19:25:00 +00004102 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4103 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4104 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4105 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4106
Ronald Cronde822812021-03-17 16:08:20 +01004107 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004108 &attributes, slot->key.data, slot->key.bytes,
4109 alg,
4110 nonce, nonce_length,
4111 additional_data, additional_data_length,
4112 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004113 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004115 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4116 memset(ciphertext, 0, ciphertext_size);
4117 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004118
David Horstmann21c1a942023-11-28 19:25:00 +00004119/* Exit label is only used for buffer copying, prevent unused warnings. */
David Horstmann42015332024-03-13 15:42:31 +00004120#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann21c1a942023-11-28 19:25:00 +00004121exit:
4122#endif
4123 LOCAL_INPUT_FREE(nonce_external, nonce);
4124 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4125 LOCAL_INPUT_FREE(plaintext_external, plaintext);
4126 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
4127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004128 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004130 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004131}
4132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004133psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4134 psa_algorithm_t alg,
David Horstmann6baf6e92023-11-28 19:43:53 +00004135 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004136 size_t nonce_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004137 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004138 size_t additional_data_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004139 const uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004140 size_t ciphertext_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004141 uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004142 size_t plaintext_size,
4143 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004144{
Ronald Cron215633c2021-03-16 17:15:37 +01004145 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4146 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004147
David Horstmann6baf6e92023-11-28 19:43:53 +00004148 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4149 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4150 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
4151 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
4152
Gilles Peskineee652a32018-06-01 19:23:52 +02004153 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004155 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4156 return PSA_ERROR_NOT_SUPPORTED;
4157 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004158
Ronald Cron9a986162021-03-26 12:40:07 +01004159 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004160 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4161 if (status != PSA_SUCCESS) {
4162 return status;
4163 }
mohammad16035955c982018-04-26 00:53:03 +03004164
Ronald Cron215633c2021-03-16 17:15:37 +01004165 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004166 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004167 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004168
David Horstmann6baf6e92023-11-28 19:43:53 +00004169 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4170 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
4171 additional_data);
4172 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
4173 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
4174
Ronald Cronde822812021-03-17 16:08:20 +01004175 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004176 &attributes, slot->key.data, slot->key.bytes,
4177 alg,
4178 nonce, nonce_length,
4179 additional_data, additional_data_length,
4180 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004181 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004183 if (status != PSA_SUCCESS && plaintext_size != 0) {
4184 memset(plaintext, 0, plaintext_size);
4185 }
mohammad160360a64d02018-06-03 17:20:42 +03004186
David Horstmann6baf6e92023-11-28 19:43:53 +00004187/* Exit label is only used for buffer copying, prevent unused warnings. */
David Horstmann42015332024-03-13 15:42:31 +00004188#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann6baf6e92023-11-28 19:43:53 +00004189exit:
4190#endif
4191 LOCAL_INPUT_FREE(nonce_external, nonce);
4192 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4193 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
4194 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
4195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004196 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004198 return status;
mohammad16035955c982018-04-26 00:53:03 +03004199}
4200
Gilles Peskinee59236f2018-01-27 23:32:46 +01004201/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004202/* Generators */
4203/****************************************************************/
4204
John Durkop07cc04a2020-11-16 22:08:34 -08004205#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4206 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4207 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4208#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02004209#endif /* At least one builtin KDF */
4210
4211#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4212 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4213 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4214static psa_status_t psa_key_derivation_start_hmac(
4215 psa_mac_operation_t *operation,
4216 psa_algorithm_t hash_alg,
4217 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004218 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02004219{
4220 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004222 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4223 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4224 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02004225
Steven Cooreman15f0d922021-05-07 14:14:37 +02004226 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004227 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02004228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004229 status = psa_driver_wrapper_mac_sign_setup(operation,
4230 &attributes,
4231 hmac_key, hmac_key_length,
4232 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02004233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004234 psa_reset_key_attributes(&attributes);
4235 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02004236}
4237#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004238
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004239#define HKDF_STATE_INIT 0 /* no input yet */
4240#define HKDF_STATE_STARTED 1 /* got salt */
4241#define HKDF_STATE_KEYED 2 /* got key */
4242#define HKDF_STATE_OUTPUT 3 /* output started */
4243
Gilles Peskinecbe66502019-05-16 16:59:18 +02004244static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004245 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004246{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004247 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4248 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4249 } else {
4250 return operation->alg;
4251 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004252}
4253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004254psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004255{
4256 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004257 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4258 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004259 /* The object has (apparently) been initialized but it is not
4260 * in use. It's ok to call abort on such an object, and there's
4261 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004262 } else
John Durkopd0321952020-10-29 21:37:36 -07004263#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004264 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4265 mbedtls_free(operation->ctx.hkdf.info);
4266 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4267 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004268#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4269#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4270 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004271 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4272 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4273 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4274 if (operation->ctx.tls12_prf.secret != NULL) {
4275 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
4276 operation->ctx.tls12_prf.secret_length);
4277 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004278 }
4279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004280 if (operation->ctx.tls12_prf.seed != NULL) {
4281 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
4282 operation->ctx.tls12_prf.seed_length);
4283 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01004284 }
4285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004286 if (operation->ctx.tls12_prf.label != NULL) {
4287 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
4288 operation->ctx.tls12_prf.label_length);
4289 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01004290 }
4291
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004292 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004293
4294 /* We leave the fields Ai and output_block to be erased safely by the
4295 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004296 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004297#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4298 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004299 {
4300 status = PSA_ERROR_BAD_STATE;
4301 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004302 mbedtls_platform_zeroize(operation, sizeof(*operation));
4303 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004304}
4305
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004306psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004307 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004308{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004309 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004310 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004311 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004312 }
4313
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004314 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004315 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004316}
4317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004318psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4319 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004320{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004321 if (operation->alg == 0) {
4322 return PSA_ERROR_BAD_STATE;
4323 }
4324 if (capacity > operation->capacity) {
4325 return PSA_ERROR_INVALID_ARGUMENT;
4326 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004327 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004328 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004329}
4330
John Durkopd0321952020-10-29 21:37:36 -07004331#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004332/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004333 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004334static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
4335 psa_algorithm_t hash_alg,
4336 uint8_t *output,
4337 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004338{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004339 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02004340 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004341 psa_status_t status;
4342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004343 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
4344 return PSA_ERROR_BAD_STATE;
4345 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004346 hkdf->state = HKDF_STATE_OUTPUT;
4347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004348 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004349 /* Copy what remains of the current block */
4350 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004351 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004352 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004353 }
4354 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004355 output += n;
4356 output_length -= n;
4357 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004358 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004359 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004360 }
Gilles Peskined54931c2018-07-17 21:06:59 +02004361 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004362 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004363 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004364 * object was corrupted or if this function is called directly
4365 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004366 if (hkdf->block_number == 0xff) {
4367 return PSA_ERROR_BAD_STATE;
4368 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004369
4370 /* We need a new block */
4371 ++hkdf->block_number;
4372 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004374 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4375 hash_alg,
4376 hkdf->prk,
4377 hash_length);
4378 if (status != PSA_SUCCESS) {
4379 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004380 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004381
4382 if (hkdf->block_number != 1) {
4383 status = psa_mac_update(&hkdf->hmac,
4384 hkdf->output_block,
4385 hash_length);
4386 if (status != PSA_SUCCESS) {
4387 return status;
4388 }
4389 }
4390 status = psa_mac_update(&hkdf->hmac,
4391 hkdf->info,
4392 hkdf->info_length);
4393 if (status != PSA_SUCCESS) {
4394 return status;
4395 }
4396 status = psa_mac_update(&hkdf->hmac,
4397 &hkdf->block_number, 1);
4398 if (status != PSA_SUCCESS) {
4399 return status;
4400 }
4401 status = psa_mac_sign_finish(&hkdf->hmac,
4402 hkdf->output_block,
4403 sizeof(hkdf->output_block),
4404 &hmac_output_length);
4405 if (status != PSA_SUCCESS) {
4406 return status;
4407 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004408 }
4409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004410 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004411}
John Durkop07cc04a2020-11-16 22:08:34 -08004412#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004413
John Durkop07cc04a2020-11-16 22:08:34 -08004414#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4415 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004416static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4417 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004418 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004419{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004420 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4421 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004422 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4423 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004424 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004425
Janos Follath7742fee2019-06-17 12:58:10 +01004426 /* We can't be wanting more output after block 0xff, otherwise
4427 * the capacity check in psa_key_derivation_output_bytes() would have
4428 * prevented this call. It could happen only if the operation
4429 * object was corrupted or if this function is called directly
4430 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004431 if (tls12_prf->block_number == 0xff) {
4432 return PSA_ERROR_CORRUPTION_DETECTED;
4433 }
Janos Follath7742fee2019-06-17 12:58:10 +01004434
4435 /* We need a new block */
4436 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004437 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004438
4439 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4440 *
4441 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4442 *
4443 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4444 * HMAC_hash(secret, A(2) + seed) +
4445 * HMAC_hash(secret, A(3) + seed) + ...
4446 *
4447 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004448 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004449 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004450 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004451 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004452 * is currently extracted as `output_block` and where i is
4453 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004454 */
4455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004456 status = psa_key_derivation_start_hmac(&hmac,
4457 hash_alg,
4458 tls12_prf->secret,
4459 tls12_prf->secret_length);
4460 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004461 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004462 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004463
4464 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004465 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004466 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4467 * the variable seed and in this instance means it in the context of the
4468 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004469 status = psa_mac_update(&hmac,
4470 tls12_prf->label,
4471 tls12_prf->label_length);
4472 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004473 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004474 }
4475 status = psa_mac_update(&hmac,
4476 tls12_prf->seed,
4477 tls12_prf->seed_length);
4478 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004479 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004480 }
4481 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004482 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004483 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4484 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004485 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004486 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004487 }
4488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004489 status = psa_mac_sign_finish(&hmac,
4490 tls12_prf->Ai, hash_length,
4491 &hmac_output_length);
4492 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004493 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004494 }
4495 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004496 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004497 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004498
4499 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004500 status = psa_key_derivation_start_hmac(&hmac,
4501 hash_alg,
4502 tls12_prf->secret,
4503 tls12_prf->secret_length);
4504 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004505 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004506 }
4507 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4508 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004509 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004510 }
4511 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4512 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004513 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004514 }
4515 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4516 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004517 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004518 }
4519 status = psa_mac_sign_finish(&hmac,
4520 tls12_prf->output_block, hash_length,
4521 &hmac_output_length);
4522 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004523 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004524 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004525
Janos Follath7742fee2019-06-17 12:58:10 +01004526
4527cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004528 cleanup_status = psa_mac_abort(&hmac);
4529 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004530 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004531 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004533 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004534}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004535
Janos Follath844eb0e2019-06-19 12:10:49 +01004536static psa_status_t psa_key_derivation_tls12_prf_read(
4537 psa_tls12_prf_key_derivation_t *tls12_prf,
4538 psa_algorithm_t alg,
4539 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004540 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004541{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004542 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4543 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004544 psa_status_t status;
4545 uint8_t offset, length;
4546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004547 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004548 case PSA_TLS12_PRF_STATE_LABEL_SET:
4549 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4550 break;
4551 case PSA_TLS12_PRF_STATE_OUTPUT:
4552 break;
4553 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004554 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004555 }
4556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004557 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004558 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004559 if (tls12_prf->left_in_block == 0) {
4560 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4561 alg);
4562 if (status != PSA_SUCCESS) {
4563 return status;
4564 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004565
4566 continue;
4567 }
4568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004569 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004570 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004571 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004572 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004573 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004574
4575 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004576 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004577 output += length;
4578 output_length -= length;
4579 tls12_prf->left_in_block -= length;
4580 }
4581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004582 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004583}
John Durkop07cc04a2020-11-16 22:08:34 -08004584#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4585 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004586
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004587psa_status_t psa_key_derivation_output_bytes(
4588 psa_key_derivation_operation_t *operation,
Ryan Everett08bd2462024-02-09 16:15:32 +00004589 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004590 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004591{
4592 psa_status_t status;
Ryan Everett08bd2462024-02-09 16:15:32 +00004593 LOCAL_OUTPUT_DECLARE(output_external, output);
4594
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004595 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004597 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004598 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004599 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004600 }
4601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004602 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004603 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004604 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004605 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4606 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004607 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004608 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004609 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004610 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004611
4612 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
4613 if (output_length > operation->capacity) {
4614 operation->capacity = 0;
4615 /* Go through the error path to wipe all confidential data now
4616 * that the operation object is useless. */
4617 status = PSA_ERROR_INSUFFICIENT_DATA;
4618 goto exit;
4619 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004620 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004621
John Durkopd0321952020-10-29 21:37:36 -07004622#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4624 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4625 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4626 output, output_length);
4627 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004628#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4629#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4630 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004631 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4632 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4633 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4634 kdf_alg, output,
4635 output_length);
4636 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004637#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4638 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004639 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004640 (void) kdf_alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004641 status = PSA_ERROR_BAD_STATE;
4642 LOCAL_OUTPUT_FREE(output_external, output);
4643
4644 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004645 }
4646
4647exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004648 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004649 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004650 * This allows us to differentiate between exhausted operations and
4651 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4652 * operations. */
4653 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004654 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004655 operation->alg = alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004656 if (output != NULL) {
4657 memset(output, '!', output_length);
4658 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004659 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004660
4661 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004662 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004663}
4664
David Brown7807bf72021-02-09 16:28:23 -07004665#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004666static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004667{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004668 if (data_size >= 8) {
4669 mbedtls_des_key_set_parity(data);
4670 }
4671 if (data_size >= 16) {
4672 mbedtls_des_key_set_parity(data + 8);
4673 }
4674 if (data_size >= 24) {
4675 mbedtls_des_key_set_parity(data + 16);
4676 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004677}
David Brown7807bf72021-02-09 16:28:23 -07004678#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004679
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004680static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004681 psa_key_slot_t *slot,
4682 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004683 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004684{
4685 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004686 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004687 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004688 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004690 if (!key_type_is_raw_bytes(slot->attr.type)) {
4691 return PSA_ERROR_INVALID_ARGUMENT;
4692 }
4693 if (bits % 8 != 0) {
4694 return PSA_ERROR_INVALID_ARGUMENT;
4695 }
4696 data = mbedtls_calloc(1, bytes);
4697 if (data == NULL) {
4698 return PSA_ERROR_INSUFFICIENT_MEMORY;
4699 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004701 status = psa_key_derivation_output_bytes(operation, data, bytes);
4702 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004703 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004704 }
David Brown12ca5032021-02-11 11:02:00 -07004705#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004706 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4707 psa_des_set_key_parity(data, bytes);
4708 }
David Brown8107e312021-02-12 08:08:46 -07004709#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004711 status = psa_allocate_buffer_to_slot(slot, bytes);
4712 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004713 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004714 }
Ronald Crondd04d422020-11-28 15:14:42 +01004715
Ronald Cronbf33c932020-11-28 18:06:53 +01004716 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004717 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004718 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004719 };
4720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004721 status = psa_driver_wrapper_import_key(&attributes,
4722 data, bytes,
4723 slot->key.data,
4724 slot->key.bytes,
4725 &slot->key.bytes, &bits);
4726 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004727 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004728 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004729
4730exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004731 mbedtls_free(data);
4732 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004733}
4734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004735psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4736 psa_key_derivation_operation_t *operation,
4737 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004738{
4739 psa_status_t status;
4740 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004741 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004742
Ronald Cron81709fc2020-11-14 12:10:32 +01004743 *key = MBEDTLS_SVC_KEY_ID_INIT;
4744
Gilles Peskine0f84d622019-09-12 19:03:13 +02004745 /* Reject any attempt to create a zero-length key so that we don't
4746 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004747 if (psa_get_key_bits(attributes) == 0) {
4748 return PSA_ERROR_INVALID_ARGUMENT;
4749 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004751 if (operation->alg == PSA_ALG_NONE) {
4752 return PSA_ERROR_BAD_STATE;
4753 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004754
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004755 if (!operation->can_output_key) {
4756 return PSA_ERROR_NOT_PERMITTED;
4757 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004759 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4760 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004761#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004762 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004763 /* Deriving a key in a secure element is not implemented yet. */
4764 status = PSA_ERROR_NOT_SUPPORTED;
4765 }
4766#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004767 if (status == PSA_SUCCESS) {
4768 status = psa_generate_derived_key_internal(slot,
4769 attributes->core.bits,
4770 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004771 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004772 if (status == PSA_SUCCESS) {
4773 status = psa_finish_key_creation(slot, driver, key);
4774 }
4775 if (status != PSA_SUCCESS) {
4776 psa_fail_key_creation(slot, driver);
4777 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004779 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004780}
4781
Gilles Peskineeab56e42018-07-12 17:12:33 +02004782
4783
4784/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004785/* Key derivation */
4786/****************************************************************/
4787
Steven Cooreman932ffb72021-02-15 12:14:32 +01004788#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004789static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004790{
4791#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004792 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4793 return 1;
4794 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004795#endif
4796#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004797 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4798 return 1;
4799 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004800#endif
4801#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004802 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4803 return 1;
4804 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004805#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004806 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004807}
4808
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004809static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004810{
4811 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004812 psa_status_t status = psa_hash_setup(&operation, alg);
4813 psa_hash_abort(&operation);
4814 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004815}
4816
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304817static psa_status_t psa_key_derivation_set_maximum_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004818 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004819 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004820{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004821 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4822 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4823 if (hash_size == 0) {
4824 return PSA_ERROR_NOT_SUPPORTED;
4825 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004826
4827 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4828 * we might fail later, which is somewhat unfriendly and potentially
4829 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004830 psa_status_t status = psa_hash_try_support(hash_alg);
4831 if (status != PSA_SUCCESS) {
4832 return status;
4833 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004834
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304835#if defined(PSA_WANT_ALG_HKDF)
4836 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4837 operation->capacity = 255 * hash_size;
4838 } else
4839#endif
4840#if defined(PSA_WANT_ALG_TLS12_PRF)
4841 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
4842 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4843 operation->capacity = SIZE_MAX;
4844 } else
4845#endif
4846#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
4847 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
4848 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4849 /* Master Secret is always 48 bytes
4850 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
4851 operation->capacity = 48U;
4852 } else
4853#endif
4854 {
4855 (void) hash_size;
4856 status = PSA_ERROR_NOT_SUPPORTED;
4857 }
4858 return status;
4859}
4860
4861
4862static psa_status_t psa_key_derivation_setup_kdf(
4863 psa_key_derivation_operation_t *operation,
4864 psa_algorithm_t kdf_alg)
4865{
4866 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4867 * initialisers for this union leave some bytes unspecified.) */
4868 memset(&operation->ctx, 0, sizeof(operation->ctx));
4869 /* Make sure that kdf_alg is a supported key derivation algorithm. */
4870 if (!is_kdf_alg_supported(kdf_alg)) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004871 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004872 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004873
Kusumit Ghoderaobfa27e32024-02-02 16:32:55 +05304874 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
4875 kdf_alg);
4876 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004877}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004879static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004880{
4881#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004882 if (alg == PSA_ALG_ECDH) {
4883 return PSA_SUCCESS;
4884 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004885#endif
4886 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004887 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004888}
John Durkop07cc04a2020-11-16 22:08:34 -08004889#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004891psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4892 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004893{
4894 psa_status_t status;
4895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004896 if (operation->alg != 0) {
4897 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004898 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004900 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4901 return PSA_ERROR_INVALID_ARGUMENT;
4902 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4903#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4904 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4905 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4906 status = psa_key_agreement_try_support(ka_alg);
4907 if (status != PSA_SUCCESS) {
4908 return status;
4909 }
4910 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4911#else
4912 return PSA_ERROR_NOT_SUPPORTED;
4913#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4914 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4915#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4916 status = psa_key_derivation_setup_kdf(operation, alg);
4917#else
4918 return PSA_ERROR_NOT_SUPPORTED;
4919#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4920 } else {
4921 return PSA_ERROR_INVALID_ARGUMENT;
4922 }
4923
4924 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004926 }
4927 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004928}
4929
John Durkopd0321952020-10-29 21:37:36 -07004930#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004931static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4932 psa_algorithm_t hash_alg,
4933 psa_key_derivation_step_t step,
4934 const uint8_t *data,
4935 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004936{
4937 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004938 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004939 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004940 if (hkdf->state != HKDF_STATE_INIT) {
4941 return PSA_ERROR_BAD_STATE;
4942 } else {
4943 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4944 hash_alg,
4945 data, data_length);
4946 if (status != PSA_SUCCESS) {
4947 return status;
4948 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004949 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004950 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004951 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004952 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004953 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004954 if (hkdf->state == HKDF_STATE_INIT) {
4955 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4956 hash_alg,
4957 NULL, 0);
4958 if (status != PSA_SUCCESS) {
4959 return status;
4960 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004961 hkdf->state = HKDF_STATE_STARTED;
4962 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004963 if (hkdf->state != HKDF_STATE_STARTED) {
4964 return PSA_ERROR_BAD_STATE;
4965 }
4966 status = psa_mac_update(&hkdf->hmac,
4967 data, data_length);
4968 if (status != PSA_SUCCESS) {
4969 return status;
4970 }
4971 status = psa_mac_sign_finish(&hkdf->hmac,
4972 hkdf->prk,
4973 sizeof(hkdf->prk),
4974 &data_length);
4975 if (status != PSA_SUCCESS) {
4976 return status;
4977 }
4978 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004979 hkdf->block_number = 0;
4980 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004981 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004982 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004983 if (hkdf->state == HKDF_STATE_OUTPUT) {
4984 return PSA_ERROR_BAD_STATE;
4985 }
4986 if (hkdf->info_set) {
4987 return PSA_ERROR_BAD_STATE;
4988 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004989 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004990 if (data_length != 0) {
4991 hkdf->info = mbedtls_calloc(1, data_length);
4992 if (hkdf->info == NULL) {
4993 return PSA_ERROR_INSUFFICIENT_MEMORY;
4994 }
4995 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004996 }
4997 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004998 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004999 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005000 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005001 }
5002}
John Durkop07cc04a2020-11-16 22:08:34 -08005003#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005004
John Durkop07cc04a2020-11-16 22:08:34 -08005005#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5006 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005007static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
5008 const uint8_t *data,
5009 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01005010{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005011 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
5012 return PSA_ERROR_BAD_STATE;
5013 }
Janos Follathf08e2652019-06-13 09:05:41 +01005014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005015 if (data_length != 0) {
5016 prf->seed = mbedtls_calloc(1, data_length);
5017 if (prf->seed == NULL) {
5018 return PSA_ERROR_INSUFFICIENT_MEMORY;
5019 }
Janos Follathf08e2652019-06-13 09:05:41 +01005020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005021 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005022 prf->seed_length = data_length;
5023 }
Janos Follathf08e2652019-06-13 09:05:41 +01005024
Gilles Peskine43f958b2020-12-13 14:55:14 +01005025 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005026
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005027 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01005028}
5029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005030static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
5031 const uint8_t *data,
5032 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01005033{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005034 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
5035 return PSA_ERROR_BAD_STATE;
5036 }
Janos Follath81550542019-06-13 14:26:34 +01005037
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005038 if (data_length != 0) {
5039 prf->secret = mbedtls_calloc(1, data_length);
5040 if (prf->secret == NULL) {
5041 return PSA_ERROR_INSUFFICIENT_MEMORY;
5042 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02005043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005044 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02005045 prf->secret_length = data_length;
5046 }
Janos Follath81550542019-06-13 14:26:34 +01005047
Gilles Peskine43f958b2020-12-13 14:55:14 +01005048 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005049
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005050 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01005051}
5052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005053static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
5054 const uint8_t *data,
5055 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01005056{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005057 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
5058 return PSA_ERROR_BAD_STATE;
5059 }
Janos Follath63028dd2019-06-13 09:15:47 +01005060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005061 if (data_length != 0) {
5062 prf->label = mbedtls_calloc(1, data_length);
5063 if (prf->label == NULL) {
5064 return PSA_ERROR_INSUFFICIENT_MEMORY;
5065 }
Janos Follath63028dd2019-06-13 09:15:47 +01005066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005067 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005068 prf->label_length = data_length;
5069 }
Janos Follath63028dd2019-06-13 09:15:47 +01005070
Gilles Peskine43f958b2020-12-13 14:55:14 +01005071 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005073 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01005074}
5075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005076static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
5077 psa_key_derivation_step_t step,
5078 const uint8_t *data,
5079 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01005080{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005081 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01005082 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005083 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01005084 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005085 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01005086 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005087 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01005088 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005089 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01005090 }
5091}
John Durkop07cc04a2020-11-16 22:08:34 -08005092#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5093 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5094
5095#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5096static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5097 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005098 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005099 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08005100{
5101 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005102 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08005103 uint8_t *cur = pms;
5104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005105 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
5106 return PSA_ERROR_INVALID_ARGUMENT;
5107 }
John Durkop07cc04a2020-11-16 22:08:34 -08005108
5109 /* Quoting RFC 4279, Section 2:
5110 *
5111 * The premaster secret is formed as follows: if the PSK is N octets
5112 * long, concatenate a uint16 with the value N, N zero octets, a second
5113 * uint16 with the value N, and the PSK itself.
5114 */
5115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005116 *cur++ = MBEDTLS_BYTE_1(data_length);
5117 *cur++ = MBEDTLS_BYTE_0(data_length);
5118 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005119 cur += data_length;
5120 *cur++ = pms[0];
5121 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005122 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005123 cur += data_length;
5124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005125 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08005126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005127 mbedtls_platform_zeroize(pms, sizeof(pms));
5128 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08005129}
Janos Follath6660f0e2019-06-17 08:44:03 +01005130
5131static psa_status_t psa_tls12_prf_psk_to_ms_input(
5132 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005133 psa_key_derivation_step_t step,
5134 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005135 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01005136{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005137 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
5138 return psa_tls12_prf_psk_to_ms_set_key(prf,
5139 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01005140 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005142 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01005143}
John Durkop07cc04a2020-11-16 22:08:34 -08005144#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005145
Gilles Peskineb8965192019-09-24 16:21:10 +02005146/** Check whether the given key type is acceptable for the given
5147 * input step of a key derivation.
5148 *
5149 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5150 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5151 * Both secret and non-secret inputs can alternatively have the type
5152 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5153 * that the input was passed as a buffer rather than via a key object.
5154 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005155static int psa_key_derivation_check_input_type(
5156 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005157 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02005158{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005159 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005160 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005161 if (key_type == PSA_KEY_TYPE_DERIVE) {
5162 return PSA_SUCCESS;
5163 }
5164 if (key_type == PSA_KEY_TYPE_NONE) {
5165 return PSA_SUCCESS;
5166 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005167 break;
5168 case PSA_KEY_DERIVATION_INPUT_LABEL:
5169 case PSA_KEY_DERIVATION_INPUT_SALT:
5170 case PSA_KEY_DERIVATION_INPUT_INFO:
5171 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005172 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
5173 return PSA_SUCCESS;
5174 }
5175 if (key_type == PSA_KEY_TYPE_NONE) {
5176 return PSA_SUCCESS;
5177 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005178 break;
5179 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005180 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005181}
5182
Janos Follathb80a94e2019-06-12 15:54:46 +01005183static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005184 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005185 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005186 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005187 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005188 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005189{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005190 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005191 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005193 status = psa_key_derivation_check_input_type(step, key_type);
5194 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005195 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005196 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005197
John Durkopd0321952020-10-29 21:37:36 -07005198#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005199 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5200 status = psa_hkdf_input(&operation->ctx.hkdf,
5201 PSA_ALG_HKDF_GET_HASH(kdf_alg),
5202 step, data, data_length);
5203 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005204#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5205#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005206 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5207 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
5208 step, data, data_length);
5209 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005210#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5211#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005212 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5213 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
5214 step, data, data_length);
5215 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005216#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005217 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005218 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005219 (void) data;
5220 (void) data_length;
5221 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005222 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005223 }
5224
Gilles Peskine224b0d62019-09-23 18:13:17 +02005225exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005226 if (status != PSA_SUCCESS) {
5227 psa_key_derivation_abort(operation);
5228 }
5229 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005230}
5231
Janos Follath51f4a0f2019-06-14 11:35:55 +01005232psa_status_t psa_key_derivation_input_bytes(
5233 psa_key_derivation_operation_t *operation,
5234 psa_key_derivation_step_t step,
Ryan Everett6f682062024-02-09 16:18:39 +00005235 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005236 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005237{
Ryan Everett6f682062024-02-09 16:18:39 +00005238 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5239 LOCAL_INPUT_DECLARE(data_external, data);
5240
5241 LOCAL_INPUT_ALLOC(data_external, data_length, data);
5242
5243 status = psa_key_derivation_input_internal(operation, step,
5244 PSA_KEY_TYPE_NONE,
5245 data, data_length);
David Horstmann42015332024-03-13 15:42:31 +00005246#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everett6f682062024-02-09 16:18:39 +00005247exit:
Ryan Everettd0d12fb2024-02-12 09:19:29 +00005248#endif
Ryan Everett6f682062024-02-09 16:18:39 +00005249 LOCAL_INPUT_FREE(data_external, data);
5250 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005251}
5252
Janos Follath51f4a0f2019-06-14 11:35:55 +01005253psa_status_t psa_key_derivation_input_key(
5254 psa_key_derivation_operation_t *operation,
5255 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005256 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005257{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005258 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005259 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005260 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005261
Ronald Cron5c522922020-11-14 16:35:34 +01005262 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005263 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5264 if (status != PSA_SUCCESS) {
5265 psa_key_derivation_abort(operation);
5266 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02005267 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005268
5269 /* Passing a key object as a SECRET input unlocks the permission
5270 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005271 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005272 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005273 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005275 status = psa_key_derivation_input_internal(operation,
5276 step, slot->attr.type,
5277 slot->key.data,
5278 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005280 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005282 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005283}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005284
5285
5286
5287/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005288/* Key agreement */
5289/****************************************************************/
5290
John Durkop6ba40d12020-11-10 08:50:04 -08005291#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005292static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
5293 size_t peer_key_length,
5294 const mbedtls_ecp_keypair *our_key,
5295 uint8_t *shared_secret,
5296 size_t shared_secret_size,
5297 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005298{
Steven Cooremand4867872020-08-05 16:31:39 +02005299 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005300 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005301 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005302 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005303 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
5304 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005305
Ronald Cron90857082020-11-25 14:58:33 +01005306 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005307 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5308 bits,
5309 peer_key,
5310 peer_key_length,
5311 &their_key);
5312 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005313 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005314 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005315
Jaeden Amero97271b32019-01-10 19:38:51 +00005316 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005317 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
5318 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005319 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005320 }
Jaeden Amero97271b32019-01-10 19:38:51 +00005321 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005322 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
5323 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005324 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005325 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005326
Jaeden Amero97271b32019-01-10 19:38:51 +00005327 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005328 mbedtls_ecdh_calc_secret(&ecdh,
5329 shared_secret_length,
5330 shared_secret, shared_secret_size,
5331 mbedtls_psa_get_random,
5332 MBEDTLS_PSA_RANDOM_STATE));
5333 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005334 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005335 }
5336 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005337 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005338 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005339
5340exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005341 if (status != PSA_SUCCESS) {
5342 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
5343 }
5344 mbedtls_ecdh_free(&ecdh);
5345 mbedtls_ecp_keypair_free(their_key);
5346 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02005347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005348 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005349}
John Durkop6ba40d12020-11-10 08:50:04 -08005350#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005351
Gilles Peskine01d718c2018-09-18 12:01:02 +02005352#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005354static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
5355 psa_key_slot_t *private_key,
5356 const uint8_t *peer_key,
5357 size_t peer_key_length,
5358 uint8_t *shared_secret,
5359 size_t shared_secret_size,
5360 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005361{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005362 mbedtls_ecp_keypair *ecp = NULL;
5363 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01005364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005365 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08005366#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005367 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005368 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
5369 return PSA_ERROR_INVALID_ARGUMENT;
5370 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005371 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005372 private_key->attr.type,
5373 private_key->attr.bits,
5374 private_key->key.data,
5375 private_key->key.bytes,
5376 &ecp);
5377 if (status != PSA_SUCCESS) {
5378 return status;
5379 }
5380 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
5381 ecp,
5382 shared_secret, shared_secret_size,
5383 shared_secret_length);
5384 mbedtls_ecp_keypair_free(ecp);
5385 mbedtls_free(ecp);
5386 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08005387#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005388 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005389 (void) ecp;
5390 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01005391 (void) private_key;
5392 (void) peer_key;
5393 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005394 (void) shared_secret;
5395 (void) shared_secret_size;
5396 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005397 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005398 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005399}
5400
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005401/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005402 * to potentially free embedded data structures and wipe confidential data.
5403 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005404static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5405 psa_key_derivation_step_t step,
5406 psa_key_slot_t *private_key,
5407 const uint8_t *peer_key,
5408 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005409{
5410 psa_status_t status;
5411 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5412 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005413 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005414
5415 /* Step 1: run the secret agreement algorithm to generate the shared
5416 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005417 status = psa_key_agreement_raw_internal(ka_alg,
5418 private_key,
5419 peer_key, peer_key_length,
5420 shared_secret,
5421 sizeof(shared_secret),
5422 &shared_secret_length);
5423 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005424 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005425 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005426
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005427 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005428 * the shared secret. A shared secret is permitted wherever a key
5429 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005430 status = psa_key_derivation_input_internal(operation, step,
5431 PSA_KEY_TYPE_DERIVE,
5432 shared_secret,
5433 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005434exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005435 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5436 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005437}
5438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005439psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5440 psa_key_derivation_step_t step,
5441 mbedtls_svc_key_id_t private_key,
Thomas Daubney9da359f2024-02-15 13:15:47 +00005442 const uint8_t *peer_key_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005443 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005444{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005445 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005446 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005447 psa_key_slot_t *slot;
Thomas Daubney9da359f2024-02-15 13:15:47 +00005448 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005450 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5451 return PSA_ERROR_INVALID_ARGUMENT;
5452 }
Ronald Cron5c522922020-11-14 16:35:34 +01005453 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005454 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5455 if (status != PSA_SUCCESS) {
5456 return status;
5457 }
Thomas Daubney9da359f2024-02-15 13:15:47 +00005458
Thomas Daubneyca928312024-03-12 17:53:30 +00005459 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005460 status = psa_key_agreement_internal(operation, step,
5461 slot,
5462 peer_key, peer_key_length);
Thomas Daubney9da359f2024-02-15 13:15:47 +00005463
David Horstmann42015332024-03-13 15:42:31 +00005464#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney9da359f2024-02-15 13:15:47 +00005465exit:
Thomas Daubney3c0c6b12024-02-15 14:25:08 +00005466#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005467 if (status != PSA_SUCCESS) {
5468 psa_key_derivation_abort(operation);
5469 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005470 /* If a private key has been added as SECRET, we allow the derived
5471 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005472 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005473 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005474 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005475 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005477 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005478
Thomas Daubney9da359f2024-02-15 13:15:47 +00005479 LOCAL_INPUT_FREE(peer_key_external, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005480 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005481}
5482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005483psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5484 mbedtls_svc_key_id_t private_key,
Thomas Daubney43042762024-02-15 12:57:26 +00005485 const uint8_t *peer_key_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005486 size_t peer_key_length,
Thomas Daubney43042762024-02-15 12:57:26 +00005487 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005488 size_t output_size,
5489 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005490{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005491 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005492 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005493 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005494 size_t expected_length;
Thomas Daubney43042762024-02-15 12:57:26 +00005495 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
5496 LOCAL_OUTPUT_DECLARE(output_external, output);
Thomas Daubney0736df32024-02-21 12:28:20 +00005497 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005499 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005500 status = PSA_ERROR_INVALID_ARGUMENT;
5501 goto exit;
5502 }
Ronald Cron5c522922020-11-14 16:35:34 +01005503 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005504 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5505 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005506 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005507 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005508
Gilles Peskine42313fb2022-04-14 00:17:15 +02005509 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5510 * for the output size. The PSA specification only guarantees that this
5511 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5512 * but it might be nice to allow smaller buffers if the output fits.
5513 * At the time of writing this comment, with only ECDH implemented,
5514 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5515 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5516 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005517 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005518 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5519 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005520 status = PSA_ERROR_BUFFER_TOO_SMALL;
5521 goto exit;
5522 }
5523
Thomas Daubney43042762024-02-15 12:57:26 +00005524 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005525 status = psa_key_agreement_raw_internal(alg, slot,
5526 peer_key, peer_key_length,
5527 output, output_size,
5528 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005529
5530exit:
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005531 /* Check for successful allocation of output,
5532 * with an unsuccessful status. */
Thomas Daubney2ea8d8f2024-02-21 15:16:01 +00005533 if (output != NULL && status != PSA_SUCCESS) {
5534 /* If an error happens and is not handled properly, the output
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005535 * may be used as a key to protect sensitive data. Arrange for such
5536 * a key to be random, which is likely to result in decryption or
5537 * verification errors. This is better than filling the buffer with
5538 * some constant data such as zeros, which would result in the data
5539 * being protected with a reproducible, easily knowable key.
5540 */
David Horstmann65bf12c2024-02-06 15:27:49 +00005541 psa_generate_random_internal(output, output_size);
Thomas Daubney2ea8d8f2024-02-21 15:16:01 +00005542 *output_length = output_size;
Thomas Daubney09cf4f22024-02-22 11:08:22 +00005543 }
5544
5545 if (output == NULL) {
Thomas Daubney0736df32024-02-21 12:28:20 +00005546 /* output allocation failed. */
5547 *output_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005548 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005550 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005551
Thomas Daubney43042762024-02-15 12:57:26 +00005552 LOCAL_INPUT_FREE(peer_key_external, peer_key);
5553 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005554 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005555}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005556
5557
5558/****************************************************************/
5559/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005560/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005561
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005562#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5563#include "mbedtls/entropy_poll.h"
5564#endif
5565
Gilles Peskine30524eb2020-11-13 17:02:26 +01005566/** Initialize the PSA random generator.
5567 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005568static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005569{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005570#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005571 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005572#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5573
Gilles Peskine30524eb2020-11-13 17:02:26 +01005574 /* Set default configuration if
5575 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005576 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005577 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005578 }
5579 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005580 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005581 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005583 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005584#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5585 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5586 /* The PSA entropy injection feature depends on using NV seed as an entropy
5587 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005588 mbedtls_entropy_add_source(&rng->entropy,
5589 mbedtls_nv_seed_poll, NULL,
5590 MBEDTLS_ENTROPY_BLOCK_SIZE,
5591 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005592#endif
5593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005594 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005595#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005596}
5597
5598/** Deinitialize the PSA random generator.
5599 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005600static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005601{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005602#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005603 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005604#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005605 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5606 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005607#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005608}
5609
5610/** Seed the PSA random generator.
5611 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005612static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005613{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005614#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5615 /* Do nothing: the external RNG seeds itself. */
5616 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005617 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005618#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005619 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005620 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5621 drbg_seed, sizeof(drbg_seed) - 1);
5622 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005623#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005624}
5625
David Horstmann65bf12c2024-02-06 15:27:49 +00005626psa_status_t psa_generate_random(uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005627 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005628{
David Horstmann65bf12c2024-02-06 15:27:49 +00005629 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005630
David Horstmann65bf12c2024-02-06 15:27:49 +00005631 LOCAL_OUTPUT_DECLARE(output_external, output);
5632 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005633
David Horstmann65bf12c2024-02-06 15:27:49 +00005634 status = psa_generate_random_internal(output, output_size);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005635
David Horstmann42015332024-03-13 15:42:31 +00005636#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann65bf12c2024-02-06 15:27:49 +00005637exit:
David Horstmann10e44f32024-02-28 14:17:10 +00005638#endif
David Horstmann65bf12c2024-02-06 15:27:49 +00005639 LOCAL_OUTPUT_FREE(output_external, output);
5640 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005641}
5642
Gilles Peskine8814fc42020-12-14 15:33:44 +01005643/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005644 *
5645 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5646 * `psa_generate_random(...)`. The state parameter is ignored since the
5647 * PSA API doesn't support passing an explicit state.
5648 *
5649 * In the non-external case, psa_generate_random() calls an
5650 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5651 * and semantics as mbedtls_psa_get_random(). As an optimization,
5652 * instead of doing this back-and-forth between the PSA API and the
5653 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5654 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005655 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005656#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5657int mbedtls_psa_get_random(void *p_rng,
5658 unsigned char *output,
5659 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005660{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005661 /* This function takes a pointer to the RNG state because that's what
5662 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5663 * its own state internally and doesn't let the caller access that state.
5664 * So we just ignore the state parameter, and in practice we'll pass
5665 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005666 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005667 psa_status_t status = psa_generate_random(output, output_size);
5668 if (status == PSA_SUCCESS) {
5669 return 0;
5670 } else {
5671 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5672 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005673}
5674#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5675
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005676#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005677psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5678 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005679{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005680 if (global_data.initialized) {
5681 return PSA_ERROR_NOT_PERMITTED;
5682 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005684 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5685 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5686 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5687 return PSA_ERROR_INVALID_ARGUMENT;
5688 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005690 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005691}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005692#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005693
Ronald Cron3772afe2021-02-08 16:10:05 +01005694/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005695 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005696 * \param type The key type
5697 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005698 *
5699 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005700 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005701 * \retval #PSA_ERROR_INVALID_ARGUMENT
5702 * The size in bits of the key is not valid.
5703 * \retval #PSA_ERROR_NOT_SUPPORTED
5704 * The type and/or the size in bits of the key or the combination of
5705 * the two is not supported.
5706 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005707static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005708 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005709{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005710 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005712 if (key_type_is_raw_bytes(type)) {
5713 status = validate_unstructured_key_bit_size(type, bits);
5714 if (status != PSA_SUCCESS) {
5715 return status;
5716 }
5717 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005718#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005719 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5720 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5721 return PSA_ERROR_NOT_SUPPORTED;
5722 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005723
Ronald Cron01b2aba2020-10-05 09:42:02 +02005724 /* Accept only byte-aligned keys, for the same reasons as
5725 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005726 if (bits % 8 != 0) {
5727 return PSA_ERROR_NOT_SUPPORTED;
5728 }
5729 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005730#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005731
Ronald Cron5c4d3862020-12-07 11:07:24 +01005732#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005733 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005734 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005735 return PSA_SUCCESS;
5736 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005737#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005738 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005739 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005740 }
5741
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005742 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005743}
5744
Ronald Cron55ed0592020-10-05 10:30:40 +02005745psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005746 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005747 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005748{
5749 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005750 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005752 if ((attributes->domain_parameters == NULL) &&
5753 (attributes->domain_parameters_size != 0)) {
5754 return PSA_ERROR_INVALID_ARGUMENT;
5755 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005757 if (key_type_is_raw_bytes(type)) {
5758 status = psa_generate_random(key_buffer, key_buffer_size);
5759 if (status != PSA_SUCCESS) {
5760 return status;
5761 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005762
David Brown12ca5032021-02-11 11:02:00 -07005763#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005764 if (type == PSA_KEY_TYPE_DES) {
5765 psa_des_set_key_parity(key_buffer, key_buffer_size);
5766 }
David Brown8107e312021-02-12 08:08:46 -07005767#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005768 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005769
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005770#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5771 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005772 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5773 return mbedtls_psa_rsa_generate_key(attributes,
5774 key_buffer,
5775 key_buffer_size,
5776 key_buffer_length);
5777 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005778#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5779 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005780
John Durkop9814fa22020-11-04 12:28:15 -08005781#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005782 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5783 return mbedtls_psa_ecp_generate_key(attributes,
5784 key_buffer,
5785 key_buffer_size,
5786 key_buffer_length);
5787 } else
John Durkop9814fa22020-11-04 12:28:15 -08005788#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005789 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005790 (void) key_buffer_length;
5791 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005792 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005794 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005795}
Darryl Green0c6575a2018-11-07 16:05:30 +00005796
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005797psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5798 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005799{
5800 psa_status_t status;
5801 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005802 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005803 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005804
Ronald Cron81709fc2020-11-14 12:10:32 +01005805 *key = MBEDTLS_SVC_KEY_ID_INIT;
5806
Gilles Peskine0f84d622019-09-12 19:03:13 +02005807 /* Reject any attempt to create a zero-length key so that we don't
5808 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005809 if (psa_get_key_bits(attributes) == 0) {
5810 return PSA_ERROR_INVALID_ARGUMENT;
5811 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005812
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005813 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005814 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5815 return PSA_ERROR_INVALID_ARGUMENT;
5816 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005818 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5819 &slot, &driver);
5820 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005821 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005822 }
Gilles Peskine11792082019-08-06 18:36:36 +02005823
Ronald Cron977c2472020-10-13 08:32:21 +02005824 /* In the case of a transparent key or an opaque key stored in local
5825 * storage (thus not in the case of generating a key in a secure element
5826 * or cryptoprocessor with storage), we have to allocate a buffer to
5827 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005828 if (slot->key.data == NULL) {
5829 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5830 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005831 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005832 attributes->core.type, attributes->core.bits);
5833 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005834 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005835 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005836
5837 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005838 attributes->core.type,
5839 attributes->core.bits);
5840 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005841 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005842 attributes, &key_buffer_size);
5843 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005844 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005845 }
Ronald Cron977c2472020-10-13 08:32:21 +02005846 }
Ronald Cron977c2472020-10-13 08:32:21 +02005847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005848 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5849 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005850 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005851 }
Ronald Cron977c2472020-10-13 08:32:21 +02005852 }
5853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005854 status = psa_driver_wrapper_generate_key(attributes,
5855 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005857 if (status != PSA_SUCCESS) {
5858 psa_remove_key_data_from_memory(slot);
5859 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005860
Gilles Peskine11792082019-08-06 18:36:36 +02005861exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005862 if (status == PSA_SUCCESS) {
5863 status = psa_finish_key_creation(slot, driver, key);
5864 }
5865 if (status != PSA_SUCCESS) {
5866 psa_fail_key_creation(slot, driver);
5867 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005868
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005869 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005870}
5871
Gilles Peskinee59236f2018-01-27 23:32:46 +01005872/****************************************************************/
5873/* Module setup */
5874/****************************************************************/
5875
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005876#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005877psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005878 void (* entropy_init)(mbedtls_entropy_context *ctx),
5879 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005880{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005881 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5882 return PSA_ERROR_BAD_STATE;
5883 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005884 global_data.rng.entropy_init = entropy_init;
5885 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005886 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005887}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005888#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005889
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005890void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005891{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005892 psa_wipe_all_key_slots();
5893 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5894 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005895 }
5896 /* Wipe all remaining data, including configuration.
5897 * In particular, this sets all state indicator to the value
5898 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005899 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005900
5901 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005902 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005903}
5904
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005905#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5906/** Recover a transaction that was interrupted by a power failure.
5907 *
5908 * This function is called during initialization, before psa_crypto_init()
5909 * returns. If this function returns a failure status, the initialization
5910 * fails.
5911 */
5912static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005913 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005914{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005915 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005916 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5917 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005918 /* TODO - fall through to the failure case until this
5919 * is implemented.
5920 * https://github.com/ARMmbed/mbed-crypto/issues/218
5921 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005922 default:
5923 /* We found an unsupported transaction in the storage.
5924 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005925 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005926 }
5927}
5928#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005930psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005931{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005932 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005933
Gilles Peskinec6b69072018-11-20 21:42:52 +01005934 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005935 if (global_data.initialized != 0) {
5936 return PSA_SUCCESS;
5937 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005938
Gilles Peskine30524eb2020-11-13 17:02:26 +01005939 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005940 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005941 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005942 status = mbedtls_psa_random_seed(&global_data.rng);
5943 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005944 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005945 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005946 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005947
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005948 status = psa_initialize_key_slots();
5949 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005950 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005951 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005952
Ronald Cron088d5d02021-04-10 16:57:30 +02005953 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005954 status = psa_driver_wrapper_init();
5955 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005956 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005957 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005958
Gilles Peskine4b734222019-07-24 15:56:31 +02005959#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005960 status = psa_crypto_load_transaction();
5961 if (status == PSA_SUCCESS) {
5962 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5963 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005964 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005965 }
5966 status = psa_crypto_stop_transaction();
5967 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005968 /* There's no transaction to complete. It's all good. */
5969 status = PSA_SUCCESS;
5970 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005971#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005972
Gilles Peskinec6b69072018-11-20 21:42:52 +01005973 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005974 global_data.initialized = 1;
5975
5976exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005977 if (status != PSA_SUCCESS) {
5978 mbedtls_psa_crypto_free();
5979 }
5980 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005981}
5982
David Horstmann114d8242023-12-07 18:39:17 +00005983/* Memory copying test hooks. These are called before input copy, after input
5984 * copy, before output copy and after output copy, respectively.
5985 * They are used by memory-poisoning tests to temporarily unpoison buffers
5986 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00005987#if defined(MBEDTLS_TEST_HOOKS)
5988void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5989void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5990void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5991void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5992#endif
5993
David Horstmanndf493552023-11-15 15:18:30 +00005994/** Copy from an input buffer to a local copy.
5995 *
5996 * \param[in] input Pointer to input buffer.
5997 * \param[in] input_len Length of the input buffer.
5998 * \param[out] input_copy Pointer to a local copy in which to store the input data.
5999 * \param[out] input_copy_len Length of the local copy buffer.
6000 * \return #PSA_SUCCESS, if the buffer was successfully
6001 * copied.
6002 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
6003 * copy is too small to hold contents of the
6004 * input buffer.
6005 */
6006MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00006007psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
6008 uint8_t *input_copy, size_t input_copy_len)
6009{
6010 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00006011 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00006012 }
6013
David Horstmann0760b152023-11-24 16:21:04 +00006014#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006015 if (psa_input_pre_copy_hook != NULL) {
6016 psa_input_pre_copy_hook(input, input_len);
6017 }
David Horstmann0760b152023-11-24 16:21:04 +00006018#endif
6019
David Horstmann660027f2023-11-15 17:33:47 +00006020 if (input_len > 0) {
6021 memcpy(input_copy, input, input_len);
6022 }
David Horstmann957f9802023-10-30 20:29:43 +00006023
David Horstmann0760b152023-11-24 16:21:04 +00006024#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006025 if (psa_input_post_copy_hook != NULL) {
6026 psa_input_post_copy_hook(input, input_len);
6027 }
David Horstmann0760b152023-11-24 16:21:04 +00006028#endif
6029
David Horstmann957f9802023-10-30 20:29:43 +00006030 return PSA_SUCCESS;
6031}
6032
David Horstmanndf493552023-11-15 15:18:30 +00006033/** Copy from a local output buffer into a user-supplied one.
6034 *
6035 * \param[in] output_copy Pointer to a local buffer containing the output.
6036 * \param[in] output_copy_len Length of the local buffer.
6037 * \param[out] output Pointer to user-supplied output buffer.
6038 * \param[out] output_len Length of the user-supplied output buffer.
6039 * \return #PSA_SUCCESS, if the buffer was successfully
6040 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00006041 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00006042 * user-supplied output buffer is too small to
6043 * hold the contents of the local buffer.
6044 */
6045MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00006046psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
6047 uint8_t *output, size_t output_len)
6048{
6049 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00006050 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00006051 }
David Horstmann660027f2023-11-15 17:33:47 +00006052
David Horstmann0760b152023-11-24 16:21:04 +00006053#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006054 if (psa_output_pre_copy_hook != NULL) {
6055 psa_output_pre_copy_hook(output, output_len);
6056 }
David Horstmann0760b152023-11-24 16:21:04 +00006057#endif
6058
David Horstmann660027f2023-11-15 17:33:47 +00006059 if (output_copy_len > 0) {
6060 memcpy(output, output_copy, output_copy_len);
6061 }
6062
David Horstmann0760b152023-11-24 16:21:04 +00006063#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006064 if (psa_output_post_copy_hook != NULL) {
6065 psa_output_post_copy_hook(output, output_len);
6066 }
David Horstmann0760b152023-11-24 16:21:04 +00006067#endif
6068
David Horstmann2bd296e2023-10-30 20:37:12 +00006069 return PSA_SUCCESS;
6070}
6071
David Horstmann81a0d572023-11-20 17:15:32 +00006072psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
6073 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006074{
6075 psa_status_t status;
6076
David Horstmann0d52c712023-11-23 15:50:37 +00006077 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006078
David Horstmann726bf052023-11-15 18:11:26 +00006079 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006080 return PSA_SUCCESS;
6081 }
6082
David Horstmann81a0d572023-11-20 17:15:32 +00006083 local_input->buffer = mbedtls_calloc(input_len, 1);
6084 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006085 /* Since we dealt with the zero-length case above, we know that
6086 * a NULL return value means a failure of allocation. */
6087 return PSA_ERROR_INSUFFICIENT_MEMORY;
6088 }
David Horstmann81a0d572023-11-20 17:15:32 +00006089 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006090
David Horstmann81a0d572023-11-20 17:15:32 +00006091 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006092
6093 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00006094 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006095 if (status != PSA_SUCCESS) {
6096 goto error;
6097 }
6098
6099 return PSA_SUCCESS;
6100
6101error:
David Horstmann81a0d572023-11-20 17:15:32 +00006102 mbedtls_free(local_input->buffer);
6103 local_input->buffer = NULL;
6104 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006105 return status;
6106}
6107
David Horstmann81a0d572023-11-20 17:15:32 +00006108void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00006109{
David Horstmann81a0d572023-11-20 17:15:32 +00006110 mbedtls_free(local_input->buffer);
6111 local_input->buffer = NULL;
6112 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00006113}
6114
David Horstmann1a76ab12023-11-20 12:54:09 +00006115psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
6116 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00006117{
David Horstmann0d52c712023-11-23 15:50:37 +00006118 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00006119
David Horstmann726bf052023-11-15 18:11:26 +00006120 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006121 return PSA_SUCCESS;
6122 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006123 local_output->buffer = mbedtls_calloc(output_len, 1);
6124 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006125 /* Since we dealt with the zero-length case above, we know that
6126 * a NULL return value means a failure of allocation. */
6127 return PSA_ERROR_INSUFFICIENT_MEMORY;
6128 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006129 local_output->length = output_len;
6130 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00006131
6132 return PSA_SUCCESS;
6133}
6134
David Horstmann1a76ab12023-11-20 12:54:09 +00006135psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00006136{
David Horstmann761761f2023-11-15 15:57:32 +00006137 psa_status_t status;
6138
David Horstmann1a76ab12023-11-20 12:54:09 +00006139 if (local_output->buffer == NULL) {
6140 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006141 return PSA_SUCCESS;
6142 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006143 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00006144 /* We have an internal copy but nothing to copy back to. */
6145 return PSA_ERROR_CORRUPTION_DETECTED;
6146 }
6147
David Horstmann1a76ab12023-11-20 12:54:09 +00006148 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
6149 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00006150 if (status != PSA_SUCCESS) {
6151 return status;
6152 }
David Horstmannaeeb2742023-11-08 17:44:18 +00006153
David Horstmann1a76ab12023-11-20 12:54:09 +00006154 mbedtls_free(local_output->buffer);
6155 local_output->buffer = NULL;
6156 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006157
6158 return PSA_SUCCESS;
6159}
6160
Gilles Peskinee59236f2018-01-27 23:32:46 +01006161#endif /* MBEDTLS_PSA_CRYPTO_C */