blob: d0dd61b32a6bac53eb5a6451bd0e2a88897bfd14 [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 Horstmannf96ae672023-12-13 14:03:40 +0000164 psa_status_t output##_local_output_status; \
165 output##_local_output_status = psa_crypto_local_output_free(&output##_copy); \
166 if (output##_local_output_status != PSA_SUCCESS) { \
David Horstmann1f532132023-12-08 14:08:18 +0000167 /* Since this error case is an internal error, it's more serious than \
168 * any existing error code and so it's fine to overwrite the existing \
169 * status. */ \
David Horstmannf96ae672023-12-13 14:03:40 +0000170 status = output##_local_output_status; \
David Horstmann34980bd2023-11-29 17:07:13 +0000171 }
David Horstmannc0a2c302023-11-29 17:24:08 +0000172#else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
173#define SWAP_FOR_LOCAL_INPUT(input, length)
174#define FREE_LOCAL_INPUT(input)
175#define SWAP_FOR_LOCAL_OUTPUT(output, length)
176#define FREE_LOCAL_OUTPUT(output)
177#endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmann34980bd2023-11-29 17:07:13 +0000178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100180{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100181 /* Mbed TLS error codes can combine a high-level error code and a
182 * low-level error code. The low-level error usually reflects the
183 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 int low_level_ret = -(-ret & 0x007f);
185 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100186 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100188
189 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
190 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
191 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200196 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200198
Gilles Peskine9a944802018-06-21 09:35:35 +0200199 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
200 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
201 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
202 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
203 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200205 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200207 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200209
Jaeden Amero93e21112019-02-20 13:57:28 +0000210#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000211 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000212#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
213 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
214#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100217 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100219
Jaeden Amero93e21112019-02-20 13:57:28 +0000220#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000221 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000222#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
223 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
224#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
230 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100232 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100236
Gilles Peskine26869f22019-05-06 15:25:00 +0200237 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200239
240 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200242 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200244
Gilles Peskinea5905292018-02-07 20:59:33 +0100245 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100255 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100257 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100259 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100261
262 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
266 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100267 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
268 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100271 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
272 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100274 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100276#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100277
278 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100282
Gilles Peskinee59236f2018-01-27 23:32:46 +0100283 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
284 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
285 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100287
288 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100290 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294
Gilles Peskine82e57d12020-11-13 21:31:17 +0100295#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100297 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
298 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100299 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100301 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
302 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100304 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100306#endif
307
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200308 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
309 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
310 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200312
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200321 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100323
Gilles Peskinef76aa772018-10-29 19:24:33 +0100324 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100326 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100328 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
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_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100332 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100334 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
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_NOT_ACCEPTABLE:
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_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100340
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100341 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100343 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
344 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100346 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100348 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
349 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100351 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100353 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
354 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100358 case MBEDTLS_ERR_PK_INVALID_ALG:
359 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
360 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100364 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100366
Gilles Peskineff2d2002019-05-06 15:26:23 +0200367 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200369 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200371
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200372 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200374
Gilles Peskinea5905292018-02-07 20:59:33 +0100375 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100377 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100379 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100381 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100383 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
384 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100386 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100388 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100390 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100392 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100394 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100396
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200397 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
398 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
399 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200401
Gilles Peskinea5905292018-02-07 20:59:33 +0100402 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100404 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100406
itayzafrir5c753392018-05-08 11:18:38 +0300407 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300408 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300410 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300412 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300414 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
415 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300417 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100419 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300421 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100423
Janos Follatha13b9052019-11-22 12:48:59 +0000424 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300426
Gilles Peskinee59236f2018-01-27 23:32:46 +0100427 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100429 }
430}
431
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200432
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200433
434
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100435/****************************************************************/
436/* Key management */
437/****************************************************************/
438
John Durkop9814fa22020-11-04 12:28:15 -0800439#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800440 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100441 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
442 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
443 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
445 size_t bits,
446 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200447{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100449 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100451#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100452 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100454#endif
455#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100456 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100458#endif
459#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100460 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100462#endif
463#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100464 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100465 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100466#endif
467#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100468 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100470 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 if (bits_is_sloppy) {
472 return MBEDTLS_ECP_DP_SECP521R1;
473 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100474 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100475#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100476 }
477 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100478
Paul Elliott8ff510a2020-06-02 17:19:28 +0100479 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100481#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100482 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100483 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100484#endif
485#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100486 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100488#endif
489#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100490 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100492#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100493 }
494 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100495
Paul Elliott8ff510a2020-06-02 17:19:28 +0100496 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100497 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100498#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100499 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100500 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100501 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502 if (bits_is_sloppy) {
503 return MBEDTLS_ECP_DP_CURVE25519;
504 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100505 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100506#endif
507#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100508 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100510#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100511 }
512 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100513
Paul Elliott8ff510a2020-06-02 17:19:28 +0100514 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100515 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100516#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100517 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100518 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100519#endif
520#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100521 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100522 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100523#endif
524#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100525 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100526 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100527#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100528 }
529 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200530 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100531
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100532 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200534}
John Durkop9814fa22020-11-04 12:28:15 -0800535#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100536 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
537 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
538 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
539 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
542 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200543{
544 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200546 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200547 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200548 case PSA_KEY_TYPE_DERIVE:
549 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100550#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200551 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100552 if (bits != 128 && bits != 192 && bits != 256) {
553 return PSA_ERROR_INVALID_ARGUMENT;
554 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200555 break;
556#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200557#if defined(PSA_WANT_KEY_TYPE_ARIA)
558 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100559 if (bits != 128 && bits != 192 && bits != 256) {
560 return PSA_ERROR_INVALID_ARGUMENT;
561 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200562 break;
563#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100564#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200565 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100566 if (bits != 128 && bits != 192 && bits != 256) {
567 return PSA_ERROR_INVALID_ARGUMENT;
568 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200569 break;
570#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100571#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200572 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100573 if (bits != 64 && bits != 128 && bits != 192) {
574 return PSA_ERROR_INVALID_ARGUMENT;
575 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200576 break;
577#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100578#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200579 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100580 if (bits < 8 || bits > 2048) {
581 return PSA_ERROR_INVALID_ARGUMENT;
582 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200583 break;
584#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100585#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200586 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100587 if (bits != 256) {
588 return PSA_ERROR_INVALID_ARGUMENT;
589 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200590 break;
591#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200592 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200594 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100595 if (bits % 8 != 0) {
596 return PSA_ERROR_INVALID_ARGUMENT;
597 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100599 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200600}
601
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100602/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100603 *
Steven Cooremancd640932021-03-08 12:00:27 +0100604 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
605 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100606 *
607 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100608 * \param[in] key_type The key type of the key to be used with the
609 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100610 *
611 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100612 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100613 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100614 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100615 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100616MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100617 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100618 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100619{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100620 if (PSA_ALG_IS_HMAC(algorithm)) {
621 if (key_type == PSA_KEY_TYPE_HMAC) {
622 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100623 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100624 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100626 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
627 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
628 * key. */
629 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
630 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
631 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
632 * the block length (larger than 1) for block ciphers. */
633 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
634 return PSA_SUCCESS;
635 }
636 }
637 }
638
639 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100640}
641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100642psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
643 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200644{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100645 if (slot->key.data != NULL) {
646 return PSA_ERROR_ALREADY_EXISTS;
647 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100649 slot->key.data = mbedtls_calloc(1, buffer_length);
650 if (slot->key.data == NULL) {
651 return PSA_ERROR_INSUFFICIENT_MEMORY;
652 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200653
Ronald Cronea0f8a62020-11-25 17:52:23 +0100654 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200656}
657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100658psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
659 const uint8_t *data,
660 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200661{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100662 psa_status_t status = psa_allocate_buffer_to_slot(slot,
663 data_length);
664 if (status != PSA_SUCCESS) {
665 return status;
666 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100668 memcpy(slot->key.data, data, data_length);
669 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200670}
671
Ronald Crona0fe59f2020-11-29 11:14:53 +0100672psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100673 const psa_key_attributes_t *attributes,
674 const uint8_t *data, size_t data_length,
675 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100676 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100678 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
679 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200681 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100682 if (data_length == 0) {
683 return PSA_ERROR_NOT_SUPPORTED;
684 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200685
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100686 if (key_type_is_raw_bytes(type)) {
687 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200688
Steven Cooreman75b74362020-07-28 14:30:13 +0200689 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100690 if (data_length > SIZE_MAX / 8) {
691 return PSA_ERROR_NOT_SUPPORTED;
692 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200693
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200694 /* Enforce a size limit, and in particular ensure that the bit
695 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100696 if ((*bits) > PSA_MAX_KEY_BITS) {
697 return PSA_ERROR_NOT_SUPPORTED;
698 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200699
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100700 status = validate_unstructured_key_bit_size(type, *bits);
701 if (status != PSA_SUCCESS) {
702 return status;
703 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200704
Ronald Crondd04d422020-11-28 15:14:42 +0100705 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100706 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100707 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100708 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100710 return PSA_SUCCESS;
711 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800712#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100713 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
714 if (PSA_KEY_TYPE_IS_ECC(type)) {
715 return mbedtls_psa_ecp_import_key(attributes,
716 data, data_length,
717 key_buffer, key_buffer_size,
718 key_buffer_length,
719 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100720 }
John Durkop9814fa22020-11-04 12:28:15 -0800721#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
722 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700723#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100724 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
725 if (PSA_KEY_TYPE_IS_RSA(type)) {
726 return mbedtls_psa_rsa_import_key(attributes,
727 data, data_length,
728 key_buffer, key_buffer_size,
729 key_buffer_length,
730 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200731 }
John Durkop9814fa22020-11-04 12:28:15 -0800732#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
733 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100734 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100735
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100736 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100737}
738
Gilles Peskinef603c712019-01-19 13:40:11 +0100739/** Calculate the intersection of two algorithm usage policies.
740 *
741 * Return 0 (which allows no operation) on incompatibility.
742 */
743static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100744 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100745 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100746 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100747{
Gilles Peskine549ea862019-05-22 11:45:59 +0200748 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100749 if (alg1 == alg2) {
750 return alg1;
751 }
Steven Cooreman03488022021-02-18 12:07:20 +0100752 /* If the policies are from the same hash-and-sign family, check
753 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100754 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
755 PSA_ALG_IS_SIGN_HASH(alg2) &&
756 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
757 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
758 return alg2;
759 }
760 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
761 return alg1;
762 }
Steven Cooreman03488022021-02-18 12:07:20 +0100763 }
764 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100765 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100766 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100767 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
768 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
769 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
770 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
771 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100772 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100773
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100774 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100775 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
776 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
777 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
778 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100779 }
780 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100781 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
782 (alg1_len <= alg2_len)) {
783 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100784 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100785 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
786 (alg2_len <= alg1_len)) {
787 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100788 }
789 }
790 /* If the policies are from the same MAC family, check whether one
791 * of them is a minimum-MAC-length policy. Calculate the most
792 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100793 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
794 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
795 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100796 /* Validate the combination of key type and algorithm. Since the base
797 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
799 return 0;
800 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100801
Steven Cooreman31a876d2021-03-03 20:47:40 +0100802 /* Get the (exact or at-least) output lengths for both sides of the
803 * requested intersection. None of the currently supported algorithms
804 * have an output length dependent on the actual key size, so setting it
805 * to a bogus value of 0 is currently OK.
806 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100807 * Note that for at-least-this-length wildcard algorithms, the output
808 * length is set to the shortest allowed length, which allows us to
809 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100810 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
811 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100812 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100813
Steven Cooremana1d83222021-02-25 10:20:29 +0100814 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100815 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
816 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
817 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100818 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100819
820 /* If only one is an at-least-this-length policy, the intersection would
821 * be the other (fixed-length) policy as long as said fixed length is
822 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100823 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
824 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100825 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100826 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
827 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100828 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100829
Steven Cooremancd640932021-03-08 12:00:27 +0100830 /* If none of them are wildcards, check whether they define the same tag
831 * length. This is still possible here when one is default-length and
832 * the other specific-length. Ensure to always return the
833 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100834 if (alg1_len == alg2_len) {
835 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
836 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100837 }
838 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100840}
841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100842static int psa_key_algorithm_permits(psa_key_type_t key_type,
843 psa_algorithm_t policy_alg,
844 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200845{
Gilles Peskine549ea862019-05-22 11:45:59 +0200846 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100847 if (requested_alg == policy_alg) {
848 return 1;
849 }
Steven Cooreman03488022021-02-18 12:07:20 +0100850 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
851 * and requested_alg is the same hash-and-sign family with any hash,
852 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100853 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
854 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
855 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
856 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100857 }
858 /* If policy_alg is a wildcard AEAD algorithm of the same base as
859 * the requested algorithm, check the requested tag length to be
860 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100861 if (PSA_ALG_IS_AEAD(policy_alg) &&
862 PSA_ALG_IS_AEAD(requested_alg) &&
863 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
864 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
865 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
866 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
867 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100868 }
Steven Cooremancd640932021-03-08 12:00:27 +0100869 /* If policy_alg is a MAC algorithm of the same base as the requested
870 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100871 if (PSA_ALG_IS_MAC(policy_alg) &&
872 PSA_ALG_IS_MAC(requested_alg) &&
873 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
874 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100875 /* Validate the combination of key type and algorithm. Since the policy
876 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100877 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
878 return 0;
879 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100880
Steven Cooreman31a876d2021-03-03 20:47:40 +0100881 /* Get both the requested output length for the algorithm which is to be
882 * verified, and the default output length for the base algorithm.
883 * Note that none of the currently supported algorithms have an output
884 * length dependent on actual key size, so setting it to a bogus value
885 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100886 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100887 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100888 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100889 key_type, 0,
890 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100891
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100892 /* If the policy is default-length, only allow an algorithm with
893 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
895 return requested_output_length == default_output_length;
896 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100897
898 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100899 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100900 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
901 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
902 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100903 }
904
Steven Cooremancd640932021-03-08 12:00:27 +0100905 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
906 * check for the requested MAC length to be equal to or longer than the
907 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100908 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
909 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
910 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100911 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200912 }
Steven Cooremance48e852020-10-05 16:02:45 +0200913 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200914 * a key derivation with that key agreement should also be allowed. This
915 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100916 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
917 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
918 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
919 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200920 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100921 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100922 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200923}
924
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100925/** Test whether a policy permits an algorithm.
926 *
927 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100928 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100929 * \note This function requires providing the key type for which the policy is
930 * being validated, since some algorithm policy definitions (e.g. MAC)
931 * have different properties depending on what kind of cipher it is
932 * combined with.
933 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100934 * \retval PSA_SUCCESS When \p alg is a specific algorithm
935 * allowed by the \p policy.
936 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
937 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
938 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100939 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100940static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
941 psa_key_type_t key_type,
942 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100943{
Steven Cooremand788fab2021-02-25 11:29:17 +0100944 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100945 if (alg == 0) {
946 return PSA_ERROR_INVALID_ARGUMENT;
947 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100948
Steven Cooreman7e39f052021-02-23 14:18:32 +0100949 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100950 if (PSA_ALG_IS_WILDCARD(alg)) {
951 return PSA_ERROR_INVALID_ARGUMENT;
952 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100954 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
955 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
956 return PSA_SUCCESS;
957 } else {
958 return PSA_ERROR_NOT_PERMITTED;
959 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100960}
961
Gilles Peskinef603c712019-01-19 13:40:11 +0100962/** Restrict a key policy based on a constraint.
963 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100964 * \note This function requires providing the key type for which the policy is
965 * being restricted, since some algorithm policy definitions (e.g. MAC)
966 * have different properties depending on what kind of cipher it is
967 * combined with.
968 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100969 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100970 * \param[in,out] policy The policy to restrict.
971 * \param[in] constraint The policy constraint to apply.
972 *
973 * \retval #PSA_SUCCESS
974 * \c *policy contains the intersection of the original value of
975 * \c *policy and \c *constraint.
976 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100977 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100978 * \c *policy is unchanged.
979 */
980static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100981 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100982 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100983 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100984{
985 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100986 psa_key_policy_algorithm_intersection(key_type, policy->alg,
987 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200988 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100989 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
990 constraint->alg2);
991 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
992 return PSA_ERROR_INVALID_ARGUMENT;
993 }
994 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
995 return PSA_ERROR_INVALID_ARGUMENT;
996 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100997 policy->usage &= constraint->usage;
998 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200999 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001000 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001001}
1002
Ronald Cron5c522922020-11-14 16:35:34 +01001003/** Get the description of a key given its identifier and policy constraints
1004 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001005 *
Ronald Cron5c522922020-11-14 16:35:34 +01001006 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +01001007 * nonzero, the key must allow operations with this algorithm. If \p alg is
1008 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +01001009 *
Ronald Cron5c522922020-11-14 16:35:34 +01001010 * In case of a persistent key, the function loads the description of the key
1011 * into a key slot if not already done.
1012 *
1013 * On success, the returned key slot is locked. It is the responsibility of
1014 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001015 */
Ronald Cron5c522922020-11-14 16:35:34 +01001016static psa_status_t psa_get_and_lock_key_slot_with_policy(
1017 mbedtls_svc_key_id_t key,
1018 psa_key_slot_t **p_slot,
1019 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001020 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001021{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001022 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1023 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001024
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001025 status = psa_get_and_lock_key_slot(key, p_slot);
1026 if (status != PSA_SUCCESS) {
1027 return status;
1028 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001029 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001030
1031 /* Enforce that usage policy for the key slot contains all the flags
1032 * required by the usage parameter. There is one exception: public
1033 * keys can always be exported, so we treat public key objects as
1034 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001035 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001036 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001037 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001039 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001040 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001041 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001042 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001043
Shaun Case0e7791f2021-12-20 21:14:10 -08001044 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001045 if (alg != 0) {
1046 status = psa_key_policy_permits(&slot->attr.policy,
1047 slot->attr.type,
1048 alg);
1049 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001050 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001051 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001052 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001054 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001055
1056error:
1057 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001058 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001060 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001061}
Darryl Green940d72c2018-07-13 13:18:51 +01001062
Ronald Cron5c522922020-11-14 16:35:34 +01001063/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001064 *
1065 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001066 * available, as opposed to a key in a secure element and/or to be used
1067 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001068 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001069 * This is a temporary function that may be used instead of
1070 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1071 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001072 *
Ronald Cron5c522922020-11-14 16:35:34 +01001073 * On success, the returned key slot is locked. It is the responsibility of the
1074 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001075 */
Ronald Cron5c522922020-11-14 16:35:34 +01001076static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1077 mbedtls_svc_key_id_t key,
1078 psa_key_slot_t **p_slot,
1079 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001080 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001081{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001082 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1083 usage, alg);
1084 if (status != PSA_SUCCESS) {
1085 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001086 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001088 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1089 psa_unlock_key_slot(*p_slot);
1090 *p_slot = NULL;
1091 return PSA_ERROR_NOT_SUPPORTED;
1092 }
1093
1094 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001095}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001097psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001098{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001099 /* Data pointer will always be either a valid pointer or NULL in an
1100 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001101 if (slot->key.data != NULL) {
1102 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1103 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001105 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001106 slot->key.data = NULL;
1107 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001109 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001110}
1111
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001112/** Completely wipe a slot in memory, including its policy.
1113 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001114psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001115{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001116 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001117
1118 /*
1119 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001120 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001121 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1122 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001123 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001124 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001125 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001126#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001127 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001128#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001129 status = PSA_ERROR_CORRUPTION_DETECTED;
1130 }
1131
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001132 /* Multipart operations may still be using the key. This is safe
1133 * because all multipart operation objects are independent from
1134 * the key slot: if they need to access the key after the setup
1135 * phase, they have a copy of the key. Note that this means that
1136 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001137 /* At this point, key material and other type-specific content has
1138 * been wiped. Clear remaining metadata. We can call memset and not
1139 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001140 memset(slot, 0, sizeof(*slot));
1141 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001142}
1143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001144psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001145{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001146 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001147 psa_status_t status; /* status of the last operation */
1148 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001149#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1150 psa_se_drv_table_entry_t *driver;
1151#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001153 if (mbedtls_svc_key_id_is_null(key)) {
1154 return PSA_SUCCESS;
1155 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001156
Ronald Cronf2911112020-10-29 17:51:10 +01001157 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001158 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001159 * key, this will load the key description from persistent memory if not
1160 * done yet. We cannot avoid this loading as without it we don't know if
1161 * the key is operated by an SE or not and this information is needed by
1162 * the current implementation.
1163 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 status = psa_get_and_lock_key_slot(key, &slot);
1165 if (status != PSA_SUCCESS) {
1166 return status;
1167 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001168
Ronald Cronf2911112020-10-29 17:51:10 +01001169 /*
1170 * If the key slot containing the key description is under access by the
1171 * library (apart from the present access), the key cannot be destroyed
1172 * yet. For the time being, just return in error. Eventually (to be
1173 * implemented), the key should be destroyed when all accesses have
1174 * stopped.
1175 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001176 if (slot->lock_count > 1) {
1177 psa_unlock_key_slot(slot);
1178 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001179 }
1180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001181 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001182 /* Refuse the destruction of a read-only key (which may or may not work
1183 * if we attempt it, depending on whether the key is merely read-only
1184 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001185 * Just do the best we can, which is to wipe the copy in memory
1186 * (done in this function's cleanup code). */
1187 overall_status = PSA_ERROR_NOT_PERMITTED;
1188 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001189 }
1190
Gilles Peskine354f7672019-07-12 23:46:38 +02001191#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001192 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1193 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001194 /* For a key in a secure element, we need to do three things:
1195 * remove the key file in internal storage, destroy the
1196 * key inside the secure element, and update the driver's
1197 * persistent data. Start a transaction that will encompass these
1198 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001199 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001200 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001201 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001202 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001203 status = psa_crypto_save_transaction();
1204 if (status != PSA_SUCCESS) {
1205 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001206 /* We should still try to destroy the key in the secure
1207 * element and the key metadata in storage. This is especially
1208 * important if the error is that the storage is full.
1209 * But how to do it exactly without risking an inconsistent
1210 * state after a reset?
1211 * https://github.com/ARMmbed/mbed-crypto/issues/215
1212 */
1213 overall_status = status;
1214 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001215 }
1216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001217 status = psa_destroy_se_key(driver,
1218 psa_key_slot_get_slot_number(slot));
1219 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001220 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001221 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001222 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001223#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1224
Darryl Greend49a4992018-06-18 17:27:26 +01001225#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001226 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1227 status = psa_destroy_persistent_key(slot->attr.id);
1228 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001229 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001230 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001231
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001232 /* TODO: other slots may have a copy of the same key. We should
1233 * invalidate them.
1234 * https://github.com/ARMmbed/mbed-crypto/issues/214
1235 */
Darryl Greend49a4992018-06-18 17:27:26 +01001236 }
1237#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001238
Gilles Peskinefc762652019-07-22 19:30:34 +02001239#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001240 if (driver != NULL) {
1241 status = psa_save_se_persistent_data(driver);
1242 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001243 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001244 }
1245 status = psa_crypto_stop_transaction();
1246 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001247 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001248 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001249 }
1250#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1251
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001252exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001253 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001254 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001255 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001256 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001257 }
1258 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001259}
1260
John Durkop07cc04a2020-11-16 22:08:34 -08001261#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001262 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001263static psa_status_t psa_get_rsa_public_exponent(
1264 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001265 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001266{
1267 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001269 uint8_t *buffer = NULL;
1270 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001271 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001273 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1274 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001275 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276 }
1277 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001278 /* It's the default value, which is reported as an empty string,
1279 * so there's nothing to do. */
1280 goto exit;
1281 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001283 buflen = mbedtls_mpi_size(&mpi);
1284 buffer = mbedtls_calloc(1, buflen);
1285 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001286 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1287 goto exit;
1288 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001289 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1290 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001291 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001292 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001293 attributes->domain_parameters = buffer;
1294 attributes->domain_parameters_size = buflen;
1295
1296exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001297 mbedtls_mpi_free(&mpi);
1298 if (ret != 0) {
1299 mbedtls_free(buffer);
1300 }
1301 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001302}
John Durkop07cc04a2020-11-16 22:08:34 -08001303#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001304 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001305
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001306/** Retrieve all the publicly-accessible attributes of a key.
1307 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001308psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1309 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001310{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001311 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001312 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001313 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001315 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001317 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1318 if (status != PSA_SUCCESS) {
1319 return status;
1320 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001321
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001322 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001323 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1324 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001325
1326#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001327 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1328 psa_set_key_slot_number(attributes,
1329 psa_key_slot_get_slot_number(slot));
1330 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001331#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001333 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001334#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001335 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001336 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001337 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001338 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001339 * is not yet implemented.
1340 * https://github.com/ARMmbed/mbed-crypto/issues/216
1341 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001342 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001343 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001344
Ronald Cron90857082020-11-25 14:58:33 +01001345 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001346 slot->attr.type,
1347 slot->key.data,
1348 slot->key.bytes,
1349 &rsa);
1350 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001351 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001352 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001354 status = psa_get_rsa_public_exponent(rsa,
1355 attributes);
1356 mbedtls_rsa_free(rsa);
1357 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001358 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001359 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001360#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001361 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001362 default:
1363 /* Nothing else to do. */
1364 break;
1365 }
1366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001367 if (status != PSA_SUCCESS) {
1368 psa_reset_key_attributes(attributes);
1369 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001371 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001373 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001374}
1375
Gilles Peskinec8000c02019-08-02 20:15:51 +02001376#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1377psa_status_t psa_get_key_slot_number(
1378 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001379 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001380{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001381 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001382 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001383 return PSA_SUCCESS;
1384 } else {
1385 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001386 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001387}
1388#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001390static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1391 size_t key_buffer_size,
1392 uint8_t *data,
1393 size_t data_size,
1394 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001395{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001396 if (key_buffer_size > data_size) {
1397 return PSA_ERROR_BUFFER_TOO_SMALL;
1398 }
1399 memcpy(data, key_buffer, key_buffer_size);
1400 memset(data + key_buffer_size, 0,
1401 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001402 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001403 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001404}
1405
Ronald Cron7285cda2020-11-26 14:46:37 +01001406psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001407 const psa_key_attributes_t *attributes,
1408 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001409 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001410{
Ronald Crond18b5f82020-11-25 16:46:05 +01001411 psa_key_type_t type = attributes->core.type;
1412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001413 if (key_type_is_raw_bytes(type) ||
1414 PSA_KEY_TYPE_IS_RSA(type) ||
1415 PSA_KEY_TYPE_IS_ECC(type)) {
1416 return psa_export_key_buffer_internal(
1417 key_buffer, key_buffer_size,
1418 data, data_size, data_length);
1419 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001420 /* This shouldn't happen in the reference implementation, but
1421 it is valid for a special-purpose implementation to omit
1422 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001423 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001424 }
1425}
1426
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001427psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1428 uint8_t *data,
1429 size_t data_size,
1430 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001431{
1432 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1433 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1434 psa_key_slot_t *slot;
1435
Ronald Crone907e552021-01-18 13:22:38 +01001436 /* Reject a zero-length output buffer now, since this can never be a
1437 * valid key representation. This way we know that data must be a valid
1438 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001439 if (data_size == 0) {
1440 return PSA_ERROR_BUFFER_TOO_SMALL;
1441 }
Ronald Crone907e552021-01-18 13:22:38 +01001442
Ronald Cron9486f9d2020-11-26 13:36:23 +01001443 /* Set the key to empty now, so that even when there are errors, we always
1444 * set data_length to a value between 0 and data_size. On error, setting
1445 * the key to empty is a good choice because an empty key representation is
1446 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001447 *data_length = 0;
1448
Ronald Cron9486f9d2020-11-26 13:36:23 +01001449 /* Export requires the EXPORT flag. There is an exception for public keys,
1450 * which don't require any flag, but
1451 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1452 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001453 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1454 PSA_KEY_USAGE_EXPORT, 0);
1455 if (status != PSA_SUCCESS) {
1456 return status;
1457 }
mohammad160306e79202018-03-28 13:17:44 +03001458
Ronald Crond18b5f82020-11-25 16:46:05 +01001459 psa_key_attributes_t attributes = {
1460 .core = slot->attr
1461 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001462 status = psa_driver_wrapper_export_key(&attributes,
1463 slot->key.data, slot->key.bytes,
1464 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001466 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001468 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001469}
1470
Ronald Cron7285cda2020-11-26 14:46:37 +01001471psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001472 const psa_key_attributes_t *attributes,
1473 const uint8_t *key_buffer,
1474 size_t key_buffer_size,
1475 uint8_t *data,
1476 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001477 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001478{
Ronald Crond18b5f82020-11-25 16:46:05 +01001479 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001481 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1482 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001483 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001484 return psa_export_key_buffer_internal(
1485 key_buffer, key_buffer_size,
1486 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001487 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001489 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001490#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001491 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1492 return mbedtls_psa_rsa_export_public_key(attributes,
1493 key_buffer,
1494 key_buffer_size,
1495 data,
1496 data_size,
1497 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001498#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001499 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001500 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001501#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1502 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001503 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001504#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001505 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1506 return mbedtls_psa_ecp_export_public_key(attributes,
1507 key_buffer,
1508 key_buffer_size,
1509 data,
1510 data_size,
1511 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001512#else
1513 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001514 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001515#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1516 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001517 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001518 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001519 /* This shouldn't happen in the reference implementation, but
1520 it is valid for a special-purpose implementation to omit
1521 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001523 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001524}
Ronald Crond18b5f82020-11-25 16:46:05 +01001525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001526psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1527 uint8_t *data,
1528 size_t data_size,
1529 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001530{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001531 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001532 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001533 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001534 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001535
Ronald Crone907e552021-01-18 13:22:38 +01001536 /* Reject a zero-length output buffer now, since this can never be a
1537 * valid key representation. This way we know that data must be a valid
1538 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001539 if (data_size == 0) {
1540 return PSA_ERROR_BUFFER_TOO_SMALL;
1541 }
Ronald Crone907e552021-01-18 13:22:38 +01001542
Darryl Greendd8fb772018-11-07 16:00:44 +00001543 /* Set the key to empty now, so that even when there are errors, we always
1544 * set data_length to a value between 0 and data_size. On error, setting
1545 * the key to empty is a good choice because an empty key representation is
1546 * unlikely to be accepted anywhere. */
1547 *data_length = 0;
1548
1549 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001550 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1551 if (status != PSA_SUCCESS) {
1552 return status;
1553 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001555 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1556 status = PSA_ERROR_INVALID_ARGUMENT;
1557 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001558 }
1559
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001560 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001561 .core = slot->attr
1562 };
Ronald Cron67227982020-11-26 15:16:05 +01001563 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001564 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001565 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001566
1567exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001568 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001570 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001571}
1572
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001573MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1574 "One or more key attribute flag is listed as both external-only and dual-use")
1575MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1576 "One or more key attribute flag is listed as both internal-only and dual-use")
1577MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1578 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001579
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001580/** Validate that a key policy is internally well-formed.
1581 *
1582 * This function only rejects invalid policies. It does not validate the
1583 * consistency of the policy with respect to other attributes of the key
1584 * such as the key type.
1585 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001586static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001587{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001588 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1589 PSA_KEY_USAGE_COPY |
1590 PSA_KEY_USAGE_ENCRYPT |
1591 PSA_KEY_USAGE_DECRYPT |
1592 PSA_KEY_USAGE_SIGN_MESSAGE |
1593 PSA_KEY_USAGE_VERIFY_MESSAGE |
1594 PSA_KEY_USAGE_SIGN_HASH |
1595 PSA_KEY_USAGE_VERIFY_HASH |
1596 PSA_KEY_USAGE_DERIVE)) != 0) {
1597 return PSA_ERROR_INVALID_ARGUMENT;
1598 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001600 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001601}
1602
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001603/** Validate the internal consistency of key attributes.
1604 *
1605 * This function only rejects invalid attribute values. If does not
1606 * validate the consistency of the attributes with any key data that may
1607 * be involved in the creation of the key.
1608 *
1609 * Call this function early in the key creation process.
1610 *
1611 * \param[in] attributes Key attributes for the new key.
1612 * \param[out] p_drv On any return, the driver for the key, if any.
1613 * NULL for a transparent key.
1614 *
1615 */
1616static psa_status_t psa_validate_key_attributes(
1617 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001618 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001619{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001620 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001621 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1622 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001624 status = psa_validate_key_location(lifetime, p_drv);
1625 if (status != PSA_SUCCESS) {
1626 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001627 }
1628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001629 status = psa_validate_key_persistence(lifetime);
1630 if (status != PSA_SUCCESS) {
1631 return status;
1632 }
1633
1634 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1635 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1636 return PSA_ERROR_INVALID_ARGUMENT;
1637 }
1638 } else {
1639 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1640 return PSA_ERROR_INVALID_ARGUMENT;
1641 }
1642 }
1643
1644 status = psa_validate_key_policy(&attributes->core.policy);
1645 if (status != PSA_SUCCESS) {
1646 return status;
1647 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001648
1649 /* Refuse to create overly large keys.
1650 * Note that this doesn't trigger on import if the attributes don't
1651 * explicitly specify a size (so psa_get_key_bits returns 0), so
1652 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001653 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1654 return PSA_ERROR_NOT_SUPPORTED;
1655 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001656
Gilles Peskine91e8c332019-08-02 19:19:39 +02001657 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001658 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1659 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1660 return PSA_ERROR_INVALID_ARGUMENT;
1661 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001663 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001664}
1665
Gilles Peskine4747d192019-04-17 15:05:45 +02001666/** Prepare a key slot to receive key material.
1667 *
1668 * This function allocates a key slot and sets its metadata.
1669 *
1670 * If this function fails, call psa_fail_key_creation().
1671 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001672 * This function is intended to be used as follows:
1673 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001674 * it with the specified attributes, and in case of a volatile key assign it
1675 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001676 * -# Populate the slot with the key material.
1677 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1678 * In case of failure at any step, stop the sequence and call
1679 * psa_fail_key_creation().
1680 *
Ronald Cron5c522922020-11-14 16:35:34 +01001681 * On success, the key slot is locked. It is the responsibility of the caller
1682 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001683 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001684 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001685 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001686 * \param[out] p_slot On success, a pointer to the prepared slot.
1687 * \param[out] p_drv On any return, the driver for the key, if any.
1688 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001689 *
1690 * \retval #PSA_SUCCESS
1691 * The key slot is ready to receive key material.
1692 * \return If this function fails, the key slot is an invalid state.
1693 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001694 */
1695static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001696 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001697 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001698 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001699 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001700{
1701 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001702 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001703 psa_key_slot_t *slot;
1704
Gilles Peskinedf179142019-07-15 22:02:14 +02001705 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001706 *p_drv = NULL;
1707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001708 status = psa_validate_key_attributes(attributes, p_drv);
1709 if (status != PSA_SUCCESS) {
1710 return status;
1711 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001713 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1714 if (status != PSA_SUCCESS) {
1715 return status;
1716 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001717 slot = *p_slot;
1718
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001719 /* We're storing the declared bit-size of the key. It's up to each
1720 * creation mechanism to verify that this information is correct.
1721 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001722 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001723 * is optional (import, copy). In case of a volatile key, assign it the
1724 * volatile key identifier associated to the slot returned to contain its
1725 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001726
1727 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001728 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001729#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1730 slot->attr.id = volatile_key_id;
1731#else
1732 slot->attr.id.key_id = volatile_key_id;
1733#endif
1734 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001735
Gilles Peskine91e8c332019-08-02 19:19:39 +02001736 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001737 * external-only flags, query `attributes`. Thanks to the check
1738 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001739 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001740 * may have set. */
1741 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001742
Gilles Peskinecbaff462019-07-12 23:46:04 +02001743#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001744 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001745 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001746 * create the key file in internal storage, create the
1747 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001748 * persistent data. This is done by starting a transaction that will
1749 * encompass these three actions.
1750 * For registering a volatile key, we just need to find an appropriate
1751 * slot number inside the SE. Since the key is designated volatile, creating
1752 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001753 /* The first thing to do is to find a slot number for the new key.
1754 * We save the slot number in persistent storage as part of the
1755 * transaction data. It will be needed to recover if the power
1756 * fails during the key creation process, to clean up on the secure
1757 * element side after restarting. Obtaining a slot number from the
1758 * secure element driver updates its persistent state, but we do not yet
1759 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001760 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001761 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001762 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001763 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1764 &slot_number);
1765 if (status != PSA_SUCCESS) {
1766 return status;
1767 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001769 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1770 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001771 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001772 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001773 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001774 status = psa_crypto_save_transaction();
1775 if (status != PSA_SUCCESS) {
1776 (void) psa_crypto_stop_transaction();
1777 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001778 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001779 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001780
1781 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001782 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001783 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001785 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001786 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001787 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001788 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001789#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1790
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001791 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001792}
Gilles Peskine4747d192019-04-17 15:05:45 +02001793
1794/** Finalize the creation of a key once its key material has been set.
1795 *
1796 * This entails writing the key to persistent storage.
1797 *
1798 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001799 * See the documentation of psa_start_key_creation() for the intended use
1800 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001801 *
Ronald Cron5c522922020-11-14 16:35:34 +01001802 * If the finalization succeeds, the function unlocks the key slot (it was
1803 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1804 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001805 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001806 * \param[in,out] slot Pointer to the slot with key material.
1807 * \param[in] driver The secure element driver for the key,
1808 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001809 * \param[out] key On success, identifier of the key. Note that the
1810 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001811 *
1812 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001813 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001814 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1815 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1816 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1817 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1818 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1819 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001820 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001821 * \return If this function fails, the key slot is an invalid state.
1822 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001823 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001824static psa_status_t psa_finish_key_creation(
1825 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001826 psa_se_drv_table_entry_t *driver,
1827 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001828{
1829 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001830 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001831 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001832
1833#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001834 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001835#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001836 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001837 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001838 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001839 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001840
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001841 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1842 sizeof(data.slot_number),
1843 "Slot number size does not match psa_se_key_data_storage_t");
1844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001845 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1846 status = psa_save_persistent_key(&slot->attr,
1847 (uint8_t *) &data,
1848 sizeof(data));
1849 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001850#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1851 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001852 /* Key material is saved in export representation in the slot, so
1853 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001854 status = psa_save_persistent_key(&slot->attr,
1855 slot->key.data,
1856 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001857 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001858 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001859#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1860
Gilles Peskinecbaff462019-07-12 23:46:04 +02001861#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001862 /* Finish the transaction for a key creation. This does not
1863 * happen when registering an existing key. Detect this case
1864 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001865 * creation of a persistent key in a secure element requires a transaction,
1866 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001867 if (driver != NULL &&
1868 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1869 status = psa_save_se_persistent_data(driver);
1870 if (status != PSA_SUCCESS) {
1871 psa_destroy_persistent_key(slot->attr.id);
1872 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001873 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001874 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001875 }
1876#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001878 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001879 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001880 status = psa_unlock_key_slot(slot);
1881 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001882 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001883 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001884 }
Ronald Cron50972942020-11-14 11:28:25 +01001885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001886 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001887}
1888
1889/** Abort the creation of a key.
1890 *
1891 * You may call this function after calling psa_start_key_creation(),
1892 * or after psa_finish_key_creation() fails. In other circumstances, this
1893 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001894 * See the documentation of psa_start_key_creation() for the intended use
1895 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001896 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001897 * \param[in,out] slot Pointer to the slot with key material.
1898 * \param[in] driver The secure element driver for the key,
1899 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001900 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001901static void psa_fail_key_creation(psa_key_slot_t *slot,
1902 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001903{
Gilles Peskine011e4282019-06-26 18:34:38 +02001904 (void) driver;
1905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001906 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001907 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001908 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001909
1910#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001911 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001912 * element, and the failure happened later (when saving metadata
1913 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001914 * element.
1915 * https://github.com/ARMmbed/mbed-crypto/issues/217
1916 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001917
Gilles Peskined7729582019-08-05 15:55:54 +02001918 /* Abort the ongoing transaction if any (there may not be one if
1919 * the creation process failed before starting one, or if the
1920 * key creation is a registration of a key in a secure element).
1921 * Earlier functions must already have done what it takes to undo any
1922 * partial creation. All that's left is to update the transaction data
1923 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001924 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001925#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1926
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001927 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001928}
1929
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001930/** Validate optional attributes during key creation.
1931 *
1932 * Some key attributes are optional during key creation. If they are
1933 * specified in the attributes structure, check that they are consistent
1934 * with the data in the slot.
1935 *
1936 * This function should be called near the end of key creation, after
1937 * the slot in memory is fully populated but before saving persistent data.
1938 */
1939static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001940 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001941 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001942{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001943 if (attributes->core.type != 0) {
1944 if (attributes->core.type != slot->attr.type) {
1945 return PSA_ERROR_INVALID_ARGUMENT;
1946 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001947 }
1948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001949 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001950#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001951 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1952 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001953 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001954 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001955 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001956
Ronald Cron90857082020-11-25 14:58:33 +01001957 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001958 slot->attr.type,
1959 slot->key.data,
1960 slot->key.bytes,
1961 &rsa);
1962 if (status != PSA_SUCCESS) {
1963 return status;
1964 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001966 mbedtls_mpi_init(&actual);
1967 mbedtls_mpi_init(&required);
1968 ret = mbedtls_rsa_export(rsa,
1969 NULL, NULL, NULL, NULL, &actual);
1970 mbedtls_rsa_free(rsa);
1971 mbedtls_free(rsa);
1972 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001973 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001974 }
1975 ret = mbedtls_mpi_read_binary(&required,
1976 attributes->domain_parameters,
1977 attributes->domain_parameters_size);
1978 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001979 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001980 }
1981 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001982 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001983 }
1984rsa_exit:
1985 mbedtls_mpi_free(&actual);
1986 mbedtls_mpi_free(&required);
1987 if (ret != 0) {
1988 return mbedtls_to_psa_error(ret);
1989 }
1990 } else
John Durkop9814fa22020-11-04 12:28:15 -08001991#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1992 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001993 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001994 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001995 }
1996 }
1997
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001998 if (attributes->core.bits != 0) {
1999 if (attributes->core.bits != slot->attr.bits) {
2000 return PSA_ERROR_INVALID_ARGUMENT;
2001 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002002 }
2003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002004 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002005}
2006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002007psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2008 const uint8_t *data,
2009 size_t data_length,
2010 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002011{
2012 psa_status_t status;
2013 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002014 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002015 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002016
Ronald Cron81709fc2020-11-14 12:10:32 +01002017 *key = MBEDTLS_SVC_KEY_ID_INIT;
2018
Gilles Peskine0f84d622019-09-12 19:03:13 +02002019 /* Reject zero-length symmetric keys (including raw data key objects).
2020 * This also rejects any key which might be encoded as an empty string,
2021 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002022 if (data_length == 0) {
2023 return PSA_ERROR_INVALID_ARGUMENT;
2024 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002026 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2027 &slot, &driver);
2028 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002029 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002030 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002031
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002032 /* In the case of a transparent key or an opaque key stored in local
2033 * storage (thus not in the case of generating a key in a secure element
2034 * or cryptoprocessor with storage), we have to allocate a buffer to
2035 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002036 if (slot->key.data == NULL) {
2037 status = psa_allocate_buffer_to_slot(slot, data_length);
2038 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002039 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002040 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002041 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002042
2043 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002044 status = psa_driver_wrapper_import_key(attributes,
2045 data, data_length,
2046 slot->key.data,
2047 slot->key.bytes,
2048 &slot->key.bytes, &bits);
2049 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002050 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002051 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002053 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002054 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002055 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002056 status = PSA_ERROR_INVALID_ARGUMENT;
2057 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002058 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002060 status = psa_validate_optional_attributes(slot, attributes);
2061 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002062 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002063 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002064
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002065 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002066exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002067 if (status != PSA_SUCCESS) {
2068 psa_fail_key_creation(slot, driver);
2069 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002071 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002072}
2073
Gilles Peskined7729582019-08-05 15:55:54 +02002074#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2075psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002076 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002077{
2078 psa_status_t status;
2079 psa_key_slot_t *slot = NULL;
2080 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002081 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002082
2083 /* Leaving attributes unspecified is not currently supported.
2084 * It could make sense to query the key type and size from the
2085 * secure element, but not all secure elements support this
2086 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002087 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2088 return PSA_ERROR_NOT_SUPPORTED;
2089 }
2090 if (psa_get_key_bits(attributes) == 0) {
2091 return PSA_ERROR_NOT_SUPPORTED;
2092 }
Gilles Peskined7729582019-08-05 15:55:54 +02002093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002094 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2095 &slot, &driver);
2096 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002097 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002098 }
Gilles Peskined7729582019-08-05 15:55:54 +02002099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002100 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002101
2102exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002103 if (status != PSA_SUCCESS) {
2104 psa_fail_key_creation(slot, driver);
2105 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002106
Gilles Peskined7729582019-08-05 15:55:54 +02002107 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002108 psa_close_key(key);
2109 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002110}
2111#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002113static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2114 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002115{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002116 psa_status_t status = psa_copy_key_material_into_slot(target,
2117 source->key.data,
2118 source->key.bytes);
2119 if (status != PSA_SUCCESS) {
2120 return status;
2121 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002122
Steven Cooreman398aee52020-10-13 14:35:45 +02002123 target->attr.type = source->attr.type;
2124 target->attr.bits = source->attr.bits;
2125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002126 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002127}
2128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002129psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2130 const psa_key_attributes_t *specified_attributes,
2131 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002132{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002133 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002134 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002135 psa_key_slot_t *source_slot = NULL;
2136 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002137 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002138 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002139
Ronald Cron81709fc2020-11-14 12:10:32 +01002140 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2141
Ronald Cron5c522922020-11-14 16:35:34 +01002142 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002143 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2144 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002145 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002146 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002148 status = psa_validate_optional_attributes(source_slot,
2149 specified_attributes);
2150 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002151 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002154 status = psa_restrict_key_policy(source_slot->attr.type,
2155 &actual_attributes.core.policy,
2156 &source_slot->attr.policy);
2157 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002158 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002159 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002161 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2162 &target_slot, &driver);
2163 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002164 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002165 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002166
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002167#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002168 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002169 /* Copying to a secure element is not implemented yet. */
2170 status = PSA_ERROR_NOT_SUPPORTED;
2171 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002172 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002173#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002175 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002176 /*
2177 * Copying through an opaque driver is not implemented yet, consider
2178 * a lifetime with an external location as an invalid parameter for
2179 * now.
2180 */
2181 status = PSA_ERROR_INVALID_ARGUMENT;
2182 goto exit;
2183 }
2184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002185 status = psa_copy_key_material(source_slot, target_slot);
2186 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002187 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002188 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002190 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002191exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002192 if (status != PSA_SUCCESS) {
2193 psa_fail_key_creation(target_slot, driver);
2194 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002196 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002198 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002199}
2200
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002201
2202
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002203/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002204/* Message digests */
2205/****************************************************************/
2206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002207psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002208{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002209 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002210 if (operation->id == 0) {
2211 return PSA_SUCCESS;
2212 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002214 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002215 operation->id = 0;
2216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002217 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002218}
2219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002220psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2221 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002222{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002223 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002224
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002225 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002226 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002227 status = PSA_ERROR_BAD_STATE;
2228 goto exit;
2229 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002231 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002232 status = PSA_ERROR_INVALID_ARGUMENT;
2233 goto exit;
2234 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002235
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002236 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2237 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002238 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002240 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002241
2242exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002243 if (status != PSA_SUCCESS) {
2244 psa_hash_abort(operation);
2245 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002246
2247 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002248}
2249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002250psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2251 const uint8_t *input,
2252 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002253{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002254 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002256 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002257 status = PSA_ERROR_BAD_STATE;
2258 goto exit;
2259 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002260
Steven Cooremanc8288352021-03-04 14:02:19 +01002261 /* Don't require hash implementations to behave correctly on a
2262 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002263 if (input_length == 0) {
2264 return PSA_SUCCESS;
2265 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002267 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002268
2269exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002270 if (status != PSA_SUCCESS) {
2271 psa_hash_abort(operation);
2272 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002274 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002275}
2276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002277psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2278 uint8_t *hash,
2279 size_t hash_size,
2280 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002281{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002282 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002283 if (operation->id == 0) {
2284 return PSA_ERROR_BAD_STATE;
2285 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286
Steven Cooreman1e582352021-02-18 17:24:37 +01002287 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002288 operation, hash, hash_size, hash_length);
2289 psa_hash_abort(operation);
2290 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002291}
2292
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002293psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2294 const uint8_t *hash,
2295 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002296{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002297 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002298 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002299 psa_status_t status = psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002300 operation,
2301 actual_hash, sizeof(actual_hash),
2302 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002304 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002305 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002306 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002308 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002309 status = PSA_ERROR_INVALID_SIGNATURE;
2310 goto exit;
2311 }
2312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002313 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002314 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002315 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002316
2317exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002318 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2319 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002320 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002321 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002322
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002323 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324}
2325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002326psa_status_t psa_hash_compute(psa_algorithm_t alg,
2327 const uint8_t *input, size_t input_length,
2328 uint8_t *hash, size_t hash_size,
2329 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002330{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002331 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002332 if (!PSA_ALG_IS_HASH(alg)) {
2333 return PSA_ERROR_INVALID_ARGUMENT;
2334 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002336 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2337 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002338}
2339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002340psa_status_t psa_hash_compare(psa_algorithm_t alg,
2341 const uint8_t *input, size_t input_length,
2342 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002343{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002344 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002345 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002347 if (!PSA_ALG_IS_HASH(alg)) {
2348 return PSA_ERROR_INVALID_ARGUMENT;
2349 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002350
Steven Cooreman1e582352021-02-18 17:24:37 +01002351 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002352 alg, input, input_length,
2353 actual_hash, sizeof(actual_hash),
2354 &actual_hash_length);
2355 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002356 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002357 }
2358 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002359 status = PSA_ERROR_INVALID_SIGNATURE;
2360 goto exit;
2361 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002362 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002363 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002364 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002365
2366exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002367 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2368 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002369}
2370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002371psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2372 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002373{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002374 if (source_operation->id == 0 ||
2375 target_operation->id != 0) {
2376 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002377 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002379 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2380 target_operation);
2381 if (status != PSA_SUCCESS) {
2382 psa_hash_abort(target_operation);
2383 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002384
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002385 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002386}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002387
2388
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002389/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002390/* MAC */
2391/****************************************************************/
2392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002393psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002394{
Steven Cooreman07897832021-03-19 18:28:56 +01002395 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002396 if (operation->id == 0) {
2397 return PSA_SUCCESS;
2398 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002400 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002401 operation->mac_size = 0;
2402 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002403 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002405 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002406}
2407
Ronald Cron1c650a12021-06-17 16:33:22 +02002408static psa_status_t psa_mac_finalize_alg_and_key_validation(
2409 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002410 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002411 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002412{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002413 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002414 psa_key_type_t key_type = psa_get_key_type(attributes);
2415 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002417 if (!PSA_ALG_IS_MAC(alg)) {
2418 return PSA_ERROR_INVALID_ARGUMENT;
2419 }
Steven Cooreman07897832021-03-19 18:28:56 +01002420
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002421 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002422 status = psa_mac_key_can_do(alg, key_type);
2423 if (status != PSA_SUCCESS) {
2424 return status;
2425 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002426
Steven Cooremana6474de2021-05-10 11:13:41 +02002427 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002428 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002430 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002431 /* A very short MAC is too short for security since it can be
2432 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2433 * so we make this our minimum, even though 32 bits is still
2434 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002435 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002436 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002438 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2439 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002440 /* It's impossible to "truncate" to a larger length than the full length
2441 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002442 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002443 }
2444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002445 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002446 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2447 * that is disabled in the compile-time configuration. The result can
2448 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2449 * configuration into account. In this case, force a return of
2450 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2451 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2452 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2453 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2454 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002455 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002456 }
2457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002458 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002459}
2460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002461static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2462 mbedtls_svc_key_id_t key,
2463 psa_algorithm_t alg,
2464 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002465{
2466 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2467 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002468 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002469 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002470
2471 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002472 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002473 status = PSA_ERROR_BAD_STATE;
2474 goto exit;
2475 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002476
2477 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002478 key,
2479 &slot,
2480 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2481 alg);
2482 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002483 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002484 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002485
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002486 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002487 .core = slot->attr
2488 };
2489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002490 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2491 &operation->mac_size);
2492 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002493 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002494 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002495
2496 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002497 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002498 if (is_sign) {
2499 status = psa_driver_wrapper_mac_sign_setup(operation,
2500 &attributes,
2501 slot->key.data,
2502 slot->key.bytes,
2503 alg);
2504 } else {
2505 status = psa_driver_wrapper_mac_verify_setup(operation,
2506 &attributes,
2507 slot->key.data,
2508 slot->key.bytes,
2509 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002510 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002511
Gilles Peskinefbfac682018-07-08 20:51:54 +02002512exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002513 if (status != PSA_SUCCESS) {
2514 psa_mac_abort(operation);
2515 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002517 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002519 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520}
2521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002522psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2523 mbedtls_svc_key_id_t key,
2524 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002525{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002526 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002527}
2528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002529psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2530 mbedtls_svc_key_id_t key,
2531 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002532{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002533 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002534}
2535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002536psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2537 const uint8_t *input,
2538 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002539{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002540 if (operation->id == 0) {
2541 return PSA_ERROR_BAD_STATE;
2542 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002543
Steven Cooreman11743f92021-03-19 18:38:46 +01002544 /* Don't require hash implementations to behave correctly on a
2545 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002546 if (input_length == 0) {
2547 return PSA_SUCCESS;
2548 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002550 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2551 input, input_length);
2552 if (status != PSA_SUCCESS) {
2553 psa_mac_abort(operation);
2554 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002556 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557}
2558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002559psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2560 uint8_t *mac,
2561 size_t mac_size,
2562 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002563{
Steven Cooreman87885df2021-03-19 19:04:39 +01002564 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2565 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002567 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002568 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002569 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002570 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002572 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002573 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002574 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002575 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002576
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002577 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2578 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002579 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002580 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002581 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002582 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002583
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002584 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002585 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002586 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002587 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002588
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002589 status = psa_driver_wrapper_mac_sign_finish(operation,
2590 mac, operation->mac_size,
2591 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002592
Dave Rodgman478ab542021-06-25 09:09:02 +01002593exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002594 /* In case of success, set the potential excess room in the output buffer
2595 * to an invalid value, to avoid potentially leaking a longer MAC.
2596 * In case of error, set the output length and content to a safe default,
2597 * such that in case the caller misses an error check, the output would be
2598 * an unachievable MAC.
2599 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002600 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002601 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002602 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002603 }
mohammad16036df908f2018-04-02 08:34:15 -07002604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002605 if (mac_size > operation->mac_size) {
2606 memset(&mac[operation->mac_size], '!',
2607 mac_size - operation->mac_size);
2608 }
Ronald Cron882eb782021-06-18 13:05:48 +02002609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002610 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002612 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002613}
2614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002615psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2616 const uint8_t *mac,
2617 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002618{
Steven Cooreman87885df2021-03-19 19:04:39 +01002619 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2620 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002622 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002623 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002624 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002625 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002626
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002627 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002628 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002629 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002630 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002631
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002632 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002633 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002634 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002635 }
2636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002637 status = psa_driver_wrapper_mac_verify_finish(operation,
2638 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002639
Dave Rodgman478ab542021-06-25 09:09:02 +01002640exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002641 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002643 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002644}
2645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002646static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2647 psa_algorithm_t alg,
2648 const uint8_t *input,
2649 size_t input_length,
2650 uint8_t *mac,
2651 size_t mac_size,
2652 size_t *mac_length,
2653 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002654{
2655 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002656 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002657 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002658 psa_key_slot_t *slot;
2659 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002660
Ronald Crondbb86462021-06-17 17:17:20 +02002661 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002662 key,
2663 &slot,
2664 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2665 alg);
2666 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002667 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002668 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002669
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002670 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002671 .core = slot->attr
2672 };
2673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002674 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2675 &operation_mac_size);
2676 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002677 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002678 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002680 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002681 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002682 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002683 }
2684
2685 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002686 &attributes,
2687 slot->key.data, slot->key.bytes,
2688 alg,
2689 input, input_length,
2690 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002691
2692exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002693 /* In case of success, set the potential excess room in the output buffer
2694 * to an invalid value, to avoid potentially leaking a longer MAC.
2695 * In case of error, set the output length and content to a safe default,
2696 * such that in case the caller misses an error check, the output would be
2697 * an unachievable MAC.
2698 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002699 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002700 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002701 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002702 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002703 if (mac_size > operation_mac_size) {
2704 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2705 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002707 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002709 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002710}
2711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002712psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002713 psa_algorithm_t alg,
2714 const uint8_t *input,
2715 size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002716 uint8_t *mac,
2717 size_t mac_size,
2718 size_t *mac_length)
2719{
2720 return psa_mac_compute_internal(key, alg,
2721 input, input_length,
2722 mac, mac_size, mac_length, 1);
2723}
2724
2725psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2726 psa_algorithm_t alg,
2727 const uint8_t *input,
2728 size_t input_length,
2729 const uint8_t *mac,
2730 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002731{
2732 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002733 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2734 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002735
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002736 status = psa_mac_compute_internal(key, alg,
2737 input, input_length,
2738 actual_mac, sizeof(actual_mac),
2739 &actual_mac_length, 0);
2740 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002741 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002742 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002744 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002745 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002746 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002747 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002748 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002749 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002750 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002751 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002752
2753exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002754 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002755
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002756 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002757}
Gilles Peskine20035e32018-02-03 22:44:14 +01002758
Gilles Peskine20035e32018-02-03 22:44:14 +01002759/****************************************************************/
2760/* Asymmetric cryptography */
2761/****************************************************************/
2762
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002763static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2764 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002765{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002766 if (input_is_message) {
2767 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2768 return PSA_ERROR_INVALID_ARGUMENT;
2769 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002771 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2772 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2773 return PSA_ERROR_INVALID_ARGUMENT;
2774 }
2775 }
2776 } else {
2777 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2778 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002779 }
2780 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002782 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002783}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002785static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2786 int input_is_message,
2787 psa_algorithm_t alg,
2788 const uint8_t *input,
2789 size_t input_length,
2790 uint8_t *signature,
2791 size_t signature_size,
2792 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002793{
2794 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2795 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002796 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002797 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002798
2799 *signature_length = 0;
2800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002801 status = psa_sign_verify_check_alg(input_is_message, alg);
2802 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002803 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002804 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002805
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002806 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002807 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002808 * buffer can in principle be empty since it doesn't actually have
2809 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002810 if (signature_size == 0) {
2811 return PSA_ERROR_BUFFER_TOO_SMALL;
2812 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002813
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002814 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002815 key, &slot,
2816 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2817 PSA_KEY_USAGE_SIGN_HASH,
2818 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002820 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002821 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002822 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002823
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002824 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002825 status = PSA_ERROR_INVALID_ARGUMENT;
2826 goto exit;
2827 }
2828
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002829 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002830 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002831 };
2832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002833 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002834 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002835 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002836 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002837 signature, signature_size, signature_length);
2838 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002839
2840 status = psa_driver_wrapper_sign_hash(
2841 &attributes, slot->key.data, slot->key.bytes,
2842 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002843 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002844 }
2845
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002846
2847exit:
2848 /* Fill the unused part of the output buffer (the whole buffer on error,
2849 * the trailing part on success) with something that isn't a valid signature
2850 * (barring an attack on the signature and deliberately-crafted input),
2851 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002852 if (status == PSA_SUCCESS) {
2853 memset(signature + *signature_length, '!',
2854 signature_size - *signature_length);
2855 } else {
2856 memset(signature, '!', signature_size);
2857 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002858 /* If signature_size is 0 then we have nothing to do. We must not call
2859 * memset because signature may be NULL in this case. */
2860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002861 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002863 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002864}
2865
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002866static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2867 int input_is_message,
2868 psa_algorithm_t alg,
2869 const uint8_t *input,
2870 size_t input_length,
2871 const uint8_t *signature,
2872 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002873{
2874 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2875 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2876 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002878 status = psa_sign_verify_check_alg(input_is_message, alg);
2879 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002880 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002881 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002882
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002883 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002884 key, &slot,
2885 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2886 PSA_KEY_USAGE_VERIFY_HASH,
2887 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002889 if (status != PSA_SUCCESS) {
2890 return status;
2891 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002892
2893 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002894 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002895 };
2896
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002897 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002898 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002899 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002900 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002901 signature, signature_length);
2902 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002903 status = psa_driver_wrapper_verify_hash(
2904 &attributes, slot->key.data, slot->key.bytes,
2905 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002906 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002907 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002909 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002911 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002912
2913}
2914
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002915psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002916 const psa_key_attributes_t *attributes,
2917 const uint8_t *key_buffer,
2918 size_t key_buffer_size,
2919 psa_algorithm_t alg,
2920 const uint8_t *input,
2921 size_t input_length,
2922 uint8_t *signature,
2923 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002924 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002925{
2926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002927
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002928 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002929 size_t hash_length;
2930 uint8_t hash[PSA_HASH_MAX_SIZE];
2931
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002932 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002933 PSA_ALG_SIGN_GET_HASH(alg),
2934 input, input_length,
2935 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002936
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002937 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002938 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002939 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002940
2941 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002942 attributes, key_buffer, key_buffer_size,
2943 alg, hash, hash_length,
2944 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002945 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002947 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002948}
2949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002950psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2951 psa_algorithm_t alg,
2952 const uint8_t *input,
2953 size_t input_length,
2954 uint8_t *signature,
2955 size_t signature_size,
2956 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002957{
2958 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002959 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002960 signature, signature_size, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002961}
2962
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002963psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002964 const psa_key_attributes_t *attributes,
2965 const uint8_t *key_buffer,
2966 size_t key_buffer_size,
2967 psa_algorithm_t alg,
2968 const uint8_t *input,
2969 size_t input_length,
2970 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002971 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002972{
2973 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002974
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002975 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002976 size_t hash_length;
2977 uint8_t hash[PSA_HASH_MAX_SIZE];
2978
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002979 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002980 PSA_ALG_SIGN_GET_HASH(alg),
2981 input, input_length,
2982 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002983
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002984 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002985 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002986 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002987
2988 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002989 attributes, key_buffer, key_buffer_size,
2990 alg, hash, hash_length,
2991 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002992 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002994 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002995}
2996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002997psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2998 psa_algorithm_t alg,
2999 const uint8_t *input,
3000 size_t input_length,
3001 const uint8_t *signature,
3002 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003003{
3004 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003005 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003006 signature, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003007}
3008
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003009psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003010 const psa_key_attributes_t *attributes,
3011 const uint8_t *key_buffer, size_t key_buffer_size,
3012 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003013 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01003014{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003015 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3016 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3017 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003018#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003019 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3020 return mbedtls_psa_rsa_sign_hash(
3021 attributes,
3022 key_buffer, key_buffer_size,
3023 alg, hash, hash_length,
3024 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003025#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3026 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003027 } else {
3028 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003029 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003030 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3031 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003032#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003033 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3034 return mbedtls_psa_ecdsa_sign_hash(
3035 attributes,
3036 key_buffer, key_buffer_size,
3037 alg, hash, hash_length,
3038 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003039#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3040 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003041 } else {
3042 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003043 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003044 }
Ronald Cron4501c982021-03-19 20:09:32 +01003045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003046 (void) key_buffer;
3047 (void) key_buffer_size;
3048 (void) hash;
3049 (void) hash_length;
3050 (void) signature;
3051 (void) signature_size;
3052 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003054 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003055}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003056
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003057psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3058 psa_algorithm_t alg,
3059 const uint8_t *hash,
3060 size_t hash_length,
3061 uint8_t *signature,
3062 size_t signature_size,
3063 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003064{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003065 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003066 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003067 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003068}
3069
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003070psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003071 const psa_key_attributes_t *attributes,
3072 const uint8_t *key_buffer, size_t key_buffer_size,
3073 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003074 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003075{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003076 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3077 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3078 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003079#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003080 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3081 return mbedtls_psa_rsa_verify_hash(
3082 attributes,
3083 key_buffer, key_buffer_size,
3084 alg, hash, hash_length,
3085 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003086#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3087 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003088 } else {
3089 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003090 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003091 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3092 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003093#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003094 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3095 return mbedtls_psa_ecdsa_verify_hash(
3096 attributes,
3097 key_buffer, key_buffer_size,
3098 alg, hash, hash_length,
3099 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003100#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3101 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003102 } else {
3103 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003104 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003105 }
Ronald Cron4501c982021-03-19 20:09:32 +01003106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003107 (void) key_buffer;
3108 (void) key_buffer_size;
3109 (void) hash;
3110 (void) hash_length;
3111 (void) signature;
3112 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003114 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003115}
3116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003117psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3118 psa_algorithm_t alg,
3119 const uint8_t *hash,
3120 size_t hash_length,
3121 const uint8_t *signature,
3122 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003123{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003124 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003125 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003126 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003127}
3128
John Durkop0e005192020-10-31 22:06:54 -07003129#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003130static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3131 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003132{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003133 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3134 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3135 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3136 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003137}
John Durkop0e005192020-10-31 22:06:54 -07003138#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003140psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3141 psa_algorithm_t alg,
3142 const uint8_t *input,
3143 size_t input_length,
3144 const uint8_t *salt,
3145 size_t salt_length,
3146 uint8_t *output,
3147 size_t output_size,
3148 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003149{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003150 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003151 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003152 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003153
Darryl Green5cc689a2018-07-24 15:34:10 +01003154 (void) input;
3155 (void) input_length;
3156 (void) salt;
3157 (void) output;
3158 (void) output_size;
3159
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003160 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003162 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3163 return PSA_ERROR_INVALID_ARGUMENT;
3164 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003165
Ronald Cron5c522922020-11-14 16:35:34 +01003166 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003167 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3168 if (status != PSA_SUCCESS) {
3169 return status;
3170 }
3171 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3172 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003173 status = PSA_ERROR_INVALID_ARGUMENT;
3174 goto exit;
3175 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003177 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003178#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003179 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003180 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003181 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3182 slot->key.data,
3183 slot->key.bytes,
3184 &rsa);
3185 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003186 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003187 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003189 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003190 status = PSA_ERROR_BUFFER_TOO_SMALL;
3191 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003192 }
Ronald Cron7207d572021-09-08 14:28:35 +02003193#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3194 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003195 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003196#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003197 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003198 mbedtls_rsa_pkcs1_encrypt(rsa,
3199 mbedtls_psa_get_random,
3200 MBEDTLS_PSA_RANDOM_STATE,
3201 MBEDTLS_RSA_PUBLIC,
3202 input_length,
3203 input,
3204 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003205#else
3206 status = PSA_ERROR_NOT_SUPPORTED;
3207#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003208 } else
3209 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003210#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003211 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003212 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003213 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3214 mbedtls_psa_get_random,
3215 MBEDTLS_PSA_RANDOM_STATE,
3216 MBEDTLS_RSA_PUBLIC,
3217 salt, salt_length,
3218 input_length,
3219 input,
3220 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003221#else
3222 status = PSA_ERROR_NOT_SUPPORTED;
3223#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003224 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003225 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003226 }
Ronald Cron7207d572021-09-08 14:28:35 +02003227#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003228 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003229rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003230 if (status == PSA_SUCCESS) {
3231 *output_length = mbedtls_rsa_get_len(rsa);
3232 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003234 mbedtls_rsa_free(rsa);
3235 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003236#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003237 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003238 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003239 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003240 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003241
3242exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003243 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003245 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003246}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003248psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3249 psa_algorithm_t alg,
3250 const uint8_t *input,
3251 size_t input_length,
3252 const uint8_t *salt,
3253 size_t salt_length,
3254 uint8_t *output,
3255 size_t output_size,
3256 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003257{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003258 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003259 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003260 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003261
Darryl Green5cc689a2018-07-24 15:34:10 +01003262 (void) input;
3263 (void) input_length;
3264 (void) salt;
3265 (void) output;
3266 (void) output_size;
3267
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003268 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003270 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3271 return PSA_ERROR_INVALID_ARGUMENT;
3272 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003273
Ronald Cron5c522922020-11-14 16:35:34 +01003274 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003275 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3276 if (status != PSA_SUCCESS) {
3277 return status;
3278 }
3279 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003280 status = PSA_ERROR_INVALID_ARGUMENT;
3281 goto exit;
3282 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003284 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003285#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003286 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003287 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003288 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3289 slot->key.data,
3290 slot->key.bytes,
3291 &rsa);
3292 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003293 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003294 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003295
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003296 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003297 status = PSA_ERROR_INVALID_ARGUMENT;
3298 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003299 }
Ronald Cron7207d572021-09-08 14:28:35 +02003300#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3301 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003303 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003304#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003305 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003306 mbedtls_rsa_pkcs1_decrypt(rsa,
3307 mbedtls_psa_get_random,
3308 MBEDTLS_PSA_RANDOM_STATE,
3309 MBEDTLS_RSA_PRIVATE,
3310 output_length,
3311 input,
3312 output,
3313 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003314#else
3315 status = PSA_ERROR_NOT_SUPPORTED;
3316#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003317 } else
3318 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003319#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003320 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003321 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003322 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3323 mbedtls_psa_get_random,
3324 MBEDTLS_PSA_RANDOM_STATE,
3325 MBEDTLS_RSA_PRIVATE,
3326 salt, salt_length,
3327 output_length,
3328 input,
3329 output,
3330 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003331#else
3332 status = PSA_ERROR_NOT_SUPPORTED;
3333#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003334 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003335 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003336 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003337
Ronald Cron7207d572021-09-08 14:28:35 +02003338#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003339 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003340rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003341 mbedtls_rsa_free(rsa);
3342 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003343#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3344 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003345 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003346 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003347 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003348
3349exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003350 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003352 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003353}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003354
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003355
3356
mohammad1603503973b2018-03-12 15:59:30 +02003357/****************************************************************/
3358/* Symmetric cryptography */
3359/****************************************************************/
3360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003361static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3362 mbedtls_svc_key_id_t key,
3363 psa_algorithm_t alg,
3364 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003365{
3366 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3367 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003368 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003369 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003370 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3371 PSA_KEY_USAGE_ENCRYPT :
3372 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003373
3374 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003375 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003376 status = PSA_ERROR_BAD_STATE;
3377 goto exit;
3378 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003380 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003381 status = PSA_ERROR_INVALID_ARGUMENT;
3382 goto exit;
3383 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003384
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003385 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3386 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003387 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003388 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003389
Ronald Cron49fafa92021-03-10 08:34:23 +01003390 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003391 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003392 * so we only set it (in the driver wrapper) after resources have been
3393 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003394 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003395 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003396 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003397 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003398 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003399 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003400 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003401 }
3402 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003403
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003404 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003405 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003406 };
3407
Ronald Cronab99ac22020-10-01 16:38:28 +02003408 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003409 if (cipher_operation == MBEDTLS_ENCRYPT) {
3410 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3411 &attributes,
3412 slot->key.data,
3413 slot->key.bytes,
3414 alg);
3415 } else {
3416 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3417 &attributes,
3418 slot->key.data,
3419 slot->key.bytes,
3420 alg);
3421 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003422
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003423exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003424 if (status != PSA_SUCCESS) {
3425 psa_cipher_abort(operation);
3426 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003428 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003430 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003431}
3432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003433psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3434 mbedtls_svc_key_id_t key,
3435 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003436{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003437 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003438}
3439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003440psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3441 mbedtls_svc_key_id_t key,
3442 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003443{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003444 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003445}
3446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003447psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3448 uint8_t *iv,
3449 size_t iv_size,
3450 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003451{
Ronald Cron6d051732020-10-01 14:10:20 +02003452 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003453 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergey04eb7c02023-03-06 15:37:23 -07003454 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003456 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003457 status = PSA_ERROR_BAD_STATE;
3458 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003459 }
3460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003461 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003462 status = PSA_ERROR_BAD_STATE;
3463 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003464 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003465
Ronald Cronc423acb2021-07-05 12:31:44 +02003466 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003467 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003468 status = PSA_ERROR_BUFFER_TOO_SMALL;
3469 goto exit;
3470 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003472 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003473 status = PSA_ERROR_GENERIC_ERROR;
3474 goto exit;
3475 }
3476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003477 status = psa_generate_random(local_iv, default_iv_length);
3478 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003479 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003480 }
Ronald Cron5618a392021-03-26 09:52:26 +01003481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003482 status = psa_driver_wrapper_cipher_set_iv(operation,
3483 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003484
3485exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003486 if (status == PSA_SUCCESS) {
3487 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003488 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003489 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003490 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003491 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003492 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003493 }
Ronald Cron6d051732020-10-01 14:10:20 +02003494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003495 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003496}
3497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003498psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3499 const uint8_t *iv,
3500 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003501{
Ronald Cron6d051732020-10-01 14:10:20 +02003502 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003504 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003505 status = PSA_ERROR_BAD_STATE;
3506 goto exit;
3507 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003509 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003510 status = PSA_ERROR_BAD_STATE;
3511 goto exit;
3512 }
Ronald Crona0d68172021-03-26 10:15:08 +01003513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003514 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003515 status = PSA_ERROR_INVALID_ARGUMENT;
3516 goto exit;
3517 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003519 status = psa_driver_wrapper_cipher_set_iv(operation,
3520 iv,
3521 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003522
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003523exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003524 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003525 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003526 } else {
3527 psa_cipher_abort(operation);
3528 }
3529 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003530}
3531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003532psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3533 const uint8_t *input,
3534 size_t input_length,
3535 uint8_t *output,
3536 size_t output_size,
3537 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003538{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003539 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003541 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003542 status = PSA_ERROR_BAD_STATE;
3543 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003544 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003546 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003547 status = PSA_ERROR_BAD_STATE;
3548 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003549 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003551 status = psa_driver_wrapper_cipher_update(operation,
3552 input,
3553 input_length,
3554 output,
3555 output_size,
3556 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003557
3558exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003559 if (status != PSA_SUCCESS) {
3560 psa_cipher_abort(operation);
3561 }
Ronald Cron6d051732020-10-01 14:10:20 +02003562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003563 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003564}
3565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003566psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3567 uint8_t *output,
3568 size_t output_size,
3569 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003570{
David Saadab4ecc272019-02-14 13:48:10 +02003571 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003573 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003574 status = PSA_ERROR_BAD_STATE;
3575 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003576 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003577
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003578 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003579 status = PSA_ERROR_BAD_STATE;
3580 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003581 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003583 status = psa_driver_wrapper_cipher_finish(operation,
3584 output,
3585 output_size,
3586 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003587
3588exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003589 if (status == PSA_SUCCESS) {
3590 return psa_cipher_abort(operation);
3591 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003592 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003593 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003594
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003595 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003596 }
mohammad1603503973b2018-03-12 15:59:30 +02003597}
3598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003599psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003600{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003601 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003602 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003603 * in use. It's ok to call abort on such an object, and there's
3604 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003605 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003606 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003608 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003609
Ronald Cron49fafa92021-03-10 08:34:23 +01003610 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003611 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003612 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003614 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003615}
3616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003617psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3618 psa_algorithm_t alg,
3619 const uint8_t *input,
3620 size_t input_length,
3621 uint8_t *output,
3622 size_t output_size,
3623 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003624{
3625 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003626 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003627 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003628 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003629 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3630 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003631
David Horstmann34980bd2023-11-29 17:07:13 +00003632 SWAP_FOR_LOCAL_INPUT(input, input_length);
3633 SWAP_FOR_LOCAL_OUTPUT(output, output_size);
David Horstmannd2ad8862023-11-27 17:43:05 +00003634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003635 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003636 status = PSA_ERROR_INVALID_ARGUMENT;
3637 goto exit;
3638 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003640 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3641 PSA_KEY_USAGE_ENCRYPT,
3642 alg);
3643 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003644 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003645 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003646
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003647 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003648 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003649 };
3650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003651 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3652 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003653 status = PSA_ERROR_GENERIC_ERROR;
3654 goto exit;
3655 }
3656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003657 if (default_iv_length > 0) {
3658 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003659 status = PSA_ERROR_BUFFER_TOO_SMALL;
3660 goto exit;
3661 }
3662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003663 status = psa_generate_random(local_iv, default_iv_length);
3664 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003665 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003666 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003667 }
3668
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003669 status = psa_driver_wrapper_cipher_encrypt(
3670 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003671 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003672 mbedtls_buffer_offset(output, default_iv_length),
3673 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003674
3675exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003676 unlock_status = psa_unlock_key_slot(slot);
3677 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003678 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003679 }
Ronald Cron16377072021-07-08 19:00:07 +02003680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003681 if (status == PSA_SUCCESS) {
3682 if (default_iv_length > 0) {
3683 memcpy(output, local_iv, default_iv_length);
3684 }
3685 *output_length += default_iv_length;
3686 } else {
3687 *output_length = 0;
3688 }
3689
David Horstmann34980bd2023-11-29 17:07:13 +00003690 FREE_LOCAL_INPUT(input);
3691 FREE_LOCAL_OUTPUT(output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003693 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003694}
3695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003696psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3697 psa_algorithm_t alg,
3698 const uint8_t *input,
3699 size_t input_length,
3700 uint8_t *output,
3701 size_t output_size,
3702 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003703{
3704 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003705 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003706 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003707 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003709 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003710 status = PSA_ERROR_INVALID_ARGUMENT;
3711 goto exit;
3712 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3715 PSA_KEY_USAGE_DECRYPT,
3716 alg);
3717 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003718 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003719 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003720
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003721 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003722 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003723 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003725 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003726 status = PSA_ERROR_INVALID_ARGUMENT;
3727 goto exit;
3728 }
3729
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003730 status = psa_driver_wrapper_cipher_decrypt(
3731 &attributes, slot->key.data, slot->key.bytes,
3732 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003733 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003734
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003735exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003736 unlock_status = psa_unlock_key_slot(slot);
3737 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003738 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003739 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003740
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003741 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003742 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003743 }
Ronald Cron16377072021-07-08 19:00:07 +02003744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003745 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003746}
3747
3748
mohammad16035955c982018-04-26 00:53:03 +03003749/****************************************************************/
3750/* AEAD */
3751/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003753psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3754 psa_algorithm_t alg,
3755 const uint8_t *nonce,
3756 size_t nonce_length,
3757 const uint8_t *additional_data,
3758 size_t additional_data_length,
3759 const uint8_t *plaintext,
3760 size_t plaintext_length,
3761 uint8_t *ciphertext,
3762 size_t ciphertext_size,
3763 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003764{
Ronald Cron215633c2021-03-16 17:15:37 +01003765 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3766 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003767
mohammad1603f08a5502018-06-03 15:05:47 +03003768 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003770 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3771 return PSA_ERROR_NOT_SUPPORTED;
3772 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003773
Ronald Cron9a986162021-03-26 12:40:07 +01003774 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003775 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3776 if (status != PSA_SUCCESS) {
3777 return status;
3778 }
mohammad16035955c982018-04-26 00:53:03 +03003779
Ronald Cron215633c2021-03-16 17:15:37 +01003780 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003781 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003782 };
mohammad16035955c982018-04-26 00:53:03 +03003783
Ronald Cronde822812021-03-17 16:08:20 +01003784 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003785 &attributes, slot->key.data, slot->key.bytes,
3786 alg,
3787 nonce, nonce_length,
3788 additional_data, additional_data_length,
3789 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003790 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003792 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3793 memset(ciphertext, 0, ciphertext_size);
3794 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003796 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003798 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003799}
3800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003801psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3802 psa_algorithm_t alg,
3803 const uint8_t *nonce,
3804 size_t nonce_length,
3805 const uint8_t *additional_data,
3806 size_t additional_data_length,
3807 const uint8_t *ciphertext,
3808 size_t ciphertext_length,
3809 uint8_t *plaintext,
3810 size_t plaintext_size,
3811 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003812{
Ronald Cron215633c2021-03-16 17:15:37 +01003813 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3814 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003815
Gilles Peskineee652a32018-06-01 19:23:52 +02003816 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003818 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3819 return PSA_ERROR_NOT_SUPPORTED;
3820 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003821
Ronald Cron9a986162021-03-26 12:40:07 +01003822 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003823 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3824 if (status != PSA_SUCCESS) {
3825 return status;
3826 }
mohammad16035955c982018-04-26 00:53:03 +03003827
Ronald Cron215633c2021-03-16 17:15:37 +01003828 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003829 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003830 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003831
Ronald Cronde822812021-03-17 16:08:20 +01003832 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003833 &attributes, slot->key.data, slot->key.bytes,
3834 alg,
3835 nonce, nonce_length,
3836 additional_data, additional_data_length,
3837 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003838 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003840 if (status != PSA_SUCCESS && plaintext_size != 0) {
3841 memset(plaintext, 0, plaintext_size);
3842 }
mohammad160360a64d02018-06-03 17:20:42 +03003843
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003844 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003846 return status;
mohammad16035955c982018-04-26 00:53:03 +03003847}
3848
Gilles Peskinee59236f2018-01-27 23:32:46 +01003849/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003850/* Generators */
3851/****************************************************************/
3852
John Durkop07cc04a2020-11-16 22:08:34 -08003853#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3854 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3855 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3856#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003857#endif /* At least one builtin KDF */
3858
3859#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3860 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3861 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3862static psa_status_t psa_key_derivation_start_hmac(
3863 psa_mac_operation_t *operation,
3864 psa_algorithm_t hash_alg,
3865 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003866 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02003867{
3868 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3869 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003870 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3871 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
3872 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02003873
Steven Cooreman15f0d922021-05-07 14:14:37 +02003874 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003875 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02003876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003877 status = psa_driver_wrapper_mac_sign_setup(operation,
3878 &attributes,
3879 hmac_key, hmac_key_length,
3880 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02003881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003882 psa_reset_key_attributes(&attributes);
3883 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02003884}
3885#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003886
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003887#define HKDF_STATE_INIT 0 /* no input yet */
3888#define HKDF_STATE_STARTED 1 /* got salt */
3889#define HKDF_STATE_KEYED 2 /* got key */
3890#define HKDF_STATE_OUTPUT 3 /* output started */
3891
Gilles Peskinecbe66502019-05-16 16:59:18 +02003892static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003893 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003894{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003895 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
3896 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
3897 } else {
3898 return operation->alg;
3899 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003900}
3901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003902psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003903{
3904 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003905 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
3906 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02003907 /* The object has (apparently) been initialized but it is not
3908 * in use. It's ok to call abort on such an object, and there's
3909 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003910 } else
John Durkopd0321952020-10-29 21:37:36 -07003911#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003912 if (PSA_ALG_IS_HKDF(kdf_alg)) {
3913 mbedtls_free(operation->ctx.hkdf.info);
3914 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
3915 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003916#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3917#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3918 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003919 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
3920 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
3921 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
3922 if (operation->ctx.tls12_prf.secret != NULL) {
3923 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
3924 operation->ctx.tls12_prf.secret_length);
3925 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003926 }
3927
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003928 if (operation->ctx.tls12_prf.seed != NULL) {
3929 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
3930 operation->ctx.tls12_prf.seed_length);
3931 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01003932 }
3933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003934 if (operation->ctx.tls12_prf.label != NULL) {
3935 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
3936 operation->ctx.tls12_prf.label_length);
3937 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01003938 }
3939
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003940 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003941
3942 /* We leave the fields Ai and output_block to be erased safely by the
3943 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003944 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003945#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3946 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003947 {
3948 status = PSA_ERROR_BAD_STATE;
3949 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003950 mbedtls_platform_zeroize(operation, sizeof(*operation));
3951 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003952}
3953
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003954psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003955 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003956{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003957 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003958 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003959 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003960 }
3961
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003962 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003963 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003964}
3965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003966psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
3967 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003968{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003969 if (operation->alg == 0) {
3970 return PSA_ERROR_BAD_STATE;
3971 }
3972 if (capacity > operation->capacity) {
3973 return PSA_ERROR_INVALID_ARGUMENT;
3974 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003975 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003976 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003977}
3978
John Durkopd0321952020-10-29 21:37:36 -07003979#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003980/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003981 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003982static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
3983 psa_algorithm_t hash_alg,
3984 uint8_t *output,
3985 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003986{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003987 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02003988 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003989 psa_status_t status;
3990
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003991 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
3992 return PSA_ERROR_BAD_STATE;
3993 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003994 hkdf->state = HKDF_STATE_OUTPUT;
3995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003996 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003997 /* Copy what remains of the current block */
3998 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003999 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004000 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004001 }
4002 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004003 output += n;
4004 output_length -= n;
4005 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004006 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004007 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004008 }
Gilles Peskined54931c2018-07-17 21:06:59 +02004009 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004010 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004011 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004012 * object was corrupted or if this function is called directly
4013 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004014 if (hkdf->block_number == 0xff) {
4015 return PSA_ERROR_BAD_STATE;
4016 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004017
4018 /* We need a new block */
4019 ++hkdf->block_number;
4020 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004022 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4023 hash_alg,
4024 hkdf->prk,
4025 hash_length);
4026 if (status != PSA_SUCCESS) {
4027 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004028 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004029
4030 if (hkdf->block_number != 1) {
4031 status = psa_mac_update(&hkdf->hmac,
4032 hkdf->output_block,
4033 hash_length);
4034 if (status != PSA_SUCCESS) {
4035 return status;
4036 }
4037 }
4038 status = psa_mac_update(&hkdf->hmac,
4039 hkdf->info,
4040 hkdf->info_length);
4041 if (status != PSA_SUCCESS) {
4042 return status;
4043 }
4044 status = psa_mac_update(&hkdf->hmac,
4045 &hkdf->block_number, 1);
4046 if (status != PSA_SUCCESS) {
4047 return status;
4048 }
4049 status = psa_mac_sign_finish(&hkdf->hmac,
4050 hkdf->output_block,
4051 sizeof(hkdf->output_block),
4052 &hmac_output_length);
4053 if (status != PSA_SUCCESS) {
4054 return status;
4055 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004056 }
4057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004058 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004059}
John Durkop07cc04a2020-11-16 22:08:34 -08004060#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004061
John Durkop07cc04a2020-11-16 22:08:34 -08004062#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4063 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004064static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4065 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004066 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004067{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004068 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4069 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004070 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4071 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004072 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004073
Janos Follath7742fee2019-06-17 12:58:10 +01004074 /* We can't be wanting more output after block 0xff, otherwise
4075 * the capacity check in psa_key_derivation_output_bytes() would have
4076 * prevented this call. It could happen only if the operation
4077 * object was corrupted or if this function is called directly
4078 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004079 if (tls12_prf->block_number == 0xff) {
4080 return PSA_ERROR_CORRUPTION_DETECTED;
4081 }
Janos Follath7742fee2019-06-17 12:58:10 +01004082
4083 /* We need a new block */
4084 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004085 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004086
4087 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4088 *
4089 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4090 *
4091 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4092 * HMAC_hash(secret, A(2) + seed) +
4093 * HMAC_hash(secret, A(3) + seed) + ...
4094 *
4095 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004096 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004097 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004098 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004099 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004100 * is currently extracted as `output_block` and where i is
4101 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004102 */
4103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004104 status = psa_key_derivation_start_hmac(&hmac,
4105 hash_alg,
4106 tls12_prf->secret,
4107 tls12_prf->secret_length);
4108 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004109 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004110 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004111
4112 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004113 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004114 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4115 * the variable seed and in this instance means it in the context of the
4116 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004117 status = psa_mac_update(&hmac,
4118 tls12_prf->label,
4119 tls12_prf->label_length);
4120 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004121 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004122 }
4123 status = psa_mac_update(&hmac,
4124 tls12_prf->seed,
4125 tls12_prf->seed_length);
4126 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004127 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004128 }
4129 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004130 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004131 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4132 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004133 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004134 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004135 }
4136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004137 status = psa_mac_sign_finish(&hmac,
4138 tls12_prf->Ai, hash_length,
4139 &hmac_output_length);
4140 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004141 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004142 }
4143 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004144 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004145 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004146
4147 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004148 status = psa_key_derivation_start_hmac(&hmac,
4149 hash_alg,
4150 tls12_prf->secret,
4151 tls12_prf->secret_length);
4152 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004153 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004154 }
4155 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4156 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004157 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004158 }
4159 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4160 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004161 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004162 }
4163 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4164 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004165 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004166 }
4167 status = psa_mac_sign_finish(&hmac,
4168 tls12_prf->output_block, hash_length,
4169 &hmac_output_length);
4170 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004171 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004172 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004173
Janos Follath7742fee2019-06-17 12:58:10 +01004174
4175cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004176 cleanup_status = psa_mac_abort(&hmac);
4177 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004178 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004179 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004181 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004182}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004183
Janos Follath844eb0e2019-06-19 12:10:49 +01004184static psa_status_t psa_key_derivation_tls12_prf_read(
4185 psa_tls12_prf_key_derivation_t *tls12_prf,
4186 psa_algorithm_t alg,
4187 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004188 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004189{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004190 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4191 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004192 psa_status_t status;
4193 uint8_t offset, length;
4194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004195 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004196 case PSA_TLS12_PRF_STATE_LABEL_SET:
4197 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4198 break;
4199 case PSA_TLS12_PRF_STATE_OUTPUT:
4200 break;
4201 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004202 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004203 }
4204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004205 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004206 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004207 if (tls12_prf->left_in_block == 0) {
4208 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4209 alg);
4210 if (status != PSA_SUCCESS) {
4211 return status;
4212 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004213
4214 continue;
4215 }
4216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004217 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004218 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004219 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004220 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004221 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004222
4223 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004224 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004225 output += length;
4226 output_length -= length;
4227 tls12_prf->left_in_block -= length;
4228 }
4229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004230 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004231}
John Durkop07cc04a2020-11-16 22:08:34 -08004232#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4233 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004234
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004235psa_status_t psa_key_derivation_output_bytes(
4236 psa_key_derivation_operation_t *operation,
4237 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004238 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004239{
4240 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004241 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004243 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004244 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004245 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004246 }
4247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004248 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004249 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004250 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004251 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004252 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004253 goto exit;
4254 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004255 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004256 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004257 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004258 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4259 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004260 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004261 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004262 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004263 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004264 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004265
John Durkopd0321952020-10-29 21:37:36 -07004266#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004267 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4268 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4269 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4270 output, output_length);
4271 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004272#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4273#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4274 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004275 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4276 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4277 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4278 kdf_alg, output,
4279 output_length);
4280 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004281#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4282 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004283 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004284 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004285 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004286 }
4287
4288exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004289 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004290 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004291 * This allows us to differentiate between exhausted operations and
4292 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4293 * operations. */
4294 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004295 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004296 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004297 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004298 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004299 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004300}
4301
David Brown7807bf72021-02-09 16:28:23 -07004302#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004303static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004304{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004305 if (data_size >= 8) {
4306 mbedtls_des_key_set_parity(data);
4307 }
4308 if (data_size >= 16) {
4309 mbedtls_des_key_set_parity(data + 8);
4310 }
4311 if (data_size >= 24) {
4312 mbedtls_des_key_set_parity(data + 16);
4313 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004314}
David Brown7807bf72021-02-09 16:28:23 -07004315#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004316
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004317static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004318 psa_key_slot_t *slot,
4319 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004320 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004321{
4322 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004323 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004324 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004325 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004327 if (!key_type_is_raw_bytes(slot->attr.type)) {
4328 return PSA_ERROR_INVALID_ARGUMENT;
4329 }
4330 if (bits % 8 != 0) {
4331 return PSA_ERROR_INVALID_ARGUMENT;
4332 }
4333 data = mbedtls_calloc(1, bytes);
4334 if (data == NULL) {
4335 return PSA_ERROR_INSUFFICIENT_MEMORY;
4336 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004338 status = psa_key_derivation_output_bytes(operation, data, bytes);
4339 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004340 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004341 }
David Brown12ca5032021-02-11 11:02:00 -07004342#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004343 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4344 psa_des_set_key_parity(data, bytes);
4345 }
David Brown8107e312021-02-12 08:08:46 -07004346#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004348 status = psa_allocate_buffer_to_slot(slot, bytes);
4349 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004350 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004351 }
Ronald Crondd04d422020-11-28 15:14:42 +01004352
Ronald Cronbf33c932020-11-28 18:06:53 +01004353 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004354 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004355 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004356 };
4357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004358 status = psa_driver_wrapper_import_key(&attributes,
4359 data, bytes,
4360 slot->key.data,
4361 slot->key.bytes,
4362 &slot->key.bytes, &bits);
4363 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004364 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004365 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004366
4367exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004368 mbedtls_free(data);
4369 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004370}
4371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004372psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4373 psa_key_derivation_operation_t *operation,
4374 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004375{
4376 psa_status_t status;
4377 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004378 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004379
Ronald Cron81709fc2020-11-14 12:10:32 +01004380 *key = MBEDTLS_SVC_KEY_ID_INIT;
4381
Gilles Peskine0f84d622019-09-12 19:03:13 +02004382 /* Reject any attempt to create a zero-length key so that we don't
4383 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004384 if (psa_get_key_bits(attributes) == 0) {
4385 return PSA_ERROR_INVALID_ARGUMENT;
4386 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004388 if (operation->alg == PSA_ALG_NONE) {
4389 return PSA_ERROR_BAD_STATE;
4390 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004392 if (!operation->can_output_key) {
4393 return PSA_ERROR_NOT_PERMITTED;
4394 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004396 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4397 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004398#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004399 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004400 /* Deriving a key in a secure element is not implemented yet. */
4401 status = PSA_ERROR_NOT_SUPPORTED;
4402 }
4403#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004404 if (status == PSA_SUCCESS) {
4405 status = psa_generate_derived_key_internal(slot,
4406 attributes->core.bits,
4407 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004408 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004409 if (status == PSA_SUCCESS) {
4410 status = psa_finish_key_creation(slot, driver, key);
4411 }
4412 if (status != PSA_SUCCESS) {
4413 psa_fail_key_creation(slot, driver);
4414 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004415
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004416 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004417}
4418
Gilles Peskineeab56e42018-07-12 17:12:33 +02004419
4420
4421/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004422/* Key derivation */
4423/****************************************************************/
4424
Steven Cooreman932ffb72021-02-15 12:14:32 +01004425#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004426static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004427{
4428#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004429 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4430 return 1;
4431 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004432#endif
4433#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004434 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4435 return 1;
4436 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004437#endif
4438#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004439 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4440 return 1;
4441 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004442#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004443 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004444}
4445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004446static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004447{
4448 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004449 psa_status_t status = psa_hash_setup(&operation, alg);
4450 psa_hash_abort(&operation);
4451 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004452}
4453
Gilles Peskine969c5d62019-01-16 15:53:06 +01004454static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004455 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004456 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004457{
Janos Follath5fe19732019-06-20 15:09:30 +01004458 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4459 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004460 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004461
Gilles Peskine969c5d62019-01-16 15:53:06 +01004462 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004463 if (!is_kdf_alg_supported(kdf_alg)) {
4464 return PSA_ERROR_NOT_SUPPORTED;
4465 }
John Durkop07cc04a2020-11-16 22:08:34 -08004466
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004467 /* All currently supported key derivation algorithms are based on a
4468 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004469 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4470 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4471 if (hash_size == 0) {
4472 return PSA_ERROR_NOT_SUPPORTED;
4473 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004474
4475 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4476 * we might fail later, which is somewhat unfriendly and potentially
4477 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004478 psa_status_t status = psa_hash_try_support(hash_alg);
4479 if (status != PSA_SUCCESS) {
4480 return status;
4481 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004483 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4484 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4485 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4486 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004487 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004488
Gilles Peskinecdacf042021-04-29 21:10:00 +02004489 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004490 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004491}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004493static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004494{
4495#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004496 if (alg == PSA_ALG_ECDH) {
4497 return PSA_SUCCESS;
4498 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004499#endif
4500 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004501 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004502}
John Durkop07cc04a2020-11-16 22:08:34 -08004503#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004504
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004505psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4506 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004507{
4508 psa_status_t status;
4509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004510 if (operation->alg != 0) {
4511 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004512 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004514 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4515 return PSA_ERROR_INVALID_ARGUMENT;
4516 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4517#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4518 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4519 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4520 status = psa_key_agreement_try_support(ka_alg);
4521 if (status != PSA_SUCCESS) {
4522 return status;
4523 }
4524 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4525#else
4526 return PSA_ERROR_NOT_SUPPORTED;
4527#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4528 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4529#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4530 status = psa_key_derivation_setup_kdf(operation, alg);
4531#else
4532 return PSA_ERROR_NOT_SUPPORTED;
4533#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4534 } else {
4535 return PSA_ERROR_INVALID_ARGUMENT;
4536 }
4537
4538 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004539 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004540 }
4541 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004542}
4543
John Durkopd0321952020-10-29 21:37:36 -07004544#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004545static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4546 psa_algorithm_t hash_alg,
4547 psa_key_derivation_step_t step,
4548 const uint8_t *data,
4549 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004550{
4551 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004552 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004553 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004554 if (hkdf->state != HKDF_STATE_INIT) {
4555 return PSA_ERROR_BAD_STATE;
4556 } else {
4557 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4558 hash_alg,
4559 data, data_length);
4560 if (status != PSA_SUCCESS) {
4561 return status;
4562 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004563 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004564 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004565 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004566 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004567 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004568 if (hkdf->state == HKDF_STATE_INIT) {
4569 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4570 hash_alg,
4571 NULL, 0);
4572 if (status != PSA_SUCCESS) {
4573 return status;
4574 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004575 hkdf->state = HKDF_STATE_STARTED;
4576 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004577 if (hkdf->state != HKDF_STATE_STARTED) {
4578 return PSA_ERROR_BAD_STATE;
4579 }
4580 status = psa_mac_update(&hkdf->hmac,
4581 data, data_length);
4582 if (status != PSA_SUCCESS) {
4583 return status;
4584 }
4585 status = psa_mac_sign_finish(&hkdf->hmac,
4586 hkdf->prk,
4587 sizeof(hkdf->prk),
4588 &data_length);
4589 if (status != PSA_SUCCESS) {
4590 return status;
4591 }
4592 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004593 hkdf->block_number = 0;
4594 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004595 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004596 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004597 if (hkdf->state == HKDF_STATE_OUTPUT) {
4598 return PSA_ERROR_BAD_STATE;
4599 }
4600 if (hkdf->info_set) {
4601 return PSA_ERROR_BAD_STATE;
4602 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004603 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004604 if (data_length != 0) {
4605 hkdf->info = mbedtls_calloc(1, data_length);
4606 if (hkdf->info == NULL) {
4607 return PSA_ERROR_INSUFFICIENT_MEMORY;
4608 }
4609 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004610 }
4611 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004612 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004613 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004614 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004615 }
4616}
John Durkop07cc04a2020-11-16 22:08:34 -08004617#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004618
John Durkop07cc04a2020-11-16 22:08:34 -08004619#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4620 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004621static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4622 const uint8_t *data,
4623 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004624{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004625 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4626 return PSA_ERROR_BAD_STATE;
4627 }
Janos Follathf08e2652019-06-13 09:05:41 +01004628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004629 if (data_length != 0) {
4630 prf->seed = mbedtls_calloc(1, data_length);
4631 if (prf->seed == NULL) {
4632 return PSA_ERROR_INSUFFICIENT_MEMORY;
4633 }
Janos Follathf08e2652019-06-13 09:05:41 +01004634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004635 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004636 prf->seed_length = data_length;
4637 }
Janos Follathf08e2652019-06-13 09:05:41 +01004638
Gilles Peskine43f958b2020-12-13 14:55:14 +01004639 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004641 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004642}
4643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004644static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4645 const uint8_t *data,
4646 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004647{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004648 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4649 return PSA_ERROR_BAD_STATE;
4650 }
Janos Follath81550542019-06-13 14:26:34 +01004651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004652 if (data_length != 0) {
4653 prf->secret = mbedtls_calloc(1, data_length);
4654 if (prf->secret == NULL) {
4655 return PSA_ERROR_INSUFFICIENT_MEMORY;
4656 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004658 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004659 prf->secret_length = data_length;
4660 }
Janos Follath81550542019-06-13 14:26:34 +01004661
Gilles Peskine43f958b2020-12-13 14:55:14 +01004662 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004663
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004664 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004665}
4666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004667static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4668 const uint8_t *data,
4669 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004670{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004671 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4672 return PSA_ERROR_BAD_STATE;
4673 }
Janos Follath63028dd2019-06-13 09:15:47 +01004674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004675 if (data_length != 0) {
4676 prf->label = mbedtls_calloc(1, data_length);
4677 if (prf->label == NULL) {
4678 return PSA_ERROR_INSUFFICIENT_MEMORY;
4679 }
Janos Follath63028dd2019-06-13 09:15:47 +01004680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004681 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004682 prf->label_length = data_length;
4683 }
Janos Follath63028dd2019-06-13 09:15:47 +01004684
Gilles Peskine43f958b2020-12-13 14:55:14 +01004685 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004687 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01004688}
4689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004690static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
4691 psa_key_derivation_step_t step,
4692 const uint8_t *data,
4693 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01004694{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004695 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01004696 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004697 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01004698 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004699 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01004700 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004701 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01004702 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004703 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01004704 }
4705}
John Durkop07cc04a2020-11-16 22:08:34 -08004706#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4707 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4708
4709#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4710static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4711 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004712 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004713 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08004714{
4715 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004716 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08004717 uint8_t *cur = pms;
4718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004719 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
4720 return PSA_ERROR_INVALID_ARGUMENT;
4721 }
John Durkop07cc04a2020-11-16 22:08:34 -08004722
4723 /* Quoting RFC 4279, Section 2:
4724 *
4725 * The premaster secret is formed as follows: if the PSK is N octets
4726 * long, concatenate a uint16 with the value N, N zero octets, a second
4727 * uint16 with the value N, and the PSK itself.
4728 */
4729
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004730 *cur++ = MBEDTLS_BYTE_1(data_length);
4731 *cur++ = MBEDTLS_BYTE_0(data_length);
4732 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004733 cur += data_length;
4734 *cur++ = pms[0];
4735 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004736 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004737 cur += data_length;
4738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004739 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08004740
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004741 mbedtls_platform_zeroize(pms, sizeof(pms));
4742 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08004743}
Janos Follath6660f0e2019-06-17 08:44:03 +01004744
4745static psa_status_t psa_tls12_prf_psk_to_ms_input(
4746 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004747 psa_key_derivation_step_t step,
4748 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004749 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01004750{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004751 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
4752 return psa_tls12_prf_psk_to_ms_set_key(prf,
4753 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01004754 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004755
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004756 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01004757}
John Durkop07cc04a2020-11-16 22:08:34 -08004758#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004759
Gilles Peskineb8965192019-09-24 16:21:10 +02004760/** Check whether the given key type is acceptable for the given
4761 * input step of a key derivation.
4762 *
4763 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4764 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4765 * Both secret and non-secret inputs can alternatively have the type
4766 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4767 * that the input was passed as a buffer rather than via a key object.
4768 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004769static int psa_key_derivation_check_input_type(
4770 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004771 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02004772{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004773 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004774 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004775 if (key_type == PSA_KEY_TYPE_DERIVE) {
4776 return PSA_SUCCESS;
4777 }
4778 if (key_type == PSA_KEY_TYPE_NONE) {
4779 return PSA_SUCCESS;
4780 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004781 break;
4782 case PSA_KEY_DERIVATION_INPUT_LABEL:
4783 case PSA_KEY_DERIVATION_INPUT_SALT:
4784 case PSA_KEY_DERIVATION_INPUT_INFO:
4785 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004786 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
4787 return PSA_SUCCESS;
4788 }
4789 if (key_type == PSA_KEY_TYPE_NONE) {
4790 return PSA_SUCCESS;
4791 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004792 break;
4793 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004794 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02004795}
4796
Janos Follathb80a94e2019-06-12 15:54:46 +01004797static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004798 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004799 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004800 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004801 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004802 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004803{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004804 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004805 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004807 status = psa_key_derivation_check_input_type(step, key_type);
4808 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004809 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004810 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004811
John Durkopd0321952020-10-29 21:37:36 -07004812#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004813 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4814 status = psa_hkdf_input(&operation->ctx.hkdf,
4815 PSA_ALG_HKDF_GET_HASH(kdf_alg),
4816 step, data, data_length);
4817 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004818#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4819#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004820 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4821 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
4822 step, data, data_length);
4823 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004824#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4825#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004826 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4827 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
4828 step, data, data_length);
4829 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004830#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004831 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004832 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004833 (void) data;
4834 (void) data_length;
4835 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004836 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004837 }
4838
Gilles Peskine224b0d62019-09-23 18:13:17 +02004839exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004840 if (status != PSA_SUCCESS) {
4841 psa_key_derivation_abort(operation);
4842 }
4843 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004844}
4845
Janos Follath51f4a0f2019-06-14 11:35:55 +01004846psa_status_t psa_key_derivation_input_bytes(
4847 psa_key_derivation_operation_t *operation,
4848 psa_key_derivation_step_t step,
4849 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004850 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004851{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004852 return psa_key_derivation_input_internal(operation, step,
4853 PSA_KEY_TYPE_NONE,
4854 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004855}
4856
Janos Follath51f4a0f2019-06-14 11:35:55 +01004857psa_status_t psa_key_derivation_input_key(
4858 psa_key_derivation_operation_t *operation,
4859 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004860 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004861{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004862 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004863 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004864 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004865
Ronald Cron5c522922020-11-14 16:35:34 +01004866 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004867 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4868 if (status != PSA_SUCCESS) {
4869 psa_key_derivation_abort(operation);
4870 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02004871 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004872
4873 /* Passing a key object as a SECRET input unlocks the permission
4874 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004875 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004876 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004877 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004879 status = psa_key_derivation_input_internal(operation,
4880 step, slot->attr.type,
4881 slot->key.data,
4882 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004884 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004886 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004887}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004888
4889
4890
4891/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004892/* Key agreement */
4893/****************************************************************/
4894
John Durkop6ba40d12020-11-10 08:50:04 -08004895#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004896static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
4897 size_t peer_key_length,
4898 const mbedtls_ecp_keypair *our_key,
4899 uint8_t *shared_secret,
4900 size_t shared_secret_size,
4901 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004902{
Steven Cooremand4867872020-08-05 16:31:39 +02004903 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004904 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004905 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004906 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004907 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
4908 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004909
Ronald Cron90857082020-11-25 14:58:33 +01004910 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004911 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
4912 bits,
4913 peer_key,
4914 peer_key_length,
4915 &their_key);
4916 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004917 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004918 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004919
Jaeden Amero97271b32019-01-10 19:38:51 +00004920 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004921 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
4922 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004923 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004924 }
Jaeden Amero97271b32019-01-10 19:38:51 +00004925 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004926 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
4927 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004928 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004929 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004930
Jaeden Amero97271b32019-01-10 19:38:51 +00004931 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004932 mbedtls_ecdh_calc_secret(&ecdh,
4933 shared_secret_length,
4934 shared_secret, shared_secret_size,
4935 mbedtls_psa_get_random,
4936 MBEDTLS_PSA_RANDOM_STATE));
4937 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004938 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004939 }
4940 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004941 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004942 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004943
4944exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004945 if (status != PSA_SUCCESS) {
4946 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
4947 }
4948 mbedtls_ecdh_free(&ecdh);
4949 mbedtls_ecp_keypair_free(their_key);
4950 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02004951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004952 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004953}
John Durkop6ba40d12020-11-10 08:50:04 -08004954#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004955
Gilles Peskine01d718c2018-09-18 12:01:02 +02004956#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4957
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004958static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
4959 psa_key_slot_t *private_key,
4960 const uint8_t *peer_key,
4961 size_t peer_key_length,
4962 uint8_t *shared_secret,
4963 size_t shared_secret_size,
4964 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004965{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004966 mbedtls_ecp_keypair *ecp = NULL;
4967 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01004968
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004969 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08004970#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004971 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004972 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
4973 return PSA_ERROR_INVALID_ARGUMENT;
4974 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004975 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004976 private_key->attr.type,
4977 private_key->attr.bits,
4978 private_key->key.data,
4979 private_key->key.bytes,
4980 &ecp);
4981 if (status != PSA_SUCCESS) {
4982 return status;
4983 }
4984 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
4985 ecp,
4986 shared_secret, shared_secret_size,
4987 shared_secret_length);
4988 mbedtls_ecp_keypair_free(ecp);
4989 mbedtls_free(ecp);
4990 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08004991#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004992 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004993 (void) ecp;
4994 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01004995 (void) private_key;
4996 (void) peer_key;
4997 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004998 (void) shared_secret;
4999 (void) shared_secret_size;
5000 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005001 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005002 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005003}
5004
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005005/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005006 * to potentially free embedded data structures and wipe confidential data.
5007 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005008static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5009 psa_key_derivation_step_t step,
5010 psa_key_slot_t *private_key,
5011 const uint8_t *peer_key,
5012 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005013{
5014 psa_status_t status;
5015 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5016 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005017 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005018
5019 /* Step 1: run the secret agreement algorithm to generate the shared
5020 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005021 status = psa_key_agreement_raw_internal(ka_alg,
5022 private_key,
5023 peer_key, peer_key_length,
5024 shared_secret,
5025 sizeof(shared_secret),
5026 &shared_secret_length);
5027 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005028 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005029 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005030
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005031 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005032 * the shared secret. A shared secret is permitted wherever a key
5033 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005034 status = psa_key_derivation_input_internal(operation, step,
5035 PSA_KEY_TYPE_DERIVE,
5036 shared_secret,
5037 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005038exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005039 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5040 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005041}
5042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005043psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5044 psa_key_derivation_step_t step,
5045 mbedtls_svc_key_id_t private_key,
5046 const uint8_t *peer_key,
5047 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005048{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005049 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005050 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005051 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005053 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5054 return PSA_ERROR_INVALID_ARGUMENT;
5055 }
Ronald Cron5c522922020-11-14 16:35:34 +01005056 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005057 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5058 if (status != PSA_SUCCESS) {
5059 return status;
5060 }
5061 status = psa_key_agreement_internal(operation, step,
5062 slot,
5063 peer_key, peer_key_length);
5064 if (status != PSA_SUCCESS) {
5065 psa_key_derivation_abort(operation);
5066 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005067 /* If a private key has been added as SECRET, we allow the derived
5068 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005069 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005070 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005071 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005072 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005074 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005076 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005077}
5078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005079psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5080 mbedtls_svc_key_id_t private_key,
5081 const uint8_t *peer_key,
5082 size_t peer_key_length,
5083 uint8_t *output,
5084 size_t output_size,
5085 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005086{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005087 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005088 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005089 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005090 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005092 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005093 status = PSA_ERROR_INVALID_ARGUMENT;
5094 goto exit;
5095 }
Ronald Cron5c522922020-11-14 16:35:34 +01005096 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005097 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5098 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005099 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005100 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005101
Gilles Peskine42313fb2022-04-14 00:17:15 +02005102 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5103 * for the output size. The PSA specification only guarantees that this
5104 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5105 * but it might be nice to allow smaller buffers if the output fits.
5106 * At the time of writing this comment, with only ECDH implemented,
5107 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5108 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5109 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005110 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005111 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5112 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005113 status = PSA_ERROR_BUFFER_TOO_SMALL;
5114 goto exit;
5115 }
5116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005117 status = psa_key_agreement_raw_internal(alg, slot,
5118 peer_key, peer_key_length,
5119 output, output_size,
5120 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005121
5122exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005123 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005124 /* If an error happens and is not handled properly, the output
5125 * may be used as a key to protect sensitive data. Arrange for such
5126 * a key to be random, which is likely to result in decryption or
5127 * verification errors. This is better than filling the buffer with
5128 * some constant data such as zeros, which would result in the data
5129 * being protected with a reproducible, easily knowable key.
5130 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005131 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005132 *output_length = output_size;
5133 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005135 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005137 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005138}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005139
5140
Gilles Peskine30524eb2020-11-13 17:02:26 +01005141
Gilles Peskine86a440b2018-11-12 18:39:40 +01005142/****************************************************************/
5143/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005144/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005145
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005146#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5147#include "mbedtls/entropy_poll.h"
5148#endif
5149
Gilles Peskine30524eb2020-11-13 17:02:26 +01005150/** Initialize the PSA random generator.
5151 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005152static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005153{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005154#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005155 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005156#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5157
Gilles Peskine30524eb2020-11-13 17:02:26 +01005158 /* Set default configuration if
5159 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005160 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005161 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005162 }
5163 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005164 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005165 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005167 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005168#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5169 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5170 /* The PSA entropy injection feature depends on using NV seed as an entropy
5171 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005172 mbedtls_entropy_add_source(&rng->entropy,
5173 mbedtls_nv_seed_poll, NULL,
5174 MBEDTLS_ENTROPY_BLOCK_SIZE,
5175 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005176#endif
5177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005178 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005179#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005180}
5181
5182/** Deinitialize the PSA random generator.
5183 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005184static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005185{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005186#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005187 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005188#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005189 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5190 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005191#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005192}
5193
5194/** Seed the PSA random generator.
5195 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005196static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005197{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005198#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5199 /* Do nothing: the external RNG seeds itself. */
5200 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005201 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005202#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005203 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005204 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5205 drbg_seed, sizeof(drbg_seed) - 1);
5206 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005207#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005208}
5209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005210psa_status_t psa_generate_random(uint8_t *output,
5211 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005212{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005213 GUARD_MODULE_INITIALIZED;
5214
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005215#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5216
5217 size_t output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005218 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
5219 output, output_size,
5220 &output_length);
5221 if (status != PSA_SUCCESS) {
5222 return status;
5223 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005224 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00005225 * for the external RNG interface. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005226 if (output_length != output_size) {
5227 return PSA_ERROR_INSUFFICIENT_ENTROPY;
5228 }
5229 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005230
5231#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005233 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005234 size_t request_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005235 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5236 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5237 output_size);
5238 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
5239 output, request_size);
5240 if (ret != 0) {
5241 return mbedtls_to_psa_error(ret);
5242 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01005243 output_size -= request_size;
5244 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005245 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005246 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005247#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005248}
5249
Gilles Peskine8814fc42020-12-14 15:33:44 +01005250/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005251 *
5252 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5253 * `psa_generate_random(...)`. The state parameter is ignored since the
5254 * PSA API doesn't support passing an explicit state.
5255 *
5256 * In the non-external case, psa_generate_random() calls an
5257 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5258 * and semantics as mbedtls_psa_get_random(). As an optimization,
5259 * instead of doing this back-and-forth between the PSA API and the
5260 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5261 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005262 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005263#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5264int mbedtls_psa_get_random(void *p_rng,
5265 unsigned char *output,
5266 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005267{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005268 /* This function takes a pointer to the RNG state because that's what
5269 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5270 * its own state internally and doesn't let the caller access that state.
5271 * So we just ignore the state parameter, and in practice we'll pass
5272 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005273 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005274 psa_status_t status = psa_generate_random(output, output_size);
5275 if (status == PSA_SUCCESS) {
5276 return 0;
5277 } else {
5278 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5279 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005280}
5281#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5282
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005283#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005284psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5285 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005286{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005287 if (global_data.initialized) {
5288 return PSA_ERROR_NOT_PERMITTED;
5289 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005291 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5292 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5293 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5294 return PSA_ERROR_INVALID_ARGUMENT;
5295 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005297 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005298}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005299#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005300
Ronald Cron3772afe2021-02-08 16:10:05 +01005301/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005302 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005303 * \param type The key type
5304 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005305 *
5306 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005307 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005308 * \retval #PSA_ERROR_INVALID_ARGUMENT
5309 * The size in bits of the key is not valid.
5310 * \retval #PSA_ERROR_NOT_SUPPORTED
5311 * The type and/or the size in bits of the key or the combination of
5312 * the two is not supported.
5313 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005314static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005315 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005316{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005317 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005319 if (key_type_is_raw_bytes(type)) {
5320 status = validate_unstructured_key_bit_size(type, bits);
5321 if (status != PSA_SUCCESS) {
5322 return status;
5323 }
5324 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005325#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005326 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5327 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5328 return PSA_ERROR_NOT_SUPPORTED;
5329 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005330
Ronald Cron01b2aba2020-10-05 09:42:02 +02005331 /* Accept only byte-aligned keys, for the same reasons as
5332 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005333 if (bits % 8 != 0) {
5334 return PSA_ERROR_NOT_SUPPORTED;
5335 }
5336 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005337#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005338
Ronald Cron5c4d3862020-12-07 11:07:24 +01005339#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005340 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005341 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005342 return PSA_SUCCESS;
5343 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005344#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005345 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005346 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005347 }
5348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005349 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005350}
5351
Ronald Cron55ed0592020-10-05 10:30:40 +02005352psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005353 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005354 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005355{
5356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005357 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005359 if ((attributes->domain_parameters == NULL) &&
5360 (attributes->domain_parameters_size != 0)) {
5361 return PSA_ERROR_INVALID_ARGUMENT;
5362 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005364 if (key_type_is_raw_bytes(type)) {
5365 status = psa_generate_random(key_buffer, key_buffer_size);
5366 if (status != PSA_SUCCESS) {
5367 return status;
5368 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005369
David Brown12ca5032021-02-11 11:02:00 -07005370#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005371 if (type == PSA_KEY_TYPE_DES) {
5372 psa_des_set_key_parity(key_buffer, key_buffer_size);
5373 }
David Brown8107e312021-02-12 08:08:46 -07005374#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005375 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005376
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005377#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5378 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005379 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5380 return mbedtls_psa_rsa_generate_key(attributes,
5381 key_buffer,
5382 key_buffer_size,
5383 key_buffer_length);
5384 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005385#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5386 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005387
John Durkop9814fa22020-11-04 12:28:15 -08005388#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005389 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5390 return mbedtls_psa_ecp_generate_key(attributes,
5391 key_buffer,
5392 key_buffer_size,
5393 key_buffer_length);
5394 } else
John Durkop9814fa22020-11-04 12:28:15 -08005395#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005396 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005397 (void) key_buffer_length;
5398 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005399 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005401 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005402}
Darryl Green0c6575a2018-11-07 16:05:30 +00005403
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005404psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5405 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005406{
5407 psa_status_t status;
5408 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005409 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005410 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005411
Ronald Cron81709fc2020-11-14 12:10:32 +01005412 *key = MBEDTLS_SVC_KEY_ID_INIT;
5413
Gilles Peskine0f84d622019-09-12 19:03:13 +02005414 /* Reject any attempt to create a zero-length key so that we don't
5415 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005416 if (psa_get_key_bits(attributes) == 0) {
5417 return PSA_ERROR_INVALID_ARGUMENT;
5418 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005419
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005420 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005421 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5422 return PSA_ERROR_INVALID_ARGUMENT;
5423 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005425 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5426 &slot, &driver);
5427 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005428 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005429 }
Gilles Peskine11792082019-08-06 18:36:36 +02005430
Ronald Cron977c2472020-10-13 08:32:21 +02005431 /* In the case of a transparent key or an opaque key stored in local
5432 * storage (thus not in the case of generating a key in a secure element
5433 * or cryptoprocessor with storage), we have to allocate a buffer to
5434 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005435 if (slot->key.data == NULL) {
5436 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5437 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005438 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005439 attributes->core.type, attributes->core.bits);
5440 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005441 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005442 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005443
5444 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005445 attributes->core.type,
5446 attributes->core.bits);
5447 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005448 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005449 attributes, &key_buffer_size);
5450 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005451 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005452 }
Ronald Cron977c2472020-10-13 08:32:21 +02005453 }
Ronald Cron977c2472020-10-13 08:32:21 +02005454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005455 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5456 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005457 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005458 }
Ronald Cron977c2472020-10-13 08:32:21 +02005459 }
5460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005461 status = psa_driver_wrapper_generate_key(attributes,
5462 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005464 if (status != PSA_SUCCESS) {
5465 psa_remove_key_data_from_memory(slot);
5466 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005467
Gilles Peskine11792082019-08-06 18:36:36 +02005468exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005469 if (status == PSA_SUCCESS) {
5470 status = psa_finish_key_creation(slot, driver, key);
5471 }
5472 if (status != PSA_SUCCESS) {
5473 psa_fail_key_creation(slot, driver);
5474 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005476 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005477}
5478
Gilles Peskinee59236f2018-01-27 23:32:46 +01005479/****************************************************************/
5480/* Module setup */
5481/****************************************************************/
5482
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005483#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005484psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005485 void (* entropy_init)(mbedtls_entropy_context *ctx),
5486 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005487{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005488 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5489 return PSA_ERROR_BAD_STATE;
5490 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005491 global_data.rng.entropy_init = entropy_init;
5492 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005493 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005494}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005495#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005497void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005498{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005499 psa_wipe_all_key_slots();
5500 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5501 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005502 }
5503 /* Wipe all remaining data, including configuration.
5504 * In particular, this sets all state indicator to the value
5505 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005506 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005507
5508 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005509 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005510}
5511
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005512#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5513/** Recover a transaction that was interrupted by a power failure.
5514 *
5515 * This function is called during initialization, before psa_crypto_init()
5516 * returns. If this function returns a failure status, the initialization
5517 * fails.
5518 */
5519static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005520 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005521{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005522 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005523 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5524 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005525 /* TODO - fall through to the failure case until this
5526 * is implemented.
5527 * https://github.com/ARMmbed/mbed-crypto/issues/218
5528 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005529 default:
5530 /* We found an unsupported transaction in the storage.
5531 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005532 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005533 }
5534}
5535#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005537psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005538{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005539 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005540
Gilles Peskinec6b69072018-11-20 21:42:52 +01005541 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005542 if (global_data.initialized != 0) {
5543 return PSA_SUCCESS;
5544 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005545
Gilles Peskine30524eb2020-11-13 17:02:26 +01005546 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005547 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005548 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005549 status = mbedtls_psa_random_seed(&global_data.rng);
5550 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005551 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005552 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005553 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005555 status = psa_initialize_key_slots();
5556 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005557 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005558 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005559
Ronald Cron088d5d02021-04-10 16:57:30 +02005560 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005561 status = psa_driver_wrapper_init();
5562 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005563 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005564 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005565
Gilles Peskine4b734222019-07-24 15:56:31 +02005566#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005567 status = psa_crypto_load_transaction();
5568 if (status == PSA_SUCCESS) {
5569 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5570 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005571 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005572 }
5573 status = psa_crypto_stop_transaction();
5574 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005575 /* There's no transaction to complete. It's all good. */
5576 status = PSA_SUCCESS;
5577 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005578#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005579
Gilles Peskinec6b69072018-11-20 21:42:52 +01005580 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005581 global_data.initialized = 1;
5582
5583exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005584 if (status != PSA_SUCCESS) {
5585 mbedtls_psa_crypto_free();
5586 }
5587 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005588}
5589
David Horstmann114d8242023-12-07 18:39:17 +00005590/* Memory copying test hooks. These are called before input copy, after input
5591 * copy, before output copy and after output copy, respectively.
5592 * They are used by memory-poisoning tests to temporarily unpoison buffers
5593 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00005594#if defined(MBEDTLS_TEST_HOOKS)
5595void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5596void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5597void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5598void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5599#endif
5600
David Horstmanndf493552023-11-15 15:18:30 +00005601/** Copy from an input buffer to a local copy.
5602 *
5603 * \param[in] input Pointer to input buffer.
5604 * \param[in] input_len Length of the input buffer.
5605 * \param[out] input_copy Pointer to a local copy in which to store the input data.
5606 * \param[out] input_copy_len Length of the local copy buffer.
5607 * \return #PSA_SUCCESS, if the buffer was successfully
5608 * copied.
5609 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
5610 * copy is too small to hold contents of the
5611 * input buffer.
5612 */
5613MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00005614psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
5615 uint8_t *input_copy, size_t input_copy_len)
5616{
5617 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00005618 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00005619 }
5620
David Horstmann0760b152023-11-24 16:21:04 +00005621#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005622 if (psa_input_pre_copy_hook != NULL) {
5623 psa_input_pre_copy_hook(input, input_len);
5624 }
David Horstmann0760b152023-11-24 16:21:04 +00005625#endif
5626
David Horstmann660027f2023-11-15 17:33:47 +00005627 if (input_len > 0) {
5628 memcpy(input_copy, input, input_len);
5629 }
David Horstmann957f9802023-10-30 20:29:43 +00005630
David Horstmann0760b152023-11-24 16:21:04 +00005631#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005632 if (psa_input_post_copy_hook != NULL) {
5633 psa_input_post_copy_hook(input, input_len);
5634 }
David Horstmann0760b152023-11-24 16:21:04 +00005635#endif
5636
David Horstmann957f9802023-10-30 20:29:43 +00005637 return PSA_SUCCESS;
5638}
5639
David Horstmanndf493552023-11-15 15:18:30 +00005640/** Copy from a local output buffer into a user-supplied one.
5641 *
5642 * \param[in] output_copy Pointer to a local buffer containing the output.
5643 * \param[in] output_copy_len Length of the local buffer.
5644 * \param[out] output Pointer to user-supplied output buffer.
5645 * \param[out] output_len Length of the user-supplied output buffer.
5646 * \return #PSA_SUCCESS, if the buffer was successfully
5647 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00005648 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00005649 * user-supplied output buffer is too small to
5650 * hold the contents of the local buffer.
5651 */
5652MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00005653psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
5654 uint8_t *output, size_t output_len)
5655{
5656 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00005657 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00005658 }
David Horstmann660027f2023-11-15 17:33:47 +00005659
David Horstmann0760b152023-11-24 16:21:04 +00005660#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005661 if (psa_output_pre_copy_hook != NULL) {
5662 psa_output_pre_copy_hook(output, output_len);
5663 }
David Horstmann0760b152023-11-24 16:21:04 +00005664#endif
5665
David Horstmann660027f2023-11-15 17:33:47 +00005666 if (output_copy_len > 0) {
5667 memcpy(output, output_copy, output_copy_len);
5668 }
5669
David Horstmann0760b152023-11-24 16:21:04 +00005670#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005671 if (psa_output_post_copy_hook != NULL) {
5672 psa_output_post_copy_hook(output, output_len);
5673 }
David Horstmann0760b152023-11-24 16:21:04 +00005674#endif
5675
David Horstmann2bd296e2023-10-30 20:37:12 +00005676 return PSA_SUCCESS;
5677}
5678
David Horstmann81a0d572023-11-20 17:15:32 +00005679psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
5680 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005681{
5682 psa_status_t status;
5683
David Horstmann0d52c712023-11-23 15:50:37 +00005684 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005685
David Horstmann726bf052023-11-15 18:11:26 +00005686 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005687 return PSA_SUCCESS;
5688 }
5689
David Horstmann81a0d572023-11-20 17:15:32 +00005690 local_input->buffer = mbedtls_calloc(input_len, 1);
5691 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005692 /* Since we dealt with the zero-length case above, we know that
5693 * a NULL return value means a failure of allocation. */
5694 return PSA_ERROR_INSUFFICIENT_MEMORY;
5695 }
David Horstmann81a0d572023-11-20 17:15:32 +00005696 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005697
David Horstmann81a0d572023-11-20 17:15:32 +00005698 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005699
5700 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00005701 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005702 if (status != PSA_SUCCESS) {
5703 goto error;
5704 }
5705
5706 return PSA_SUCCESS;
5707
5708error:
David Horstmann81a0d572023-11-20 17:15:32 +00005709 mbedtls_free(local_input->buffer);
5710 local_input->buffer = NULL;
5711 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005712 return status;
5713}
5714
David Horstmann81a0d572023-11-20 17:15:32 +00005715void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00005716{
David Horstmann81a0d572023-11-20 17:15:32 +00005717 mbedtls_free(local_input->buffer);
5718 local_input->buffer = NULL;
5719 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00005720}
5721
David Horstmann1a76ab12023-11-20 12:54:09 +00005722psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
5723 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00005724{
David Horstmann0d52c712023-11-23 15:50:37 +00005725 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00005726
David Horstmann726bf052023-11-15 18:11:26 +00005727 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005728 return PSA_SUCCESS;
5729 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005730 local_output->buffer = mbedtls_calloc(output_len, 1);
5731 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005732 /* Since we dealt with the zero-length case above, we know that
5733 * a NULL return value means a failure of allocation. */
5734 return PSA_ERROR_INSUFFICIENT_MEMORY;
5735 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005736 local_output->length = output_len;
5737 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00005738
5739 return PSA_SUCCESS;
5740}
5741
David Horstmann1a76ab12023-11-20 12:54:09 +00005742psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00005743{
David Horstmann761761f2023-11-15 15:57:32 +00005744 psa_status_t status;
5745
David Horstmann1a76ab12023-11-20 12:54:09 +00005746 if (local_output->buffer == NULL) {
5747 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005748 return PSA_SUCCESS;
5749 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005750 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00005751 /* We have an internal copy but nothing to copy back to. */
5752 return PSA_ERROR_CORRUPTION_DETECTED;
5753 }
5754
David Horstmann1a76ab12023-11-20 12:54:09 +00005755 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
5756 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00005757 if (status != PSA_SUCCESS) {
5758 return status;
5759 }
David Horstmannaeeb2742023-11-08 17:44:18 +00005760
David Horstmann1a76ab12023-11-20 12:54:09 +00005761 mbedtls_free(local_output->buffer);
5762 local_output->buffer = NULL;
5763 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005764
5765 return PSA_SUCCESS;
5766}
5767
Gilles Peskinee59236f2018-01-27 23:32:46 +01005768#endif /* MBEDTLS_PSA_CRYPTO_C */