blob: e4a6b5ac80f5da98a55fa85e9714b7bd6ec256b3 [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 Horstmannc0a2c302023-11-29 17:24:08 +0000109#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
David Horstmann34980bd2023-11-29 17:07:13 +0000110/* Substitute an input buffer for a local copy of itself.
111 * Assumptions:
112 * - psa_status_t status exists
113 * - An exit label is declared
David Horstmann1f532132023-12-08 14:08:18 +0000114 * - The name <input>_copy is not used for the given value of <input>
David Horstmann34980bd2023-11-29 17:07:13 +0000115 */
116#define SWAP_FOR_LOCAL_INPUT(input, length) \
David Horstmannf96ae672023-12-13 14:03:40 +0000117 psa_crypto_local_input_t input##_copy = PSA_CRYPTO_LOCAL_INPUT_INIT; \
118 status = psa_crypto_local_input_alloc(input, length, &input##_copy); \
David Horstmann34980bd2023-11-29 17:07:13 +0000119 if (status != PSA_SUCCESS) { \
120 goto exit; \
121 } \
David Horstmannf96ae672023-12-13 14:03:40 +0000122 input = input##_copy.buffer;
David Horstmann34980bd2023-11-29 17:07:13 +0000123
David Horstmann1f532132023-12-08 14:08:18 +0000124/* Free the substituted input buffer copy created by SWAP_FOR_LOCAL_INPUT().
125 * Note that this does not restore the pointer to the original buffer.
126 * Assumptions:
127 * - psa_crypto_local_input_t <input>_copy exists, for the given value of
128 * <input>
129 * - <input>_copy was previously allocated by psa_crypto_local_input_alloc()
130 * - <input> points to <input>_copy.buffer
131 */
David Horstmann34980bd2023-11-29 17:07:13 +0000132#define FREE_LOCAL_INPUT(input) \
David Horstmann1f532132023-12-08 14:08:18 +0000133 input = NULL; \
David Horstmannf96ae672023-12-13 14:03:40 +0000134 psa_crypto_local_input_free(&input##_copy);
David Horstmann34980bd2023-11-29 17:07:13 +0000135
136/* Substitute an output buffer for a local copy of itself.
137 * Assumptions:
138 * - psa_status_t status exists
139 * - An exit label is declared
David Horstmann1f532132023-12-08 14:08:18 +0000140 * - The name <output>_copy is not used for the given value of <output>
David Horstmann34980bd2023-11-29 17:07:13 +0000141 */
142#define SWAP_FOR_LOCAL_OUTPUT(output, length) \
David Horstmannf96ae672023-12-13 14:03:40 +0000143 psa_crypto_local_output_t output##_copy = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
144 status = psa_crypto_local_output_alloc(output, length, &output##_copy); \
David Horstmann34980bd2023-11-29 17:07:13 +0000145 if (status != PSA_SUCCESS) { \
146 goto exit; \
147 } \
David Horstmannf96ae672023-12-13 14:03:40 +0000148 output = output##_copy.buffer;
David Horstmann34980bd2023-11-29 17:07:13 +0000149
David Horstmann1f532132023-12-08 14:08:18 +0000150/* Free the substituted output buffer copy created by SWAP_FOR_LOCAL_OUTPUT()
151 * after first copying back its contents to the original buffer.
152 * Note that this does not restore the pointer to the original buffer.
153 * Assumptions:
154 * - psa_crypto_local_output_t <output>_copy exists, for the given value of
155 * <output>
156 * - <output>_copy was previously allocated by psa_crypto_local_output_alloc()
157 * - <output> points to <output>_copy.buffer
158 * - psa_status_t status exists
David Horstmanne800aa82023-12-11 15:45:20 +0000159 * - The name <output>_local_output_status is not used for the given value of
160 * <output>
David Horstmann1f532132023-12-08 14:08:18 +0000161 */
David Horstmann34980bd2023-11-29 17:07:13 +0000162#define FREE_LOCAL_OUTPUT(output) \
David Horstmann1f532132023-12-08 14:08:18 +0000163 output = NULL; \
David Horstmann2b70a662023-12-13 14:09:08 +0000164 do { \
165 psa_status_t local_output_status; \
166 local_output_status = psa_crypto_local_output_free(&output##_copy); \
167 if (local_output_status != PSA_SUCCESS) { \
168 /* Since this error case is an internal error, it's more serious than \
169 * any existing error code and so it's fine to overwrite the existing \
170 * status. */ \
171 status = local_output_status; \
172 } \
173 } while (0)
David Horstmannc0a2c302023-11-29 17:24:08 +0000174#else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
175#define SWAP_FOR_LOCAL_INPUT(input, length)
176#define FREE_LOCAL_INPUT(input)
177#define SWAP_FOR_LOCAL_OUTPUT(output, length)
178#define FREE_LOCAL_OUTPUT(output)
179#endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmann34980bd2023-11-29 17:07:13 +0000180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100182{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100183 /* Mbed TLS error codes can combine a high-level error code and a
184 * low-level error code. The low-level error usually reflects the
185 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 int low_level_ret = -(-ret & 0x007f);
187 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100188 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100190
191 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
192 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
193 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200198 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200200
Gilles Peskine9a944802018-06-21 09:35:35 +0200201 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
202 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
203 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
204 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
205 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200207 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200209 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200211
Jaeden Amero93e21112019-02-20 13:57:28 +0000212#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000213 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000214#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
215 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
216#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100217 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100219 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100221
Jaeden Amero93e21112019-02-20 13:57:28 +0000222#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000223 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000224#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
225 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
226#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
232 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100238
Gilles Peskine26869f22019-05-06 15:25:00 +0200239 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200241
242 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200244 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200246
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100255 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100257 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100259 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100261 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100263
264 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
268 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100269 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
270 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100271 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100273 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
274 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100276 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100278#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100279
280 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100282 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100284
Gilles Peskinee59236f2018-01-27 23:32:46 +0100285 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
286 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
287 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100289
290 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296
Gilles Peskine82e57d12020-11-13 21:31:17 +0100297#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100299 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
300 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100301 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100303 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
304 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100306 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100308#endif
309
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200310 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
311 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
312 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200314
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100321 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200323 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100325
Gilles Peskinef76aa772018-10-29 19:24:33 +0100326 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100328 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100330 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100332 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100334 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100336 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100338 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100340 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100342
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100343 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100345 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
346 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100348 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100350 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
351 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100353 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100355 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
356 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100358 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100360 case MBEDTLS_ERR_PK_INVALID_ALG:
361 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
362 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100364 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100366 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100368
Gilles Peskineff2d2002019-05-06 15:26:23 +0200369 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200371 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200373
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200374 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200376
Gilles Peskinea5905292018-02-07 20:59:33 +0100377 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100379 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100381 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100383 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100385 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
386 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100388 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100390 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100392 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100394 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100396 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100398
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200399 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
400 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
401 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200403
Gilles Peskinea5905292018-02-07 20:59:33 +0100404 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100406 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100408
itayzafrir5c753392018-05-08 11:18:38 +0300409 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300410 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300412 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300414 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300416 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
417 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300419 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100421 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300423 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100424 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100425
Janos Follatha13b9052019-11-22 12:48:59 +0000426 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300428
Gilles Peskinee59236f2018-01-27 23:32:46 +0100429 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100431 }
432}
433
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200434
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200435
436
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100437/****************************************************************/
438/* Key management */
439/****************************************************************/
440
John Durkop9814fa22020-11-04 12:28:15 -0800441#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800442 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100443 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
444 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
445 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
447 size_t bits,
448 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200449{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100451 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100453#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100454 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100456#endif
457#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100458 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100460#endif
461#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100462 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100464#endif
465#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100466 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100467 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100468#endif
469#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100470 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100472 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 if (bits_is_sloppy) {
474 return MBEDTLS_ECP_DP_SECP521R1;
475 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100476 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100477#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100478 }
479 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100480
Paul Elliott8ff510a2020-06-02 17:19:28 +0100481 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100483#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100484 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100486#endif
487#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100488 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100490#endif
491#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100492 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100493 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100494#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100495 }
496 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100497
Paul Elliott8ff510a2020-06-02 17:19:28 +0100498 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100499 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100500#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100501 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100503 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100504 if (bits_is_sloppy) {
505 return MBEDTLS_ECP_DP_CURVE25519;
506 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100507 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100508#endif
509#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100510 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100511 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100512#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100513 }
514 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100515
Paul Elliott8ff510a2020-06-02 17:19:28 +0100516 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100517 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100518#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100519 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100521#endif
522#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100523 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100525#endif
526#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100527 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100529#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100530 }
531 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200532 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100533
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100534 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100535 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200536}
John Durkop9814fa22020-11-04 12:28:15 -0800537#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100538 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
539 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
540 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
541 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200542
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100543static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
544 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200545{
546 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100547 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200548 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200549 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200550 case PSA_KEY_TYPE_DERIVE:
551 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100552#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200553 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100554 if (bits != 128 && bits != 192 && bits != 256) {
555 return PSA_ERROR_INVALID_ARGUMENT;
556 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200557 break;
558#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200559#if defined(PSA_WANT_KEY_TYPE_ARIA)
560 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100561 if (bits != 128 && bits != 192 && bits != 256) {
562 return PSA_ERROR_INVALID_ARGUMENT;
563 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200564 break;
565#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100566#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200567 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100568 if (bits != 128 && bits != 192 && bits != 256) {
569 return PSA_ERROR_INVALID_ARGUMENT;
570 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200571 break;
572#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100573#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200574 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575 if (bits != 64 && bits != 128 && bits != 192) {
576 return PSA_ERROR_INVALID_ARGUMENT;
577 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200578 break;
579#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100580#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200581 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 if (bits < 8 || bits > 2048) {
583 return PSA_ERROR_INVALID_ARGUMENT;
584 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200585 break;
586#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100587#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200588 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100589 if (bits != 256) {
590 return PSA_ERROR_INVALID_ARGUMENT;
591 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200592 break;
593#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200594 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100595 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200596 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100597 if (bits % 8 != 0) {
598 return PSA_ERROR_INVALID_ARGUMENT;
599 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100601 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200602}
603
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100604/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100605 *
Steven Cooremancd640932021-03-08 12:00:27 +0100606 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
607 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100608 *
609 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100610 * \param[in] key_type The key type of the key to be used with the
611 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100612 *
613 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100614 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100615 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100616 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100617 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100618MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100619 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100620 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100621{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100622 if (PSA_ALG_IS_HMAC(algorithm)) {
623 if (key_type == PSA_KEY_TYPE_HMAC) {
624 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100625 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100626 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100627
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100628 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
629 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
630 * key. */
631 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
632 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
633 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
634 * the block length (larger than 1) for block ciphers. */
635 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
636 return PSA_SUCCESS;
637 }
638 }
639 }
640
641 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100642}
643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100644psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
645 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200646{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100647 if (slot->key.data != NULL) {
648 return PSA_ERROR_ALREADY_EXISTS;
649 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100651 slot->key.data = mbedtls_calloc(1, buffer_length);
652 if (slot->key.data == NULL) {
653 return PSA_ERROR_INSUFFICIENT_MEMORY;
654 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200655
Ronald Cronea0f8a62020-11-25 17:52:23 +0100656 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100657 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200658}
659
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100660psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
661 const uint8_t *data,
662 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200663{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100664 psa_status_t status = psa_allocate_buffer_to_slot(slot,
665 data_length);
666 if (status != PSA_SUCCESS) {
667 return status;
668 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100670 memcpy(slot->key.data, data, data_length);
671 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200672}
673
Ronald Crona0fe59f2020-11-29 11:14:53 +0100674psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100675 const psa_key_attributes_t *attributes,
676 const uint8_t *data, size_t data_length,
677 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100678 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100680 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
681 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200683 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100684 if (data_length == 0) {
685 return PSA_ERROR_NOT_SUPPORTED;
686 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100688 if (key_type_is_raw_bytes(type)) {
689 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200690
Steven Cooreman75b74362020-07-28 14:30:13 +0200691 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100692 if (data_length > SIZE_MAX / 8) {
693 return PSA_ERROR_NOT_SUPPORTED;
694 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200695
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200696 /* Enforce a size limit, and in particular ensure that the bit
697 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100698 if ((*bits) > PSA_MAX_KEY_BITS) {
699 return PSA_ERROR_NOT_SUPPORTED;
700 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100702 status = validate_unstructured_key_bit_size(type, *bits);
703 if (status != PSA_SUCCESS) {
704 return status;
705 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200706
Ronald Crondd04d422020-11-28 15:14:42 +0100707 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100708 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100709 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100710 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100712 return PSA_SUCCESS;
713 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800714#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
716 if (PSA_KEY_TYPE_IS_ECC(type)) {
717 return mbedtls_psa_ecp_import_key(attributes,
718 data, data_length,
719 key_buffer, key_buffer_size,
720 key_buffer_length,
721 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100722 }
John Durkop9814fa22020-11-04 12:28:15 -0800723#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
724 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700725#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
727 if (PSA_KEY_TYPE_IS_RSA(type)) {
728 return mbedtls_psa_rsa_import_key(attributes,
729 data, data_length,
730 key_buffer, key_buffer_size,
731 key_buffer_length,
732 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200733 }
John Durkop9814fa22020-11-04 12:28:15 -0800734#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
735 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100736 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100738 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100739}
740
Gilles Peskinef603c712019-01-19 13:40:11 +0100741/** Calculate the intersection of two algorithm usage policies.
742 *
743 * Return 0 (which allows no operation) on incompatibility.
744 */
745static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100746 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100747 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100748 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100749{
Gilles Peskine549ea862019-05-22 11:45:59 +0200750 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100751 if (alg1 == alg2) {
752 return alg1;
753 }
Steven Cooreman03488022021-02-18 12:07:20 +0100754 /* If the policies are from the same hash-and-sign family, check
755 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100756 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
757 PSA_ALG_IS_SIGN_HASH(alg2) &&
758 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
759 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
760 return alg2;
761 }
762 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
763 return alg1;
764 }
Steven Cooreman03488022021-02-18 12:07:20 +0100765 }
766 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100767 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100768 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100769 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
770 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
771 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
772 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
773 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100774 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100775
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100776 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100777 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
778 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
779 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
780 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100781 }
782 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100783 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
784 (alg1_len <= alg2_len)) {
785 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100786 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100787 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
788 (alg2_len <= alg1_len)) {
789 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100790 }
791 }
792 /* If the policies are from the same MAC family, check whether one
793 * of them is a minimum-MAC-length policy. Calculate the most
794 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100795 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
796 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
797 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100798 /* Validate the combination of key type and algorithm. Since the base
799 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100800 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
801 return 0;
802 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100803
Steven Cooreman31a876d2021-03-03 20:47:40 +0100804 /* Get the (exact or at-least) output lengths for both sides of the
805 * requested intersection. None of the currently supported algorithms
806 * have an output length dependent on the actual key size, so setting it
807 * to a bogus value of 0 is currently OK.
808 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100809 * Note that for at-least-this-length wildcard algorithms, the output
810 * length is set to the shortest allowed length, which allows us to
811 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100812 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
813 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100814 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100815
Steven Cooremana1d83222021-02-25 10:20:29 +0100816 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100817 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
818 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
819 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100820 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100821
822 /* If only one is an at-least-this-length policy, the intersection would
823 * be the other (fixed-length) policy as long as said fixed length is
824 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100825 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
826 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100827 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100828 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
829 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100830 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100831
Steven Cooremancd640932021-03-08 12:00:27 +0100832 /* If none of them are wildcards, check whether they define the same tag
833 * length. This is still possible here when one is default-length and
834 * the other specific-length. Ensure to always return the
835 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100836 if (alg1_len == alg2_len) {
837 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
838 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100839 }
840 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100841 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100842}
843
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100844static int psa_key_algorithm_permits(psa_key_type_t key_type,
845 psa_algorithm_t policy_alg,
846 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200847{
Gilles Peskine549ea862019-05-22 11:45:59 +0200848 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100849 if (requested_alg == policy_alg) {
850 return 1;
851 }
Steven Cooreman03488022021-02-18 12:07:20 +0100852 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
853 * and requested_alg is the same hash-and-sign family with any hash,
854 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100855 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
856 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
857 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
858 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100859 }
860 /* If policy_alg is a wildcard AEAD algorithm of the same base as
861 * the requested algorithm, check the requested tag length to be
862 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100863 if (PSA_ALG_IS_AEAD(policy_alg) &&
864 PSA_ALG_IS_AEAD(requested_alg) &&
865 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
866 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
867 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
868 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
869 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100870 }
Steven Cooremancd640932021-03-08 12:00:27 +0100871 /* If policy_alg is a MAC algorithm of the same base as the requested
872 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100873 if (PSA_ALG_IS_MAC(policy_alg) &&
874 PSA_ALG_IS_MAC(requested_alg) &&
875 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
876 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100877 /* Validate the combination of key type and algorithm. Since the policy
878 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100879 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
880 return 0;
881 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100882
Steven Cooreman31a876d2021-03-03 20:47:40 +0100883 /* Get both the requested output length for the algorithm which is to be
884 * verified, and the default output length for the base algorithm.
885 * Note that none of the currently supported algorithms have an output
886 * length dependent on actual key size, so setting it to a bogus value
887 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100888 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100889 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100890 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100891 key_type, 0,
892 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100893
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100894 /* If the policy is default-length, only allow an algorithm with
895 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100896 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
897 return requested_output_length == default_output_length;
898 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100899
900 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100901 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100902 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
903 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
904 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100905 }
906
Steven Cooremancd640932021-03-08 12:00:27 +0100907 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
908 * check for the requested MAC length to be equal to or longer than the
909 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100910 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
911 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
912 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100913 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200914 }
Steven Cooremance48e852020-10-05 16:02:45 +0200915 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200916 * a key derivation with that key agreement should also be allowed. This
917 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100918 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
919 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
920 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
921 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200922 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100923 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100924 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200925}
926
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100927/** Test whether a policy permits an algorithm.
928 *
929 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100930 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100931 * \note This function requires providing the key type for which the policy is
932 * being validated, since some algorithm policy definitions (e.g. MAC)
933 * have different properties depending on what kind of cipher it is
934 * combined with.
935 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100936 * \retval PSA_SUCCESS When \p alg is a specific algorithm
937 * allowed by the \p policy.
938 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
939 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
940 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100941 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100942static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
943 psa_key_type_t key_type,
944 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100945{
Steven Cooremand788fab2021-02-25 11:29:17 +0100946 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100947 if (alg == 0) {
948 return PSA_ERROR_INVALID_ARGUMENT;
949 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100950
Steven Cooreman7e39f052021-02-23 14:18:32 +0100951 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100952 if (PSA_ALG_IS_WILDCARD(alg)) {
953 return PSA_ERROR_INVALID_ARGUMENT;
954 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100956 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
957 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
958 return PSA_SUCCESS;
959 } else {
960 return PSA_ERROR_NOT_PERMITTED;
961 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100962}
963
Gilles Peskinef603c712019-01-19 13:40:11 +0100964/** Restrict a key policy based on a constraint.
965 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100966 * \note This function requires providing the key type for which the policy is
967 * being restricted, since some algorithm policy definitions (e.g. MAC)
968 * have different properties depending on what kind of cipher it is
969 * combined with.
970 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100971 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100972 * \param[in,out] policy The policy to restrict.
973 * \param[in] constraint The policy constraint to apply.
974 *
975 * \retval #PSA_SUCCESS
976 * \c *policy contains the intersection of the original value of
977 * \c *policy and \c *constraint.
978 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100979 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100980 * \c *policy is unchanged.
981 */
982static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100983 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100984 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100985 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100986{
987 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100988 psa_key_policy_algorithm_intersection(key_type, policy->alg,
989 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200990 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100991 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
992 constraint->alg2);
993 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
994 return PSA_ERROR_INVALID_ARGUMENT;
995 }
996 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
997 return PSA_ERROR_INVALID_ARGUMENT;
998 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100999 policy->usage &= constraint->usage;
1000 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001001 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001002 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001003}
1004
Ronald Cron5c522922020-11-14 16:35:34 +01001005/** Get the description of a key given its identifier and policy constraints
1006 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001007 *
Ronald Cron5c522922020-11-14 16:35:34 +01001008 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +01001009 * nonzero, the key must allow operations with this algorithm. If \p alg is
1010 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +01001011 *
Ronald Cron5c522922020-11-14 16:35:34 +01001012 * In case of a persistent key, the function loads the description of the key
1013 * into a key slot if not already done.
1014 *
1015 * On success, the returned key slot is locked. It is the responsibility of
1016 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001017 */
Ronald Cron5c522922020-11-14 16:35:34 +01001018static psa_status_t psa_get_and_lock_key_slot_with_policy(
1019 mbedtls_svc_key_id_t key,
1020 psa_key_slot_t **p_slot,
1021 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001022 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001023{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001024 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1025 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001026
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001027 status = psa_get_and_lock_key_slot(key, p_slot);
1028 if (status != PSA_SUCCESS) {
1029 return status;
1030 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001031 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001032
1033 /* Enforce that usage policy for the key slot contains all the flags
1034 * required by the usage parameter. There is one exception: public
1035 * keys can always be exported, so we treat public key objects as
1036 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001037 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001038 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001039 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001041 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001042 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001043 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001044 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001045
Shaun Case0e7791f2021-12-20 21:14:10 -08001046 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001047 if (alg != 0) {
1048 status = psa_key_policy_permits(&slot->attr.policy,
1049 slot->attr.type,
1050 alg);
1051 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001052 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001053 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001054 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001056 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001057
1058error:
1059 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001060 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001062 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001063}
Darryl Green940d72c2018-07-13 13:18:51 +01001064
Ronald Cron5c522922020-11-14 16:35:34 +01001065/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001066 *
1067 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001068 * available, as opposed to a key in a secure element and/or to be used
1069 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001070 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001071 * This is a temporary function that may be used instead of
1072 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1073 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001074 *
Ronald Cron5c522922020-11-14 16:35:34 +01001075 * On success, the returned key slot is locked. It is the responsibility of the
1076 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001077 */
Ronald Cron5c522922020-11-14 16:35:34 +01001078static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1079 mbedtls_svc_key_id_t key,
1080 psa_key_slot_t **p_slot,
1081 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001082 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001083{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001084 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1085 usage, alg);
1086 if (status != PSA_SUCCESS) {
1087 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001088 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001090 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1091 psa_unlock_key_slot(*p_slot);
1092 *p_slot = NULL;
1093 return PSA_ERROR_NOT_SUPPORTED;
1094 }
1095
1096 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001097}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001099psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001100{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001101 /* Data pointer will always be either a valid pointer or NULL in an
1102 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001103 if (slot->key.data != NULL) {
1104 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1105 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001107 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001108 slot->key.data = NULL;
1109 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001111 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001112}
1113
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001114/** Completely wipe a slot in memory, including its policy.
1115 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001116psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001117{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001118 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001119
1120 /*
1121 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001122 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001123 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1124 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001125 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001126 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001127 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001128#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001129 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001130#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001131 status = PSA_ERROR_CORRUPTION_DETECTED;
1132 }
1133
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001134 /* Multipart operations may still be using the key. This is safe
1135 * because all multipart operation objects are independent from
1136 * the key slot: if they need to access the key after the setup
1137 * phase, they have a copy of the key. Note that this means that
1138 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001139 /* At this point, key material and other type-specific content has
1140 * been wiped. Clear remaining metadata. We can call memset and not
1141 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001142 memset(slot, 0, sizeof(*slot));
1143 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001144}
1145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001146psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001147{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001148 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001149 psa_status_t status; /* status of the last operation */
1150 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001151#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1152 psa_se_drv_table_entry_t *driver;
1153#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001155 if (mbedtls_svc_key_id_is_null(key)) {
1156 return PSA_SUCCESS;
1157 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001158
Ronald Cronf2911112020-10-29 17:51:10 +01001159 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001160 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001161 * key, this will load the key description from persistent memory if not
1162 * done yet. We cannot avoid this loading as without it we don't know if
1163 * the key is operated by an SE or not and this information is needed by
1164 * the current implementation.
1165 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001166 status = psa_get_and_lock_key_slot(key, &slot);
1167 if (status != PSA_SUCCESS) {
1168 return status;
1169 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001170
Ronald Cronf2911112020-10-29 17:51:10 +01001171 /*
1172 * If the key slot containing the key description is under access by the
1173 * library (apart from the present access), the key cannot be destroyed
1174 * yet. For the time being, just return in error. Eventually (to be
1175 * implemented), the key should be destroyed when all accesses have
1176 * stopped.
1177 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001178 if (slot->lock_count > 1) {
1179 psa_unlock_key_slot(slot);
1180 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001181 }
1182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001183 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001184 /* Refuse the destruction of a read-only key (which may or may not work
1185 * if we attempt it, depending on whether the key is merely read-only
1186 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001187 * Just do the best we can, which is to wipe the copy in memory
1188 * (done in this function's cleanup code). */
1189 overall_status = PSA_ERROR_NOT_PERMITTED;
1190 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001191 }
1192
Gilles Peskine354f7672019-07-12 23:46:38 +02001193#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001194 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1195 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001196 /* For a key in a secure element, we need to do three things:
1197 * remove the key file in internal storage, destroy the
1198 * key inside the secure element, and update the driver's
1199 * persistent data. Start a transaction that will encompass these
1200 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001201 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001202 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001203 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001204 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001205 status = psa_crypto_save_transaction();
1206 if (status != PSA_SUCCESS) {
1207 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001208 /* We should still try to destroy the key in the secure
1209 * element and the key metadata in storage. This is especially
1210 * important if the error is that the storage is full.
1211 * But how to do it exactly without risking an inconsistent
1212 * state after a reset?
1213 * https://github.com/ARMmbed/mbed-crypto/issues/215
1214 */
1215 overall_status = status;
1216 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001217 }
1218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001219 status = psa_destroy_se_key(driver,
1220 psa_key_slot_get_slot_number(slot));
1221 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001222 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001223 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001224 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001225#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1226
Darryl Greend49a4992018-06-18 17:27:26 +01001227#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001228 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1229 status = psa_destroy_persistent_key(slot->attr.id);
1230 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001231 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001232 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001233
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001234 /* TODO: other slots may have a copy of the same key. We should
1235 * invalidate them.
1236 * https://github.com/ARMmbed/mbed-crypto/issues/214
1237 */
Darryl Greend49a4992018-06-18 17:27:26 +01001238 }
1239#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001240
Gilles Peskinefc762652019-07-22 19:30:34 +02001241#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001242 if (driver != NULL) {
1243 status = psa_save_se_persistent_data(driver);
1244 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001245 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001246 }
1247 status = psa_crypto_stop_transaction();
1248 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001249 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001250 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001251 }
1252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1253
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001254exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001255 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001256 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001257 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001258 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001259 }
1260 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001261}
1262
John Durkop07cc04a2020-11-16 22:08:34 -08001263#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001264 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001265static psa_status_t psa_get_rsa_public_exponent(
1266 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001267 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001268{
1269 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001271 uint8_t *buffer = NULL;
1272 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001273 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001275 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1276 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001277 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001278 }
1279 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001280 /* It's the default value, which is reported as an empty string,
1281 * so there's nothing to do. */
1282 goto exit;
1283 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001285 buflen = mbedtls_mpi_size(&mpi);
1286 buffer = mbedtls_calloc(1, buflen);
1287 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001288 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1289 goto exit;
1290 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001291 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1292 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001293 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001294 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001295 attributes->domain_parameters = buffer;
1296 attributes->domain_parameters_size = buflen;
1297
1298exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001299 mbedtls_mpi_free(&mpi);
1300 if (ret != 0) {
1301 mbedtls_free(buffer);
1302 }
1303 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001304}
John Durkop07cc04a2020-11-16 22:08:34 -08001305#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001306 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001307
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001308/** Retrieve all the publicly-accessible attributes of a key.
1309 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001310psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1311 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001312{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001313 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001314 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001315 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001317 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001319 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1320 if (status != PSA_SUCCESS) {
1321 return status;
1322 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001323
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001324 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001325 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1326 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001327
1328#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001329 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1330 psa_set_key_slot_number(attributes,
1331 psa_key_slot_get_slot_number(slot));
1332 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001333#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001335 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001336#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001337 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001338 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001339 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001340 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001341 * is not yet implemented.
1342 * https://github.com/ARMmbed/mbed-crypto/issues/216
1343 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001344 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001345 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001346
Ronald Cron90857082020-11-25 14:58:33 +01001347 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001348 slot->attr.type,
1349 slot->key.data,
1350 slot->key.bytes,
1351 &rsa);
1352 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001353 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001354 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001356 status = psa_get_rsa_public_exponent(rsa,
1357 attributes);
1358 mbedtls_rsa_free(rsa);
1359 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001360 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001361 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001362#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001363 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001364 default:
1365 /* Nothing else to do. */
1366 break;
1367 }
1368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001369 if (status != PSA_SUCCESS) {
1370 psa_reset_key_attributes(attributes);
1371 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001373 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001375 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001376}
1377
Gilles Peskinec8000c02019-08-02 20:15:51 +02001378#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1379psa_status_t psa_get_key_slot_number(
1380 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001381 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001382{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001383 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001384 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001385 return PSA_SUCCESS;
1386 } else {
1387 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001388 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001389}
1390#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001392static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1393 size_t key_buffer_size,
1394 uint8_t *data,
1395 size_t data_size,
1396 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001397{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001398 if (key_buffer_size > data_size) {
1399 return PSA_ERROR_BUFFER_TOO_SMALL;
1400 }
1401 memcpy(data, key_buffer, key_buffer_size);
1402 memset(data + key_buffer_size, 0,
1403 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001404 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001405 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001406}
1407
Ronald Cron7285cda2020-11-26 14:46:37 +01001408psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001409 const psa_key_attributes_t *attributes,
1410 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001411 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001412{
Ronald Crond18b5f82020-11-25 16:46:05 +01001413 psa_key_type_t type = attributes->core.type;
1414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001415 if (key_type_is_raw_bytes(type) ||
1416 PSA_KEY_TYPE_IS_RSA(type) ||
1417 PSA_KEY_TYPE_IS_ECC(type)) {
1418 return psa_export_key_buffer_internal(
1419 key_buffer, key_buffer_size,
1420 data, data_size, data_length);
1421 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001422 /* This shouldn't happen in the reference implementation, but
1423 it is valid for a special-purpose implementation to omit
1424 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001425 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001426 }
1427}
1428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001429psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1430 uint8_t *data,
1431 size_t data_size,
1432 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001433{
1434 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1435 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1436 psa_key_slot_t *slot;
1437
Ronald Crone907e552021-01-18 13:22:38 +01001438 /* Reject a zero-length output buffer now, since this can never be a
1439 * valid key representation. This way we know that data must be a valid
1440 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001441 if (data_size == 0) {
1442 return PSA_ERROR_BUFFER_TOO_SMALL;
1443 }
Ronald Crone907e552021-01-18 13:22:38 +01001444
Ronald Cron9486f9d2020-11-26 13:36:23 +01001445 /* Set the key to empty now, so that even when there are errors, we always
1446 * set data_length to a value between 0 and data_size. On error, setting
1447 * the key to empty is a good choice because an empty key representation is
1448 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001449 *data_length = 0;
1450
Ronald Cron9486f9d2020-11-26 13:36:23 +01001451 /* Export requires the EXPORT flag. There is an exception for public keys,
1452 * which don't require any flag, but
1453 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1454 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001455 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1456 PSA_KEY_USAGE_EXPORT, 0);
1457 if (status != PSA_SUCCESS) {
1458 return status;
1459 }
mohammad160306e79202018-03-28 13:17:44 +03001460
Ronald Crond18b5f82020-11-25 16:46:05 +01001461 psa_key_attributes_t attributes = {
1462 .core = slot->attr
1463 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001464 status = psa_driver_wrapper_export_key(&attributes,
1465 slot->key.data, slot->key.bytes,
1466 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001468 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001470 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001471}
1472
Ronald Cron7285cda2020-11-26 14:46:37 +01001473psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001474 const psa_key_attributes_t *attributes,
1475 const uint8_t *key_buffer,
1476 size_t key_buffer_size,
1477 uint8_t *data,
1478 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001479 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001480{
Ronald Crond18b5f82020-11-25 16:46:05 +01001481 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001483 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1484 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001485 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001486 return psa_export_key_buffer_internal(
1487 key_buffer, key_buffer_size,
1488 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001489 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001491 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001492#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001493 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1494 return mbedtls_psa_rsa_export_public_key(attributes,
1495 key_buffer,
1496 key_buffer_size,
1497 data,
1498 data_size,
1499 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001500#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001501 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001502 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001503#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1504 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001505 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001506#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001507 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1508 return mbedtls_psa_ecp_export_public_key(attributes,
1509 key_buffer,
1510 key_buffer_size,
1511 data,
1512 data_size,
1513 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001514#else
1515 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001516 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001517#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1518 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001519 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001520 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001521 /* This shouldn't happen in the reference implementation, but
1522 it is valid for a special-purpose implementation to omit
1523 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001524 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001525 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001526}
Ronald Crond18b5f82020-11-25 16:46:05 +01001527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001528psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1529 uint8_t *data,
1530 size_t data_size,
1531 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001532{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001533 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001534 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001535 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001536 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001537
Ronald Crone907e552021-01-18 13:22:38 +01001538 /* Reject a zero-length output buffer now, since this can never be a
1539 * valid key representation. This way we know that data must be a valid
1540 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001541 if (data_size == 0) {
1542 return PSA_ERROR_BUFFER_TOO_SMALL;
1543 }
Ronald Crone907e552021-01-18 13:22:38 +01001544
Darryl Greendd8fb772018-11-07 16:00:44 +00001545 /* Set the key to empty now, so that even when there are errors, we always
1546 * set data_length to a value between 0 and data_size. On error, setting
1547 * the key to empty is a good choice because an empty key representation is
1548 * unlikely to be accepted anywhere. */
1549 *data_length = 0;
1550
1551 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001552 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1553 if (status != PSA_SUCCESS) {
1554 return status;
1555 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001557 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1558 status = PSA_ERROR_INVALID_ARGUMENT;
1559 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001560 }
1561
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001562 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001563 .core = slot->attr
1564 };
Ronald Cron67227982020-11-26 15:16:05 +01001565 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001566 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001567 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001568
1569exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001570 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001572 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001573}
1574
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001575MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1576 "One or more key attribute flag is listed as both external-only and dual-use")
1577MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1578 "One or more key attribute flag is listed as both internal-only and dual-use")
1579MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1580 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001581
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001582/** Validate that a key policy is internally well-formed.
1583 *
1584 * This function only rejects invalid policies. It does not validate the
1585 * consistency of the policy with respect to other attributes of the key
1586 * such as the key type.
1587 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001588static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001589{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001590 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1591 PSA_KEY_USAGE_COPY |
1592 PSA_KEY_USAGE_ENCRYPT |
1593 PSA_KEY_USAGE_DECRYPT |
1594 PSA_KEY_USAGE_SIGN_MESSAGE |
1595 PSA_KEY_USAGE_VERIFY_MESSAGE |
1596 PSA_KEY_USAGE_SIGN_HASH |
1597 PSA_KEY_USAGE_VERIFY_HASH |
1598 PSA_KEY_USAGE_DERIVE)) != 0) {
1599 return PSA_ERROR_INVALID_ARGUMENT;
1600 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001602 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001603}
1604
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001605/** Validate the internal consistency of key attributes.
1606 *
1607 * This function only rejects invalid attribute values. If does not
1608 * validate the consistency of the attributes with any key data that may
1609 * be involved in the creation of the key.
1610 *
1611 * Call this function early in the key creation process.
1612 *
1613 * \param[in] attributes Key attributes for the new key.
1614 * \param[out] p_drv On any return, the driver for the key, if any.
1615 * NULL for a transparent key.
1616 *
1617 */
1618static psa_status_t psa_validate_key_attributes(
1619 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001620 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001621{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001622 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001623 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1624 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001626 status = psa_validate_key_location(lifetime, p_drv);
1627 if (status != PSA_SUCCESS) {
1628 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001629 }
1630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001631 status = psa_validate_key_persistence(lifetime);
1632 if (status != PSA_SUCCESS) {
1633 return status;
1634 }
1635
1636 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1637 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1638 return PSA_ERROR_INVALID_ARGUMENT;
1639 }
1640 } else {
1641 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1642 return PSA_ERROR_INVALID_ARGUMENT;
1643 }
1644 }
1645
1646 status = psa_validate_key_policy(&attributes->core.policy);
1647 if (status != PSA_SUCCESS) {
1648 return status;
1649 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001650
1651 /* Refuse to create overly large keys.
1652 * Note that this doesn't trigger on import if the attributes don't
1653 * explicitly specify a size (so psa_get_key_bits returns 0), so
1654 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001655 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1656 return PSA_ERROR_NOT_SUPPORTED;
1657 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001658
Gilles Peskine91e8c332019-08-02 19:19:39 +02001659 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001660 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1661 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1662 return PSA_ERROR_INVALID_ARGUMENT;
1663 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001665 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001666}
1667
Gilles Peskine4747d192019-04-17 15:05:45 +02001668/** Prepare a key slot to receive key material.
1669 *
1670 * This function allocates a key slot and sets its metadata.
1671 *
1672 * If this function fails, call psa_fail_key_creation().
1673 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001674 * This function is intended to be used as follows:
1675 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001676 * it with the specified attributes, and in case of a volatile key assign it
1677 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001678 * -# Populate the slot with the key material.
1679 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1680 * In case of failure at any step, stop the sequence and call
1681 * psa_fail_key_creation().
1682 *
Ronald Cron5c522922020-11-14 16:35:34 +01001683 * On success, the key slot is locked. It is the responsibility of the caller
1684 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001685 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001686 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001687 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001688 * \param[out] p_slot On success, a pointer to the prepared slot.
1689 * \param[out] p_drv On any return, the driver for the key, if any.
1690 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001691 *
1692 * \retval #PSA_SUCCESS
1693 * The key slot is ready to receive key material.
1694 * \return If this function fails, the key slot is an invalid state.
1695 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001696 */
1697static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001698 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001699 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001700 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001701 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001702{
1703 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001704 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001705 psa_key_slot_t *slot;
1706
Gilles Peskinedf179142019-07-15 22:02:14 +02001707 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001708 *p_drv = NULL;
1709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001710 status = psa_validate_key_attributes(attributes, p_drv);
1711 if (status != PSA_SUCCESS) {
1712 return status;
1713 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001715 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1716 if (status != PSA_SUCCESS) {
1717 return status;
1718 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001719 slot = *p_slot;
1720
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001721 /* We're storing the declared bit-size of the key. It's up to each
1722 * creation mechanism to verify that this information is correct.
1723 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001724 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001725 * is optional (import, copy). In case of a volatile key, assign it the
1726 * volatile key identifier associated to the slot returned to contain its
1727 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001728
1729 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001730 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001731#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1732 slot->attr.id = volatile_key_id;
1733#else
1734 slot->attr.id.key_id = volatile_key_id;
1735#endif
1736 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001737
Gilles Peskine91e8c332019-08-02 19:19:39 +02001738 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001739 * external-only flags, query `attributes`. Thanks to the check
1740 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001741 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001742 * may have set. */
1743 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001744
Gilles Peskinecbaff462019-07-12 23:46:04 +02001745#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001746 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001747 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001748 * create the key file in internal storage, create the
1749 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001750 * persistent data. This is done by starting a transaction that will
1751 * encompass these three actions.
1752 * For registering a volatile key, we just need to find an appropriate
1753 * slot number inside the SE. Since the key is designated volatile, creating
1754 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001755 /* The first thing to do is to find a slot number for the new key.
1756 * We save the slot number in persistent storage as part of the
1757 * transaction data. It will be needed to recover if the power
1758 * fails during the key creation process, to clean up on the secure
1759 * element side after restarting. Obtaining a slot number from the
1760 * secure element driver updates its persistent state, but we do not yet
1761 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001762 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001763 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001764 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001765 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1766 &slot_number);
1767 if (status != PSA_SUCCESS) {
1768 return status;
1769 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001771 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1772 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001773 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001774 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001775 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001776 status = psa_crypto_save_transaction();
1777 if (status != PSA_SUCCESS) {
1778 (void) psa_crypto_stop_transaction();
1779 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001780 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001781 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001782
1783 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001784 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001785 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001786
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001787 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001788 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001789 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001790 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001791#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001793 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001794}
Gilles Peskine4747d192019-04-17 15:05:45 +02001795
1796/** Finalize the creation of a key once its key material has been set.
1797 *
1798 * This entails writing the key to persistent storage.
1799 *
1800 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001801 * See the documentation of psa_start_key_creation() for the intended use
1802 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001803 *
Ronald Cron5c522922020-11-14 16:35:34 +01001804 * If the finalization succeeds, the function unlocks the key slot (it was
1805 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1806 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001807 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001808 * \param[in,out] slot Pointer to the slot with key material.
1809 * \param[in] driver The secure element driver for the key,
1810 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001811 * \param[out] key On success, identifier of the key. Note that the
1812 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001813 *
1814 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001815 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001816 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1817 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1818 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1819 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1820 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1821 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001822 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001823 * \return If this function fails, the key slot is an invalid state.
1824 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001825 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001826static psa_status_t psa_finish_key_creation(
1827 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001828 psa_se_drv_table_entry_t *driver,
1829 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001830{
1831 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001832 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001833 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001834
1835#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001836 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001837#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001838 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001839 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001840 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001841 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001842
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001843 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1844 sizeof(data.slot_number),
1845 "Slot number size does not match psa_se_key_data_storage_t");
1846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001847 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1848 status = psa_save_persistent_key(&slot->attr,
1849 (uint8_t *) &data,
1850 sizeof(data));
1851 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001852#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1853 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001854 /* Key material is saved in export representation in the slot, so
1855 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001856 status = psa_save_persistent_key(&slot->attr,
1857 slot->key.data,
1858 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001859 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001860 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001861#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1862
Gilles Peskinecbaff462019-07-12 23:46:04 +02001863#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001864 /* Finish the transaction for a key creation. This does not
1865 * happen when registering an existing key. Detect this case
1866 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001867 * creation of a persistent key in a secure element requires a transaction,
1868 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001869 if (driver != NULL &&
1870 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1871 status = psa_save_se_persistent_data(driver);
1872 if (status != PSA_SUCCESS) {
1873 psa_destroy_persistent_key(slot->attr.id);
1874 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001875 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001876 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001877 }
1878#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001880 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001881 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001882 status = psa_unlock_key_slot(slot);
1883 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001884 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001885 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001886 }
Ronald Cron50972942020-11-14 11:28:25 +01001887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001888 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001889}
1890
1891/** Abort the creation of a key.
1892 *
1893 * You may call this function after calling psa_start_key_creation(),
1894 * or after psa_finish_key_creation() fails. In other circumstances, this
1895 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001896 * See the documentation of psa_start_key_creation() for the intended use
1897 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001898 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001899 * \param[in,out] slot Pointer to the slot with key material.
1900 * \param[in] driver The secure element driver for the key,
1901 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001902 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001903static void psa_fail_key_creation(psa_key_slot_t *slot,
1904 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001905{
Gilles Peskine011e4282019-06-26 18:34:38 +02001906 (void) driver;
1907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001908 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001909 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001910 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001911
1912#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001913 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001914 * element, and the failure happened later (when saving metadata
1915 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001916 * element.
1917 * https://github.com/ARMmbed/mbed-crypto/issues/217
1918 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001919
Gilles Peskined7729582019-08-05 15:55:54 +02001920 /* Abort the ongoing transaction if any (there may not be one if
1921 * the creation process failed before starting one, or if the
1922 * key creation is a registration of a key in a secure element).
1923 * Earlier functions must already have done what it takes to undo any
1924 * partial creation. All that's left is to update the transaction data
1925 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001926 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001927#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1928
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001929 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001930}
1931
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001932/** Validate optional attributes during key creation.
1933 *
1934 * Some key attributes are optional during key creation. If they are
1935 * specified in the attributes structure, check that they are consistent
1936 * with the data in the slot.
1937 *
1938 * This function should be called near the end of key creation, after
1939 * the slot in memory is fully populated but before saving persistent data.
1940 */
1941static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001942 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001943 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001944{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001945 if (attributes->core.type != 0) {
1946 if (attributes->core.type != slot->attr.type) {
1947 return PSA_ERROR_INVALID_ARGUMENT;
1948 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001949 }
1950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001951 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001952#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001953 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1954 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001955 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001956 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001957 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001958
Ronald Cron90857082020-11-25 14:58:33 +01001959 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001960 slot->attr.type,
1961 slot->key.data,
1962 slot->key.bytes,
1963 &rsa);
1964 if (status != PSA_SUCCESS) {
1965 return status;
1966 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001968 mbedtls_mpi_init(&actual);
1969 mbedtls_mpi_init(&required);
1970 ret = mbedtls_rsa_export(rsa,
1971 NULL, NULL, NULL, NULL, &actual);
1972 mbedtls_rsa_free(rsa);
1973 mbedtls_free(rsa);
1974 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001975 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001976 }
1977 ret = mbedtls_mpi_read_binary(&required,
1978 attributes->domain_parameters,
1979 attributes->domain_parameters_size);
1980 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001981 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001982 }
1983 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001984 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001985 }
1986rsa_exit:
1987 mbedtls_mpi_free(&actual);
1988 mbedtls_mpi_free(&required);
1989 if (ret != 0) {
1990 return mbedtls_to_psa_error(ret);
1991 }
1992 } else
John Durkop9814fa22020-11-04 12:28:15 -08001993#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1994 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001995 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001996 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001997 }
1998 }
1999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002000 if (attributes->core.bits != 0) {
2001 if (attributes->core.bits != slot->attr.bits) {
2002 return PSA_ERROR_INVALID_ARGUMENT;
2003 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002004 }
2005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002006 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002007}
2008
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002009psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2010 const uint8_t *data,
2011 size_t data_length,
2012 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002013{
2014 psa_status_t status;
2015 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002016 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002017 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002018
Ronald Cron81709fc2020-11-14 12:10:32 +01002019 *key = MBEDTLS_SVC_KEY_ID_INIT;
2020
Gilles Peskine0f84d622019-09-12 19:03:13 +02002021 /* Reject zero-length symmetric keys (including raw data key objects).
2022 * This also rejects any key which might be encoded as an empty string,
2023 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002024 if (data_length == 0) {
2025 return PSA_ERROR_INVALID_ARGUMENT;
2026 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002027
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2029 &slot, &driver);
2030 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002031 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002032 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002033
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002034 /* In the case of a transparent key or an opaque key stored in local
2035 * storage (thus not in the case of generating a key in a secure element
2036 * or cryptoprocessor with storage), we have to allocate a buffer to
2037 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002038 if (slot->key.data == NULL) {
2039 status = psa_allocate_buffer_to_slot(slot, data_length);
2040 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002041 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002042 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002043 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002044
2045 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002046 status = psa_driver_wrapper_import_key(attributes,
2047 data, data_length,
2048 slot->key.data,
2049 slot->key.bytes,
2050 &slot->key.bytes, &bits);
2051 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002052 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002053 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002055 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002056 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002057 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002058 status = PSA_ERROR_INVALID_ARGUMENT;
2059 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002060 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002062 status = psa_validate_optional_attributes(slot, attributes);
2063 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002064 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002065 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002067 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002068exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002069 if (status != PSA_SUCCESS) {
2070 psa_fail_key_creation(slot, driver);
2071 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002073 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002074}
2075
Gilles Peskined7729582019-08-05 15:55:54 +02002076#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2077psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002078 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002079{
2080 psa_status_t status;
2081 psa_key_slot_t *slot = NULL;
2082 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002083 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002084
2085 /* Leaving attributes unspecified is not currently supported.
2086 * It could make sense to query the key type and size from the
2087 * secure element, but not all secure elements support this
2088 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002089 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2090 return PSA_ERROR_NOT_SUPPORTED;
2091 }
2092 if (psa_get_key_bits(attributes) == 0) {
2093 return PSA_ERROR_NOT_SUPPORTED;
2094 }
Gilles Peskined7729582019-08-05 15:55:54 +02002095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002096 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2097 &slot, &driver);
2098 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002099 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002100 }
Gilles Peskined7729582019-08-05 15:55:54 +02002101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002102 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002103
2104exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002105 if (status != PSA_SUCCESS) {
2106 psa_fail_key_creation(slot, driver);
2107 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002108
Gilles Peskined7729582019-08-05 15:55:54 +02002109 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002110 psa_close_key(key);
2111 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002112}
2113#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002115static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2116 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002117{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002118 psa_status_t status = psa_copy_key_material_into_slot(target,
2119 source->key.data,
2120 source->key.bytes);
2121 if (status != PSA_SUCCESS) {
2122 return status;
2123 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002124
Steven Cooreman398aee52020-10-13 14:35:45 +02002125 target->attr.type = source->attr.type;
2126 target->attr.bits = source->attr.bits;
2127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002128 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002129}
2130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002131psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2132 const psa_key_attributes_t *specified_attributes,
2133 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002134{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002135 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002136 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002137 psa_key_slot_t *source_slot = NULL;
2138 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002139 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002140 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002141
Ronald Cron81709fc2020-11-14 12:10:32 +01002142 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2143
Ronald Cron5c522922020-11-14 16:35:34 +01002144 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002145 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2146 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002147 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002148 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002150 status = psa_validate_optional_attributes(source_slot,
2151 specified_attributes);
2152 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002153 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002154 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002156 status = psa_restrict_key_policy(source_slot->attr.type,
2157 &actual_attributes.core.policy,
2158 &source_slot->attr.policy);
2159 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002160 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002161 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002163 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2164 &target_slot, &driver);
2165 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002166 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002167 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002168
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002169#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002170 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002171 /* Copying to a secure element is not implemented yet. */
2172 status = PSA_ERROR_NOT_SUPPORTED;
2173 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002174 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002175#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002177 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002178 /*
2179 * Copying through an opaque driver is not implemented yet, consider
2180 * a lifetime with an external location as an invalid parameter for
2181 * now.
2182 */
2183 status = PSA_ERROR_INVALID_ARGUMENT;
2184 goto exit;
2185 }
2186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002187 status = psa_copy_key_material(source_slot, target_slot);
2188 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002189 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002190 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002192 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002193exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002194 if (status != PSA_SUCCESS) {
2195 psa_fail_key_creation(target_slot, driver);
2196 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002198 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002200 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002201}
2202
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002203
2204
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002205/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002206/* Message digests */
2207/****************************************************************/
2208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002209psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002210{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002211 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002212 if (operation->id == 0) {
2213 return PSA_SUCCESS;
2214 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002216 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002217 operation->id = 0;
2218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002219 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002220}
2221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002222psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2223 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002224{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002225 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002226
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002227 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002228 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002229 status = PSA_ERROR_BAD_STATE;
2230 goto exit;
2231 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002233 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002234 status = PSA_ERROR_INVALID_ARGUMENT;
2235 goto exit;
2236 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002237
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002238 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2239 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002240 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002242 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002243
2244exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002245 if (status != PSA_SUCCESS) {
2246 psa_hash_abort(operation);
2247 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002248
2249 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002250}
2251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002252psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2253 const uint8_t *input,
2254 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002255{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002256 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002258 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002259 status = PSA_ERROR_BAD_STATE;
2260 goto exit;
2261 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002262
Steven Cooremanc8288352021-03-04 14:02:19 +01002263 /* Don't require hash implementations to behave correctly on a
2264 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002265 if (input_length == 0) {
2266 return PSA_SUCCESS;
2267 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002269 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002270
2271exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002272 if (status != PSA_SUCCESS) {
2273 psa_hash_abort(operation);
2274 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002276 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002277}
2278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002279psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2280 uint8_t *hash,
2281 size_t hash_size,
2282 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002283{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002284 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002285 if (operation->id == 0) {
2286 return PSA_ERROR_BAD_STATE;
2287 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288
Steven Cooreman1e582352021-02-18 17:24:37 +01002289 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002290 operation, hash, hash_size, hash_length);
2291 psa_hash_abort(operation);
2292 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002293}
2294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002295psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2296 const uint8_t *hash,
2297 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002298{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002299 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002300 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002301 psa_status_t status = psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002302 operation,
2303 actual_hash, sizeof(actual_hash),
2304 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002306 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002307 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002308 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002310 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002311 status = PSA_ERROR_INVALID_SIGNATURE;
2312 goto exit;
2313 }
2314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002315 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002316 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002317 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002318
2319exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002320 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2321 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002322 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002323 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002325 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002326}
2327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002328psa_status_t psa_hash_compute(psa_algorithm_t alg,
2329 const uint8_t *input, size_t input_length,
2330 uint8_t *hash, size_t hash_size,
2331 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002332{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002333 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002334 if (!PSA_ALG_IS_HASH(alg)) {
2335 return PSA_ERROR_INVALID_ARGUMENT;
2336 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002338 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2339 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002340}
2341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002342psa_status_t psa_hash_compare(psa_algorithm_t alg,
2343 const uint8_t *input, size_t input_length,
2344 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002345{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002346 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002347 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002349 if (!PSA_ALG_IS_HASH(alg)) {
2350 return PSA_ERROR_INVALID_ARGUMENT;
2351 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002352
Steven Cooreman1e582352021-02-18 17:24:37 +01002353 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002354 alg, input, input_length,
2355 actual_hash, sizeof(actual_hash),
2356 &actual_hash_length);
2357 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002358 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002359 }
2360 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002361 status = PSA_ERROR_INVALID_SIGNATURE;
2362 goto exit;
2363 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002364 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002365 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002366 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002367
2368exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002369 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2370 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002371}
2372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002373psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2374 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002375{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002376 if (source_operation->id == 0 ||
2377 target_operation->id != 0) {
2378 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002379 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002381 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2382 target_operation);
2383 if (status != PSA_SUCCESS) {
2384 psa_hash_abort(target_operation);
2385 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002387 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002388}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002389
2390
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002391/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002392/* MAC */
2393/****************************************************************/
2394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002395psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002396{
Steven Cooreman07897832021-03-19 18:28:56 +01002397 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002398 if (operation->id == 0) {
2399 return PSA_SUCCESS;
2400 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002402 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002403 operation->mac_size = 0;
2404 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002405 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002407 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002408}
2409
Ronald Cron1c650a12021-06-17 16:33:22 +02002410static psa_status_t psa_mac_finalize_alg_and_key_validation(
2411 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002412 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002413 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002414{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002415 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002416 psa_key_type_t key_type = psa_get_key_type(attributes);
2417 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002419 if (!PSA_ALG_IS_MAC(alg)) {
2420 return PSA_ERROR_INVALID_ARGUMENT;
2421 }
Steven Cooreman07897832021-03-19 18:28:56 +01002422
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002423 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002424 status = psa_mac_key_can_do(alg, key_type);
2425 if (status != PSA_SUCCESS) {
2426 return status;
2427 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002428
Steven Cooremana6474de2021-05-10 11:13:41 +02002429 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002430 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002432 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002433 /* A very short MAC is too short for security since it can be
2434 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2435 * so we make this our minimum, even though 32 bits is still
2436 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002437 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002438 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002440 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2441 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002442 /* It's impossible to "truncate" to a larger length than the full length
2443 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002444 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002445 }
2446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002447 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002448 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2449 * that is disabled in the compile-time configuration. The result can
2450 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2451 * configuration into account. In this case, force a return of
2452 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2453 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2454 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2455 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2456 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002457 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002458 }
2459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002460 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002461}
2462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002463static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2464 mbedtls_svc_key_id_t key,
2465 psa_algorithm_t alg,
2466 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002467{
2468 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2469 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002470 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002471 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002472
2473 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002474 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002475 status = PSA_ERROR_BAD_STATE;
2476 goto exit;
2477 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002478
2479 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002480 key,
2481 &slot,
2482 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2483 alg);
2484 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002485 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002486 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002487
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002488 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002489 .core = slot->attr
2490 };
2491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002492 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2493 &operation->mac_size);
2494 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002495 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002496 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002497
2498 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002499 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002500 if (is_sign) {
2501 status = psa_driver_wrapper_mac_sign_setup(operation,
2502 &attributes,
2503 slot->key.data,
2504 slot->key.bytes,
2505 alg);
2506 } else {
2507 status = psa_driver_wrapper_mac_verify_setup(operation,
2508 &attributes,
2509 slot->key.data,
2510 slot->key.bytes,
2511 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002512 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002513
Gilles Peskinefbfac682018-07-08 20:51:54 +02002514exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002515 if (status != PSA_SUCCESS) {
2516 psa_mac_abort(operation);
2517 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002519 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002521 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002522}
2523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002524psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2525 mbedtls_svc_key_id_t key,
2526 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002527{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002528 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002529}
2530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002531psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2532 mbedtls_svc_key_id_t key,
2533 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002534{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002535 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002536}
2537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002538psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2539 const uint8_t *input,
2540 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002541{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002542 if (operation->id == 0) {
2543 return PSA_ERROR_BAD_STATE;
2544 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545
Steven Cooreman11743f92021-03-19 18:38:46 +01002546 /* Don't require hash implementations to behave correctly on a
2547 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002548 if (input_length == 0) {
2549 return PSA_SUCCESS;
2550 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002552 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2553 input, input_length);
2554 if (status != PSA_SUCCESS) {
2555 psa_mac_abort(operation);
2556 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002558 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002559}
2560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002561psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2562 uint8_t *mac,
2563 size_t mac_size,
2564 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002565{
Steven Cooreman87885df2021-03-19 19:04:39 +01002566 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2567 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002569 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002570 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002571 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002572 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002574 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002575 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002576 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002577 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002578
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002579 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2580 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002581 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002582 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002583 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002584 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002585
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002586 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002587 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002588 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002589 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002591 status = psa_driver_wrapper_mac_sign_finish(operation,
2592 mac, operation->mac_size,
2593 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002594
Dave Rodgman478ab542021-06-25 09:09:02 +01002595exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002596 /* In case of success, set the potential excess room in the output buffer
2597 * to an invalid value, to avoid potentially leaking a longer MAC.
2598 * In case of error, set the output length and content to a safe default,
2599 * such that in case the caller misses an error check, the output would be
2600 * an unachievable MAC.
2601 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002602 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002603 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002604 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002605 }
mohammad16036df908f2018-04-02 08:34:15 -07002606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002607 if (mac_size > operation->mac_size) {
2608 memset(&mac[operation->mac_size], '!',
2609 mac_size - operation->mac_size);
2610 }
Ronald Cron882eb782021-06-18 13:05:48 +02002611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002612 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002614 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002615}
2616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002617psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2618 const uint8_t *mac,
2619 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002620{
Steven Cooreman87885df2021-03-19 19:04:39 +01002621 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2622 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002624 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002625 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002626 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002627 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002629 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002630 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002631 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002632 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002633
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002634 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002635 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002636 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002637 }
2638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002639 status = psa_driver_wrapper_mac_verify_finish(operation,
2640 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002641
Dave Rodgman478ab542021-06-25 09:09:02 +01002642exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002643 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002645 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002646}
2647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002648static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2649 psa_algorithm_t alg,
2650 const uint8_t *input,
2651 size_t input_length,
2652 uint8_t *mac,
2653 size_t mac_size,
2654 size_t *mac_length,
2655 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002656{
2657 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002658 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002659 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002660 psa_key_slot_t *slot;
2661 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002662
Ronald Crondbb86462021-06-17 17:17:20 +02002663 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002664 key,
2665 &slot,
2666 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2667 alg);
2668 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002669 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002670 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002671
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002672 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002673 .core = slot->attr
2674 };
2675
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002676 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2677 &operation_mac_size);
2678 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002679 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002680 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002682 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002683 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002684 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002685 }
2686
2687 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002688 &attributes,
2689 slot->key.data, slot->key.bytes,
2690 alg,
2691 input, input_length,
2692 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002693
2694exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002695 /* In case of success, set the potential excess room in the output buffer
2696 * to an invalid value, to avoid potentially leaking a longer MAC.
2697 * In case of error, set the output length and content to a safe default,
2698 * such that in case the caller misses an error check, the output would be
2699 * an unachievable MAC.
2700 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002701 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002702 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002703 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002704 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002705 if (mac_size > operation_mac_size) {
2706 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2707 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002709 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002711 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002712}
2713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002714psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002715 psa_algorithm_t alg,
2716 const uint8_t *input,
2717 size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002718 uint8_t *mac,
2719 size_t mac_size,
2720 size_t *mac_length)
2721{
2722 return psa_mac_compute_internal(key, alg,
2723 input, input_length,
2724 mac, mac_size, mac_length, 1);
2725}
2726
2727psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2728 psa_algorithm_t alg,
2729 const uint8_t *input,
2730 size_t input_length,
2731 const uint8_t *mac,
2732 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002733{
2734 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002735 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2736 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002738 status = psa_mac_compute_internal(key, alg,
2739 input, input_length,
2740 actual_mac, sizeof(actual_mac),
2741 &actual_mac_length, 0);
2742 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002743 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002744 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002745
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002746 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002747 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002748 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002749 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002750 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002751 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002752 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002753 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002754
2755exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002756 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002758 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002759}
Gilles Peskine20035e32018-02-03 22:44:14 +01002760
Gilles Peskine20035e32018-02-03 22:44:14 +01002761/****************************************************************/
2762/* Asymmetric cryptography */
2763/****************************************************************/
2764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002765static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2766 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002767{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002768 if (input_is_message) {
2769 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2770 return PSA_ERROR_INVALID_ARGUMENT;
2771 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002773 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2774 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2775 return PSA_ERROR_INVALID_ARGUMENT;
2776 }
2777 }
2778 } else {
2779 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2780 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002781 }
2782 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002783
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002784 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002785}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002786
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002787static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2788 int input_is_message,
2789 psa_algorithm_t alg,
2790 const uint8_t *input,
2791 size_t input_length,
2792 uint8_t *signature,
2793 size_t signature_size,
2794 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002795{
2796 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2797 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002798 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002799 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002800
2801 *signature_length = 0;
2802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002803 status = psa_sign_verify_check_alg(input_is_message, alg);
2804 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002805 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002806 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002807
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002808 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002809 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002810 * buffer can in principle be empty since it doesn't actually have
2811 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002812 if (signature_size == 0) {
2813 return PSA_ERROR_BUFFER_TOO_SMALL;
2814 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002815
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002816 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002817 key, &slot,
2818 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2819 PSA_KEY_USAGE_SIGN_HASH,
2820 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002822 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002823 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002824 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002826 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002827 status = PSA_ERROR_INVALID_ARGUMENT;
2828 goto exit;
2829 }
2830
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002831 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002832 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002833 };
2834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002835 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002836 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002837 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002838 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002839 signature, signature_size, signature_length);
2840 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002841
2842 status = psa_driver_wrapper_sign_hash(
2843 &attributes, slot->key.data, slot->key.bytes,
2844 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002845 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002846 }
2847
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002848
2849exit:
2850 /* Fill the unused part of the output buffer (the whole buffer on error,
2851 * the trailing part on success) with something that isn't a valid signature
2852 * (barring an attack on the signature and deliberately-crafted input),
2853 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002854 if (status == PSA_SUCCESS) {
2855 memset(signature + *signature_length, '!',
2856 signature_size - *signature_length);
2857 } else {
2858 memset(signature, '!', signature_size);
2859 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002860 /* If signature_size is 0 then we have nothing to do. We must not call
2861 * memset because signature may be NULL in this case. */
2862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002863 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002865 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002866}
2867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002868static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2869 int input_is_message,
2870 psa_algorithm_t alg,
2871 const uint8_t *input,
2872 size_t input_length,
2873 const uint8_t *signature,
2874 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002875{
2876 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2877 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2878 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002880 status = psa_sign_verify_check_alg(input_is_message, alg);
2881 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002882 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002883 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002884
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002885 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002886 key, &slot,
2887 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2888 PSA_KEY_USAGE_VERIFY_HASH,
2889 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002891 if (status != PSA_SUCCESS) {
2892 return status;
2893 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002894
2895 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002896 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002897 };
2898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002899 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002900 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002901 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002902 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002903 signature, signature_length);
2904 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002905 status = psa_driver_wrapper_verify_hash(
2906 &attributes, slot->key.data, slot->key.bytes,
2907 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002908 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002909 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002911 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002912
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002913 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002914
2915}
2916
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002917psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002918 const psa_key_attributes_t *attributes,
2919 const uint8_t *key_buffer,
2920 size_t key_buffer_size,
2921 psa_algorithm_t alg,
2922 const uint8_t *input,
2923 size_t input_length,
2924 uint8_t *signature,
2925 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002926 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002927{
2928 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002930 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002931 size_t hash_length;
2932 uint8_t hash[PSA_HASH_MAX_SIZE];
2933
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002934 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002935 PSA_ALG_SIGN_GET_HASH(alg),
2936 input, input_length,
2937 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002939 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002940 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002941 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002942
2943 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002944 attributes, key_buffer, key_buffer_size,
2945 alg, hash, hash_length,
2946 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002947 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002949 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002950}
2951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002952psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2953 psa_algorithm_t alg,
2954 const uint8_t *input,
2955 size_t input_length,
2956 uint8_t *signature,
2957 size_t signature_size,
2958 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002959{
2960 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002961 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002962 signature, signature_size, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002963}
2964
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002965psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002966 const psa_key_attributes_t *attributes,
2967 const uint8_t *key_buffer,
2968 size_t key_buffer_size,
2969 psa_algorithm_t alg,
2970 const uint8_t *input,
2971 size_t input_length,
2972 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002973 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002974{
2975 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002976
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002977 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002978 size_t hash_length;
2979 uint8_t hash[PSA_HASH_MAX_SIZE];
2980
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002981 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002982 PSA_ALG_SIGN_GET_HASH(alg),
2983 input, input_length,
2984 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002985
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002986 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002987 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002988 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002989
2990 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002991 attributes, key_buffer, key_buffer_size,
2992 alg, hash, hash_length,
2993 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002994 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002996 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002997}
2998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002999psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3000 psa_algorithm_t alg,
3001 const uint8_t *input,
3002 size_t input_length,
3003 const uint8_t *signature,
3004 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003005{
3006 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003007 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003008 signature, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003009}
3010
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003011psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003012 const psa_key_attributes_t *attributes,
3013 const uint8_t *key_buffer, size_t key_buffer_size,
3014 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003015 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01003016{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003017 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3018 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3019 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003020#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003021 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3022 return mbedtls_psa_rsa_sign_hash(
3023 attributes,
3024 key_buffer, key_buffer_size,
3025 alg, hash, hash_length,
3026 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003027#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3028 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003029 } else {
3030 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003031 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003032 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3033 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003034#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003035 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3036 return mbedtls_psa_ecdsa_sign_hash(
3037 attributes,
3038 key_buffer, key_buffer_size,
3039 alg, hash, hash_length,
3040 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003041#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3042 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003043 } else {
3044 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003045 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003046 }
Ronald Cron4501c982021-03-19 20:09:32 +01003047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003048 (void) key_buffer;
3049 (void) key_buffer_size;
3050 (void) hash;
3051 (void) hash_length;
3052 (void) signature;
3053 (void) signature_size;
3054 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003056 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003057}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003059psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3060 psa_algorithm_t alg,
3061 const uint8_t *hash,
3062 size_t hash_length,
3063 uint8_t *signature,
3064 size_t signature_size,
3065 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003066{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003067 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003068 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003069 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003070}
3071
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003072psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003073 const psa_key_attributes_t *attributes,
3074 const uint8_t *key_buffer, size_t key_buffer_size,
3075 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003076 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003077{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003078 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3079 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3080 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003081#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3083 return mbedtls_psa_rsa_verify_hash(
3084 attributes,
3085 key_buffer, key_buffer_size,
3086 alg, hash, hash_length,
3087 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003088#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3089 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003090 } else {
3091 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003092 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003093 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3094 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003095#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003096 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3097 return mbedtls_psa_ecdsa_verify_hash(
3098 attributes,
3099 key_buffer, key_buffer_size,
3100 alg, hash, hash_length,
3101 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003102#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3103 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003104 } else {
3105 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003106 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003107 }
Ronald Cron4501c982021-03-19 20:09:32 +01003108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003109 (void) key_buffer;
3110 (void) key_buffer_size;
3111 (void) hash;
3112 (void) hash_length;
3113 (void) signature;
3114 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003116 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003117}
3118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003119psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3120 psa_algorithm_t alg,
3121 const uint8_t *hash,
3122 size_t hash_length,
3123 const uint8_t *signature,
3124 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003125{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003126 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003127 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003128 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003129}
3130
John Durkop0e005192020-10-31 22:06:54 -07003131#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003132static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3133 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003134{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003135 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3136 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3137 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3138 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003139}
John Durkop0e005192020-10-31 22:06:54 -07003140#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003142psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3143 psa_algorithm_t alg,
3144 const uint8_t *input,
3145 size_t input_length,
3146 const uint8_t *salt,
3147 size_t salt_length,
3148 uint8_t *output,
3149 size_t output_size,
3150 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003151{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003152 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003153 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003154 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003155
Darryl Green5cc689a2018-07-24 15:34:10 +01003156 (void) input;
3157 (void) input_length;
3158 (void) salt;
3159 (void) output;
3160 (void) output_size;
3161
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003162 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003164 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3165 return PSA_ERROR_INVALID_ARGUMENT;
3166 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003167
Ronald Cron5c522922020-11-14 16:35:34 +01003168 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003169 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3170 if (status != PSA_SUCCESS) {
3171 return status;
3172 }
3173 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3174 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003175 status = PSA_ERROR_INVALID_ARGUMENT;
3176 goto exit;
3177 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003179 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003180#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003181 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003182 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003183 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3184 slot->key.data,
3185 slot->key.bytes,
3186 &rsa);
3187 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003188 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003189 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003191 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003192 status = PSA_ERROR_BUFFER_TOO_SMALL;
3193 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003194 }
Ronald Cron7207d572021-09-08 14:28:35 +02003195#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3196 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003197 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003198#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003199 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003200 mbedtls_rsa_pkcs1_encrypt(rsa,
3201 mbedtls_psa_get_random,
3202 MBEDTLS_PSA_RANDOM_STATE,
3203 MBEDTLS_RSA_PUBLIC,
3204 input_length,
3205 input,
3206 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003207#else
3208 status = PSA_ERROR_NOT_SUPPORTED;
3209#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003210 } else
3211 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003212#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003213 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003214 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003215 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3216 mbedtls_psa_get_random,
3217 MBEDTLS_PSA_RANDOM_STATE,
3218 MBEDTLS_RSA_PUBLIC,
3219 salt, salt_length,
3220 input_length,
3221 input,
3222 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003223#else
3224 status = PSA_ERROR_NOT_SUPPORTED;
3225#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003226 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003227 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003228 }
Ronald Cron7207d572021-09-08 14:28:35 +02003229#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003230 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003231rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003232 if (status == PSA_SUCCESS) {
3233 *output_length = mbedtls_rsa_get_len(rsa);
3234 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003236 mbedtls_rsa_free(rsa);
3237 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003238#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003239 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003240 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003241 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003242 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003243
3244exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003245 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003247 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003248}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003250psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3251 psa_algorithm_t alg,
3252 const uint8_t *input,
3253 size_t input_length,
3254 const uint8_t *salt,
3255 size_t salt_length,
3256 uint8_t *output,
3257 size_t output_size,
3258 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003259{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003260 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003261 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003262 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003263
Darryl Green5cc689a2018-07-24 15:34:10 +01003264 (void) input;
3265 (void) input_length;
3266 (void) salt;
3267 (void) output;
3268 (void) output_size;
3269
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003270 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003272 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3273 return PSA_ERROR_INVALID_ARGUMENT;
3274 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003275
Ronald Cron5c522922020-11-14 16:35:34 +01003276 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003277 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3278 if (status != PSA_SUCCESS) {
3279 return status;
3280 }
3281 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003282 status = PSA_ERROR_INVALID_ARGUMENT;
3283 goto exit;
3284 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003286 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003287#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003288 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003289 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003290 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3291 slot->key.data,
3292 slot->key.bytes,
3293 &rsa);
3294 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003295 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003296 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003298 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003299 status = PSA_ERROR_INVALID_ARGUMENT;
3300 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003301 }
Ronald Cron7207d572021-09-08 14:28:35 +02003302#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3303 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003305 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003306#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003307 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003308 mbedtls_rsa_pkcs1_decrypt(rsa,
3309 mbedtls_psa_get_random,
3310 MBEDTLS_PSA_RANDOM_STATE,
3311 MBEDTLS_RSA_PRIVATE,
3312 output_length,
3313 input,
3314 output,
3315 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003316#else
3317 status = PSA_ERROR_NOT_SUPPORTED;
3318#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003319 } else
3320 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003321#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003322 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003323 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003324 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3325 mbedtls_psa_get_random,
3326 MBEDTLS_PSA_RANDOM_STATE,
3327 MBEDTLS_RSA_PRIVATE,
3328 salt, salt_length,
3329 output_length,
3330 input,
3331 output,
3332 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003333#else
3334 status = PSA_ERROR_NOT_SUPPORTED;
3335#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003336 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003337 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003338 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003339
Ronald Cron7207d572021-09-08 14:28:35 +02003340#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003341 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003342rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003343 mbedtls_rsa_free(rsa);
3344 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003345#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3346 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003347 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003348 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003349 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003350
3351exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003352 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003354 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003355}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003356
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003357
3358
mohammad1603503973b2018-03-12 15:59:30 +02003359/****************************************************************/
3360/* Symmetric cryptography */
3361/****************************************************************/
3362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003363static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3364 mbedtls_svc_key_id_t key,
3365 psa_algorithm_t alg,
3366 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003367{
3368 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3369 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003370 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003371 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003372 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3373 PSA_KEY_USAGE_ENCRYPT :
3374 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003375
3376 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003377 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003378 status = PSA_ERROR_BAD_STATE;
3379 goto exit;
3380 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003382 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003383 status = PSA_ERROR_INVALID_ARGUMENT;
3384 goto exit;
3385 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003387 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3388 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003389 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003390 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003391
Ronald Cron49fafa92021-03-10 08:34:23 +01003392 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003393 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003394 * so we only set it (in the driver wrapper) after resources have been
3395 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003396 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003397 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003398 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003399 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003400 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003401 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003402 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003403 }
3404 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003405
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003406 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003407 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003408 };
3409
Ronald Cronab99ac22020-10-01 16:38:28 +02003410 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003411 if (cipher_operation == MBEDTLS_ENCRYPT) {
3412 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3413 &attributes,
3414 slot->key.data,
3415 slot->key.bytes,
3416 alg);
3417 } else {
3418 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3419 &attributes,
3420 slot->key.data,
3421 slot->key.bytes,
3422 alg);
3423 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003424
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003425exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003426 if (status != PSA_SUCCESS) {
3427 psa_cipher_abort(operation);
3428 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003430 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003432 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003433}
3434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003435psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3436 mbedtls_svc_key_id_t key,
3437 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003438{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003439 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003440}
3441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003442psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3443 mbedtls_svc_key_id_t key,
3444 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003445{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003446 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003447}
3448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003449psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3450 uint8_t *iv,
3451 size_t iv_size,
3452 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003453{
Ronald Cron6d051732020-10-01 14:10:20 +02003454 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003455 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergey04eb7c02023-03-06 15:37:23 -07003456 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003458 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003459 status = PSA_ERROR_BAD_STATE;
3460 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003461 }
3462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003463 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003464 status = PSA_ERROR_BAD_STATE;
3465 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003466 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003467
Ronald Cronc423acb2021-07-05 12:31:44 +02003468 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003469 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003470 status = PSA_ERROR_BUFFER_TOO_SMALL;
3471 goto exit;
3472 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003474 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003475 status = PSA_ERROR_GENERIC_ERROR;
3476 goto exit;
3477 }
3478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003479 status = psa_generate_random(local_iv, default_iv_length);
3480 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003481 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003482 }
Ronald Cron5618a392021-03-26 09:52:26 +01003483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003484 status = psa_driver_wrapper_cipher_set_iv(operation,
3485 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003486
3487exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003488 if (status == PSA_SUCCESS) {
3489 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003490 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003491 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003492 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003493 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003494 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003495 }
Ronald Cron6d051732020-10-01 14:10:20 +02003496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003497 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003498}
3499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003500psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3501 const uint8_t *iv,
3502 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003503{
Ronald Cron6d051732020-10-01 14:10:20 +02003504 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003506 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003507 status = PSA_ERROR_BAD_STATE;
3508 goto exit;
3509 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003511 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003512 status = PSA_ERROR_BAD_STATE;
3513 goto exit;
3514 }
Ronald Crona0d68172021-03-26 10:15:08 +01003515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003516 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003517 status = PSA_ERROR_INVALID_ARGUMENT;
3518 goto exit;
3519 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003521 status = psa_driver_wrapper_cipher_set_iv(operation,
3522 iv,
3523 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003524
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003525exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003526 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003527 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003528 } else {
3529 psa_cipher_abort(operation);
3530 }
3531 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003532}
3533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003534psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3535 const uint8_t *input,
3536 size_t input_length,
3537 uint8_t *output,
3538 size_t output_size,
3539 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003540{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003541 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003542
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003543 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003544 status = PSA_ERROR_BAD_STATE;
3545 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003546 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003548 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003549 status = PSA_ERROR_BAD_STATE;
3550 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003551 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003553 status = psa_driver_wrapper_cipher_update(operation,
3554 input,
3555 input_length,
3556 output,
3557 output_size,
3558 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003559
3560exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003561 if (status != PSA_SUCCESS) {
3562 psa_cipher_abort(operation);
3563 }
Ronald Cron6d051732020-10-01 14:10:20 +02003564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003565 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003566}
3567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003568psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3569 uint8_t *output,
3570 size_t output_size,
3571 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003572{
David Saadab4ecc272019-02-14 13:48:10 +02003573 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003575 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003576 status = PSA_ERROR_BAD_STATE;
3577 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003578 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003580 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003581 status = PSA_ERROR_BAD_STATE;
3582 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003583 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003585 status = psa_driver_wrapper_cipher_finish(operation,
3586 output,
3587 output_size,
3588 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003589
3590exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003591 if (status == PSA_SUCCESS) {
3592 return psa_cipher_abort(operation);
3593 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003594 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003595 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003597 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003598 }
mohammad1603503973b2018-03-12 15:59:30 +02003599}
3600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003601psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003602{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003603 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003604 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003605 * in use. It's ok to call abort on such an object, and there's
3606 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003607 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003608 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003610 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003611
Ronald Cron49fafa92021-03-10 08:34:23 +01003612 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003613 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003614 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003616 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003617}
3618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003619psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3620 psa_algorithm_t alg,
3621 const uint8_t *input,
3622 size_t input_length,
3623 uint8_t *output,
3624 size_t output_size,
3625 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003626{
3627 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003628 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003629 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003630 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003631 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3632 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003633
David Horstmann34980bd2023-11-29 17:07:13 +00003634 SWAP_FOR_LOCAL_INPUT(input, input_length);
3635 SWAP_FOR_LOCAL_OUTPUT(output, output_size);
David Horstmannd2ad8862023-11-27 17:43:05 +00003636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003637 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003638 status = PSA_ERROR_INVALID_ARGUMENT;
3639 goto exit;
3640 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003642 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3643 PSA_KEY_USAGE_ENCRYPT,
3644 alg);
3645 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003646 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003647 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003648
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003649 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003650 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003651 };
3652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003653 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3654 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003655 status = PSA_ERROR_GENERIC_ERROR;
3656 goto exit;
3657 }
3658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003659 if (default_iv_length > 0) {
3660 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003661 status = PSA_ERROR_BUFFER_TOO_SMALL;
3662 goto exit;
3663 }
3664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003665 status = psa_generate_random(local_iv, default_iv_length);
3666 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003667 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003668 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003669 }
3670
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003671 status = psa_driver_wrapper_cipher_encrypt(
3672 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003673 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003674 mbedtls_buffer_offset(output, default_iv_length),
3675 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003676
3677exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003678 unlock_status = psa_unlock_key_slot(slot);
3679 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003680 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003681 }
Ronald Cron16377072021-07-08 19:00:07 +02003682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003683 if (status == PSA_SUCCESS) {
3684 if (default_iv_length > 0) {
3685 memcpy(output, local_iv, default_iv_length);
3686 }
3687 *output_length += default_iv_length;
3688 } else {
3689 *output_length = 0;
3690 }
3691
David Horstmann34980bd2023-11-29 17:07:13 +00003692 FREE_LOCAL_INPUT(input);
3693 FREE_LOCAL_OUTPUT(output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003695 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003696}
3697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003698psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3699 psa_algorithm_t alg,
3700 const uint8_t *input,
3701 size_t input_length,
3702 uint8_t *output,
3703 size_t output_size,
3704 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003705{
3706 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003707 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003708 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003709 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003711 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003712 status = PSA_ERROR_INVALID_ARGUMENT;
3713 goto exit;
3714 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003716 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3717 PSA_KEY_USAGE_DECRYPT,
3718 alg);
3719 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003720 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003721 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003722
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003723 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003724 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003725 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003727 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003728 status = PSA_ERROR_INVALID_ARGUMENT;
3729 goto exit;
3730 }
3731
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003732 status = psa_driver_wrapper_cipher_decrypt(
3733 &attributes, slot->key.data, slot->key.bytes,
3734 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003735 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003736
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003737exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003738 unlock_status = psa_unlock_key_slot(slot);
3739 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003740 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003741 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003743 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003744 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003745 }
Ronald Cron16377072021-07-08 19:00:07 +02003746
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003747 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003748}
3749
3750
mohammad16035955c982018-04-26 00:53:03 +03003751/****************************************************************/
3752/* AEAD */
3753/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003754
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003755psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3756 psa_algorithm_t alg,
3757 const uint8_t *nonce,
3758 size_t nonce_length,
3759 const uint8_t *additional_data,
3760 size_t additional_data_length,
3761 const uint8_t *plaintext,
3762 size_t plaintext_length,
3763 uint8_t *ciphertext,
3764 size_t ciphertext_size,
3765 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003766{
Ronald Cron215633c2021-03-16 17:15:37 +01003767 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3768 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003769
mohammad1603f08a5502018-06-03 15:05:47 +03003770 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003772 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3773 return PSA_ERROR_NOT_SUPPORTED;
3774 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003775
Ronald Cron9a986162021-03-26 12:40:07 +01003776 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003777 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3778 if (status != PSA_SUCCESS) {
3779 return status;
3780 }
mohammad16035955c982018-04-26 00:53:03 +03003781
Ronald Cron215633c2021-03-16 17:15:37 +01003782 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003783 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003784 };
mohammad16035955c982018-04-26 00:53:03 +03003785
Ronald Cronde822812021-03-17 16:08:20 +01003786 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003787 &attributes, slot->key.data, slot->key.bytes,
3788 alg,
3789 nonce, nonce_length,
3790 additional_data, additional_data_length,
3791 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003792 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003794 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3795 memset(ciphertext, 0, ciphertext_size);
3796 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003798 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003800 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003801}
3802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003803psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3804 psa_algorithm_t alg,
3805 const uint8_t *nonce,
3806 size_t nonce_length,
3807 const uint8_t *additional_data,
3808 size_t additional_data_length,
3809 const uint8_t *ciphertext,
3810 size_t ciphertext_length,
3811 uint8_t *plaintext,
3812 size_t plaintext_size,
3813 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003814{
Ronald Cron215633c2021-03-16 17:15:37 +01003815 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3816 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003817
Gilles Peskineee652a32018-06-01 19:23:52 +02003818 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003820 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3821 return PSA_ERROR_NOT_SUPPORTED;
3822 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003823
Ronald Cron9a986162021-03-26 12:40:07 +01003824 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003825 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3826 if (status != PSA_SUCCESS) {
3827 return status;
3828 }
mohammad16035955c982018-04-26 00:53:03 +03003829
Ronald Cron215633c2021-03-16 17:15:37 +01003830 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003831 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003832 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003833
Ronald Cronde822812021-03-17 16:08:20 +01003834 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003835 &attributes, slot->key.data, slot->key.bytes,
3836 alg,
3837 nonce, nonce_length,
3838 additional_data, additional_data_length,
3839 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003840 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003842 if (status != PSA_SUCCESS && plaintext_size != 0) {
3843 memset(plaintext, 0, plaintext_size);
3844 }
mohammad160360a64d02018-06-03 17:20:42 +03003845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003846 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003848 return status;
mohammad16035955c982018-04-26 00:53:03 +03003849}
3850
Gilles Peskinee59236f2018-01-27 23:32:46 +01003851/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003852/* Generators */
3853/****************************************************************/
3854
John Durkop07cc04a2020-11-16 22:08:34 -08003855#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3856 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3857 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3858#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003859#endif /* At least one builtin KDF */
3860
3861#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3862 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3863 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3864static psa_status_t psa_key_derivation_start_hmac(
3865 psa_mac_operation_t *operation,
3866 psa_algorithm_t hash_alg,
3867 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003868 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02003869{
3870 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3871 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003872 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3873 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
3874 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02003875
Steven Cooreman15f0d922021-05-07 14:14:37 +02003876 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003877 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02003878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003879 status = psa_driver_wrapper_mac_sign_setup(operation,
3880 &attributes,
3881 hmac_key, hmac_key_length,
3882 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02003883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003884 psa_reset_key_attributes(&attributes);
3885 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02003886}
3887#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003888
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003889#define HKDF_STATE_INIT 0 /* no input yet */
3890#define HKDF_STATE_STARTED 1 /* got salt */
3891#define HKDF_STATE_KEYED 2 /* got key */
3892#define HKDF_STATE_OUTPUT 3 /* output started */
3893
Gilles Peskinecbe66502019-05-16 16:59:18 +02003894static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003895 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003896{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003897 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
3898 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
3899 } else {
3900 return operation->alg;
3901 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003902}
3903
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003904psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003905{
3906 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003907 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
3908 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02003909 /* The object has (apparently) been initialized but it is not
3910 * in use. It's ok to call abort on such an object, and there's
3911 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003912 } else
John Durkopd0321952020-10-29 21:37:36 -07003913#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003914 if (PSA_ALG_IS_HKDF(kdf_alg)) {
3915 mbedtls_free(operation->ctx.hkdf.info);
3916 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
3917 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003918#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3919#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3920 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003921 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
3922 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
3923 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
3924 if (operation->ctx.tls12_prf.secret != NULL) {
3925 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
3926 operation->ctx.tls12_prf.secret_length);
3927 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003928 }
3929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003930 if (operation->ctx.tls12_prf.seed != NULL) {
3931 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
3932 operation->ctx.tls12_prf.seed_length);
3933 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01003934 }
3935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003936 if (operation->ctx.tls12_prf.label != NULL) {
3937 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
3938 operation->ctx.tls12_prf.label_length);
3939 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01003940 }
3941
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003942 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003943
3944 /* We leave the fields Ai and output_block to be erased safely by the
3945 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003946 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003947#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3948 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003949 {
3950 status = PSA_ERROR_BAD_STATE;
3951 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003952 mbedtls_platform_zeroize(operation, sizeof(*operation));
3953 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003954}
3955
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003956psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003957 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003958{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003959 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003960 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003961 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003962 }
3963
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003964 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003965 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003966}
3967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003968psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
3969 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003970{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003971 if (operation->alg == 0) {
3972 return PSA_ERROR_BAD_STATE;
3973 }
3974 if (capacity > operation->capacity) {
3975 return PSA_ERROR_INVALID_ARGUMENT;
3976 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003977 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003978 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003979}
3980
John Durkopd0321952020-10-29 21:37:36 -07003981#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003982/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003983 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003984static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
3985 psa_algorithm_t hash_alg,
3986 uint8_t *output,
3987 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003988{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003989 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02003990 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003991 psa_status_t status;
3992
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003993 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
3994 return PSA_ERROR_BAD_STATE;
3995 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003996 hkdf->state = HKDF_STATE_OUTPUT;
3997
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003998 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003999 /* Copy what remains of the current block */
4000 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004001 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004002 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004003 }
4004 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004005 output += n;
4006 output_length -= n;
4007 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004008 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004009 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004010 }
Gilles Peskined54931c2018-07-17 21:06:59 +02004011 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004012 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004013 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004014 * object was corrupted or if this function is called directly
4015 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004016 if (hkdf->block_number == 0xff) {
4017 return PSA_ERROR_BAD_STATE;
4018 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004019
4020 /* We need a new block */
4021 ++hkdf->block_number;
4022 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004023
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004024 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4025 hash_alg,
4026 hkdf->prk,
4027 hash_length);
4028 if (status != PSA_SUCCESS) {
4029 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004030 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004031
4032 if (hkdf->block_number != 1) {
4033 status = psa_mac_update(&hkdf->hmac,
4034 hkdf->output_block,
4035 hash_length);
4036 if (status != PSA_SUCCESS) {
4037 return status;
4038 }
4039 }
4040 status = psa_mac_update(&hkdf->hmac,
4041 hkdf->info,
4042 hkdf->info_length);
4043 if (status != PSA_SUCCESS) {
4044 return status;
4045 }
4046 status = psa_mac_update(&hkdf->hmac,
4047 &hkdf->block_number, 1);
4048 if (status != PSA_SUCCESS) {
4049 return status;
4050 }
4051 status = psa_mac_sign_finish(&hkdf->hmac,
4052 hkdf->output_block,
4053 sizeof(hkdf->output_block),
4054 &hmac_output_length);
4055 if (status != PSA_SUCCESS) {
4056 return status;
4057 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004058 }
4059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004060 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004061}
John Durkop07cc04a2020-11-16 22:08:34 -08004062#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004063
John Durkop07cc04a2020-11-16 22:08:34 -08004064#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4065 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004066static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4067 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004068 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004069{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004070 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4071 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004072 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4073 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004074 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004075
Janos Follath7742fee2019-06-17 12:58:10 +01004076 /* We can't be wanting more output after block 0xff, otherwise
4077 * the capacity check in psa_key_derivation_output_bytes() would have
4078 * prevented this call. It could happen only if the operation
4079 * object was corrupted or if this function is called directly
4080 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004081 if (tls12_prf->block_number == 0xff) {
4082 return PSA_ERROR_CORRUPTION_DETECTED;
4083 }
Janos Follath7742fee2019-06-17 12:58:10 +01004084
4085 /* We need a new block */
4086 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004087 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004088
4089 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4090 *
4091 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4092 *
4093 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4094 * HMAC_hash(secret, A(2) + seed) +
4095 * HMAC_hash(secret, A(3) + seed) + ...
4096 *
4097 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004098 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004099 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004100 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004101 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004102 * is currently extracted as `output_block` and where i is
4103 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004104 */
4105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004106 status = psa_key_derivation_start_hmac(&hmac,
4107 hash_alg,
4108 tls12_prf->secret,
4109 tls12_prf->secret_length);
4110 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004111 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004112 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004113
4114 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004115 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004116 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4117 * the variable seed and in this instance means it in the context of the
4118 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004119 status = psa_mac_update(&hmac,
4120 tls12_prf->label,
4121 tls12_prf->label_length);
4122 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004123 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004124 }
4125 status = psa_mac_update(&hmac,
4126 tls12_prf->seed,
4127 tls12_prf->seed_length);
4128 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004129 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004130 }
4131 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004132 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004133 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4134 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004135 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004136 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004137 }
4138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004139 status = psa_mac_sign_finish(&hmac,
4140 tls12_prf->Ai, hash_length,
4141 &hmac_output_length);
4142 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004143 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004144 }
4145 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004146 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004147 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004148
4149 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004150 status = psa_key_derivation_start_hmac(&hmac,
4151 hash_alg,
4152 tls12_prf->secret,
4153 tls12_prf->secret_length);
4154 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004155 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004156 }
4157 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4158 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004159 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004160 }
4161 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4162 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004163 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004164 }
4165 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4166 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004167 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004168 }
4169 status = psa_mac_sign_finish(&hmac,
4170 tls12_prf->output_block, hash_length,
4171 &hmac_output_length);
4172 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004173 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004174 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004175
Janos Follath7742fee2019-06-17 12:58:10 +01004176
4177cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004178 cleanup_status = psa_mac_abort(&hmac);
4179 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004180 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004181 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004183 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004184}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004185
Janos Follath844eb0e2019-06-19 12:10:49 +01004186static psa_status_t psa_key_derivation_tls12_prf_read(
4187 psa_tls12_prf_key_derivation_t *tls12_prf,
4188 psa_algorithm_t alg,
4189 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004190 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004191{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004192 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4193 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004194 psa_status_t status;
4195 uint8_t offset, length;
4196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004197 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004198 case PSA_TLS12_PRF_STATE_LABEL_SET:
4199 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4200 break;
4201 case PSA_TLS12_PRF_STATE_OUTPUT:
4202 break;
4203 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004204 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004205 }
4206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004207 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004208 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004209 if (tls12_prf->left_in_block == 0) {
4210 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4211 alg);
4212 if (status != PSA_SUCCESS) {
4213 return status;
4214 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004215
4216 continue;
4217 }
4218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004219 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004220 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004221 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004222 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004223 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004224
4225 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004226 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004227 output += length;
4228 output_length -= length;
4229 tls12_prf->left_in_block -= length;
4230 }
4231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004232 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004233}
John Durkop07cc04a2020-11-16 22:08:34 -08004234#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4235 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004236
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004237psa_status_t psa_key_derivation_output_bytes(
4238 psa_key_derivation_operation_t *operation,
4239 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004240 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004241{
4242 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004243 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004245 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004246 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004247 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004248 }
4249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004250 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004251 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004252 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004253 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004254 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004255 goto exit;
4256 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004257 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004258 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004259 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004260 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4261 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004262 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004263 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004264 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004265 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004266 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004267
John Durkopd0321952020-10-29 21:37:36 -07004268#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004269 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4270 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4271 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4272 output, output_length);
4273 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004274#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4275#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4276 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004277 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4278 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4279 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4280 kdf_alg, output,
4281 output_length);
4282 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004283#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4284 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004285 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004286 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004287 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004288 }
4289
4290exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004291 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004292 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004293 * This allows us to differentiate between exhausted operations and
4294 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4295 * operations. */
4296 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004297 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004298 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004299 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004300 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004301 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004302}
4303
David Brown7807bf72021-02-09 16:28:23 -07004304#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004305static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004306{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004307 if (data_size >= 8) {
4308 mbedtls_des_key_set_parity(data);
4309 }
4310 if (data_size >= 16) {
4311 mbedtls_des_key_set_parity(data + 8);
4312 }
4313 if (data_size >= 24) {
4314 mbedtls_des_key_set_parity(data + 16);
4315 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004316}
David Brown7807bf72021-02-09 16:28:23 -07004317#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004318
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004319static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004320 psa_key_slot_t *slot,
4321 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004322 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004323{
4324 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004325 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004326 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004327 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004329 if (!key_type_is_raw_bytes(slot->attr.type)) {
4330 return PSA_ERROR_INVALID_ARGUMENT;
4331 }
4332 if (bits % 8 != 0) {
4333 return PSA_ERROR_INVALID_ARGUMENT;
4334 }
4335 data = mbedtls_calloc(1, bytes);
4336 if (data == NULL) {
4337 return PSA_ERROR_INSUFFICIENT_MEMORY;
4338 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004340 status = psa_key_derivation_output_bytes(operation, data, bytes);
4341 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004342 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004343 }
David Brown12ca5032021-02-11 11:02:00 -07004344#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004345 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4346 psa_des_set_key_parity(data, bytes);
4347 }
David Brown8107e312021-02-12 08:08:46 -07004348#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004350 status = psa_allocate_buffer_to_slot(slot, bytes);
4351 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004352 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004353 }
Ronald Crondd04d422020-11-28 15:14:42 +01004354
Ronald Cronbf33c932020-11-28 18:06:53 +01004355 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004356 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004357 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004358 };
4359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004360 status = psa_driver_wrapper_import_key(&attributes,
4361 data, bytes,
4362 slot->key.data,
4363 slot->key.bytes,
4364 &slot->key.bytes, &bits);
4365 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004366 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004367 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004368
4369exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004370 mbedtls_free(data);
4371 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004372}
4373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004374psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4375 psa_key_derivation_operation_t *operation,
4376 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004377{
4378 psa_status_t status;
4379 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004380 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004381
Ronald Cron81709fc2020-11-14 12:10:32 +01004382 *key = MBEDTLS_SVC_KEY_ID_INIT;
4383
Gilles Peskine0f84d622019-09-12 19:03:13 +02004384 /* Reject any attempt to create a zero-length key so that we don't
4385 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004386 if (psa_get_key_bits(attributes) == 0) {
4387 return PSA_ERROR_INVALID_ARGUMENT;
4388 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004390 if (operation->alg == PSA_ALG_NONE) {
4391 return PSA_ERROR_BAD_STATE;
4392 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004394 if (!operation->can_output_key) {
4395 return PSA_ERROR_NOT_PERMITTED;
4396 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004398 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4399 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004400#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004401 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004402 /* Deriving a key in a secure element is not implemented yet. */
4403 status = PSA_ERROR_NOT_SUPPORTED;
4404 }
4405#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004406 if (status == PSA_SUCCESS) {
4407 status = psa_generate_derived_key_internal(slot,
4408 attributes->core.bits,
4409 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004410 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004411 if (status == PSA_SUCCESS) {
4412 status = psa_finish_key_creation(slot, driver, key);
4413 }
4414 if (status != PSA_SUCCESS) {
4415 psa_fail_key_creation(slot, driver);
4416 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004418 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004419}
4420
Gilles Peskineeab56e42018-07-12 17:12:33 +02004421
4422
4423/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004424/* Key derivation */
4425/****************************************************************/
4426
Steven Cooreman932ffb72021-02-15 12:14:32 +01004427#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004428static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004429{
4430#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004431 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4432 return 1;
4433 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004434#endif
4435#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004436 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4437 return 1;
4438 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004439#endif
4440#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004441 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4442 return 1;
4443 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004444#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004445 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004446}
4447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004448static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004449{
4450 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004451 psa_status_t status = psa_hash_setup(&operation, alg);
4452 psa_hash_abort(&operation);
4453 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004454}
4455
Gilles Peskine969c5d62019-01-16 15:53:06 +01004456static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004457 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004458 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004459{
Janos Follath5fe19732019-06-20 15:09:30 +01004460 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4461 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004462 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004463
Gilles Peskine969c5d62019-01-16 15:53:06 +01004464 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004465 if (!is_kdf_alg_supported(kdf_alg)) {
4466 return PSA_ERROR_NOT_SUPPORTED;
4467 }
John Durkop07cc04a2020-11-16 22:08:34 -08004468
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004469 /* All currently supported key derivation algorithms are based on a
4470 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004471 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4472 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4473 if (hash_size == 0) {
4474 return PSA_ERROR_NOT_SUPPORTED;
4475 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004476
4477 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4478 * we might fail later, which is somewhat unfriendly and potentially
4479 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004480 psa_status_t status = psa_hash_try_support(hash_alg);
4481 if (status != PSA_SUCCESS) {
4482 return status;
4483 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004485 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4486 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4487 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4488 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004489 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004490
Gilles Peskinecdacf042021-04-29 21:10:00 +02004491 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004492 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004493}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004495static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004496{
4497#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004498 if (alg == PSA_ALG_ECDH) {
4499 return PSA_SUCCESS;
4500 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004501#endif
4502 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004503 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004504}
John Durkop07cc04a2020-11-16 22:08:34 -08004505#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004507psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4508 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004509{
4510 psa_status_t status;
4511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004512 if (operation->alg != 0) {
4513 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004514 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004516 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4517 return PSA_ERROR_INVALID_ARGUMENT;
4518 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4519#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4520 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4521 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4522 status = psa_key_agreement_try_support(ka_alg);
4523 if (status != PSA_SUCCESS) {
4524 return status;
4525 }
4526 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4527#else
4528 return PSA_ERROR_NOT_SUPPORTED;
4529#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4530 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4531#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4532 status = psa_key_derivation_setup_kdf(operation, alg);
4533#else
4534 return PSA_ERROR_NOT_SUPPORTED;
4535#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4536 } else {
4537 return PSA_ERROR_INVALID_ARGUMENT;
4538 }
4539
4540 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004541 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004542 }
4543 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004544}
4545
John Durkopd0321952020-10-29 21:37:36 -07004546#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004547static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4548 psa_algorithm_t hash_alg,
4549 psa_key_derivation_step_t step,
4550 const uint8_t *data,
4551 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004552{
4553 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004554 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004555 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004556 if (hkdf->state != HKDF_STATE_INIT) {
4557 return PSA_ERROR_BAD_STATE;
4558 } else {
4559 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4560 hash_alg,
4561 data, data_length);
4562 if (status != PSA_SUCCESS) {
4563 return status;
4564 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004565 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004566 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004567 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004568 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004569 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004570 if (hkdf->state == HKDF_STATE_INIT) {
4571 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4572 hash_alg,
4573 NULL, 0);
4574 if (status != PSA_SUCCESS) {
4575 return status;
4576 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004577 hkdf->state = HKDF_STATE_STARTED;
4578 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004579 if (hkdf->state != HKDF_STATE_STARTED) {
4580 return PSA_ERROR_BAD_STATE;
4581 }
4582 status = psa_mac_update(&hkdf->hmac,
4583 data, data_length);
4584 if (status != PSA_SUCCESS) {
4585 return status;
4586 }
4587 status = psa_mac_sign_finish(&hkdf->hmac,
4588 hkdf->prk,
4589 sizeof(hkdf->prk),
4590 &data_length);
4591 if (status != PSA_SUCCESS) {
4592 return status;
4593 }
4594 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004595 hkdf->block_number = 0;
4596 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004597 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004598 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004599 if (hkdf->state == HKDF_STATE_OUTPUT) {
4600 return PSA_ERROR_BAD_STATE;
4601 }
4602 if (hkdf->info_set) {
4603 return PSA_ERROR_BAD_STATE;
4604 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004605 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004606 if (data_length != 0) {
4607 hkdf->info = mbedtls_calloc(1, data_length);
4608 if (hkdf->info == NULL) {
4609 return PSA_ERROR_INSUFFICIENT_MEMORY;
4610 }
4611 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004612 }
4613 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004614 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004615 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004616 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004617 }
4618}
John Durkop07cc04a2020-11-16 22:08:34 -08004619#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004620
John Durkop07cc04a2020-11-16 22:08:34 -08004621#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4622 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4624 const uint8_t *data,
4625 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004626{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004627 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4628 return PSA_ERROR_BAD_STATE;
4629 }
Janos Follathf08e2652019-06-13 09:05:41 +01004630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004631 if (data_length != 0) {
4632 prf->seed = mbedtls_calloc(1, data_length);
4633 if (prf->seed == NULL) {
4634 return PSA_ERROR_INSUFFICIENT_MEMORY;
4635 }
Janos Follathf08e2652019-06-13 09:05:41 +01004636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004637 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004638 prf->seed_length = data_length;
4639 }
Janos Follathf08e2652019-06-13 09:05:41 +01004640
Gilles Peskine43f958b2020-12-13 14:55:14 +01004641 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004643 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004644}
4645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004646static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4647 const uint8_t *data,
4648 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004649{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004650 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4651 return PSA_ERROR_BAD_STATE;
4652 }
Janos Follath81550542019-06-13 14:26:34 +01004653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004654 if (data_length != 0) {
4655 prf->secret = mbedtls_calloc(1, data_length);
4656 if (prf->secret == NULL) {
4657 return PSA_ERROR_INSUFFICIENT_MEMORY;
4658 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004659
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004660 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004661 prf->secret_length = data_length;
4662 }
Janos Follath81550542019-06-13 14:26:34 +01004663
Gilles Peskine43f958b2020-12-13 14:55:14 +01004664 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004666 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004667}
4668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004669static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4670 const uint8_t *data,
4671 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004672{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004673 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4674 return PSA_ERROR_BAD_STATE;
4675 }
Janos Follath63028dd2019-06-13 09:15:47 +01004676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004677 if (data_length != 0) {
4678 prf->label = mbedtls_calloc(1, data_length);
4679 if (prf->label == NULL) {
4680 return PSA_ERROR_INSUFFICIENT_MEMORY;
4681 }
Janos Follath63028dd2019-06-13 09:15:47 +01004682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004683 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004684 prf->label_length = data_length;
4685 }
Janos Follath63028dd2019-06-13 09:15:47 +01004686
Gilles Peskine43f958b2020-12-13 14:55:14 +01004687 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004689 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01004690}
4691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004692static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
4693 psa_key_derivation_step_t step,
4694 const uint8_t *data,
4695 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01004696{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004697 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01004698 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004699 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01004700 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004701 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01004702 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004703 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01004704 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004705 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01004706 }
4707}
John Durkop07cc04a2020-11-16 22:08:34 -08004708#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4709 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4710
4711#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4712static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4713 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004714 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004715 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08004716{
4717 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004718 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08004719 uint8_t *cur = pms;
4720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004721 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
4722 return PSA_ERROR_INVALID_ARGUMENT;
4723 }
John Durkop07cc04a2020-11-16 22:08:34 -08004724
4725 /* Quoting RFC 4279, Section 2:
4726 *
4727 * The premaster secret is formed as follows: if the PSK is N octets
4728 * long, concatenate a uint16 with the value N, N zero octets, a second
4729 * uint16 with the value N, and the PSK itself.
4730 */
4731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004732 *cur++ = MBEDTLS_BYTE_1(data_length);
4733 *cur++ = MBEDTLS_BYTE_0(data_length);
4734 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004735 cur += data_length;
4736 *cur++ = pms[0];
4737 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004738 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004739 cur += data_length;
4740
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004741 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08004742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004743 mbedtls_platform_zeroize(pms, sizeof(pms));
4744 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08004745}
Janos Follath6660f0e2019-06-17 08:44:03 +01004746
4747static psa_status_t psa_tls12_prf_psk_to_ms_input(
4748 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004749 psa_key_derivation_step_t step,
4750 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004751 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01004752{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004753 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
4754 return psa_tls12_prf_psk_to_ms_set_key(prf,
4755 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01004756 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004758 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01004759}
John Durkop07cc04a2020-11-16 22:08:34 -08004760#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004761
Gilles Peskineb8965192019-09-24 16:21:10 +02004762/** Check whether the given key type is acceptable for the given
4763 * input step of a key derivation.
4764 *
4765 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4766 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4767 * Both secret and non-secret inputs can alternatively have the type
4768 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4769 * that the input was passed as a buffer rather than via a key object.
4770 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004771static int psa_key_derivation_check_input_type(
4772 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004773 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02004774{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004775 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004776 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004777 if (key_type == PSA_KEY_TYPE_DERIVE) {
4778 return PSA_SUCCESS;
4779 }
4780 if (key_type == PSA_KEY_TYPE_NONE) {
4781 return PSA_SUCCESS;
4782 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004783 break;
4784 case PSA_KEY_DERIVATION_INPUT_LABEL:
4785 case PSA_KEY_DERIVATION_INPUT_SALT:
4786 case PSA_KEY_DERIVATION_INPUT_INFO:
4787 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004788 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
4789 return PSA_SUCCESS;
4790 }
4791 if (key_type == PSA_KEY_TYPE_NONE) {
4792 return PSA_SUCCESS;
4793 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004794 break;
4795 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004796 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02004797}
4798
Janos Follathb80a94e2019-06-12 15:54:46 +01004799static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004800 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004801 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004802 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004803 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004804 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004805{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004806 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004807 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004808
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004809 status = psa_key_derivation_check_input_type(step, key_type);
4810 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004811 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004812 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004813
John Durkopd0321952020-10-29 21:37:36 -07004814#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004815 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4816 status = psa_hkdf_input(&operation->ctx.hkdf,
4817 PSA_ALG_HKDF_GET_HASH(kdf_alg),
4818 step, data, data_length);
4819 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004820#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4821#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004822 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4823 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
4824 step, data, data_length);
4825 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004826#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4827#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004828 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4829 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
4830 step, data, data_length);
4831 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004832#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004833 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004834 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004835 (void) data;
4836 (void) data_length;
4837 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004838 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004839 }
4840
Gilles Peskine224b0d62019-09-23 18:13:17 +02004841exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004842 if (status != PSA_SUCCESS) {
4843 psa_key_derivation_abort(operation);
4844 }
4845 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004846}
4847
Janos Follath51f4a0f2019-06-14 11:35:55 +01004848psa_status_t psa_key_derivation_input_bytes(
4849 psa_key_derivation_operation_t *operation,
4850 psa_key_derivation_step_t step,
4851 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004852 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004853{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004854 return psa_key_derivation_input_internal(operation, step,
4855 PSA_KEY_TYPE_NONE,
4856 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004857}
4858
Janos Follath51f4a0f2019-06-14 11:35:55 +01004859psa_status_t psa_key_derivation_input_key(
4860 psa_key_derivation_operation_t *operation,
4861 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004862 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004863{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004864 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004865 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004866 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004867
Ronald Cron5c522922020-11-14 16:35:34 +01004868 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004869 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4870 if (status != PSA_SUCCESS) {
4871 psa_key_derivation_abort(operation);
4872 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02004873 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004874
4875 /* Passing a key object as a SECRET input unlocks the permission
4876 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004877 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004878 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004879 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004881 status = psa_key_derivation_input_internal(operation,
4882 step, slot->attr.type,
4883 slot->key.data,
4884 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004886 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004888 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004889}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004890
4891
4892
4893/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004894/* Key agreement */
4895/****************************************************************/
4896
John Durkop6ba40d12020-11-10 08:50:04 -08004897#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004898static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
4899 size_t peer_key_length,
4900 const mbedtls_ecp_keypair *our_key,
4901 uint8_t *shared_secret,
4902 size_t shared_secret_size,
4903 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004904{
Steven Cooremand4867872020-08-05 16:31:39 +02004905 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004906 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004907 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004908 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004909 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
4910 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004911
Ronald Cron90857082020-11-25 14:58:33 +01004912 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004913 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
4914 bits,
4915 peer_key,
4916 peer_key_length,
4917 &their_key);
4918 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004919 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004920 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004921
Jaeden Amero97271b32019-01-10 19:38:51 +00004922 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004923 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
4924 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004925 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004926 }
Jaeden Amero97271b32019-01-10 19:38:51 +00004927 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004928 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
4929 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004930 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004931 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004932
Jaeden Amero97271b32019-01-10 19:38:51 +00004933 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004934 mbedtls_ecdh_calc_secret(&ecdh,
4935 shared_secret_length,
4936 shared_secret, shared_secret_size,
4937 mbedtls_psa_get_random,
4938 MBEDTLS_PSA_RANDOM_STATE));
4939 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004940 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004941 }
4942 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004943 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004944 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004945
4946exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004947 if (status != PSA_SUCCESS) {
4948 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
4949 }
4950 mbedtls_ecdh_free(&ecdh);
4951 mbedtls_ecp_keypair_free(their_key);
4952 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02004953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004954 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004955}
John Durkop6ba40d12020-11-10 08:50:04 -08004956#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004957
Gilles Peskine01d718c2018-09-18 12:01:02 +02004958#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004960static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
4961 psa_key_slot_t *private_key,
4962 const uint8_t *peer_key,
4963 size_t peer_key_length,
4964 uint8_t *shared_secret,
4965 size_t shared_secret_size,
4966 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004967{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004968 mbedtls_ecp_keypair *ecp = NULL;
4969 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01004970
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004971 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08004972#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004973 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004974 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
4975 return PSA_ERROR_INVALID_ARGUMENT;
4976 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004977 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004978 private_key->attr.type,
4979 private_key->attr.bits,
4980 private_key->key.data,
4981 private_key->key.bytes,
4982 &ecp);
4983 if (status != PSA_SUCCESS) {
4984 return status;
4985 }
4986 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
4987 ecp,
4988 shared_secret, shared_secret_size,
4989 shared_secret_length);
4990 mbedtls_ecp_keypair_free(ecp);
4991 mbedtls_free(ecp);
4992 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08004993#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004994 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004995 (void) ecp;
4996 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01004997 (void) private_key;
4998 (void) peer_key;
4999 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005000 (void) shared_secret;
5001 (void) shared_secret_size;
5002 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005003 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005004 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005005}
5006
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005007/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005008 * to potentially free embedded data structures and wipe confidential data.
5009 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005010static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5011 psa_key_derivation_step_t step,
5012 psa_key_slot_t *private_key,
5013 const uint8_t *peer_key,
5014 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005015{
5016 psa_status_t status;
5017 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5018 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005019 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005020
5021 /* Step 1: run the secret agreement algorithm to generate the shared
5022 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005023 status = psa_key_agreement_raw_internal(ka_alg,
5024 private_key,
5025 peer_key, peer_key_length,
5026 shared_secret,
5027 sizeof(shared_secret),
5028 &shared_secret_length);
5029 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005030 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005031 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005032
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005033 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005034 * the shared secret. A shared secret is permitted wherever a key
5035 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005036 status = psa_key_derivation_input_internal(operation, step,
5037 PSA_KEY_TYPE_DERIVE,
5038 shared_secret,
5039 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005040exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005041 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5042 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005043}
5044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005045psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5046 psa_key_derivation_step_t step,
5047 mbedtls_svc_key_id_t private_key,
5048 const uint8_t *peer_key,
5049 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005050{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005052 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005053 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005055 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5056 return PSA_ERROR_INVALID_ARGUMENT;
5057 }
Ronald Cron5c522922020-11-14 16:35:34 +01005058 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005059 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5060 if (status != PSA_SUCCESS) {
5061 return status;
5062 }
5063 status = psa_key_agreement_internal(operation, step,
5064 slot,
5065 peer_key, peer_key_length);
5066 if (status != PSA_SUCCESS) {
5067 psa_key_derivation_abort(operation);
5068 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005069 /* If a private key has been added as SECRET, we allow the derived
5070 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005071 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005072 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005073 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005074 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005076 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005078 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005079}
5080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005081psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5082 mbedtls_svc_key_id_t private_key,
5083 const uint8_t *peer_key,
5084 size_t peer_key_length,
5085 uint8_t *output,
5086 size_t output_size,
5087 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005088{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005089 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005090 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005091 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005092 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005094 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005095 status = PSA_ERROR_INVALID_ARGUMENT;
5096 goto exit;
5097 }
Ronald Cron5c522922020-11-14 16:35:34 +01005098 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005099 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5100 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005101 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005102 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005103
Gilles Peskine42313fb2022-04-14 00:17:15 +02005104 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5105 * for the output size. The PSA specification only guarantees that this
5106 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5107 * but it might be nice to allow smaller buffers if the output fits.
5108 * At the time of writing this comment, with only ECDH implemented,
5109 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5110 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5111 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005112 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005113 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5114 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005115 status = PSA_ERROR_BUFFER_TOO_SMALL;
5116 goto exit;
5117 }
5118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005119 status = psa_key_agreement_raw_internal(alg, slot,
5120 peer_key, peer_key_length,
5121 output, output_size,
5122 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005123
5124exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005125 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005126 /* If an error happens and is not handled properly, the output
5127 * may be used as a key to protect sensitive data. Arrange for such
5128 * a key to be random, which is likely to result in decryption or
5129 * verification errors. This is better than filling the buffer with
5130 * some constant data such as zeros, which would result in the data
5131 * being protected with a reproducible, easily knowable key.
5132 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005133 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005134 *output_length = output_size;
5135 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005137 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005139 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005140}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005141
5142
Gilles Peskine30524eb2020-11-13 17:02:26 +01005143
Gilles Peskine86a440b2018-11-12 18:39:40 +01005144/****************************************************************/
5145/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005146/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005147
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005148#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5149#include "mbedtls/entropy_poll.h"
5150#endif
5151
Gilles Peskine30524eb2020-11-13 17:02:26 +01005152/** Initialize the PSA random generator.
5153 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005154static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005155{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005156#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005157 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005158#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5159
Gilles Peskine30524eb2020-11-13 17:02:26 +01005160 /* Set default configuration if
5161 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005162 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005163 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005164 }
5165 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005166 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005167 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005169 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005170#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5171 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5172 /* The PSA entropy injection feature depends on using NV seed as an entropy
5173 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005174 mbedtls_entropy_add_source(&rng->entropy,
5175 mbedtls_nv_seed_poll, NULL,
5176 MBEDTLS_ENTROPY_BLOCK_SIZE,
5177 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005178#endif
5179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005180 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005181#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005182}
5183
5184/** Deinitialize the PSA random generator.
5185 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005186static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005187{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005188#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005189 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005190#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005191 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5192 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005193#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005194}
5195
5196/** Seed the PSA random generator.
5197 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005198static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005199{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005200#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5201 /* Do nothing: the external RNG seeds itself. */
5202 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005203 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005204#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005205 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005206 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5207 drbg_seed, sizeof(drbg_seed) - 1);
5208 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005209#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005210}
5211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005212psa_status_t psa_generate_random(uint8_t *output,
5213 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005214{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005215 GUARD_MODULE_INITIALIZED;
5216
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005217#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5218
5219 size_t output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005220 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
5221 output, output_size,
5222 &output_length);
5223 if (status != PSA_SUCCESS) {
5224 return status;
5225 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005226 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00005227 * for the external RNG interface. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005228 if (output_length != output_size) {
5229 return PSA_ERROR_INSUFFICIENT_ENTROPY;
5230 }
5231 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005232
5233#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005235 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005236 size_t request_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005237 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5238 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5239 output_size);
5240 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
5241 output, request_size);
5242 if (ret != 0) {
5243 return mbedtls_to_psa_error(ret);
5244 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01005245 output_size -= request_size;
5246 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005247 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005248 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005249#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005250}
5251
Gilles Peskine8814fc42020-12-14 15:33:44 +01005252/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005253 *
5254 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5255 * `psa_generate_random(...)`. The state parameter is ignored since the
5256 * PSA API doesn't support passing an explicit state.
5257 *
5258 * In the non-external case, psa_generate_random() calls an
5259 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5260 * and semantics as mbedtls_psa_get_random(). As an optimization,
5261 * instead of doing this back-and-forth between the PSA API and the
5262 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5263 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005264 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005265#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5266int mbedtls_psa_get_random(void *p_rng,
5267 unsigned char *output,
5268 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005269{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005270 /* This function takes a pointer to the RNG state because that's what
5271 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5272 * its own state internally and doesn't let the caller access that state.
5273 * So we just ignore the state parameter, and in practice we'll pass
5274 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005275 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005276 psa_status_t status = psa_generate_random(output, output_size);
5277 if (status == PSA_SUCCESS) {
5278 return 0;
5279 } else {
5280 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5281 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005282}
5283#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5284
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005285#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005286psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5287 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005288{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005289 if (global_data.initialized) {
5290 return PSA_ERROR_NOT_PERMITTED;
5291 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005292
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005293 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5294 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5295 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5296 return PSA_ERROR_INVALID_ARGUMENT;
5297 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005299 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005300}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005301#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005302
Ronald Cron3772afe2021-02-08 16:10:05 +01005303/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005304 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005305 * \param type The key type
5306 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005307 *
5308 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005309 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005310 * \retval #PSA_ERROR_INVALID_ARGUMENT
5311 * The size in bits of the key is not valid.
5312 * \retval #PSA_ERROR_NOT_SUPPORTED
5313 * The type and/or the size in bits of the key or the combination of
5314 * the two is not supported.
5315 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005316static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005317 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005318{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005319 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005321 if (key_type_is_raw_bytes(type)) {
5322 status = validate_unstructured_key_bit_size(type, bits);
5323 if (status != PSA_SUCCESS) {
5324 return status;
5325 }
5326 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005327#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005328 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5329 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5330 return PSA_ERROR_NOT_SUPPORTED;
5331 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005332
Ronald Cron01b2aba2020-10-05 09:42:02 +02005333 /* Accept only byte-aligned keys, for the same reasons as
5334 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005335 if (bits % 8 != 0) {
5336 return PSA_ERROR_NOT_SUPPORTED;
5337 }
5338 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005339#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005340
Ronald Cron5c4d3862020-12-07 11:07:24 +01005341#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005342 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005343 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005344 return PSA_SUCCESS;
5345 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005346#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005347 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005348 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005349 }
5350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005351 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005352}
5353
Ronald Cron55ed0592020-10-05 10:30:40 +02005354psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005355 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005356 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005357{
5358 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005359 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005361 if ((attributes->domain_parameters == NULL) &&
5362 (attributes->domain_parameters_size != 0)) {
5363 return PSA_ERROR_INVALID_ARGUMENT;
5364 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005366 if (key_type_is_raw_bytes(type)) {
5367 status = psa_generate_random(key_buffer, key_buffer_size);
5368 if (status != PSA_SUCCESS) {
5369 return status;
5370 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005371
David Brown12ca5032021-02-11 11:02:00 -07005372#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005373 if (type == PSA_KEY_TYPE_DES) {
5374 psa_des_set_key_parity(key_buffer, key_buffer_size);
5375 }
David Brown8107e312021-02-12 08:08:46 -07005376#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005377 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005378
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005379#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5380 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005381 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5382 return mbedtls_psa_rsa_generate_key(attributes,
5383 key_buffer,
5384 key_buffer_size,
5385 key_buffer_length);
5386 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005387#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5388 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005389
John Durkop9814fa22020-11-04 12:28:15 -08005390#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005391 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5392 return mbedtls_psa_ecp_generate_key(attributes,
5393 key_buffer,
5394 key_buffer_size,
5395 key_buffer_length);
5396 } else
John Durkop9814fa22020-11-04 12:28:15 -08005397#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005398 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005399 (void) key_buffer_length;
5400 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005401 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005403 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005404}
Darryl Green0c6575a2018-11-07 16:05:30 +00005405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005406psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5407 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005408{
5409 psa_status_t status;
5410 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005411 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005412 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005413
Ronald Cron81709fc2020-11-14 12:10:32 +01005414 *key = MBEDTLS_SVC_KEY_ID_INIT;
5415
Gilles Peskine0f84d622019-09-12 19:03:13 +02005416 /* Reject any attempt to create a zero-length key so that we don't
5417 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005418 if (psa_get_key_bits(attributes) == 0) {
5419 return PSA_ERROR_INVALID_ARGUMENT;
5420 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005421
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005422 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005423 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5424 return PSA_ERROR_INVALID_ARGUMENT;
5425 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005426
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005427 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5428 &slot, &driver);
5429 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005430 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005431 }
Gilles Peskine11792082019-08-06 18:36:36 +02005432
Ronald Cron977c2472020-10-13 08:32:21 +02005433 /* In the case of a transparent key or an opaque key stored in local
5434 * storage (thus not in the case of generating a key in a secure element
5435 * or cryptoprocessor with storage), we have to allocate a buffer to
5436 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005437 if (slot->key.data == NULL) {
5438 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5439 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005440 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005441 attributes->core.type, attributes->core.bits);
5442 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005443 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005444 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005445
5446 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005447 attributes->core.type,
5448 attributes->core.bits);
5449 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005450 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005451 attributes, &key_buffer_size);
5452 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005453 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005454 }
Ronald Cron977c2472020-10-13 08:32:21 +02005455 }
Ronald Cron977c2472020-10-13 08:32:21 +02005456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005457 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5458 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005459 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005460 }
Ronald Cron977c2472020-10-13 08:32:21 +02005461 }
5462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005463 status = psa_driver_wrapper_generate_key(attributes,
5464 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005466 if (status != PSA_SUCCESS) {
5467 psa_remove_key_data_from_memory(slot);
5468 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005469
Gilles Peskine11792082019-08-06 18:36:36 +02005470exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005471 if (status == PSA_SUCCESS) {
5472 status = psa_finish_key_creation(slot, driver, key);
5473 }
5474 if (status != PSA_SUCCESS) {
5475 psa_fail_key_creation(slot, driver);
5476 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005478 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005479}
5480
Gilles Peskinee59236f2018-01-27 23:32:46 +01005481/****************************************************************/
5482/* Module setup */
5483/****************************************************************/
5484
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005485#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005486psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005487 void (* entropy_init)(mbedtls_entropy_context *ctx),
5488 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005489{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005490 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5491 return PSA_ERROR_BAD_STATE;
5492 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005493 global_data.rng.entropy_init = entropy_init;
5494 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005495 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005496}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005497#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005499void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005500{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005501 psa_wipe_all_key_slots();
5502 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5503 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005504 }
5505 /* Wipe all remaining data, including configuration.
5506 * In particular, this sets all state indicator to the value
5507 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005508 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005509
5510 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005511 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005512}
5513
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005514#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5515/** Recover a transaction that was interrupted by a power failure.
5516 *
5517 * This function is called during initialization, before psa_crypto_init()
5518 * returns. If this function returns a failure status, the initialization
5519 * fails.
5520 */
5521static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005522 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005523{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005524 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005525 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5526 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005527 /* TODO - fall through to the failure case until this
5528 * is implemented.
5529 * https://github.com/ARMmbed/mbed-crypto/issues/218
5530 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005531 default:
5532 /* We found an unsupported transaction in the storage.
5533 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005534 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005535 }
5536}
5537#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005539psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005540{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005541 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005542
Gilles Peskinec6b69072018-11-20 21:42:52 +01005543 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005544 if (global_data.initialized != 0) {
5545 return PSA_SUCCESS;
5546 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005547
Gilles Peskine30524eb2020-11-13 17:02:26 +01005548 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005549 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005550 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005551 status = mbedtls_psa_random_seed(&global_data.rng);
5552 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005553 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005554 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005555 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005557 status = psa_initialize_key_slots();
5558 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005559 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005560 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005561
Ronald Cron088d5d02021-04-10 16:57:30 +02005562 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005563 status = psa_driver_wrapper_init();
5564 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005565 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005566 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005567
Gilles Peskine4b734222019-07-24 15:56:31 +02005568#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005569 status = psa_crypto_load_transaction();
5570 if (status == PSA_SUCCESS) {
5571 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5572 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005573 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005574 }
5575 status = psa_crypto_stop_transaction();
5576 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005577 /* There's no transaction to complete. It's all good. */
5578 status = PSA_SUCCESS;
5579 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005580#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005581
Gilles Peskinec6b69072018-11-20 21:42:52 +01005582 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005583 global_data.initialized = 1;
5584
5585exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005586 if (status != PSA_SUCCESS) {
5587 mbedtls_psa_crypto_free();
5588 }
5589 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005590}
5591
David Horstmann114d8242023-12-07 18:39:17 +00005592/* Memory copying test hooks. These are called before input copy, after input
5593 * copy, before output copy and after output copy, respectively.
5594 * They are used by memory-poisoning tests to temporarily unpoison buffers
5595 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00005596#if defined(MBEDTLS_TEST_HOOKS)
5597void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5598void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5599void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5600void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5601#endif
5602
David Horstmanndf493552023-11-15 15:18:30 +00005603/** Copy from an input buffer to a local copy.
5604 *
5605 * \param[in] input Pointer to input buffer.
5606 * \param[in] input_len Length of the input buffer.
5607 * \param[out] input_copy Pointer to a local copy in which to store the input data.
5608 * \param[out] input_copy_len Length of the local copy buffer.
5609 * \return #PSA_SUCCESS, if the buffer was successfully
5610 * copied.
5611 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
5612 * copy is too small to hold contents of the
5613 * input buffer.
5614 */
5615MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00005616psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
5617 uint8_t *input_copy, size_t input_copy_len)
5618{
5619 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00005620 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00005621 }
5622
David Horstmann0760b152023-11-24 16:21:04 +00005623#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005624 if (psa_input_pre_copy_hook != NULL) {
5625 psa_input_pre_copy_hook(input, input_len);
5626 }
David Horstmann0760b152023-11-24 16:21:04 +00005627#endif
5628
David Horstmann660027f2023-11-15 17:33:47 +00005629 if (input_len > 0) {
5630 memcpy(input_copy, input, input_len);
5631 }
David Horstmann957f9802023-10-30 20:29:43 +00005632
David Horstmann0760b152023-11-24 16:21:04 +00005633#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005634 if (psa_input_post_copy_hook != NULL) {
5635 psa_input_post_copy_hook(input, input_len);
5636 }
David Horstmann0760b152023-11-24 16:21:04 +00005637#endif
5638
David Horstmann957f9802023-10-30 20:29:43 +00005639 return PSA_SUCCESS;
5640}
5641
David Horstmanndf493552023-11-15 15:18:30 +00005642/** Copy from a local output buffer into a user-supplied one.
5643 *
5644 * \param[in] output_copy Pointer to a local buffer containing the output.
5645 * \param[in] output_copy_len Length of the local buffer.
5646 * \param[out] output Pointer to user-supplied output buffer.
5647 * \param[out] output_len Length of the user-supplied output buffer.
5648 * \return #PSA_SUCCESS, if the buffer was successfully
5649 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00005650 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00005651 * user-supplied output buffer is too small to
5652 * hold the contents of the local buffer.
5653 */
5654MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00005655psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
5656 uint8_t *output, size_t output_len)
5657{
5658 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00005659 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00005660 }
David Horstmann660027f2023-11-15 17:33:47 +00005661
David Horstmann0760b152023-11-24 16:21:04 +00005662#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005663 if (psa_output_pre_copy_hook != NULL) {
5664 psa_output_pre_copy_hook(output, output_len);
5665 }
David Horstmann0760b152023-11-24 16:21:04 +00005666#endif
5667
David Horstmann660027f2023-11-15 17:33:47 +00005668 if (output_copy_len > 0) {
5669 memcpy(output, output_copy, output_copy_len);
5670 }
5671
David Horstmann0760b152023-11-24 16:21:04 +00005672#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005673 if (psa_output_post_copy_hook != NULL) {
5674 psa_output_post_copy_hook(output, output_len);
5675 }
David Horstmann0760b152023-11-24 16:21:04 +00005676#endif
5677
David Horstmann2bd296e2023-10-30 20:37:12 +00005678 return PSA_SUCCESS;
5679}
5680
David Horstmann81a0d572023-11-20 17:15:32 +00005681psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
5682 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005683{
5684 psa_status_t status;
5685
David Horstmann0d52c712023-11-23 15:50:37 +00005686 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005687
David Horstmann726bf052023-11-15 18:11:26 +00005688 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005689 return PSA_SUCCESS;
5690 }
5691
David Horstmann81a0d572023-11-20 17:15:32 +00005692 local_input->buffer = mbedtls_calloc(input_len, 1);
5693 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005694 /* Since we dealt with the zero-length case above, we know that
5695 * a NULL return value means a failure of allocation. */
5696 return PSA_ERROR_INSUFFICIENT_MEMORY;
5697 }
David Horstmann81a0d572023-11-20 17:15:32 +00005698 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005699
David Horstmann81a0d572023-11-20 17:15:32 +00005700 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005701
5702 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00005703 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005704 if (status != PSA_SUCCESS) {
5705 goto error;
5706 }
5707
5708 return PSA_SUCCESS;
5709
5710error:
David Horstmann81a0d572023-11-20 17:15:32 +00005711 mbedtls_free(local_input->buffer);
5712 local_input->buffer = NULL;
5713 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005714 return status;
5715}
5716
David Horstmann81a0d572023-11-20 17:15:32 +00005717void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00005718{
David Horstmann81a0d572023-11-20 17:15:32 +00005719 mbedtls_free(local_input->buffer);
5720 local_input->buffer = NULL;
5721 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00005722}
5723
David Horstmann1a76ab12023-11-20 12:54:09 +00005724psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
5725 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00005726{
David Horstmann0d52c712023-11-23 15:50:37 +00005727 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00005728
David Horstmann726bf052023-11-15 18:11:26 +00005729 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005730 return PSA_SUCCESS;
5731 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005732 local_output->buffer = mbedtls_calloc(output_len, 1);
5733 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005734 /* Since we dealt with the zero-length case above, we know that
5735 * a NULL return value means a failure of allocation. */
5736 return PSA_ERROR_INSUFFICIENT_MEMORY;
5737 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005738 local_output->length = output_len;
5739 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00005740
5741 return PSA_SUCCESS;
5742}
5743
David Horstmann1a76ab12023-11-20 12:54:09 +00005744psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00005745{
David Horstmann761761f2023-11-15 15:57:32 +00005746 psa_status_t status;
5747
David Horstmann1a76ab12023-11-20 12:54:09 +00005748 if (local_output->buffer == NULL) {
5749 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005750 return PSA_SUCCESS;
5751 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005752 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00005753 /* We have an internal copy but nothing to copy back to. */
5754 return PSA_ERROR_CORRUPTION_DETECTED;
5755 }
5756
David Horstmann1a76ab12023-11-20 12:54:09 +00005757 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
5758 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00005759 if (status != PSA_SUCCESS) {
5760 return status;
5761 }
David Horstmannaeeb2742023-11-08 17:44:18 +00005762
David Horstmann1a76ab12023-11-20 12:54:09 +00005763 mbedtls_free(local_output->buffer);
5764 local_output->buffer = NULL;
5765 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005766
5767 return PSA_SUCCESS;
5768}
5769
Gilles Peskinee59236f2018-01-27 23:32:46 +01005770#endif /* MBEDTLS_PSA_CRYPTO_C */