blob: 3ca3be1cb8c04cb4785c444daa777550b5eb3a81 [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 Horstmann926193a2023-12-13 15:55:25 +0000110
David Horstmannb80e35a2023-12-14 18:12:47 +0000111/* Declare a local copy of an input buffer and a variable that will be used
112 * to store a pointer to the start of the buffer.
David Horstmann926193a2023-12-13 15:55:25 +0000113 *
114 * Note: This macro must be called before any operations which may jump to
115 * the exit label, so that the local input copy object is safe to be freed.
116 *
117 * Assumptions:
118 * - input is the name of a pointer to the buffer to be copied
119 * - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope
David Horstmannb80e35a2023-12-14 18:12:47 +0000120 * - input_copy_name is a name that is unused in the current scope
David Horstmann926193a2023-12-13 15:55:25 +0000121 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000122#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
123 psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT; \
124 const uint8_t *input_copy_name = NULL;
David Horstmann926193a2023-12-13 15:55:25 +0000125
David Horstmannb80e35a2023-12-14 18:12:47 +0000126/* Allocate a copy of the buffer input and set the pointer input_copy to
127 * point to the start of the copy.
David Horstmann926193a2023-12-13 15:55:25 +0000128 *
David Horstmann34980bd2023-11-29 17:07:13 +0000129 * Assumptions:
130 * - psa_status_t status exists
131 * - An exit label is declared
David Horstmann926193a2023-12-13 15:55:25 +0000132 * - input is the name of a pointer to the buffer to be copied
David Horstmannb80e35a2023-12-14 18:12:47 +0000133 * - LOCAL_INPUT_DECLARE(input, input_copy) has previously been called
David Horstmann34980bd2023-11-29 17:07:13 +0000134 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000135#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
David Horstmann926193a2023-12-13 15:55:25 +0000136 status = psa_crypto_local_input_alloc(input, length, \
137 &LOCAL_INPUT_COPY_OF_##input); \
David Horstmann34980bd2023-11-29 17:07:13 +0000138 if (status != PSA_SUCCESS) { \
139 goto exit; \
140 } \
David Horstmannb80e35a2023-12-14 18:12:47 +0000141 input_copy = LOCAL_INPUT_COPY_OF_##input.buffer;
David Horstmann34980bd2023-11-29 17:07:13 +0000142
David Horstmann926193a2023-12-13 15:55:25 +0000143/* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC()
144 *
David Horstmann1f532132023-12-08 14:08:18 +0000145 * Assumptions:
David Horstmannb80e35a2023-12-14 18:12:47 +0000146 * - input_copy is the name of the input copy pointer set by LOCAL_INPUT_ALLOC()
David Horstmann926193a2023-12-13 15:55:25 +0000147 * - input is the name of the original buffer that was copied
David Horstmann1f532132023-12-08 14:08:18 +0000148 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000149#define LOCAL_INPUT_FREE(input, input_copy) \
150 input_copy = NULL; \
David Horstmann926193a2023-12-13 15:55:25 +0000151 psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input);
David Horstmann34980bd2023-11-29 17:07:13 +0000152
David Horstmannb80e35a2023-12-14 18:12:47 +0000153/* Declare a local copy of an output buffer and a variable that will be used
154 * to store a pointer to the start of the buffer.
David Horstmann926193a2023-12-13 15:55:25 +0000155 *
156 * Note: This macro must be called before any operations which may jump to
157 * the exit label, so that the local output copy object is safe to be freed.
158 *
159 * Assumptions:
160 * - output is the name of a pointer to the buffer to be copied
161 * - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope
David Horstmannb80e35a2023-12-14 18:12:47 +0000162 * - output_copy_name is a name that is unused in the current scope
David Horstmann926193a2023-12-13 15:55:25 +0000163 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000164#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
165 psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
166 uint8_t *output_copy_name = NULL;
David Horstmann926193a2023-12-13 15:55:25 +0000167
David Horstmannb80e35a2023-12-14 18:12:47 +0000168/* Allocate a copy of the buffer output and set the pointer output_copy to
169 * point to the start of the copy.
David Horstmann926193a2023-12-13 15:55:25 +0000170 *
David Horstmann34980bd2023-11-29 17:07:13 +0000171 * Assumptions:
172 * - psa_status_t status exists
173 * - An exit label is declared
David Horstmann926193a2023-12-13 15:55:25 +0000174 * - output is the name of a pointer to the buffer to be copied
David Horstmannb80e35a2023-12-14 18:12:47 +0000175 * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called
David Horstmann34980bd2023-11-29 17:07:13 +0000176 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000177#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
David Horstmann926193a2023-12-13 15:55:25 +0000178 status = psa_crypto_local_output_alloc(output, length, \
179 &LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmann34980bd2023-11-29 17:07:13 +0000180 if (status != PSA_SUCCESS) { \
181 goto exit; \
182 } \
David Horstmannb80e35a2023-12-14 18:12:47 +0000183 output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer;
David Horstmann34980bd2023-11-29 17:07:13 +0000184
David Horstmannb80e35a2023-12-14 18:12:47 +0000185/* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC()
David Horstmann1f532132023-12-08 14:08:18 +0000186 * after first copying back its contents to the original buffer.
David Horstmann926193a2023-12-13 15:55:25 +0000187 *
David Horstmann1f532132023-12-08 14:08:18 +0000188 * Assumptions:
David Horstmann1f532132023-12-08 14:08:18 +0000189 * - psa_status_t status exists
David Horstmannb80e35a2023-12-14 18:12:47 +0000190 * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC()
191 * - output is the name of the original buffer that was copied
David Horstmann1f532132023-12-08 14:08:18 +0000192 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000193#define LOCAL_OUTPUT_FREE(output, output_copy) \
194 output_copy = NULL; \
David Horstmann2b70a662023-12-13 14:09:08 +0000195 do { \
196 psa_status_t local_output_status; \
David Horstmann926193a2023-12-13 15:55:25 +0000197 local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmann2b70a662023-12-13 14:09:08 +0000198 if (local_output_status != PSA_SUCCESS) { \
199 /* Since this error case is an internal error, it's more serious than \
200 * any existing error code and so it's fine to overwrite the existing \
201 * status. */ \
202 status = local_output_status; \
203 } \
204 } while (0)
David Horstmannc0a2c302023-11-29 17:24:08 +0000205#else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmannb80e35a2023-12-14 18:12:47 +0000206#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
207 const uint8_t *input_copy_name = NULL;
208#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
209 input_copy = input;
210#define LOCAL_INPUT_FREE(input, input_copy) \
211 input_copy = NULL;
212#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
213 uint8_t *output_copy_name = NULL;
214#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
215 output_copy = output;
216#define LOCAL_OUTPUT_FREE(output, output_copy) \
217 output_copy = NULL;
David Horstmannc0a2c302023-11-29 17:24:08 +0000218#endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmann34980bd2023-11-29 17:07:13 +0000219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100221{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100222 /* Mbed TLS error codes can combine a high-level error code and a
223 * low-level error code. The low-level error usually reflects the
224 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 int low_level_ret = -(-ret & 0x007f);
226 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100227 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
230 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
231 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
232 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100236
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200237 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200239
Gilles Peskine9a944802018-06-21 09:35:35 +0200240 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
241 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
242 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
243 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
244 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200246 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200248 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200250
Jaeden Amero93e21112019-02-20 13:57:28 +0000251#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000252 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000253#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
254 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
255#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100256 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100258 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100260
Jaeden Amero93e21112019-02-20 13:57:28 +0000261#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000262 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000263#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
264 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
265#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100268 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100270
271 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100273 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100275 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100277
Gilles Peskine26869f22019-05-06 15:25:00 +0200278 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200280
281 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200283 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200285
Gilles Peskinea5905292018-02-07 20:59:33 +0100286 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100290 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100300 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100302
303 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
307 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100308 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
309 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100312 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
313 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100317#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100318
319 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100321 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100323
Gilles Peskinee59236f2018-01-27 23:32:46 +0100324 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
325 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
326 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100328
329 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100331 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100333 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100335
Gilles Peskine82e57d12020-11-13 21:31:17 +0100336#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100338 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
339 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100340 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100342 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
343 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100345 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100347#endif
348
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200349 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
350 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
351 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200353
Gilles Peskinea5905292018-02-07 20:59:33 +0100354 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100356 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100358 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100360 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200362 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100364
Gilles Peskinef76aa772018-10-29 19:24:33 +0100365 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100367 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100369 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100371 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100373 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100375 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100377 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100379 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100381
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100382 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100384 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
385 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100387 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100389 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
390 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100392 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100394 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
395 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100396 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100397 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100399 case MBEDTLS_ERR_PK_INVALID_ALG:
400 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
401 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100403 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100405 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100407
Gilles Peskineff2d2002019-05-06 15:26:23 +0200408 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200410 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200412
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200413 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200415
Gilles Peskinea5905292018-02-07 20:59:33 +0100416 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100418 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100420 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100422 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100424 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
425 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100427 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100429 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100431 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100433 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100435 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100437
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200438 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
439 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
440 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200442
Gilles Peskinea5905292018-02-07 20:59:33 +0100443 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100445 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100447
itayzafrir5c753392018-05-08 11:18:38 +0300448 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300449 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300451 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300453 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300455 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
456 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300458 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100460 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300462 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100464
Janos Follatha13b9052019-11-22 12:48:59 +0000465 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100466 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300467
Gilles Peskinee59236f2018-01-27 23:32:46 +0100468 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100470 }
471}
472
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200473
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200474
475
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100476/****************************************************************/
477/* Key management */
478/****************************************************************/
479
John Durkop9814fa22020-11-04 12:28:15 -0800480#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800481 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100482 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
483 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
484 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
486 size_t bits,
487 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200488{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100490 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100492#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100493 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100494 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100495#endif
496#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100497 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100498 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100499#endif
500#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100501 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100503#endif
504#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100505 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100507#endif
508#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100509 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100511 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100512 if (bits_is_sloppy) {
513 return MBEDTLS_ECP_DP_SECP521R1;
514 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100515 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100516#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100517 }
518 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100519
Paul Elliott8ff510a2020-06-02 17:19:28 +0100520 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100521 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100522#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100523 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100525#endif
526#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100527 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100529#endif
530#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100531 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100533#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100534 }
535 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100536
Paul Elliott8ff510a2020-06-02 17:19:28 +0100537 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100539#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100540 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100542 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100543 if (bits_is_sloppy) {
544 return MBEDTLS_ECP_DP_CURVE25519;
545 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100546 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100547#endif
548#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100549 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100551#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100552 }
553 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100554
Paul Elliott8ff510a2020-06-02 17:19:28 +0100555 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100557#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100558 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100559 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100560#endif
561#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100562 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100564#endif
565#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100566 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100568#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100569 }
570 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200571 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100572
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100573 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200575}
John Durkop9814fa22020-11-04 12:28:15 -0800576#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100577 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
578 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
579 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
580 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
583 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200584{
585 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200587 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200588 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200589 case PSA_KEY_TYPE_DERIVE:
590 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100591#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200592 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 if (bits != 128 && bits != 192 && bits != 256) {
594 return PSA_ERROR_INVALID_ARGUMENT;
595 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200596 break;
597#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200598#if defined(PSA_WANT_KEY_TYPE_ARIA)
599 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100600 if (bits != 128 && bits != 192 && bits != 256) {
601 return PSA_ERROR_INVALID_ARGUMENT;
602 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200603 break;
604#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100605#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200606 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100607 if (bits != 128 && bits != 192 && bits != 256) {
608 return PSA_ERROR_INVALID_ARGUMENT;
609 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200610 break;
611#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100612#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200613 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100614 if (bits != 64 && bits != 128 && bits != 192) {
615 return PSA_ERROR_INVALID_ARGUMENT;
616 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200617 break;
618#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100619#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200620 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100621 if (bits < 8 || bits > 2048) {
622 return PSA_ERROR_INVALID_ARGUMENT;
623 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200624 break;
625#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100626#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200627 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100628 if (bits != 256) {
629 return PSA_ERROR_INVALID_ARGUMENT;
630 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200631 break;
632#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200633 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100634 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200635 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 if (bits % 8 != 0) {
637 return PSA_ERROR_INVALID_ARGUMENT;
638 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200641}
642
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100643/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100644 *
Steven Cooremancd640932021-03-08 12:00:27 +0100645 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
646 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100647 *
648 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100649 * \param[in] key_type The key type of the key to be used with the
650 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100651 *
652 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100653 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100654 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100655 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100656 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100657MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100658 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100660{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100661 if (PSA_ALG_IS_HMAC(algorithm)) {
662 if (key_type == PSA_KEY_TYPE_HMAC) {
663 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100664 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100665 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100667 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
668 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
669 * key. */
670 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
671 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
672 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
673 * the block length (larger than 1) for block ciphers. */
674 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
675 return PSA_SUCCESS;
676 }
677 }
678 }
679
680 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100681}
682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
684 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200685{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100686 if (slot->key.data != NULL) {
687 return PSA_ERROR_ALREADY_EXISTS;
688 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100690 slot->key.data = mbedtls_calloc(1, buffer_length);
691 if (slot->key.data == NULL) {
692 return PSA_ERROR_INSUFFICIENT_MEMORY;
693 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200694
Ronald Cronea0f8a62020-11-25 17:52:23 +0100695 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100696 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200697}
698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100699psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
700 const uint8_t *data,
701 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200702{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100703 psa_status_t status = psa_allocate_buffer_to_slot(slot,
704 data_length);
705 if (status != PSA_SUCCESS) {
706 return status;
707 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709 memcpy(slot->key.data, data, data_length);
710 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200711}
712
Ronald Crona0fe59f2020-11-29 11:14:53 +0100713psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100714 const psa_key_attributes_t *attributes,
715 const uint8_t *data, size_t data_length,
716 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100717 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100718{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100719 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
720 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100721
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200722 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100723 if (data_length == 0) {
724 return PSA_ERROR_NOT_SUPPORTED;
725 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100727 if (key_type_is_raw_bytes(type)) {
728 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200729
Steven Cooreman75b74362020-07-28 14:30:13 +0200730 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100731 if (data_length > SIZE_MAX / 8) {
732 return PSA_ERROR_NOT_SUPPORTED;
733 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200734
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200735 /* Enforce a size limit, and in particular ensure that the bit
736 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100737 if ((*bits) > PSA_MAX_KEY_BITS) {
738 return PSA_ERROR_NOT_SUPPORTED;
739 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200740
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100741 status = validate_unstructured_key_bit_size(type, *bits);
742 if (status != PSA_SUCCESS) {
743 return status;
744 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200745
Ronald Crondd04d422020-11-28 15:14:42 +0100746 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100747 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100748 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100749 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100751 return PSA_SUCCESS;
752 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800753#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100754 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
755 if (PSA_KEY_TYPE_IS_ECC(type)) {
756 return mbedtls_psa_ecp_import_key(attributes,
757 data, data_length,
758 key_buffer, key_buffer_size,
759 key_buffer_length,
760 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100761 }
John Durkop9814fa22020-11-04 12:28:15 -0800762#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
763 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700764#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100765 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
766 if (PSA_KEY_TYPE_IS_RSA(type)) {
767 return mbedtls_psa_rsa_import_key(attributes,
768 data, data_length,
769 key_buffer, key_buffer_size,
770 key_buffer_length,
771 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200772 }
John Durkop9814fa22020-11-04 12:28:15 -0800773#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
774 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100775 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100777 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100778}
779
Gilles Peskinef603c712019-01-19 13:40:11 +0100780/** Calculate the intersection of two algorithm usage policies.
781 *
782 * Return 0 (which allows no operation) on incompatibility.
783 */
784static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100785 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100786 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100787 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100788{
Gilles Peskine549ea862019-05-22 11:45:59 +0200789 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790 if (alg1 == alg2) {
791 return alg1;
792 }
Steven Cooreman03488022021-02-18 12:07:20 +0100793 /* If the policies are from the same hash-and-sign family, check
794 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100795 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
796 PSA_ALG_IS_SIGN_HASH(alg2) &&
797 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
798 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
799 return alg2;
800 }
801 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
802 return alg1;
803 }
Steven Cooreman03488022021-02-18 12:07:20 +0100804 }
805 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100806 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100807 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100808 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
809 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
810 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
811 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
812 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100813 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100814
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100815 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100816 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
817 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
818 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
819 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100820 }
821 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100822 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
823 (alg1_len <= alg2_len)) {
824 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100825 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100826 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
827 (alg2_len <= alg1_len)) {
828 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100829 }
830 }
831 /* If the policies are from the same MAC family, check whether one
832 * of them is a minimum-MAC-length policy. Calculate the most
833 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100834 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
835 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
836 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100837 /* Validate the combination of key type and algorithm. Since the base
838 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
840 return 0;
841 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100842
Steven Cooreman31a876d2021-03-03 20:47:40 +0100843 /* Get the (exact or at-least) output lengths for both sides of the
844 * requested intersection. None of the currently supported algorithms
845 * have an output length dependent on the actual key size, so setting it
846 * to a bogus value of 0 is currently OK.
847 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100848 * Note that for at-least-this-length wildcard algorithms, the output
849 * length is set to the shortest allowed length, which allows us to
850 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100851 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
852 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100853 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100854
Steven Cooremana1d83222021-02-25 10:20:29 +0100855 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100856 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
857 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
858 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100859 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100860
861 /* If only one is an at-least-this-length policy, the intersection would
862 * be the other (fixed-length) policy as long as said fixed length is
863 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100864 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
865 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100866 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100867 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
868 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100869 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100870
Steven Cooremancd640932021-03-08 12:00:27 +0100871 /* If none of them are wildcards, check whether they define the same tag
872 * length. This is still possible here when one is default-length and
873 * the other specific-length. Ensure to always return the
874 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100875 if (alg1_len == alg2_len) {
876 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
877 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100878 }
879 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100880 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100881}
882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100883static int psa_key_algorithm_permits(psa_key_type_t key_type,
884 psa_algorithm_t policy_alg,
885 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200886{
Gilles Peskine549ea862019-05-22 11:45:59 +0200887 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100888 if (requested_alg == policy_alg) {
889 return 1;
890 }
Steven Cooreman03488022021-02-18 12:07:20 +0100891 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
892 * and requested_alg is the same hash-and-sign family with any hash,
893 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
895 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
896 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
897 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100898 }
899 /* If policy_alg is a wildcard AEAD algorithm of the same base as
900 * the requested algorithm, check the requested tag length to be
901 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100902 if (PSA_ALG_IS_AEAD(policy_alg) &&
903 PSA_ALG_IS_AEAD(requested_alg) &&
904 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
905 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
906 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
907 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
908 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100909 }
Steven Cooremancd640932021-03-08 12:00:27 +0100910 /* If policy_alg is a MAC algorithm of the same base as the requested
911 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100912 if (PSA_ALG_IS_MAC(policy_alg) &&
913 PSA_ALG_IS_MAC(requested_alg) &&
914 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
915 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100916 /* Validate the combination of key type and algorithm. Since the policy
917 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100918 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
919 return 0;
920 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100921
Steven Cooreman31a876d2021-03-03 20:47:40 +0100922 /* Get both the requested output length for the algorithm which is to be
923 * verified, and the default output length for the base algorithm.
924 * Note that none of the currently supported algorithms have an output
925 * length dependent on actual key size, so setting it to a bogus value
926 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100927 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100928 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100929 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 key_type, 0,
931 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100932
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100933 /* If the policy is default-length, only allow an algorithm with
934 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100935 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
936 return requested_output_length == default_output_length;
937 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100938
939 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100940 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100941 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
942 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
943 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100944 }
945
Steven Cooremancd640932021-03-08 12:00:27 +0100946 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
947 * check for the requested MAC length to be equal to or longer than the
948 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100949 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
950 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
951 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100952 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200953 }
Steven Cooremance48e852020-10-05 16:02:45 +0200954 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200955 * a key derivation with that key agreement should also be allowed. This
956 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100957 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
958 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
959 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
960 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200961 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100962 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100963 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200964}
965
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100966/** Test whether a policy permits an algorithm.
967 *
968 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100969 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100970 * \note This function requires providing the key type for which the policy is
971 * being validated, since some algorithm policy definitions (e.g. MAC)
972 * have different properties depending on what kind of cipher it is
973 * combined with.
974 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100975 * \retval PSA_SUCCESS When \p alg is a specific algorithm
976 * allowed by the \p policy.
977 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
978 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
979 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100980 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100981static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
982 psa_key_type_t key_type,
983 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100984{
Steven Cooremand788fab2021-02-25 11:29:17 +0100985 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100986 if (alg == 0) {
987 return PSA_ERROR_INVALID_ARGUMENT;
988 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100989
Steven Cooreman7e39f052021-02-23 14:18:32 +0100990 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100991 if (PSA_ALG_IS_WILDCARD(alg)) {
992 return PSA_ERROR_INVALID_ARGUMENT;
993 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100995 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
996 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
997 return PSA_SUCCESS;
998 } else {
999 return PSA_ERROR_NOT_PERMITTED;
1000 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001001}
1002
Gilles Peskinef603c712019-01-19 13:40:11 +01001003/** Restrict a key policy based on a constraint.
1004 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001005 * \note This function requires providing the key type for which the policy is
1006 * being restricted, since some algorithm policy definitions (e.g. MAC)
1007 * have different properties depending on what kind of cipher it is
1008 * combined with.
1009 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001010 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001011 * \param[in,out] policy The policy to restrict.
1012 * \param[in] constraint The policy constraint to apply.
1013 *
1014 * \retval #PSA_SUCCESS
1015 * \c *policy contains the intersection of the original value of
1016 * \c *policy and \c *constraint.
1017 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001018 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001019 * \c *policy is unchanged.
1020 */
1021static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001022 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001023 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001024 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001025{
1026 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001027 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1028 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001029 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001030 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1031 constraint->alg2);
1032 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1033 return PSA_ERROR_INVALID_ARGUMENT;
1034 }
1035 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1036 return PSA_ERROR_INVALID_ARGUMENT;
1037 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001038 policy->usage &= constraint->usage;
1039 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001040 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001041 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001042}
1043
Ronald Cron5c522922020-11-14 16:35:34 +01001044/** Get the description of a key given its identifier and policy constraints
1045 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001046 *
Ronald Cron5c522922020-11-14 16:35:34 +01001047 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +01001048 * nonzero, the key must allow operations with this algorithm. If \p alg is
1049 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +01001050 *
Ronald Cron5c522922020-11-14 16:35:34 +01001051 * In case of a persistent key, the function loads the description of the key
1052 * into a key slot if not already done.
1053 *
1054 * On success, the returned key slot is locked. It is the responsibility of
1055 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001056 */
Ronald Cron5c522922020-11-14 16:35:34 +01001057static psa_status_t psa_get_and_lock_key_slot_with_policy(
1058 mbedtls_svc_key_id_t key,
1059 psa_key_slot_t **p_slot,
1060 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001061 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001062{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001063 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1064 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001066 status = psa_get_and_lock_key_slot(key, p_slot);
1067 if (status != PSA_SUCCESS) {
1068 return status;
1069 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001070 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001071
1072 /* Enforce that usage policy for the key slot contains all the flags
1073 * required by the usage parameter. There is one exception: public
1074 * keys can always be exported, so we treat public key objects as
1075 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001076 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001077 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001078 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001080 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001081 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001082 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001083 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001084
Shaun Case0e7791f2021-12-20 21:14:10 -08001085 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001086 if (alg != 0) {
1087 status = psa_key_policy_permits(&slot->attr.policy,
1088 slot->attr.type,
1089 alg);
1090 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001091 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001092 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001093 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001095 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001096
1097error:
1098 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001099 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001101 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001102}
Darryl Green940d72c2018-07-13 13:18:51 +01001103
Ronald Cron5c522922020-11-14 16:35:34 +01001104/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001105 *
1106 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001107 * available, as opposed to a key in a secure element and/or to be used
1108 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001109 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001110 * This is a temporary function that may be used instead of
1111 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1112 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001113 *
Ronald Cron5c522922020-11-14 16:35:34 +01001114 * On success, the returned key slot is locked. It is the responsibility of the
1115 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001116 */
Ronald Cron5c522922020-11-14 16:35:34 +01001117static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1118 mbedtls_svc_key_id_t key,
1119 psa_key_slot_t **p_slot,
1120 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001121 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001122{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001123 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1124 usage, alg);
1125 if (status != PSA_SUCCESS) {
1126 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001127 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001129 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1130 psa_unlock_key_slot(*p_slot);
1131 *p_slot = NULL;
1132 return PSA_ERROR_NOT_SUPPORTED;
1133 }
1134
1135 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001136}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001138psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001139{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001140 /* Data pointer will always be either a valid pointer or NULL in an
1141 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001142 if (slot->key.data != NULL) {
1143 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1144 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001146 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001147 slot->key.data = NULL;
1148 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001150 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001151}
1152
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001153/** Completely wipe a slot in memory, including its policy.
1154 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001155psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001156{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001157 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001158
1159 /*
1160 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001161 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001162 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1163 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001164 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001165 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001166 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001167#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001168 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001169#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001170 status = PSA_ERROR_CORRUPTION_DETECTED;
1171 }
1172
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001173 /* Multipart operations may still be using the key. This is safe
1174 * because all multipart operation objects are independent from
1175 * the key slot: if they need to access the key after the setup
1176 * phase, they have a copy of the key. Note that this means that
1177 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001178 /* At this point, key material and other type-specific content has
1179 * been wiped. Clear remaining metadata. We can call memset and not
1180 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001181 memset(slot, 0, sizeof(*slot));
1182 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001183}
1184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001185psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001186{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001187 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001188 psa_status_t status; /* status of the last operation */
1189 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001190#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1191 psa_se_drv_table_entry_t *driver;
1192#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001194 if (mbedtls_svc_key_id_is_null(key)) {
1195 return PSA_SUCCESS;
1196 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001197
Ronald Cronf2911112020-10-29 17:51:10 +01001198 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001199 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001200 * key, this will load the key description from persistent memory if not
1201 * done yet. We cannot avoid this loading as without it we don't know if
1202 * the key is operated by an SE or not and this information is needed by
1203 * the current implementation.
1204 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001205 status = psa_get_and_lock_key_slot(key, &slot);
1206 if (status != PSA_SUCCESS) {
1207 return status;
1208 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001209
Ronald Cronf2911112020-10-29 17:51:10 +01001210 /*
1211 * If the key slot containing the key description is under access by the
1212 * library (apart from the present access), the key cannot be destroyed
1213 * yet. For the time being, just return in error. Eventually (to be
1214 * implemented), the key should be destroyed when all accesses have
1215 * stopped.
1216 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001217 if (slot->lock_count > 1) {
1218 psa_unlock_key_slot(slot);
1219 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001220 }
1221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001222 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001223 /* Refuse the destruction of a read-only key (which may or may not work
1224 * if we attempt it, depending on whether the key is merely read-only
1225 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001226 * Just do the best we can, which is to wipe the copy in memory
1227 * (done in this function's cleanup code). */
1228 overall_status = PSA_ERROR_NOT_PERMITTED;
1229 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001230 }
1231
Gilles Peskine354f7672019-07-12 23:46:38 +02001232#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001233 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1234 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001235 /* For a key in a secure element, we need to do three things:
1236 * remove the key file in internal storage, destroy the
1237 * key inside the secure element, and update the driver's
1238 * persistent data. Start a transaction that will encompass these
1239 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001240 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001241 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001242 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001243 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001244 status = psa_crypto_save_transaction();
1245 if (status != PSA_SUCCESS) {
1246 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001247 /* We should still try to destroy the key in the secure
1248 * element and the key metadata in storage. This is especially
1249 * important if the error is that the storage is full.
1250 * But how to do it exactly without risking an inconsistent
1251 * state after a reset?
1252 * https://github.com/ARMmbed/mbed-crypto/issues/215
1253 */
1254 overall_status = status;
1255 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001256 }
1257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001258 status = psa_destroy_se_key(driver,
1259 psa_key_slot_get_slot_number(slot));
1260 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001261 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001262 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001263 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001264#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1265
Darryl Greend49a4992018-06-18 17:27:26 +01001266#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001267 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1268 status = psa_destroy_persistent_key(slot->attr.id);
1269 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001270 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001271 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001272
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001273 /* TODO: other slots may have a copy of the same key. We should
1274 * invalidate them.
1275 * https://github.com/ARMmbed/mbed-crypto/issues/214
1276 */
Darryl Greend49a4992018-06-18 17:27:26 +01001277 }
1278#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001279
Gilles Peskinefc762652019-07-22 19:30:34 +02001280#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001281 if (driver != NULL) {
1282 status = psa_save_se_persistent_data(driver);
1283 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001284 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001285 }
1286 status = psa_crypto_stop_transaction();
1287 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001288 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001289 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001290 }
1291#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1292
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001293exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001294 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001295 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001296 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001297 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001298 }
1299 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001300}
1301
John Durkop07cc04a2020-11-16 22:08:34 -08001302#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001303 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001304static psa_status_t psa_get_rsa_public_exponent(
1305 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001306 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001307{
1308 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001310 uint8_t *buffer = NULL;
1311 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001312 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001314 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1315 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001316 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001317 }
1318 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001319 /* It's the default value, which is reported as an empty string,
1320 * so there's nothing to do. */
1321 goto exit;
1322 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001324 buflen = mbedtls_mpi_size(&mpi);
1325 buffer = mbedtls_calloc(1, buflen);
1326 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001327 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1328 goto exit;
1329 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001330 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1331 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001332 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001333 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001334 attributes->domain_parameters = buffer;
1335 attributes->domain_parameters_size = buflen;
1336
1337exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001338 mbedtls_mpi_free(&mpi);
1339 if (ret != 0) {
1340 mbedtls_free(buffer);
1341 }
1342 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001343}
John Durkop07cc04a2020-11-16 22:08:34 -08001344#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001345 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001346
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001347/** Retrieve all the publicly-accessible attributes of a key.
1348 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001349psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1350 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001351{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001352 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001353 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001354 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001356 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001358 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1359 if (status != PSA_SUCCESS) {
1360 return status;
1361 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001362
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001363 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001364 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1365 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001366
1367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001368 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1369 psa_set_key_slot_number(attributes,
1370 psa_key_slot_get_slot_number(slot));
1371 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001374 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001375#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001376 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001377 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001378 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001379 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001380 * is not yet implemented.
1381 * https://github.com/ARMmbed/mbed-crypto/issues/216
1382 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001383 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001384 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001385
Ronald Cron90857082020-11-25 14:58:33 +01001386 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001387 slot->attr.type,
1388 slot->key.data,
1389 slot->key.bytes,
1390 &rsa);
1391 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001392 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001393 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001395 status = psa_get_rsa_public_exponent(rsa,
1396 attributes);
1397 mbedtls_rsa_free(rsa);
1398 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001399 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001400 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001401#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001402 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001403 default:
1404 /* Nothing else to do. */
1405 break;
1406 }
1407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001408 if (status != PSA_SUCCESS) {
1409 psa_reset_key_attributes(attributes);
1410 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001412 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001414 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001415}
1416
Gilles Peskinec8000c02019-08-02 20:15:51 +02001417#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1418psa_status_t psa_get_key_slot_number(
1419 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001420 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001421{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001422 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001423 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001424 return PSA_SUCCESS;
1425 } else {
1426 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001427 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001428}
1429#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001431static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1432 size_t key_buffer_size,
1433 uint8_t *data,
1434 size_t data_size,
1435 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001436{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001437 if (key_buffer_size > data_size) {
1438 return PSA_ERROR_BUFFER_TOO_SMALL;
1439 }
1440 memcpy(data, key_buffer, key_buffer_size);
1441 memset(data + key_buffer_size, 0,
1442 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001443 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001444 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001445}
1446
Ronald Cron7285cda2020-11-26 14:46:37 +01001447psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001448 const psa_key_attributes_t *attributes,
1449 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001450 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001451{
Ronald Crond18b5f82020-11-25 16:46:05 +01001452 psa_key_type_t type = attributes->core.type;
1453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001454 if (key_type_is_raw_bytes(type) ||
1455 PSA_KEY_TYPE_IS_RSA(type) ||
1456 PSA_KEY_TYPE_IS_ECC(type)) {
1457 return psa_export_key_buffer_internal(
1458 key_buffer, key_buffer_size,
1459 data, data_size, data_length);
1460 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001461 /* This shouldn't happen in the reference implementation, but
1462 it is valid for a special-purpose implementation to omit
1463 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001464 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001465 }
1466}
1467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001468psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1469 uint8_t *data,
1470 size_t data_size,
1471 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001472{
1473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1474 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1475 psa_key_slot_t *slot;
1476
Ronald Crone907e552021-01-18 13:22:38 +01001477 /* Reject a zero-length output buffer now, since this can never be a
1478 * valid key representation. This way we know that data must be a valid
1479 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001480 if (data_size == 0) {
1481 return PSA_ERROR_BUFFER_TOO_SMALL;
1482 }
Ronald Crone907e552021-01-18 13:22:38 +01001483
Ronald Cron9486f9d2020-11-26 13:36:23 +01001484 /* Set the key to empty now, so that even when there are errors, we always
1485 * set data_length to a value between 0 and data_size. On error, setting
1486 * the key to empty is a good choice because an empty key representation is
1487 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001488 *data_length = 0;
1489
Ronald Cron9486f9d2020-11-26 13:36:23 +01001490 /* Export requires the EXPORT flag. There is an exception for public keys,
1491 * which don't require any flag, but
1492 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1493 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001494 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1495 PSA_KEY_USAGE_EXPORT, 0);
1496 if (status != PSA_SUCCESS) {
1497 return status;
1498 }
mohammad160306e79202018-03-28 13:17:44 +03001499
Ronald Crond18b5f82020-11-25 16:46:05 +01001500 psa_key_attributes_t attributes = {
1501 .core = slot->attr
1502 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001503 status = psa_driver_wrapper_export_key(&attributes,
1504 slot->key.data, slot->key.bytes,
1505 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001507 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001509 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001510}
1511
Ronald Cron7285cda2020-11-26 14:46:37 +01001512psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001513 const psa_key_attributes_t *attributes,
1514 const uint8_t *key_buffer,
1515 size_t key_buffer_size,
1516 uint8_t *data,
1517 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001518 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001519{
Ronald Crond18b5f82020-11-25 16:46:05 +01001520 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1523 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001524 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001525 return psa_export_key_buffer_internal(
1526 key_buffer, key_buffer_size,
1527 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001528 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001530 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001531#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001532 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1533 return mbedtls_psa_rsa_export_public_key(attributes,
1534 key_buffer,
1535 key_buffer_size,
1536 data,
1537 data_size,
1538 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001539#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001540 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001541 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001542#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1543 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001544 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001545#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001546 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1547 return mbedtls_psa_ecp_export_public_key(attributes,
1548 key_buffer,
1549 key_buffer_size,
1550 data,
1551 data_size,
1552 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001553#else
1554 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001555 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001556#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1557 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001558 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001559 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001560 /* This shouldn't happen in the reference implementation, but
1561 it is valid for a special-purpose implementation to omit
1562 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001563 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001564 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001565}
Ronald Crond18b5f82020-11-25 16:46:05 +01001566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001567psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1568 uint8_t *data,
1569 size_t data_size,
1570 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001571{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001572 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001573 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001574 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001575 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001576
Ronald Crone907e552021-01-18 13:22:38 +01001577 /* Reject a zero-length output buffer now, since this can never be a
1578 * valid key representation. This way we know that data must be a valid
1579 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001580 if (data_size == 0) {
1581 return PSA_ERROR_BUFFER_TOO_SMALL;
1582 }
Ronald Crone907e552021-01-18 13:22:38 +01001583
Darryl Greendd8fb772018-11-07 16:00:44 +00001584 /* Set the key to empty now, so that even when there are errors, we always
1585 * set data_length to a value between 0 and data_size. On error, setting
1586 * the key to empty is a good choice because an empty key representation is
1587 * unlikely to be accepted anywhere. */
1588 *data_length = 0;
1589
1590 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001591 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1592 if (status != PSA_SUCCESS) {
1593 return status;
1594 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001596 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1597 status = PSA_ERROR_INVALID_ARGUMENT;
1598 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001599 }
1600
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001601 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001602 .core = slot->attr
1603 };
Ronald Cron67227982020-11-26 15:16:05 +01001604 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001605 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001606 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001607
1608exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001609 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001611 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001612}
1613
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001614MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1615 "One or more key attribute flag is listed as both external-only and dual-use")
1616MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1617 "One or more key attribute flag is listed as both internal-only and dual-use")
1618MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1619 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001620
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001621/** Validate that a key policy is internally well-formed.
1622 *
1623 * This function only rejects invalid policies. It does not validate the
1624 * consistency of the policy with respect to other attributes of the key
1625 * such as the key type.
1626 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001627static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001628{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001629 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1630 PSA_KEY_USAGE_COPY |
1631 PSA_KEY_USAGE_ENCRYPT |
1632 PSA_KEY_USAGE_DECRYPT |
1633 PSA_KEY_USAGE_SIGN_MESSAGE |
1634 PSA_KEY_USAGE_VERIFY_MESSAGE |
1635 PSA_KEY_USAGE_SIGN_HASH |
1636 PSA_KEY_USAGE_VERIFY_HASH |
1637 PSA_KEY_USAGE_DERIVE)) != 0) {
1638 return PSA_ERROR_INVALID_ARGUMENT;
1639 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001641 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001642}
1643
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001644/** Validate the internal consistency of key attributes.
1645 *
1646 * This function only rejects invalid attribute values. If does not
1647 * validate the consistency of the attributes with any key data that may
1648 * be involved in the creation of the key.
1649 *
1650 * Call this function early in the key creation process.
1651 *
1652 * \param[in] attributes Key attributes for the new key.
1653 * \param[out] p_drv On any return, the driver for the key, if any.
1654 * NULL for a transparent key.
1655 *
1656 */
1657static psa_status_t psa_validate_key_attributes(
1658 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001659 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001660{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001661 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001662 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1663 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001665 status = psa_validate_key_location(lifetime, p_drv);
1666 if (status != PSA_SUCCESS) {
1667 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001668 }
1669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001670 status = psa_validate_key_persistence(lifetime);
1671 if (status != PSA_SUCCESS) {
1672 return status;
1673 }
1674
1675 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1676 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1677 return PSA_ERROR_INVALID_ARGUMENT;
1678 }
1679 } else {
1680 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1681 return PSA_ERROR_INVALID_ARGUMENT;
1682 }
1683 }
1684
1685 status = psa_validate_key_policy(&attributes->core.policy);
1686 if (status != PSA_SUCCESS) {
1687 return status;
1688 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001689
1690 /* Refuse to create overly large keys.
1691 * Note that this doesn't trigger on import if the attributes don't
1692 * explicitly specify a size (so psa_get_key_bits returns 0), so
1693 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001694 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1695 return PSA_ERROR_NOT_SUPPORTED;
1696 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001697
Gilles Peskine91e8c332019-08-02 19:19:39 +02001698 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001699 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1700 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1701 return PSA_ERROR_INVALID_ARGUMENT;
1702 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001704 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001705}
1706
Gilles Peskine4747d192019-04-17 15:05:45 +02001707/** Prepare a key slot to receive key material.
1708 *
1709 * This function allocates a key slot and sets its metadata.
1710 *
1711 * If this function fails, call psa_fail_key_creation().
1712 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001713 * This function is intended to be used as follows:
1714 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001715 * it with the specified attributes, and in case of a volatile key assign it
1716 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001717 * -# Populate the slot with the key material.
1718 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1719 * In case of failure at any step, stop the sequence and call
1720 * psa_fail_key_creation().
1721 *
Ronald Cron5c522922020-11-14 16:35:34 +01001722 * On success, the key slot is locked. It is the responsibility of the caller
1723 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001724 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001725 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001726 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001727 * \param[out] p_slot On success, a pointer to the prepared slot.
1728 * \param[out] p_drv On any return, the driver for the key, if any.
1729 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001730 *
1731 * \retval #PSA_SUCCESS
1732 * The key slot is ready to receive key material.
1733 * \return If this function fails, the key slot is an invalid state.
1734 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001735 */
1736static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001737 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001738 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001739 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001740 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001741{
1742 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001743 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001744 psa_key_slot_t *slot;
1745
Gilles Peskinedf179142019-07-15 22:02:14 +02001746 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001747 *p_drv = NULL;
1748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001749 status = psa_validate_key_attributes(attributes, p_drv);
1750 if (status != PSA_SUCCESS) {
1751 return status;
1752 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001754 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1755 if (status != PSA_SUCCESS) {
1756 return status;
1757 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001758 slot = *p_slot;
1759
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001760 /* We're storing the declared bit-size of the key. It's up to each
1761 * creation mechanism to verify that this information is correct.
1762 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001763 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001764 * is optional (import, copy). In case of a volatile key, assign it the
1765 * volatile key identifier associated to the slot returned to contain its
1766 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001767
1768 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001769 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001770#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1771 slot->attr.id = volatile_key_id;
1772#else
1773 slot->attr.id.key_id = volatile_key_id;
1774#endif
1775 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001776
Gilles Peskine91e8c332019-08-02 19:19:39 +02001777 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001778 * external-only flags, query `attributes`. Thanks to the check
1779 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001780 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001781 * may have set. */
1782 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001783
Gilles Peskinecbaff462019-07-12 23:46:04 +02001784#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001785 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001786 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001787 * create the key file in internal storage, create the
1788 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001789 * persistent data. This is done by starting a transaction that will
1790 * encompass these three actions.
1791 * For registering a volatile key, we just need to find an appropriate
1792 * slot number inside the SE. Since the key is designated volatile, creating
1793 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001794 /* The first thing to do is to find a slot number for the new key.
1795 * We save the slot number in persistent storage as part of the
1796 * transaction data. It will be needed to recover if the power
1797 * fails during the key creation process, to clean up on the secure
1798 * element side after restarting. Obtaining a slot number from the
1799 * secure element driver updates its persistent state, but we do not yet
1800 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001801 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001802 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001803 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001804 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1805 &slot_number);
1806 if (status != PSA_SUCCESS) {
1807 return status;
1808 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001810 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1811 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001812 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001813 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001814 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001815 status = psa_crypto_save_transaction();
1816 if (status != PSA_SUCCESS) {
1817 (void) psa_crypto_stop_transaction();
1818 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001819 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001820 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001821
1822 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001823 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001824 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001826 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001827 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001828 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001829 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001830#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1831
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001832 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001833}
Gilles Peskine4747d192019-04-17 15:05:45 +02001834
1835/** Finalize the creation of a key once its key material has been set.
1836 *
1837 * This entails writing the key to persistent storage.
1838 *
1839 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001840 * See the documentation of psa_start_key_creation() for the intended use
1841 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001842 *
Ronald Cron5c522922020-11-14 16:35:34 +01001843 * If the finalization succeeds, the function unlocks the key slot (it was
1844 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1845 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001846 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001847 * \param[in,out] slot Pointer to the slot with key material.
1848 * \param[in] driver The secure element driver for the key,
1849 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001850 * \param[out] key On success, identifier of the key. Note that the
1851 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001852 *
1853 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001854 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001855 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1856 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1857 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1858 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1859 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1860 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001861 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001862 * \return If this function fails, the key slot is an invalid state.
1863 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001864 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001865static psa_status_t psa_finish_key_creation(
1866 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001867 psa_se_drv_table_entry_t *driver,
1868 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001869{
1870 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001871 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001872 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001873
1874#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001875 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001876#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001877 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001878 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001879 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001880 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001881
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001882 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1883 sizeof(data.slot_number),
1884 "Slot number size does not match psa_se_key_data_storage_t");
1885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001886 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1887 status = psa_save_persistent_key(&slot->attr,
1888 (uint8_t *) &data,
1889 sizeof(data));
1890 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001891#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1892 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001893 /* Key material is saved in export representation in the slot, so
1894 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001895 status = psa_save_persistent_key(&slot->attr,
1896 slot->key.data,
1897 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001898 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001899 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001900#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1901
Gilles Peskinecbaff462019-07-12 23:46:04 +02001902#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001903 /* Finish the transaction for a key creation. This does not
1904 * happen when registering an existing key. Detect this case
1905 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001906 * creation of a persistent key in a secure element requires a transaction,
1907 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001908 if (driver != NULL &&
1909 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1910 status = psa_save_se_persistent_data(driver);
1911 if (status != PSA_SUCCESS) {
1912 psa_destroy_persistent_key(slot->attr.id);
1913 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001914 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001915 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001916 }
1917#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001919 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001920 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001921 status = psa_unlock_key_slot(slot);
1922 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001923 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001924 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001925 }
Ronald Cron50972942020-11-14 11:28:25 +01001926
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001927 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001928}
1929
1930/** Abort the creation of a key.
1931 *
1932 * You may call this function after calling psa_start_key_creation(),
1933 * or after psa_finish_key_creation() fails. In other circumstances, this
1934 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001935 * See the documentation of psa_start_key_creation() for the intended use
1936 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001937 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001938 * \param[in,out] slot Pointer to the slot with key material.
1939 * \param[in] driver The secure element driver for the key,
1940 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001941 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001942static void psa_fail_key_creation(psa_key_slot_t *slot,
1943 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001944{
Gilles Peskine011e4282019-06-26 18:34:38 +02001945 (void) driver;
1946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001947 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001948 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001949 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001950
1951#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001952 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001953 * element, and the failure happened later (when saving metadata
1954 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001955 * element.
1956 * https://github.com/ARMmbed/mbed-crypto/issues/217
1957 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001958
Gilles Peskined7729582019-08-05 15:55:54 +02001959 /* Abort the ongoing transaction if any (there may not be one if
1960 * the creation process failed before starting one, or if the
1961 * key creation is a registration of a key in a secure element).
1962 * Earlier functions must already have done what it takes to undo any
1963 * partial creation. All that's left is to update the transaction data
1964 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001965 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001966#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001968 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001969}
1970
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001971/** Validate optional attributes during key creation.
1972 *
1973 * Some key attributes are optional during key creation. If they are
1974 * specified in the attributes structure, check that they are consistent
1975 * with the data in the slot.
1976 *
1977 * This function should be called near the end of key creation, after
1978 * the slot in memory is fully populated but before saving persistent data.
1979 */
1980static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001981 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001982 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001983{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001984 if (attributes->core.type != 0) {
1985 if (attributes->core.type != slot->attr.type) {
1986 return PSA_ERROR_INVALID_ARGUMENT;
1987 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001988 }
1989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001990 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001991#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001992 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1993 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001994 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001995 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001996 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001997
Ronald Cron90857082020-11-25 14:58:33 +01001998 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001999 slot->attr.type,
2000 slot->key.data,
2001 slot->key.bytes,
2002 &rsa);
2003 if (status != PSA_SUCCESS) {
2004 return status;
2005 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002007 mbedtls_mpi_init(&actual);
2008 mbedtls_mpi_init(&required);
2009 ret = mbedtls_rsa_export(rsa,
2010 NULL, NULL, NULL, NULL, &actual);
2011 mbedtls_rsa_free(rsa);
2012 mbedtls_free(rsa);
2013 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002014 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002015 }
2016 ret = mbedtls_mpi_read_binary(&required,
2017 attributes->domain_parameters,
2018 attributes->domain_parameters_size);
2019 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002020 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002021 }
2022 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002023 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002024 }
2025rsa_exit:
2026 mbedtls_mpi_free(&actual);
2027 mbedtls_mpi_free(&required);
2028 if (ret != 0) {
2029 return mbedtls_to_psa_error(ret);
2030 }
2031 } else
John Durkop9814fa22020-11-04 12:28:15 -08002032#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2033 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002034 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002035 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002036 }
2037 }
2038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002039 if (attributes->core.bits != 0) {
2040 if (attributes->core.bits != slot->attr.bits) {
2041 return PSA_ERROR_INVALID_ARGUMENT;
2042 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002043 }
2044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002045 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002046}
2047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002048psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2049 const uint8_t *data,
2050 size_t data_length,
2051 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002052{
2053 psa_status_t status;
2054 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002055 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002056 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002057
Ronald Cron81709fc2020-11-14 12:10:32 +01002058 *key = MBEDTLS_SVC_KEY_ID_INIT;
2059
Gilles Peskine0f84d622019-09-12 19:03:13 +02002060 /* Reject zero-length symmetric keys (including raw data key objects).
2061 * This also rejects any key which might be encoded as an empty string,
2062 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002063 if (data_length == 0) {
2064 return PSA_ERROR_INVALID_ARGUMENT;
2065 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002067 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2068 &slot, &driver);
2069 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002070 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002071 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002072
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002073 /* In the case of a transparent key or an opaque key stored in local
2074 * storage (thus not in the case of generating a key in a secure element
2075 * or cryptoprocessor with storage), we have to allocate a buffer to
2076 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002077 if (slot->key.data == NULL) {
2078 status = psa_allocate_buffer_to_slot(slot, data_length);
2079 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002080 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002081 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002082 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002083
2084 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002085 status = psa_driver_wrapper_import_key(attributes,
2086 data, data_length,
2087 slot->key.data,
2088 slot->key.bytes,
2089 &slot->key.bytes, &bits);
2090 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002091 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002092 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002094 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002095 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002096 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002097 status = PSA_ERROR_INVALID_ARGUMENT;
2098 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002099 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 status = psa_validate_optional_attributes(slot, attributes);
2102 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002103 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002104 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002106 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002107exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002108 if (status != PSA_SUCCESS) {
2109 psa_fail_key_creation(slot, driver);
2110 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002112 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002113}
2114
Gilles Peskined7729582019-08-05 15:55:54 +02002115#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2116psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002117 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002118{
2119 psa_status_t status;
2120 psa_key_slot_t *slot = NULL;
2121 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002122 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002123
2124 /* Leaving attributes unspecified is not currently supported.
2125 * It could make sense to query the key type and size from the
2126 * secure element, but not all secure elements support this
2127 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002128 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2129 return PSA_ERROR_NOT_SUPPORTED;
2130 }
2131 if (psa_get_key_bits(attributes) == 0) {
2132 return PSA_ERROR_NOT_SUPPORTED;
2133 }
Gilles Peskined7729582019-08-05 15:55:54 +02002134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002135 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2136 &slot, &driver);
2137 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002138 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002139 }
Gilles Peskined7729582019-08-05 15:55:54 +02002140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002141 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002142
2143exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002144 if (status != PSA_SUCCESS) {
2145 psa_fail_key_creation(slot, driver);
2146 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002147
Gilles Peskined7729582019-08-05 15:55:54 +02002148 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002149 psa_close_key(key);
2150 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002151}
2152#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002154static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2155 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002156{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002157 psa_status_t status = psa_copy_key_material_into_slot(target,
2158 source->key.data,
2159 source->key.bytes);
2160 if (status != PSA_SUCCESS) {
2161 return status;
2162 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002163
Steven Cooreman398aee52020-10-13 14:35:45 +02002164 target->attr.type = source->attr.type;
2165 target->attr.bits = source->attr.bits;
2166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002167 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002168}
2169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002170psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2171 const psa_key_attributes_t *specified_attributes,
2172 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002173{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002174 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002175 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002176 psa_key_slot_t *source_slot = NULL;
2177 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002178 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002179 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002180
Ronald Cron81709fc2020-11-14 12:10:32 +01002181 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2182
Ronald Cron5c522922020-11-14 16:35:34 +01002183 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002184 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2185 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002186 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002187 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002189 status = psa_validate_optional_attributes(source_slot,
2190 specified_attributes);
2191 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002192 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002193 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002195 status = psa_restrict_key_policy(source_slot->attr.type,
2196 &actual_attributes.core.policy,
2197 &source_slot->attr.policy);
2198 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002199 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002200 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002202 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2203 &target_slot, &driver);
2204 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002205 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002206 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002207
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002208#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002209 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002210 /* Copying to a secure element is not implemented yet. */
2211 status = PSA_ERROR_NOT_SUPPORTED;
2212 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002213 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002214#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002216 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002217 /*
2218 * Copying through an opaque driver is not implemented yet, consider
2219 * a lifetime with an external location as an invalid parameter for
2220 * now.
2221 */
2222 status = PSA_ERROR_INVALID_ARGUMENT;
2223 goto exit;
2224 }
2225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002226 status = psa_copy_key_material(source_slot, target_slot);
2227 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002228 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002229 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002231 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002232exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002233 if (status != PSA_SUCCESS) {
2234 psa_fail_key_creation(target_slot, driver);
2235 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002237 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002239 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002240}
2241
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002242
2243
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002244/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002245/* Message digests */
2246/****************************************************************/
2247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002248psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002250 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002251 if (operation->id == 0) {
2252 return PSA_SUCCESS;
2253 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002255 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002256 operation->id = 0;
2257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002258 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002259}
2260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002261psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2262 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002263{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002264 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002265
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002266 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002267 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002268 status = PSA_ERROR_BAD_STATE;
2269 goto exit;
2270 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002272 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002273 status = PSA_ERROR_INVALID_ARGUMENT;
2274 goto exit;
2275 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002276
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002277 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2278 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002279 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002281 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002282
2283exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002284 if (status != PSA_SUCCESS) {
2285 psa_hash_abort(operation);
2286 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002287
2288 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002289}
2290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002291psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2292 const uint8_t *input,
2293 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002294{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002295 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002297 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002298 status = PSA_ERROR_BAD_STATE;
2299 goto exit;
2300 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002301
Steven Cooremanc8288352021-03-04 14:02:19 +01002302 /* Don't require hash implementations to behave correctly on a
2303 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002304 if (input_length == 0) {
2305 return PSA_SUCCESS;
2306 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002308 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002309
2310exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002311 if (status != PSA_SUCCESS) {
2312 psa_hash_abort(operation);
2313 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002315 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002316}
2317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002318psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2319 uint8_t *hash,
2320 size_t hash_size,
2321 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002323 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002324 if (operation->id == 0) {
2325 return PSA_ERROR_BAD_STATE;
2326 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327
Steven Cooreman1e582352021-02-18 17:24:37 +01002328 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002329 operation, hash, hash_size, hash_length);
2330 psa_hash_abort(operation);
2331 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332}
2333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002334psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2335 const uint8_t *hash,
2336 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002338 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002340 psa_status_t status = psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002341 operation,
2342 actual_hash, sizeof(actual_hash),
2343 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002345 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002346 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002347 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002349 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002350 status = PSA_ERROR_INVALID_SIGNATURE;
2351 goto exit;
2352 }
2353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002354 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002355 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002356 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002357
2358exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002359 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2360 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002361 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002362 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002364 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002365}
2366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002367psa_status_t psa_hash_compute(psa_algorithm_t alg,
2368 const uint8_t *input, size_t input_length,
2369 uint8_t *hash, size_t hash_size,
2370 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002371{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002372 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002373 if (!PSA_ALG_IS_HASH(alg)) {
2374 return PSA_ERROR_INVALID_ARGUMENT;
2375 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002377 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2378 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002379}
2380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002381psa_status_t psa_hash_compare(psa_algorithm_t alg,
2382 const uint8_t *input, size_t input_length,
2383 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002384{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002385 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002386 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002388 if (!PSA_ALG_IS_HASH(alg)) {
2389 return PSA_ERROR_INVALID_ARGUMENT;
2390 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002391
Steven Cooreman1e582352021-02-18 17:24:37 +01002392 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002393 alg, input, input_length,
2394 actual_hash, sizeof(actual_hash),
2395 &actual_hash_length);
2396 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002397 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002398 }
2399 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002400 status = PSA_ERROR_INVALID_SIGNATURE;
2401 goto exit;
2402 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002404 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002405 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002406
2407exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002408 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2409 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002410}
2411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002412psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2413 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002414{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002415 if (source_operation->id == 0 ||
2416 target_operation->id != 0) {
2417 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002418 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002420 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2421 target_operation);
2422 if (status != PSA_SUCCESS) {
2423 psa_hash_abort(target_operation);
2424 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002426 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002427}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002428
2429
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002430/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002431/* MAC */
2432/****************************************************************/
2433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002434psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002435{
Steven Cooreman07897832021-03-19 18:28:56 +01002436 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002437 if (operation->id == 0) {
2438 return PSA_SUCCESS;
2439 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002441 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002442 operation->mac_size = 0;
2443 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002444 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002446 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002447}
2448
Ronald Cron1c650a12021-06-17 16:33:22 +02002449static psa_status_t psa_mac_finalize_alg_and_key_validation(
2450 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002451 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002452 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002454 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002455 psa_key_type_t key_type = psa_get_key_type(attributes);
2456 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002458 if (!PSA_ALG_IS_MAC(alg)) {
2459 return PSA_ERROR_INVALID_ARGUMENT;
2460 }
Steven Cooreman07897832021-03-19 18:28:56 +01002461
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002462 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002463 status = psa_mac_key_can_do(alg, key_type);
2464 if (status != PSA_SUCCESS) {
2465 return status;
2466 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002467
Steven Cooremana6474de2021-05-10 11:13:41 +02002468 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002469 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002471 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002472 /* A very short MAC is too short for security since it can be
2473 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2474 * so we make this our minimum, even though 32 bits is still
2475 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002476 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002477 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002479 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2480 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002481 /* It's impossible to "truncate" to a larger length than the full length
2482 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002483 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002484 }
2485
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002486 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002487 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2488 * that is disabled in the compile-time configuration. The result can
2489 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2490 * configuration into account. In this case, force a return of
2491 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2492 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2493 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2494 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2495 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002496 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002497 }
2498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002499 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002500}
2501
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002502static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2503 mbedtls_svc_key_id_t key,
2504 psa_algorithm_t alg,
2505 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002506{
2507 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2508 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002509 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002510 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002511
2512 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002513 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002514 status = PSA_ERROR_BAD_STATE;
2515 goto exit;
2516 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002517
2518 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002519 key,
2520 &slot,
2521 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2522 alg);
2523 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002524 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002525 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002526
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002527 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002528 .core = slot->attr
2529 };
2530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002531 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2532 &operation->mac_size);
2533 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002534 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002535 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002536
2537 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002538 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002539 if (is_sign) {
2540 status = psa_driver_wrapper_mac_sign_setup(operation,
2541 &attributes,
2542 slot->key.data,
2543 slot->key.bytes,
2544 alg);
2545 } else {
2546 status = psa_driver_wrapper_mac_verify_setup(operation,
2547 &attributes,
2548 slot->key.data,
2549 slot->key.bytes,
2550 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002551 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002552
Gilles Peskinefbfac682018-07-08 20:51:54 +02002553exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002554 if (status != PSA_SUCCESS) {
2555 psa_mac_abort(operation);
2556 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002558 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002560 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002561}
2562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002563psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2564 mbedtls_svc_key_id_t key,
2565 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002566{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002567 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002568}
2569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002570psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2571 mbedtls_svc_key_id_t key,
2572 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002573{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002574 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002575}
2576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002577psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2578 const uint8_t *input,
2579 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002581 if (operation->id == 0) {
2582 return PSA_ERROR_BAD_STATE;
2583 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002584
Steven Cooreman11743f92021-03-19 18:38:46 +01002585 /* Don't require hash implementations to behave correctly on a
2586 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002587 if (input_length == 0) {
2588 return PSA_SUCCESS;
2589 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002591 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2592 input, input_length);
2593 if (status != PSA_SUCCESS) {
2594 psa_mac_abort(operation);
2595 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002597 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598}
2599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002600psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2601 uint8_t *mac,
2602 size_t mac_size,
2603 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002604{
Steven Cooreman87885df2021-03-19 19:04:39 +01002605 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2606 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002608 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002609 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002610 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002611 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002613 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002614 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002615 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002616 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002617
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002618 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2619 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002620 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002621 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002622 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002623 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002624
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002625 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002626 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002627 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002628 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002630 status = psa_driver_wrapper_mac_sign_finish(operation,
2631 mac, operation->mac_size,
2632 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002633
Dave Rodgman478ab542021-06-25 09:09:02 +01002634exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002635 /* In case of success, set the potential excess room in the output buffer
2636 * to an invalid value, to avoid potentially leaking a longer MAC.
2637 * In case of error, set the output length and content to a safe default,
2638 * such that in case the caller misses an error check, the output would be
2639 * an unachievable MAC.
2640 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002641 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002642 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002643 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002644 }
mohammad16036df908f2018-04-02 08:34:15 -07002645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002646 if (mac_size > operation->mac_size) {
2647 memset(&mac[operation->mac_size], '!',
2648 mac_size - operation->mac_size);
2649 }
Ronald Cron882eb782021-06-18 13:05:48 +02002650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002651 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002653 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002654}
2655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002656psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2657 const uint8_t *mac,
2658 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002659{
Steven Cooreman87885df2021-03-19 19:04:39 +01002660 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2661 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002663 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002664 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002665 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002666 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002668 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002669 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002670 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002671 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002673 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002674 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002675 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002676 }
2677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002678 status = psa_driver_wrapper_mac_verify_finish(operation,
2679 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002680
Dave Rodgman478ab542021-06-25 09:09:02 +01002681exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002682 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002684 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002685}
2686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002687static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2688 psa_algorithm_t alg,
2689 const uint8_t *input,
2690 size_t input_length,
2691 uint8_t *mac,
2692 size_t mac_size,
2693 size_t *mac_length,
2694 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002695{
2696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002697 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002698 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002699 psa_key_slot_t *slot;
2700 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002701
Ronald Crondbb86462021-06-17 17:17:20 +02002702 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002703 key,
2704 &slot,
2705 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2706 alg);
2707 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002708 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002709 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002710
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002711 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002712 .core = slot->attr
2713 };
2714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002715 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2716 &operation_mac_size);
2717 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002718 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002719 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002721 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002722 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002723 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002724 }
2725
2726 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002727 &attributes,
2728 slot->key.data, slot->key.bytes,
2729 alg,
2730 input, input_length,
2731 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002732
2733exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002734 /* In case of success, set the potential excess room in the output buffer
2735 * to an invalid value, to avoid potentially leaking a longer MAC.
2736 * In case of error, set the output length and content to a safe default,
2737 * such that in case the caller misses an error check, the output would be
2738 * an unachievable MAC.
2739 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002740 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002741 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002742 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002743 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002744 if (mac_size > operation_mac_size) {
2745 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2746 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002747
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002748 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002750 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002751}
2752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002753psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002754 psa_algorithm_t alg,
2755 const uint8_t *input,
2756 size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002757 uint8_t *mac,
2758 size_t mac_size,
2759 size_t *mac_length)
2760{
2761 return psa_mac_compute_internal(key, alg,
2762 input, input_length,
2763 mac, mac_size, mac_length, 1);
2764}
2765
2766psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2767 psa_algorithm_t alg,
2768 const uint8_t *input,
2769 size_t input_length,
2770 const uint8_t *mac,
2771 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002772{
2773 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002774 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2775 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002777 status = psa_mac_compute_internal(key, alg,
2778 input, input_length,
2779 actual_mac, sizeof(actual_mac),
2780 &actual_mac_length, 0);
2781 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002782 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002783 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002785 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002786 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002787 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002788 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002789 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002790 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002791 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002792 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002793
2794exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002795 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002796
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002797 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002798}
Gilles Peskine20035e32018-02-03 22:44:14 +01002799
Gilles Peskine20035e32018-02-03 22:44:14 +01002800/****************************************************************/
2801/* Asymmetric cryptography */
2802/****************************************************************/
2803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002804static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2805 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002806{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002807 if (input_is_message) {
2808 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2809 return PSA_ERROR_INVALID_ARGUMENT;
2810 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002812 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2813 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2814 return PSA_ERROR_INVALID_ARGUMENT;
2815 }
2816 }
2817 } else {
2818 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2819 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002820 }
2821 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002822
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002823 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002824}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002826static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2827 int input_is_message,
2828 psa_algorithm_t alg,
2829 const uint8_t *input,
2830 size_t input_length,
2831 uint8_t *signature,
2832 size_t signature_size,
2833 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002834{
2835 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2836 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002837 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002838 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002839
2840 *signature_length = 0;
2841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002842 status = psa_sign_verify_check_alg(input_is_message, alg);
2843 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002844 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002845 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002846
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002847 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002848 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002849 * buffer can in principle be empty since it doesn't actually have
2850 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002851 if (signature_size == 0) {
2852 return PSA_ERROR_BUFFER_TOO_SMALL;
2853 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002854
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002855 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002856 key, &slot,
2857 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2858 PSA_KEY_USAGE_SIGN_HASH,
2859 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002861 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002862 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002863 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002865 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002866 status = PSA_ERROR_INVALID_ARGUMENT;
2867 goto exit;
2868 }
2869
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002870 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002871 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002872 };
2873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002874 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002875 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002876 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002877 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002878 signature, signature_size, signature_length);
2879 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002880
2881 status = psa_driver_wrapper_sign_hash(
2882 &attributes, slot->key.data, slot->key.bytes,
2883 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002884 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002885 }
2886
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002887
2888exit:
2889 /* Fill the unused part of the output buffer (the whole buffer on error,
2890 * the trailing part on success) with something that isn't a valid signature
2891 * (barring an attack on the signature and deliberately-crafted input),
2892 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002893 if (status == PSA_SUCCESS) {
2894 memset(signature + *signature_length, '!',
2895 signature_size - *signature_length);
2896 } else {
2897 memset(signature, '!', signature_size);
2898 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002899 /* If signature_size is 0 then we have nothing to do. We must not call
2900 * memset because signature may be NULL in this case. */
2901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002902 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002903
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002904 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002905}
2906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002907static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2908 int input_is_message,
2909 psa_algorithm_t alg,
2910 const uint8_t *input,
2911 size_t input_length,
2912 const uint8_t *signature,
2913 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002914{
2915 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2916 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2917 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002919 status = psa_sign_verify_check_alg(input_is_message, alg);
2920 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002921 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002923
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002924 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002925 key, &slot,
2926 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2927 PSA_KEY_USAGE_VERIFY_HASH,
2928 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002930 if (status != PSA_SUCCESS) {
2931 return status;
2932 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002933
2934 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002935 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002936 };
2937
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002938 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002939 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002940 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002941 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002942 signature, signature_length);
2943 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002944 status = psa_driver_wrapper_verify_hash(
2945 &attributes, slot->key.data, slot->key.bytes,
2946 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002947 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002948 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002950 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002952 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002953
2954}
2955
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002956psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002957 const psa_key_attributes_t *attributes,
2958 const uint8_t *key_buffer,
2959 size_t key_buffer_size,
2960 psa_algorithm_t alg,
2961 const uint8_t *input,
2962 size_t input_length,
2963 uint8_t *signature,
2964 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002965 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002966{
2967 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002968
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002969 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002970 size_t hash_length;
2971 uint8_t hash[PSA_HASH_MAX_SIZE];
2972
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002973 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002974 PSA_ALG_SIGN_GET_HASH(alg),
2975 input, input_length,
2976 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002977
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002978 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002979 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002980 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002981
2982 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002983 attributes, key_buffer, key_buffer_size,
2984 alg, hash, hash_length,
2985 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002986 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002987
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002988 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002989}
2990
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002991psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2992 psa_algorithm_t alg,
2993 const uint8_t *input,
2994 size_t input_length,
2995 uint8_t *signature,
2996 size_t signature_size,
2997 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002998{
2999 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003000 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003001 signature, signature_size, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003002}
3003
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003004psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003005 const psa_key_attributes_t *attributes,
3006 const uint8_t *key_buffer,
3007 size_t key_buffer_size,
3008 psa_algorithm_t alg,
3009 const uint8_t *input,
3010 size_t input_length,
3011 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003012 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003013{
3014 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003015
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003016 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003017 size_t hash_length;
3018 uint8_t hash[PSA_HASH_MAX_SIZE];
3019
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003020 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003021 PSA_ALG_SIGN_GET_HASH(alg),
3022 input, input_length,
3023 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003024
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003025 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003026 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003027 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003028
3029 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003030 attributes, key_buffer, key_buffer_size,
3031 alg, hash, hash_length,
3032 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003033 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003035 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003036}
3037
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003038psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3039 psa_algorithm_t alg,
3040 const uint8_t *input,
3041 size_t input_length,
3042 const uint8_t *signature,
3043 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003044{
3045 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003046 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003047 signature, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003048}
3049
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003050psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003051 const psa_key_attributes_t *attributes,
3052 const uint8_t *key_buffer, size_t key_buffer_size,
3053 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003054 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01003055{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003056 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3057 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3058 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003059#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003060 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3061 return mbedtls_psa_rsa_sign_hash(
3062 attributes,
3063 key_buffer, key_buffer_size,
3064 alg, hash, hash_length,
3065 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003066#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3067 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003068 } else {
3069 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003070 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003071 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3072 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003073#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003074 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3075 return mbedtls_psa_ecdsa_sign_hash(
3076 attributes,
3077 key_buffer, key_buffer_size,
3078 alg, hash, hash_length,
3079 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003080#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3081 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082 } else {
3083 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003084 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003085 }
Ronald Cron4501c982021-03-19 20:09:32 +01003086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003087 (void) key_buffer;
3088 (void) key_buffer_size;
3089 (void) hash;
3090 (void) hash_length;
3091 (void) signature;
3092 (void) signature_size;
3093 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003095 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003096}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003098psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3099 psa_algorithm_t alg,
3100 const uint8_t *hash,
3101 size_t hash_length,
3102 uint8_t *signature,
3103 size_t signature_size,
3104 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003105{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003106 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003107 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003108 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003109}
3110
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003111psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003112 const psa_key_attributes_t *attributes,
3113 const uint8_t *key_buffer, size_t key_buffer_size,
3114 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003115 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003116{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003117 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3118 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3119 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003120#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003121 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3122 return mbedtls_psa_rsa_verify_hash(
3123 attributes,
3124 key_buffer, key_buffer_size,
3125 alg, hash, hash_length,
3126 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003127#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3128 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003129 } else {
3130 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003131 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003132 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3133 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003134#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003135 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3136 return mbedtls_psa_ecdsa_verify_hash(
3137 attributes,
3138 key_buffer, key_buffer_size,
3139 alg, hash, hash_length,
3140 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003141#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3142 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003143 } else {
3144 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003145 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003146 }
Ronald Cron4501c982021-03-19 20:09:32 +01003147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003148 (void) key_buffer;
3149 (void) key_buffer_size;
3150 (void) hash;
3151 (void) hash_length;
3152 (void) signature;
3153 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003155 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003156}
3157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003158psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3159 psa_algorithm_t alg,
3160 const uint8_t *hash,
3161 size_t hash_length,
3162 const uint8_t *signature,
3163 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003164{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003165 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003166 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003167 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003168}
3169
John Durkop0e005192020-10-31 22:06:54 -07003170#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003171static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3172 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003173{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003174 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3175 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3176 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3177 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003178}
John Durkop0e005192020-10-31 22:06:54 -07003179#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003181psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3182 psa_algorithm_t alg,
3183 const uint8_t *input,
3184 size_t input_length,
3185 const uint8_t *salt,
3186 size_t salt_length,
3187 uint8_t *output,
3188 size_t output_size,
3189 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003190{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003192 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003193 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003194
Darryl Green5cc689a2018-07-24 15:34:10 +01003195 (void) input;
3196 (void) input_length;
3197 (void) salt;
3198 (void) output;
3199 (void) output_size;
3200
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003201 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003203 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3204 return PSA_ERROR_INVALID_ARGUMENT;
3205 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003206
Ronald Cron5c522922020-11-14 16:35:34 +01003207 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003208 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3209 if (status != PSA_SUCCESS) {
3210 return status;
3211 }
3212 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3213 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003214 status = PSA_ERROR_INVALID_ARGUMENT;
3215 goto exit;
3216 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003218 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003219#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003220 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003221 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003222 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3223 slot->key.data,
3224 slot->key.bytes,
3225 &rsa);
3226 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003227 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003228 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003230 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003231 status = PSA_ERROR_BUFFER_TOO_SMALL;
3232 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003233 }
Ronald Cron7207d572021-09-08 14:28:35 +02003234#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3235 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003236 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003237#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003238 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003239 mbedtls_rsa_pkcs1_encrypt(rsa,
3240 mbedtls_psa_get_random,
3241 MBEDTLS_PSA_RANDOM_STATE,
3242 MBEDTLS_RSA_PUBLIC,
3243 input_length,
3244 input,
3245 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003246#else
3247 status = PSA_ERROR_NOT_SUPPORTED;
3248#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003249 } else
3250 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003251#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003252 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003253 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003254 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3255 mbedtls_psa_get_random,
3256 MBEDTLS_PSA_RANDOM_STATE,
3257 MBEDTLS_RSA_PUBLIC,
3258 salt, salt_length,
3259 input_length,
3260 input,
3261 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003262#else
3263 status = PSA_ERROR_NOT_SUPPORTED;
3264#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003265 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003266 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267 }
Ronald Cron7207d572021-09-08 14:28:35 +02003268#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003269 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003270rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003271 if (status == PSA_SUCCESS) {
3272 *output_length = mbedtls_rsa_get_len(rsa);
3273 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003275 mbedtls_rsa_free(rsa);
3276 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003277#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003278 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003279 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003280 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003281 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003282
3283exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003284 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003286 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003287}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003289psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3290 psa_algorithm_t alg,
3291 const uint8_t *input,
3292 size_t input_length,
3293 const uint8_t *salt,
3294 size_t salt_length,
3295 uint8_t *output,
3296 size_t output_size,
3297 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003298{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003299 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003300 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003301 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003302
Darryl Green5cc689a2018-07-24 15:34:10 +01003303 (void) input;
3304 (void) input_length;
3305 (void) salt;
3306 (void) output;
3307 (void) output_size;
3308
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003309 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003311 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3312 return PSA_ERROR_INVALID_ARGUMENT;
3313 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003314
Ronald Cron5c522922020-11-14 16:35:34 +01003315 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003316 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3317 if (status != PSA_SUCCESS) {
3318 return status;
3319 }
3320 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003321 status = PSA_ERROR_INVALID_ARGUMENT;
3322 goto exit;
3323 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003325 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003326#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003327 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003328 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003329 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3330 slot->key.data,
3331 slot->key.bytes,
3332 &rsa);
3333 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003334 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003335 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003337 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003338 status = PSA_ERROR_INVALID_ARGUMENT;
3339 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003340 }
Ronald Cron7207d572021-09-08 14:28:35 +02003341#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3342 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003344 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003345#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003346 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003347 mbedtls_rsa_pkcs1_decrypt(rsa,
3348 mbedtls_psa_get_random,
3349 MBEDTLS_PSA_RANDOM_STATE,
3350 MBEDTLS_RSA_PRIVATE,
3351 output_length,
3352 input,
3353 output,
3354 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003355#else
3356 status = PSA_ERROR_NOT_SUPPORTED;
3357#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003358 } else
3359 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003360#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003361 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003362 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003363 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3364 mbedtls_psa_get_random,
3365 MBEDTLS_PSA_RANDOM_STATE,
3366 MBEDTLS_RSA_PRIVATE,
3367 salt, salt_length,
3368 output_length,
3369 input,
3370 output,
3371 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003372#else
3373 status = PSA_ERROR_NOT_SUPPORTED;
3374#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003375 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003376 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003377 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003378
Ronald Cron7207d572021-09-08 14:28:35 +02003379#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003380 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003381rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003382 mbedtls_rsa_free(rsa);
3383 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003384#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3385 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003386 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003387 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003388 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003389
3390exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003391 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003393 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003394}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003395
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003396
3397
mohammad1603503973b2018-03-12 15:59:30 +02003398/****************************************************************/
3399/* Symmetric cryptography */
3400/****************************************************************/
3401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003402static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3403 mbedtls_svc_key_id_t key,
3404 psa_algorithm_t alg,
3405 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003406{
3407 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3408 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003409 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003410 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003411 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3412 PSA_KEY_USAGE_ENCRYPT :
3413 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003414
3415 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003416 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003417 status = PSA_ERROR_BAD_STATE;
3418 goto exit;
3419 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003421 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003422 status = PSA_ERROR_INVALID_ARGUMENT;
3423 goto exit;
3424 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003426 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3427 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003428 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003429 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003430
Ronald Cron49fafa92021-03-10 08:34:23 +01003431 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003432 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003433 * so we only set it (in the driver wrapper) after resources have been
3434 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003435 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003436 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003437 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003438 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003439 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003440 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003441 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003442 }
3443 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003444
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003445 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003446 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003447 };
3448
Ronald Cronab99ac22020-10-01 16:38:28 +02003449 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003450 if (cipher_operation == MBEDTLS_ENCRYPT) {
3451 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3452 &attributes,
3453 slot->key.data,
3454 slot->key.bytes,
3455 alg);
3456 } else {
3457 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3458 &attributes,
3459 slot->key.data,
3460 slot->key.bytes,
3461 alg);
3462 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003463
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003464exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003465 if (status != PSA_SUCCESS) {
3466 psa_cipher_abort(operation);
3467 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003469 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003471 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003472}
3473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003474psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3475 mbedtls_svc_key_id_t key,
3476 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003477{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003478 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003479}
3480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003481psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3482 mbedtls_svc_key_id_t key,
3483 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003484{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003485 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003486}
3487
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003488psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3489 uint8_t *iv,
3490 size_t iv_size,
3491 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003492{
Ronald Cron6d051732020-10-01 14:10:20 +02003493 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003494 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergey04eb7c02023-03-06 15:37:23 -07003495 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003497 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003498 status = PSA_ERROR_BAD_STATE;
3499 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003500 }
3501
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003502 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003503 status = PSA_ERROR_BAD_STATE;
3504 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003505 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003506
Ronald Cronc423acb2021-07-05 12:31:44 +02003507 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003508 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003509 status = PSA_ERROR_BUFFER_TOO_SMALL;
3510 goto exit;
3511 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003513 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003514 status = PSA_ERROR_GENERIC_ERROR;
3515 goto exit;
3516 }
3517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003518 status = psa_generate_random(local_iv, default_iv_length);
3519 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003520 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003521 }
Ronald Cron5618a392021-03-26 09:52:26 +01003522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003523 status = psa_driver_wrapper_cipher_set_iv(operation,
3524 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003525
3526exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003527 if (status == PSA_SUCCESS) {
3528 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003529 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003530 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003531 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003532 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003533 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003534 }
Ronald Cron6d051732020-10-01 14:10:20 +02003535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003536 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003537}
3538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003539psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3540 const uint8_t *iv,
3541 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003542{
Ronald Cron6d051732020-10-01 14:10:20 +02003543 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003545 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003546 status = PSA_ERROR_BAD_STATE;
3547 goto exit;
3548 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003550 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003551 status = PSA_ERROR_BAD_STATE;
3552 goto exit;
3553 }
Ronald Crona0d68172021-03-26 10:15:08 +01003554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003555 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003556 status = PSA_ERROR_INVALID_ARGUMENT;
3557 goto exit;
3558 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003560 status = psa_driver_wrapper_cipher_set_iv(operation,
3561 iv,
3562 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003563
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003564exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003565 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003566 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003567 } else {
3568 psa_cipher_abort(operation);
3569 }
3570 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003571}
3572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003573psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3574 const uint8_t *input,
3575 size_t input_length,
3576 uint8_t *output,
3577 size_t output_size,
3578 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003579{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003580 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003582 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003583 status = PSA_ERROR_BAD_STATE;
3584 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003585 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003587 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003588 status = PSA_ERROR_BAD_STATE;
3589 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003590 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003592 status = psa_driver_wrapper_cipher_update(operation,
3593 input,
3594 input_length,
3595 output,
3596 output_size,
3597 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003598
3599exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003600 if (status != PSA_SUCCESS) {
3601 psa_cipher_abort(operation);
3602 }
Ronald Cron6d051732020-10-01 14:10:20 +02003603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003604 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003605}
3606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003607psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3608 uint8_t *output,
3609 size_t output_size,
3610 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003611{
David Saadab4ecc272019-02-14 13:48:10 +02003612 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003614 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003615 status = PSA_ERROR_BAD_STATE;
3616 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003617 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003619 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003620 status = PSA_ERROR_BAD_STATE;
3621 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003622 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003624 status = psa_driver_wrapper_cipher_finish(operation,
3625 output,
3626 output_size,
3627 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003628
3629exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003630 if (status == PSA_SUCCESS) {
3631 return psa_cipher_abort(operation);
3632 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003633 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003634 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003636 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003637 }
mohammad1603503973b2018-03-12 15:59:30 +02003638}
3639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003640psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003641{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003642 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003643 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003644 * in use. It's ok to call abort on such an object, and there's
3645 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003646 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003647 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003649 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003650
Ronald Cron49fafa92021-03-10 08:34:23 +01003651 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003652 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003653 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003655 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003656}
3657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003658psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3659 psa_algorithm_t alg,
David Horstmann926193a2023-12-13 15:55:25 +00003660 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003661 size_t input_length,
David Horstmann926193a2023-12-13 15:55:25 +00003662 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003663 size_t output_size,
3664 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003665{
3666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003667 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003668 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003669 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003670 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3671 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003672
David Horstmannb80e35a2023-12-14 18:12:47 +00003673 LOCAL_INPUT_DECLARE(input_external, input);
3674 LOCAL_OUTPUT_DECLARE(output_external, output);
3675
David Horstmann926193a2023-12-13 15:55:25 +00003676 LOCAL_INPUT_ALLOC(input_external, input_length, input);
David Horstmann926193a2023-12-13 15:55:25 +00003677 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003679 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003680 status = PSA_ERROR_INVALID_ARGUMENT;
3681 goto exit;
3682 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003684 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3685 PSA_KEY_USAGE_ENCRYPT,
3686 alg);
3687 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003688 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003689 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003690
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003691 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003692 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003693 };
3694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003695 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3696 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003697 status = PSA_ERROR_GENERIC_ERROR;
3698 goto exit;
3699 }
3700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003701 if (default_iv_length > 0) {
3702 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003703 status = PSA_ERROR_BUFFER_TOO_SMALL;
3704 goto exit;
3705 }
3706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003707 status = psa_generate_random(local_iv, default_iv_length);
3708 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003709 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003710 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003711 }
3712
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003713 status = psa_driver_wrapper_cipher_encrypt(
3714 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003715 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003716 mbedtls_buffer_offset(output, default_iv_length),
3717 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003718
3719exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003720 unlock_status = psa_unlock_key_slot(slot);
3721 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003722 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003723 }
Ronald Cron16377072021-07-08 19:00:07 +02003724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003725 if (status == PSA_SUCCESS) {
3726 if (default_iv_length > 0) {
3727 memcpy(output, local_iv, default_iv_length);
3728 }
3729 *output_length += default_iv_length;
3730 } else {
3731 *output_length = 0;
3732 }
3733
David Horstmannb80e35a2023-12-14 18:12:47 +00003734 LOCAL_INPUT_FREE(input_external, input);
3735 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003737 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003738}
3739
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003740psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3741 psa_algorithm_t alg,
3742 const uint8_t *input,
3743 size_t input_length,
3744 uint8_t *output,
3745 size_t output_size,
3746 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003747{
3748 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003749 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003750 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003751 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003753 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003754 status = PSA_ERROR_INVALID_ARGUMENT;
3755 goto exit;
3756 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003758 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3759 PSA_KEY_USAGE_DECRYPT,
3760 alg);
3761 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003762 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003763 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003764
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003765 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003766 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003767 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003769 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003770 status = PSA_ERROR_INVALID_ARGUMENT;
3771 goto exit;
3772 }
3773
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003774 status = psa_driver_wrapper_cipher_decrypt(
3775 &attributes, slot->key.data, slot->key.bytes,
3776 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003777 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003778
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003779exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003780 unlock_status = psa_unlock_key_slot(slot);
3781 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003782 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003783 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003785 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003786 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003787 }
Ronald Cron16377072021-07-08 19:00:07 +02003788
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003789 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003790}
3791
3792
mohammad16035955c982018-04-26 00:53:03 +03003793/****************************************************************/
3794/* AEAD */
3795/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003796
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003797psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3798 psa_algorithm_t alg,
David Horstmann21c1a942023-11-28 19:25:00 +00003799 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003800 size_t nonce_length,
David Horstmann21c1a942023-11-28 19:25:00 +00003801 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003802 size_t additional_data_length,
David Horstmann21c1a942023-11-28 19:25:00 +00003803 const uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003804 size_t plaintext_length,
David Horstmann21c1a942023-11-28 19:25:00 +00003805 uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003806 size_t ciphertext_size,
3807 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003808{
Ronald Cron215633c2021-03-16 17:15:37 +01003809 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3810 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003811
David Horstmann21c1a942023-11-28 19:25:00 +00003812 LOCAL_INPUT_DECLARE(nonce_external, nonce);
3813 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
3814 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
3815 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
3816
mohammad1603f08a5502018-06-03 15:05:47 +03003817 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003819 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3820 return PSA_ERROR_NOT_SUPPORTED;
3821 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003822
Ronald Cron9a986162021-03-26 12:40:07 +01003823 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003824 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3825 if (status != PSA_SUCCESS) {
3826 return status;
3827 }
mohammad16035955c982018-04-26 00:53:03 +03003828
Ronald Cron215633c2021-03-16 17:15:37 +01003829 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003830 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003831 };
mohammad16035955c982018-04-26 00:53:03 +03003832
David Horstmann21c1a942023-11-28 19:25:00 +00003833 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
3834 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
3835 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
3836 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
3837
Ronald Cronde822812021-03-17 16:08:20 +01003838 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003839 &attributes, slot->key.data, slot->key.bytes,
3840 alg,
3841 nonce, nonce_length,
3842 additional_data, additional_data_length,
3843 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003844 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003846 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3847 memset(ciphertext, 0, ciphertext_size);
3848 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003849
David Horstmann21c1a942023-11-28 19:25:00 +00003850/* Exit label is only used for buffer copying, prevent unused warnings. */
3851#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
3852exit:
3853#endif
3854 LOCAL_INPUT_FREE(nonce_external, nonce);
3855 LOCAL_INPUT_FREE(additional_data_external, additional_data);
3856 LOCAL_INPUT_FREE(plaintext_external, plaintext);
3857 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
3858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003859 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003861 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003862}
3863
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003864psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3865 psa_algorithm_t alg,
David Horstmann6baf6e92023-11-28 19:43:53 +00003866 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003867 size_t nonce_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00003868 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003869 size_t additional_data_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00003870 const uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003871 size_t ciphertext_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00003872 uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003873 size_t plaintext_size,
3874 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003875{
Ronald Cron215633c2021-03-16 17:15:37 +01003876 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3877 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003878
David Horstmann6baf6e92023-11-28 19:43:53 +00003879 LOCAL_INPUT_DECLARE(nonce_external, nonce);
3880 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
3881 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
3882 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
3883
Gilles Peskineee652a32018-06-01 19:23:52 +02003884 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003886 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3887 return PSA_ERROR_NOT_SUPPORTED;
3888 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003889
Ronald Cron9a986162021-03-26 12:40:07 +01003890 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003891 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3892 if (status != PSA_SUCCESS) {
3893 return status;
3894 }
mohammad16035955c982018-04-26 00:53:03 +03003895
Ronald Cron215633c2021-03-16 17:15:37 +01003896 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003897 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003898 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003899
David Horstmann6baf6e92023-11-28 19:43:53 +00003900 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
3901 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
3902 additional_data);
3903 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
3904 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
3905
Ronald Cronde822812021-03-17 16:08:20 +01003906 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003907 &attributes, slot->key.data, slot->key.bytes,
3908 alg,
3909 nonce, nonce_length,
3910 additional_data, additional_data_length,
3911 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003912 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003913
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003914 if (status != PSA_SUCCESS && plaintext_size != 0) {
3915 memset(plaintext, 0, plaintext_size);
3916 }
mohammad160360a64d02018-06-03 17:20:42 +03003917
David Horstmann6baf6e92023-11-28 19:43:53 +00003918/* Exit label is only used for buffer copying, prevent unused warnings. */
3919#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
3920exit:
3921#endif
3922 LOCAL_INPUT_FREE(nonce_external, nonce);
3923 LOCAL_INPUT_FREE(additional_data_external, additional_data);
3924 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
3925 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
3926
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003927 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003928
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003929 return status;
mohammad16035955c982018-04-26 00:53:03 +03003930}
3931
Gilles Peskinee59236f2018-01-27 23:32:46 +01003932/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003933/* Generators */
3934/****************************************************************/
3935
John Durkop07cc04a2020-11-16 22:08:34 -08003936#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3937 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3938 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3939#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003940#endif /* At least one builtin KDF */
3941
3942#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3943 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3944 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3945static psa_status_t psa_key_derivation_start_hmac(
3946 psa_mac_operation_t *operation,
3947 psa_algorithm_t hash_alg,
3948 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003949 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02003950{
3951 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3952 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003953 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3954 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
3955 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02003956
Steven Cooreman15f0d922021-05-07 14:14:37 +02003957 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003958 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02003959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003960 status = psa_driver_wrapper_mac_sign_setup(operation,
3961 &attributes,
3962 hmac_key, hmac_key_length,
3963 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02003964
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003965 psa_reset_key_attributes(&attributes);
3966 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02003967}
3968#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003969
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003970#define HKDF_STATE_INIT 0 /* no input yet */
3971#define HKDF_STATE_STARTED 1 /* got salt */
3972#define HKDF_STATE_KEYED 2 /* got key */
3973#define HKDF_STATE_OUTPUT 3 /* output started */
3974
Gilles Peskinecbe66502019-05-16 16:59:18 +02003975static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003976 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003977{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003978 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
3979 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
3980 } else {
3981 return operation->alg;
3982 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003983}
3984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003985psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003986{
3987 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003988 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
3989 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02003990 /* The object has (apparently) been initialized but it is not
3991 * in use. It's ok to call abort on such an object, and there's
3992 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003993 } else
John Durkopd0321952020-10-29 21:37:36 -07003994#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003995 if (PSA_ALG_IS_HKDF(kdf_alg)) {
3996 mbedtls_free(operation->ctx.hkdf.info);
3997 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
3998 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003999#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4000#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4001 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004002 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4003 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4004 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4005 if (operation->ctx.tls12_prf.secret != NULL) {
4006 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
4007 operation->ctx.tls12_prf.secret_length);
4008 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004009 }
4010
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004011 if (operation->ctx.tls12_prf.seed != NULL) {
4012 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
4013 operation->ctx.tls12_prf.seed_length);
4014 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01004015 }
4016
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004017 if (operation->ctx.tls12_prf.label != NULL) {
4018 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
4019 operation->ctx.tls12_prf.label_length);
4020 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01004021 }
4022
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004023 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004024
4025 /* We leave the fields Ai and output_block to be erased safely by the
4026 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004027 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004028#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4029 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004030 {
4031 status = PSA_ERROR_BAD_STATE;
4032 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004033 mbedtls_platform_zeroize(operation, sizeof(*operation));
4034 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004035}
4036
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004037psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004038 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004039{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004040 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004041 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004042 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004043 }
4044
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004045 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004046 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004047}
4048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004049psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4050 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004051{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004052 if (operation->alg == 0) {
4053 return PSA_ERROR_BAD_STATE;
4054 }
4055 if (capacity > operation->capacity) {
4056 return PSA_ERROR_INVALID_ARGUMENT;
4057 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004058 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004059 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004060}
4061
John Durkopd0321952020-10-29 21:37:36 -07004062#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004063/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004064 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004065static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
4066 psa_algorithm_t hash_alg,
4067 uint8_t *output,
4068 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004069{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004070 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02004071 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004072 psa_status_t status;
4073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004074 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
4075 return PSA_ERROR_BAD_STATE;
4076 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004077 hkdf->state = HKDF_STATE_OUTPUT;
4078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004079 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004080 /* Copy what remains of the current block */
4081 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004082 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004083 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004084 }
4085 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004086 output += n;
4087 output_length -= n;
4088 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004089 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004090 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004091 }
Gilles Peskined54931c2018-07-17 21:06:59 +02004092 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004093 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004094 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004095 * object was corrupted or if this function is called directly
4096 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004097 if (hkdf->block_number == 0xff) {
4098 return PSA_ERROR_BAD_STATE;
4099 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004100
4101 /* We need a new block */
4102 ++hkdf->block_number;
4103 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004105 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4106 hash_alg,
4107 hkdf->prk,
4108 hash_length);
4109 if (status != PSA_SUCCESS) {
4110 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004111 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004112
4113 if (hkdf->block_number != 1) {
4114 status = psa_mac_update(&hkdf->hmac,
4115 hkdf->output_block,
4116 hash_length);
4117 if (status != PSA_SUCCESS) {
4118 return status;
4119 }
4120 }
4121 status = psa_mac_update(&hkdf->hmac,
4122 hkdf->info,
4123 hkdf->info_length);
4124 if (status != PSA_SUCCESS) {
4125 return status;
4126 }
4127 status = psa_mac_update(&hkdf->hmac,
4128 &hkdf->block_number, 1);
4129 if (status != PSA_SUCCESS) {
4130 return status;
4131 }
4132 status = psa_mac_sign_finish(&hkdf->hmac,
4133 hkdf->output_block,
4134 sizeof(hkdf->output_block),
4135 &hmac_output_length);
4136 if (status != PSA_SUCCESS) {
4137 return status;
4138 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004139 }
4140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004141 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004142}
John Durkop07cc04a2020-11-16 22:08:34 -08004143#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004144
John Durkop07cc04a2020-11-16 22:08:34 -08004145#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4146 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004147static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4148 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004149 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004150{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004151 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4152 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004153 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4154 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004155 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004156
Janos Follath7742fee2019-06-17 12:58:10 +01004157 /* We can't be wanting more output after block 0xff, otherwise
4158 * the capacity check in psa_key_derivation_output_bytes() would have
4159 * prevented this call. It could happen only if the operation
4160 * object was corrupted or if this function is called directly
4161 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004162 if (tls12_prf->block_number == 0xff) {
4163 return PSA_ERROR_CORRUPTION_DETECTED;
4164 }
Janos Follath7742fee2019-06-17 12:58:10 +01004165
4166 /* We need a new block */
4167 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004168 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004169
4170 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4171 *
4172 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4173 *
4174 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4175 * HMAC_hash(secret, A(2) + seed) +
4176 * HMAC_hash(secret, A(3) + seed) + ...
4177 *
4178 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004179 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004180 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004181 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004182 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004183 * is currently extracted as `output_block` and where i is
4184 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004185 */
4186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004187 status = psa_key_derivation_start_hmac(&hmac,
4188 hash_alg,
4189 tls12_prf->secret,
4190 tls12_prf->secret_length);
4191 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004192 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004193 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004194
4195 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004196 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004197 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4198 * the variable seed and in this instance means it in the context of the
4199 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004200 status = psa_mac_update(&hmac,
4201 tls12_prf->label,
4202 tls12_prf->label_length);
4203 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004204 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004205 }
4206 status = psa_mac_update(&hmac,
4207 tls12_prf->seed,
4208 tls12_prf->seed_length);
4209 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004210 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004211 }
4212 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004213 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004214 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4215 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004216 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004217 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004218 }
4219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004220 status = psa_mac_sign_finish(&hmac,
4221 tls12_prf->Ai, hash_length,
4222 &hmac_output_length);
4223 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004224 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004225 }
4226 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004227 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004228 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004229
4230 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004231 status = psa_key_derivation_start_hmac(&hmac,
4232 hash_alg,
4233 tls12_prf->secret,
4234 tls12_prf->secret_length);
4235 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004236 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004237 }
4238 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4239 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004240 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004241 }
4242 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4243 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004244 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004245 }
4246 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4247 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004248 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004249 }
4250 status = psa_mac_sign_finish(&hmac,
4251 tls12_prf->output_block, hash_length,
4252 &hmac_output_length);
4253 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004254 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004255 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004256
Janos Follath7742fee2019-06-17 12:58:10 +01004257
4258cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004259 cleanup_status = psa_mac_abort(&hmac);
4260 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004261 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004262 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004264 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004265}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004266
Janos Follath844eb0e2019-06-19 12:10:49 +01004267static psa_status_t psa_key_derivation_tls12_prf_read(
4268 psa_tls12_prf_key_derivation_t *tls12_prf,
4269 psa_algorithm_t alg,
4270 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004271 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004272{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004273 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4274 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004275 psa_status_t status;
4276 uint8_t offset, length;
4277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004278 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004279 case PSA_TLS12_PRF_STATE_LABEL_SET:
4280 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4281 break;
4282 case PSA_TLS12_PRF_STATE_OUTPUT:
4283 break;
4284 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004285 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004286 }
4287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004288 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004289 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004290 if (tls12_prf->left_in_block == 0) {
4291 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4292 alg);
4293 if (status != PSA_SUCCESS) {
4294 return status;
4295 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004296
4297 continue;
4298 }
4299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004300 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004301 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004302 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004303 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004304 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004305
4306 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004307 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004308 output += length;
4309 output_length -= length;
4310 tls12_prf->left_in_block -= length;
4311 }
4312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004313 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004314}
John Durkop07cc04a2020-11-16 22:08:34 -08004315#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4316 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004317
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004318psa_status_t psa_key_derivation_output_bytes(
4319 psa_key_derivation_operation_t *operation,
4320 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004321 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004322{
4323 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004324 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004326 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004327 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004328 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004329 }
4330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004331 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004332 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004333 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004334 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004335 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004336 goto exit;
4337 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004338 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004339 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004340 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004341 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4342 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004343 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004344 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004345 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004346 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004347 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004348
John Durkopd0321952020-10-29 21:37:36 -07004349#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004350 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4351 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4352 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4353 output, output_length);
4354 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004355#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4356#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4357 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004358 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4359 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4360 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4361 kdf_alg, output,
4362 output_length);
4363 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004364#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4365 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004366 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004367 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004368 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004369 }
4370
4371exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004372 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004373 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004374 * This allows us to differentiate between exhausted operations and
4375 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4376 * operations. */
4377 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004378 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004379 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004380 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004381 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004382 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004383}
4384
David Brown7807bf72021-02-09 16:28:23 -07004385#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004386static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004387{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004388 if (data_size >= 8) {
4389 mbedtls_des_key_set_parity(data);
4390 }
4391 if (data_size >= 16) {
4392 mbedtls_des_key_set_parity(data + 8);
4393 }
4394 if (data_size >= 24) {
4395 mbedtls_des_key_set_parity(data + 16);
4396 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004397}
David Brown7807bf72021-02-09 16:28:23 -07004398#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004399
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004400static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004401 psa_key_slot_t *slot,
4402 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004403 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004404{
4405 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004406 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004407 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004408 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004410 if (!key_type_is_raw_bytes(slot->attr.type)) {
4411 return PSA_ERROR_INVALID_ARGUMENT;
4412 }
4413 if (bits % 8 != 0) {
4414 return PSA_ERROR_INVALID_ARGUMENT;
4415 }
4416 data = mbedtls_calloc(1, bytes);
4417 if (data == NULL) {
4418 return PSA_ERROR_INSUFFICIENT_MEMORY;
4419 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004421 status = psa_key_derivation_output_bytes(operation, data, bytes);
4422 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004423 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004424 }
David Brown12ca5032021-02-11 11:02:00 -07004425#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004426 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4427 psa_des_set_key_parity(data, bytes);
4428 }
David Brown8107e312021-02-12 08:08:46 -07004429#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004431 status = psa_allocate_buffer_to_slot(slot, bytes);
4432 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004433 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004434 }
Ronald Crondd04d422020-11-28 15:14:42 +01004435
Ronald Cronbf33c932020-11-28 18:06:53 +01004436 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004437 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004438 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004439 };
4440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004441 status = psa_driver_wrapper_import_key(&attributes,
4442 data, bytes,
4443 slot->key.data,
4444 slot->key.bytes,
4445 &slot->key.bytes, &bits);
4446 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004447 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004448 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004449
4450exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004451 mbedtls_free(data);
4452 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004453}
4454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004455psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4456 psa_key_derivation_operation_t *operation,
4457 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004458{
4459 psa_status_t status;
4460 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004461 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004462
Ronald Cron81709fc2020-11-14 12:10:32 +01004463 *key = MBEDTLS_SVC_KEY_ID_INIT;
4464
Gilles Peskine0f84d622019-09-12 19:03:13 +02004465 /* Reject any attempt to create a zero-length key so that we don't
4466 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004467 if (psa_get_key_bits(attributes) == 0) {
4468 return PSA_ERROR_INVALID_ARGUMENT;
4469 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004471 if (operation->alg == PSA_ALG_NONE) {
4472 return PSA_ERROR_BAD_STATE;
4473 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004474
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004475 if (!operation->can_output_key) {
4476 return PSA_ERROR_NOT_PERMITTED;
4477 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004479 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4480 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004481#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004482 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004483 /* Deriving a key in a secure element is not implemented yet. */
4484 status = PSA_ERROR_NOT_SUPPORTED;
4485 }
4486#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004487 if (status == PSA_SUCCESS) {
4488 status = psa_generate_derived_key_internal(slot,
4489 attributes->core.bits,
4490 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004491 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004492 if (status == PSA_SUCCESS) {
4493 status = psa_finish_key_creation(slot, driver, key);
4494 }
4495 if (status != PSA_SUCCESS) {
4496 psa_fail_key_creation(slot, driver);
4497 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004499 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004500}
4501
Gilles Peskineeab56e42018-07-12 17:12:33 +02004502
4503
4504/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004505/* Key derivation */
4506/****************************************************************/
4507
Steven Cooreman932ffb72021-02-15 12:14:32 +01004508#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004509static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004510{
4511#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004512 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4513 return 1;
4514 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004515#endif
4516#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004517 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4518 return 1;
4519 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004520#endif
4521#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004522 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4523 return 1;
4524 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004525#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004526 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004527}
4528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004529static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004530{
4531 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004532 psa_status_t status = psa_hash_setup(&operation, alg);
4533 psa_hash_abort(&operation);
4534 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004535}
4536
Gilles Peskine969c5d62019-01-16 15:53:06 +01004537static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004538 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004539 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004540{
Janos Follath5fe19732019-06-20 15:09:30 +01004541 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4542 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004543 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004544
Gilles Peskine969c5d62019-01-16 15:53:06 +01004545 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004546 if (!is_kdf_alg_supported(kdf_alg)) {
4547 return PSA_ERROR_NOT_SUPPORTED;
4548 }
John Durkop07cc04a2020-11-16 22:08:34 -08004549
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004550 /* All currently supported key derivation algorithms are based on a
4551 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004552 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4553 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4554 if (hash_size == 0) {
4555 return PSA_ERROR_NOT_SUPPORTED;
4556 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004557
4558 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4559 * we might fail later, which is somewhat unfriendly and potentially
4560 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004561 psa_status_t status = psa_hash_try_support(hash_alg);
4562 if (status != PSA_SUCCESS) {
4563 return status;
4564 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004566 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4567 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4568 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4569 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004570 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004571
Gilles Peskinecdacf042021-04-29 21:10:00 +02004572 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004573 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004574}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004576static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004577{
4578#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004579 if (alg == PSA_ALG_ECDH) {
4580 return PSA_SUCCESS;
4581 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004582#endif
4583 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004584 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004585}
John Durkop07cc04a2020-11-16 22:08:34 -08004586#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004588psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4589 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004590{
4591 psa_status_t status;
4592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004593 if (operation->alg != 0) {
4594 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004595 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004597 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4598 return PSA_ERROR_INVALID_ARGUMENT;
4599 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4600#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4601 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4602 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4603 status = psa_key_agreement_try_support(ka_alg);
4604 if (status != PSA_SUCCESS) {
4605 return status;
4606 }
4607 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4608#else
4609 return PSA_ERROR_NOT_SUPPORTED;
4610#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4611 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4612#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4613 status = psa_key_derivation_setup_kdf(operation, alg);
4614#else
4615 return PSA_ERROR_NOT_SUPPORTED;
4616#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4617 } else {
4618 return PSA_ERROR_INVALID_ARGUMENT;
4619 }
4620
4621 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004622 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623 }
4624 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004625}
4626
John Durkopd0321952020-10-29 21:37:36 -07004627#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004628static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4629 psa_algorithm_t hash_alg,
4630 psa_key_derivation_step_t step,
4631 const uint8_t *data,
4632 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004633{
4634 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004635 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004636 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004637 if (hkdf->state != HKDF_STATE_INIT) {
4638 return PSA_ERROR_BAD_STATE;
4639 } else {
4640 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4641 hash_alg,
4642 data, data_length);
4643 if (status != PSA_SUCCESS) {
4644 return status;
4645 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004646 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004647 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004648 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004649 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004650 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004651 if (hkdf->state == HKDF_STATE_INIT) {
4652 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4653 hash_alg,
4654 NULL, 0);
4655 if (status != PSA_SUCCESS) {
4656 return status;
4657 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004658 hkdf->state = HKDF_STATE_STARTED;
4659 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004660 if (hkdf->state != HKDF_STATE_STARTED) {
4661 return PSA_ERROR_BAD_STATE;
4662 }
4663 status = psa_mac_update(&hkdf->hmac,
4664 data, data_length);
4665 if (status != PSA_SUCCESS) {
4666 return status;
4667 }
4668 status = psa_mac_sign_finish(&hkdf->hmac,
4669 hkdf->prk,
4670 sizeof(hkdf->prk),
4671 &data_length);
4672 if (status != PSA_SUCCESS) {
4673 return status;
4674 }
4675 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004676 hkdf->block_number = 0;
4677 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004678 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004679 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004680 if (hkdf->state == HKDF_STATE_OUTPUT) {
4681 return PSA_ERROR_BAD_STATE;
4682 }
4683 if (hkdf->info_set) {
4684 return PSA_ERROR_BAD_STATE;
4685 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004686 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004687 if (data_length != 0) {
4688 hkdf->info = mbedtls_calloc(1, data_length);
4689 if (hkdf->info == NULL) {
4690 return PSA_ERROR_INSUFFICIENT_MEMORY;
4691 }
4692 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004693 }
4694 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004695 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004696 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004697 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004698 }
4699}
John Durkop07cc04a2020-11-16 22:08:34 -08004700#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004701
John Durkop07cc04a2020-11-16 22:08:34 -08004702#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4703 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004704static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4705 const uint8_t *data,
4706 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004707{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004708 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4709 return PSA_ERROR_BAD_STATE;
4710 }
Janos Follathf08e2652019-06-13 09:05:41 +01004711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004712 if (data_length != 0) {
4713 prf->seed = mbedtls_calloc(1, data_length);
4714 if (prf->seed == NULL) {
4715 return PSA_ERROR_INSUFFICIENT_MEMORY;
4716 }
Janos Follathf08e2652019-06-13 09:05:41 +01004717
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004718 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004719 prf->seed_length = data_length;
4720 }
Janos Follathf08e2652019-06-13 09:05:41 +01004721
Gilles Peskine43f958b2020-12-13 14:55:14 +01004722 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004723
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004724 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004725}
4726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004727static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4728 const uint8_t *data,
4729 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004730{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004731 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4732 return PSA_ERROR_BAD_STATE;
4733 }
Janos Follath81550542019-06-13 14:26:34 +01004734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004735 if (data_length != 0) {
4736 prf->secret = mbedtls_calloc(1, data_length);
4737 if (prf->secret == NULL) {
4738 return PSA_ERROR_INSUFFICIENT_MEMORY;
4739 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004740
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004741 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004742 prf->secret_length = data_length;
4743 }
Janos Follath81550542019-06-13 14:26:34 +01004744
Gilles Peskine43f958b2020-12-13 14:55:14 +01004745 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004746
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004747 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004748}
4749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004750static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4751 const uint8_t *data,
4752 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004753{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004754 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4755 return PSA_ERROR_BAD_STATE;
4756 }
Janos Follath63028dd2019-06-13 09:15:47 +01004757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004758 if (data_length != 0) {
4759 prf->label = mbedtls_calloc(1, data_length);
4760 if (prf->label == NULL) {
4761 return PSA_ERROR_INSUFFICIENT_MEMORY;
4762 }
Janos Follath63028dd2019-06-13 09:15:47 +01004763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004764 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004765 prf->label_length = data_length;
4766 }
Janos Follath63028dd2019-06-13 09:15:47 +01004767
Gilles Peskine43f958b2020-12-13 14:55:14 +01004768 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004770 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01004771}
4772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004773static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
4774 psa_key_derivation_step_t step,
4775 const uint8_t *data,
4776 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01004777{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004778 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01004779 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004780 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01004781 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004782 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01004783 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004784 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01004785 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004786 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01004787 }
4788}
John Durkop07cc04a2020-11-16 22:08:34 -08004789#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4790 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4791
4792#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4793static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4794 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004795 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004796 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08004797{
4798 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004799 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08004800 uint8_t *cur = pms;
4801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004802 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
4803 return PSA_ERROR_INVALID_ARGUMENT;
4804 }
John Durkop07cc04a2020-11-16 22:08:34 -08004805
4806 /* Quoting RFC 4279, Section 2:
4807 *
4808 * The premaster secret is formed as follows: if the PSK is N octets
4809 * long, concatenate a uint16 with the value N, N zero octets, a second
4810 * uint16 with the value N, and the PSK itself.
4811 */
4812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004813 *cur++ = MBEDTLS_BYTE_1(data_length);
4814 *cur++ = MBEDTLS_BYTE_0(data_length);
4815 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004816 cur += data_length;
4817 *cur++ = pms[0];
4818 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004819 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004820 cur += data_length;
4821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004822 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08004823
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004824 mbedtls_platform_zeroize(pms, sizeof(pms));
4825 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08004826}
Janos Follath6660f0e2019-06-17 08:44:03 +01004827
4828static psa_status_t psa_tls12_prf_psk_to_ms_input(
4829 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004830 psa_key_derivation_step_t step,
4831 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004832 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01004833{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004834 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
4835 return psa_tls12_prf_psk_to_ms_set_key(prf,
4836 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01004837 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004839 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01004840}
John Durkop07cc04a2020-11-16 22:08:34 -08004841#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004842
Gilles Peskineb8965192019-09-24 16:21:10 +02004843/** Check whether the given key type is acceptable for the given
4844 * input step of a key derivation.
4845 *
4846 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4847 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4848 * Both secret and non-secret inputs can alternatively have the type
4849 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4850 * that the input was passed as a buffer rather than via a key object.
4851 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004852static int psa_key_derivation_check_input_type(
4853 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004854 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02004855{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004856 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004857 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004858 if (key_type == PSA_KEY_TYPE_DERIVE) {
4859 return PSA_SUCCESS;
4860 }
4861 if (key_type == PSA_KEY_TYPE_NONE) {
4862 return PSA_SUCCESS;
4863 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004864 break;
4865 case PSA_KEY_DERIVATION_INPUT_LABEL:
4866 case PSA_KEY_DERIVATION_INPUT_SALT:
4867 case PSA_KEY_DERIVATION_INPUT_INFO:
4868 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004869 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
4870 return PSA_SUCCESS;
4871 }
4872 if (key_type == PSA_KEY_TYPE_NONE) {
4873 return PSA_SUCCESS;
4874 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004875 break;
4876 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004877 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02004878}
4879
Janos Follathb80a94e2019-06-12 15:54:46 +01004880static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004881 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004882 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004883 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004884 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004885 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004886{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004887 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004888 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004889
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004890 status = psa_key_derivation_check_input_type(step, key_type);
4891 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004892 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004893 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004894
John Durkopd0321952020-10-29 21:37:36 -07004895#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004896 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4897 status = psa_hkdf_input(&operation->ctx.hkdf,
4898 PSA_ALG_HKDF_GET_HASH(kdf_alg),
4899 step, data, data_length);
4900 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004901#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4902#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004903 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4904 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
4905 step, data, data_length);
4906 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004907#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4908#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004909 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4910 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
4911 step, data, data_length);
4912 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004913#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004914 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004915 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004916 (void) data;
4917 (void) data_length;
4918 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004919 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004920 }
4921
Gilles Peskine224b0d62019-09-23 18:13:17 +02004922exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004923 if (status != PSA_SUCCESS) {
4924 psa_key_derivation_abort(operation);
4925 }
4926 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004927}
4928
Janos Follath51f4a0f2019-06-14 11:35:55 +01004929psa_status_t psa_key_derivation_input_bytes(
4930 psa_key_derivation_operation_t *operation,
4931 psa_key_derivation_step_t step,
4932 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004933 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004934{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004935 return psa_key_derivation_input_internal(operation, step,
4936 PSA_KEY_TYPE_NONE,
4937 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004938}
4939
Janos Follath51f4a0f2019-06-14 11:35:55 +01004940psa_status_t psa_key_derivation_input_key(
4941 psa_key_derivation_operation_t *operation,
4942 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004943 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004944{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004945 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004946 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004947 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004948
Ronald Cron5c522922020-11-14 16:35:34 +01004949 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004950 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4951 if (status != PSA_SUCCESS) {
4952 psa_key_derivation_abort(operation);
4953 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02004954 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004955
4956 /* Passing a key object as a SECRET input unlocks the permission
4957 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004958 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004959 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004960 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004962 status = psa_key_derivation_input_internal(operation,
4963 step, slot->attr.type,
4964 slot->key.data,
4965 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004967 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004968
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004969 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004970}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004971
4972
4973
4974/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004975/* Key agreement */
4976/****************************************************************/
4977
John Durkop6ba40d12020-11-10 08:50:04 -08004978#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004979static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
4980 size_t peer_key_length,
4981 const mbedtls_ecp_keypair *our_key,
4982 uint8_t *shared_secret,
4983 size_t shared_secret_size,
4984 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004985{
Steven Cooremand4867872020-08-05 16:31:39 +02004986 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004987 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004988 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004989 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004990 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
4991 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004992
Ronald Cron90857082020-11-25 14:58:33 +01004993 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004994 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
4995 bits,
4996 peer_key,
4997 peer_key_length,
4998 &their_key);
4999 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005000 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005001 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005002
Jaeden Amero97271b32019-01-10 19:38:51 +00005003 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005004 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
5005 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005006 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005007 }
Jaeden Amero97271b32019-01-10 19:38:51 +00005008 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005009 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
5010 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005011 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005012 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005013
Jaeden Amero97271b32019-01-10 19:38:51 +00005014 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005015 mbedtls_ecdh_calc_secret(&ecdh,
5016 shared_secret_length,
5017 shared_secret, shared_secret_size,
5018 mbedtls_psa_get_random,
5019 MBEDTLS_PSA_RANDOM_STATE));
5020 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005021 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005022 }
5023 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005024 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005025 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005026
5027exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005028 if (status != PSA_SUCCESS) {
5029 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
5030 }
5031 mbedtls_ecdh_free(&ecdh);
5032 mbedtls_ecp_keypair_free(their_key);
5033 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02005034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005035 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005036}
John Durkop6ba40d12020-11-10 08:50:04 -08005037#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005038
Gilles Peskine01d718c2018-09-18 12:01:02 +02005039#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005041static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
5042 psa_key_slot_t *private_key,
5043 const uint8_t *peer_key,
5044 size_t peer_key_length,
5045 uint8_t *shared_secret,
5046 size_t shared_secret_size,
5047 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005048{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005049 mbedtls_ecp_keypair *ecp = NULL;
5050 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01005051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005052 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08005053#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005054 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005055 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
5056 return PSA_ERROR_INVALID_ARGUMENT;
5057 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005058 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005059 private_key->attr.type,
5060 private_key->attr.bits,
5061 private_key->key.data,
5062 private_key->key.bytes,
5063 &ecp);
5064 if (status != PSA_SUCCESS) {
5065 return status;
5066 }
5067 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
5068 ecp,
5069 shared_secret, shared_secret_size,
5070 shared_secret_length);
5071 mbedtls_ecp_keypair_free(ecp);
5072 mbedtls_free(ecp);
5073 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08005074#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005075 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005076 (void) ecp;
5077 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01005078 (void) private_key;
5079 (void) peer_key;
5080 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005081 (void) shared_secret;
5082 (void) shared_secret_size;
5083 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005084 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005085 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005086}
5087
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005088/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005089 * to potentially free embedded data structures and wipe confidential data.
5090 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005091static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5092 psa_key_derivation_step_t step,
5093 psa_key_slot_t *private_key,
5094 const uint8_t *peer_key,
5095 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005096{
5097 psa_status_t status;
5098 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5099 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005100 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005101
5102 /* Step 1: run the secret agreement algorithm to generate the shared
5103 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005104 status = psa_key_agreement_raw_internal(ka_alg,
5105 private_key,
5106 peer_key, peer_key_length,
5107 shared_secret,
5108 sizeof(shared_secret),
5109 &shared_secret_length);
5110 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005111 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005112 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005113
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005114 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005115 * the shared secret. A shared secret is permitted wherever a key
5116 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005117 status = psa_key_derivation_input_internal(operation, step,
5118 PSA_KEY_TYPE_DERIVE,
5119 shared_secret,
5120 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005121exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005122 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5123 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005124}
5125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005126psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5127 psa_key_derivation_step_t step,
5128 mbedtls_svc_key_id_t private_key,
5129 const uint8_t *peer_key,
5130 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005131{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005132 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005133 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005134 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005136 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5137 return PSA_ERROR_INVALID_ARGUMENT;
5138 }
Ronald Cron5c522922020-11-14 16:35:34 +01005139 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005140 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5141 if (status != PSA_SUCCESS) {
5142 return status;
5143 }
5144 status = psa_key_agreement_internal(operation, step,
5145 slot,
5146 peer_key, peer_key_length);
5147 if (status != PSA_SUCCESS) {
5148 psa_key_derivation_abort(operation);
5149 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005150 /* If a private key has been added as SECRET, we allow the derived
5151 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005152 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005153 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005154 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005155 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005157 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005159 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005160}
5161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005162psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5163 mbedtls_svc_key_id_t private_key,
5164 const uint8_t *peer_key,
5165 size_t peer_key_length,
5166 uint8_t *output,
5167 size_t output_size,
5168 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005169{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005171 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005172 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005173 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005175 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005176 status = PSA_ERROR_INVALID_ARGUMENT;
5177 goto exit;
5178 }
Ronald Cron5c522922020-11-14 16:35:34 +01005179 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005180 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5181 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005182 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005183 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005184
Gilles Peskine42313fb2022-04-14 00:17:15 +02005185 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5186 * for the output size. The PSA specification only guarantees that this
5187 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5188 * but it might be nice to allow smaller buffers if the output fits.
5189 * At the time of writing this comment, with only ECDH implemented,
5190 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5191 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5192 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005193 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005194 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5195 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005196 status = PSA_ERROR_BUFFER_TOO_SMALL;
5197 goto exit;
5198 }
5199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005200 status = psa_key_agreement_raw_internal(alg, slot,
5201 peer_key, peer_key_length,
5202 output, output_size,
5203 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005204
5205exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005206 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005207 /* If an error happens and is not handled properly, the output
5208 * may be used as a key to protect sensitive data. Arrange for such
5209 * a key to be random, which is likely to result in decryption or
5210 * verification errors. This is better than filling the buffer with
5211 * some constant data such as zeros, which would result in the data
5212 * being protected with a reproducible, easily knowable key.
5213 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005214 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005215 *output_length = output_size;
5216 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005218 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005220 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005221}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005222
5223
Gilles Peskine30524eb2020-11-13 17:02:26 +01005224
Gilles Peskine86a440b2018-11-12 18:39:40 +01005225/****************************************************************/
5226/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005227/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005228
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005229#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5230#include "mbedtls/entropy_poll.h"
5231#endif
5232
Gilles Peskine30524eb2020-11-13 17:02:26 +01005233/** Initialize the PSA random generator.
5234 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005235static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005236{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005237#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005238 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005239#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5240
Gilles Peskine30524eb2020-11-13 17:02:26 +01005241 /* Set default configuration if
5242 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005243 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005244 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005245 }
5246 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005247 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005248 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005250 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005251#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5252 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5253 /* The PSA entropy injection feature depends on using NV seed as an entropy
5254 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005255 mbedtls_entropy_add_source(&rng->entropy,
5256 mbedtls_nv_seed_poll, NULL,
5257 MBEDTLS_ENTROPY_BLOCK_SIZE,
5258 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005259#endif
5260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005261 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005262#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005263}
5264
5265/** Deinitialize the PSA random generator.
5266 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005267static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005268{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005269#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005270 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005271#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005272 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5273 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005274#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005275}
5276
5277/** Seed the PSA random generator.
5278 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005279static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005280{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005281#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5282 /* Do nothing: the external RNG seeds itself. */
5283 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005284 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005285#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005286 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005287 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5288 drbg_seed, sizeof(drbg_seed) - 1);
5289 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005290#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005291}
5292
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005293psa_status_t psa_generate_random(uint8_t *output,
5294 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005295{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005296 GUARD_MODULE_INITIALIZED;
5297
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005298#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5299
5300 size_t output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005301 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
5302 output, output_size,
5303 &output_length);
5304 if (status != PSA_SUCCESS) {
5305 return status;
5306 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005307 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00005308 * for the external RNG interface. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005309 if (output_length != output_size) {
5310 return PSA_ERROR_INSUFFICIENT_ENTROPY;
5311 }
5312 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005313
5314#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005316 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005317 size_t request_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005318 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5319 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5320 output_size);
5321 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
5322 output, request_size);
5323 if (ret != 0) {
5324 return mbedtls_to_psa_error(ret);
5325 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01005326 output_size -= request_size;
5327 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005328 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005329 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005330#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005331}
5332
Gilles Peskine8814fc42020-12-14 15:33:44 +01005333/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005334 *
5335 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5336 * `psa_generate_random(...)`. The state parameter is ignored since the
5337 * PSA API doesn't support passing an explicit state.
5338 *
5339 * In the non-external case, psa_generate_random() calls an
5340 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5341 * and semantics as mbedtls_psa_get_random(). As an optimization,
5342 * instead of doing this back-and-forth between the PSA API and the
5343 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5344 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005345 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005346#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5347int mbedtls_psa_get_random(void *p_rng,
5348 unsigned char *output,
5349 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005350{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005351 /* This function takes a pointer to the RNG state because that's what
5352 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5353 * its own state internally and doesn't let the caller access that state.
5354 * So we just ignore the state parameter, and in practice we'll pass
5355 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005356 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005357 psa_status_t status = psa_generate_random(output, output_size);
5358 if (status == PSA_SUCCESS) {
5359 return 0;
5360 } else {
5361 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5362 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005363}
5364#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5365
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005366#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005367psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5368 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005369{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005370 if (global_data.initialized) {
5371 return PSA_ERROR_NOT_PERMITTED;
5372 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005374 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5375 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5376 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5377 return PSA_ERROR_INVALID_ARGUMENT;
5378 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005380 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005381}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005382#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005383
Ronald Cron3772afe2021-02-08 16:10:05 +01005384/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005385 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005386 * \param type The key type
5387 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005388 *
5389 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005390 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005391 * \retval #PSA_ERROR_INVALID_ARGUMENT
5392 * The size in bits of the key is not valid.
5393 * \retval #PSA_ERROR_NOT_SUPPORTED
5394 * The type and/or the size in bits of the key or the combination of
5395 * the two is not supported.
5396 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005397static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005398 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005399{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005400 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005402 if (key_type_is_raw_bytes(type)) {
5403 status = validate_unstructured_key_bit_size(type, bits);
5404 if (status != PSA_SUCCESS) {
5405 return status;
5406 }
5407 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005408#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005409 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5410 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5411 return PSA_ERROR_NOT_SUPPORTED;
5412 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005413
Ronald Cron01b2aba2020-10-05 09:42:02 +02005414 /* Accept only byte-aligned keys, for the same reasons as
5415 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005416 if (bits % 8 != 0) {
5417 return PSA_ERROR_NOT_SUPPORTED;
5418 }
5419 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005420#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005421
Ronald Cron5c4d3862020-12-07 11:07:24 +01005422#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005423 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005424 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005425 return PSA_SUCCESS;
5426 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005427#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005428 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005429 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005430 }
5431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005432 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005433}
5434
Ronald Cron55ed0592020-10-05 10:30:40 +02005435psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005436 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005437 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005438{
5439 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005440 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005442 if ((attributes->domain_parameters == NULL) &&
5443 (attributes->domain_parameters_size != 0)) {
5444 return PSA_ERROR_INVALID_ARGUMENT;
5445 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005447 if (key_type_is_raw_bytes(type)) {
5448 status = psa_generate_random(key_buffer, key_buffer_size);
5449 if (status != PSA_SUCCESS) {
5450 return status;
5451 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005452
David Brown12ca5032021-02-11 11:02:00 -07005453#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005454 if (type == PSA_KEY_TYPE_DES) {
5455 psa_des_set_key_parity(key_buffer, key_buffer_size);
5456 }
David Brown8107e312021-02-12 08:08:46 -07005457#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005458 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005459
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005460#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5461 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005462 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5463 return mbedtls_psa_rsa_generate_key(attributes,
5464 key_buffer,
5465 key_buffer_size,
5466 key_buffer_length);
5467 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005468#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5469 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005470
John Durkop9814fa22020-11-04 12:28:15 -08005471#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005472 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5473 return mbedtls_psa_ecp_generate_key(attributes,
5474 key_buffer,
5475 key_buffer_size,
5476 key_buffer_length);
5477 } else
John Durkop9814fa22020-11-04 12:28:15 -08005478#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005479 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005480 (void) key_buffer_length;
5481 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005482 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005484 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005485}
Darryl Green0c6575a2018-11-07 16:05:30 +00005486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005487psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5488 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005489{
5490 psa_status_t status;
5491 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005492 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005493 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005494
Ronald Cron81709fc2020-11-14 12:10:32 +01005495 *key = MBEDTLS_SVC_KEY_ID_INIT;
5496
Gilles Peskine0f84d622019-09-12 19:03:13 +02005497 /* Reject any attempt to create a zero-length key so that we don't
5498 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005499 if (psa_get_key_bits(attributes) == 0) {
5500 return PSA_ERROR_INVALID_ARGUMENT;
5501 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005502
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005503 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005504 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5505 return PSA_ERROR_INVALID_ARGUMENT;
5506 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005507
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005508 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5509 &slot, &driver);
5510 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005511 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005512 }
Gilles Peskine11792082019-08-06 18:36:36 +02005513
Ronald Cron977c2472020-10-13 08:32:21 +02005514 /* In the case of a transparent key or an opaque key stored in local
5515 * storage (thus not in the case of generating a key in a secure element
5516 * or cryptoprocessor with storage), we have to allocate a buffer to
5517 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005518 if (slot->key.data == NULL) {
5519 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5520 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005521 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005522 attributes->core.type, attributes->core.bits);
5523 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005524 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005525 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005526
5527 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005528 attributes->core.type,
5529 attributes->core.bits);
5530 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005531 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005532 attributes, &key_buffer_size);
5533 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005534 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005535 }
Ronald Cron977c2472020-10-13 08:32:21 +02005536 }
Ronald Cron977c2472020-10-13 08:32:21 +02005537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005538 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5539 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005540 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005541 }
Ronald Cron977c2472020-10-13 08:32:21 +02005542 }
5543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005544 status = psa_driver_wrapper_generate_key(attributes,
5545 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005547 if (status != PSA_SUCCESS) {
5548 psa_remove_key_data_from_memory(slot);
5549 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005550
Gilles Peskine11792082019-08-06 18:36:36 +02005551exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005552 if (status == PSA_SUCCESS) {
5553 status = psa_finish_key_creation(slot, driver, key);
5554 }
5555 if (status != PSA_SUCCESS) {
5556 psa_fail_key_creation(slot, driver);
5557 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005559 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005560}
5561
Gilles Peskinee59236f2018-01-27 23:32:46 +01005562/****************************************************************/
5563/* Module setup */
5564/****************************************************************/
5565
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005566#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005567psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005568 void (* entropy_init)(mbedtls_entropy_context *ctx),
5569 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005570{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005571 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5572 return PSA_ERROR_BAD_STATE;
5573 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005574 global_data.rng.entropy_init = entropy_init;
5575 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005576 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005577}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005578#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005580void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005581{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005582 psa_wipe_all_key_slots();
5583 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5584 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005585 }
5586 /* Wipe all remaining data, including configuration.
5587 * In particular, this sets all state indicator to the value
5588 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005589 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005590
5591 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005592 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005593}
5594
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005595#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5596/** Recover a transaction that was interrupted by a power failure.
5597 *
5598 * This function is called during initialization, before psa_crypto_init()
5599 * returns. If this function returns a failure status, the initialization
5600 * fails.
5601 */
5602static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005603 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005604{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005605 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005606 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5607 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005608 /* TODO - fall through to the failure case until this
5609 * is implemented.
5610 * https://github.com/ARMmbed/mbed-crypto/issues/218
5611 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005612 default:
5613 /* We found an unsupported transaction in the storage.
5614 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005615 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005616 }
5617}
5618#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005620psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005621{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005622 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005623
Gilles Peskinec6b69072018-11-20 21:42:52 +01005624 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005625 if (global_data.initialized != 0) {
5626 return PSA_SUCCESS;
5627 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005628
Gilles Peskine30524eb2020-11-13 17:02:26 +01005629 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005630 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005631 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005632 status = mbedtls_psa_random_seed(&global_data.rng);
5633 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005634 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005635 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005636 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005638 status = psa_initialize_key_slots();
5639 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005640 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005641 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005642
Ronald Cron088d5d02021-04-10 16:57:30 +02005643 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005644 status = psa_driver_wrapper_init();
5645 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005646 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005647 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005648
Gilles Peskine4b734222019-07-24 15:56:31 +02005649#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005650 status = psa_crypto_load_transaction();
5651 if (status == PSA_SUCCESS) {
5652 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5653 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005654 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005655 }
5656 status = psa_crypto_stop_transaction();
5657 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005658 /* There's no transaction to complete. It's all good. */
5659 status = PSA_SUCCESS;
5660 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005661#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005662
Gilles Peskinec6b69072018-11-20 21:42:52 +01005663 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005664 global_data.initialized = 1;
5665
5666exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005667 if (status != PSA_SUCCESS) {
5668 mbedtls_psa_crypto_free();
5669 }
5670 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005671}
5672
David Horstmann114d8242023-12-07 18:39:17 +00005673/* Memory copying test hooks. These are called before input copy, after input
5674 * copy, before output copy and after output copy, respectively.
5675 * They are used by memory-poisoning tests to temporarily unpoison buffers
5676 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00005677#if defined(MBEDTLS_TEST_HOOKS)
5678void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5679void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5680void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5681void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5682#endif
5683
David Horstmanndf493552023-11-15 15:18:30 +00005684/** Copy from an input buffer to a local copy.
5685 *
5686 * \param[in] input Pointer to input buffer.
5687 * \param[in] input_len Length of the input buffer.
5688 * \param[out] input_copy Pointer to a local copy in which to store the input data.
5689 * \param[out] input_copy_len Length of the local copy buffer.
5690 * \return #PSA_SUCCESS, if the buffer was successfully
5691 * copied.
5692 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
5693 * copy is too small to hold contents of the
5694 * input buffer.
5695 */
5696MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00005697psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
5698 uint8_t *input_copy, size_t input_copy_len)
5699{
5700 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00005701 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00005702 }
5703
David Horstmann0760b152023-11-24 16:21:04 +00005704#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005705 if (psa_input_pre_copy_hook != NULL) {
5706 psa_input_pre_copy_hook(input, input_len);
5707 }
David Horstmann0760b152023-11-24 16:21:04 +00005708#endif
5709
David Horstmann660027f2023-11-15 17:33:47 +00005710 if (input_len > 0) {
5711 memcpy(input_copy, input, input_len);
5712 }
David Horstmann957f9802023-10-30 20:29:43 +00005713
David Horstmann0760b152023-11-24 16:21:04 +00005714#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005715 if (psa_input_post_copy_hook != NULL) {
5716 psa_input_post_copy_hook(input, input_len);
5717 }
David Horstmann0760b152023-11-24 16:21:04 +00005718#endif
5719
David Horstmann957f9802023-10-30 20:29:43 +00005720 return PSA_SUCCESS;
5721}
5722
David Horstmanndf493552023-11-15 15:18:30 +00005723/** Copy from a local output buffer into a user-supplied one.
5724 *
5725 * \param[in] output_copy Pointer to a local buffer containing the output.
5726 * \param[in] output_copy_len Length of the local buffer.
5727 * \param[out] output Pointer to user-supplied output buffer.
5728 * \param[out] output_len Length of the user-supplied output buffer.
5729 * \return #PSA_SUCCESS, if the buffer was successfully
5730 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00005731 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00005732 * user-supplied output buffer is too small to
5733 * hold the contents of the local buffer.
5734 */
5735MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00005736psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
5737 uint8_t *output, size_t output_len)
5738{
5739 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00005740 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00005741 }
David Horstmann660027f2023-11-15 17:33:47 +00005742
David Horstmann0760b152023-11-24 16:21:04 +00005743#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005744 if (psa_output_pre_copy_hook != NULL) {
5745 psa_output_pre_copy_hook(output, output_len);
5746 }
David Horstmann0760b152023-11-24 16:21:04 +00005747#endif
5748
David Horstmann660027f2023-11-15 17:33:47 +00005749 if (output_copy_len > 0) {
5750 memcpy(output, output_copy, output_copy_len);
5751 }
5752
David Horstmann0760b152023-11-24 16:21:04 +00005753#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005754 if (psa_output_post_copy_hook != NULL) {
5755 psa_output_post_copy_hook(output, output_len);
5756 }
David Horstmann0760b152023-11-24 16:21:04 +00005757#endif
5758
David Horstmann2bd296e2023-10-30 20:37:12 +00005759 return PSA_SUCCESS;
5760}
5761
David Horstmann81a0d572023-11-20 17:15:32 +00005762psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
5763 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005764{
5765 psa_status_t status;
5766
David Horstmann0d52c712023-11-23 15:50:37 +00005767 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005768
David Horstmann726bf052023-11-15 18:11:26 +00005769 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005770 return PSA_SUCCESS;
5771 }
5772
David Horstmann81a0d572023-11-20 17:15:32 +00005773 local_input->buffer = mbedtls_calloc(input_len, 1);
5774 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005775 /* Since we dealt with the zero-length case above, we know that
5776 * a NULL return value means a failure of allocation. */
5777 return PSA_ERROR_INSUFFICIENT_MEMORY;
5778 }
David Horstmann81a0d572023-11-20 17:15:32 +00005779 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005780
David Horstmann81a0d572023-11-20 17:15:32 +00005781 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005782
5783 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00005784 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005785 if (status != PSA_SUCCESS) {
5786 goto error;
5787 }
5788
5789 return PSA_SUCCESS;
5790
5791error:
David Horstmann81a0d572023-11-20 17:15:32 +00005792 mbedtls_free(local_input->buffer);
5793 local_input->buffer = NULL;
5794 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005795 return status;
5796}
5797
David Horstmann81a0d572023-11-20 17:15:32 +00005798void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00005799{
David Horstmann81a0d572023-11-20 17:15:32 +00005800 mbedtls_free(local_input->buffer);
5801 local_input->buffer = NULL;
5802 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00005803}
5804
David Horstmann1a76ab12023-11-20 12:54:09 +00005805psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
5806 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00005807{
David Horstmann0d52c712023-11-23 15:50:37 +00005808 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00005809
David Horstmann726bf052023-11-15 18:11:26 +00005810 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005811 return PSA_SUCCESS;
5812 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005813 local_output->buffer = mbedtls_calloc(output_len, 1);
5814 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005815 /* Since we dealt with the zero-length case above, we know that
5816 * a NULL return value means a failure of allocation. */
5817 return PSA_ERROR_INSUFFICIENT_MEMORY;
5818 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005819 local_output->length = output_len;
5820 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00005821
5822 return PSA_SUCCESS;
5823}
5824
David Horstmann1a76ab12023-11-20 12:54:09 +00005825psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00005826{
David Horstmann761761f2023-11-15 15:57:32 +00005827 psa_status_t status;
5828
David Horstmann1a76ab12023-11-20 12:54:09 +00005829 if (local_output->buffer == NULL) {
5830 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005831 return PSA_SUCCESS;
5832 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005833 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00005834 /* We have an internal copy but nothing to copy back to. */
5835 return PSA_ERROR_CORRUPTION_DETECTED;
5836 }
5837
David Horstmann1a76ab12023-11-20 12:54:09 +00005838 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
5839 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00005840 if (status != PSA_SUCCESS) {
5841 return status;
5842 }
David Horstmannaeeb2742023-11-08 17:44:18 +00005843
David Horstmann1a76ab12023-11-20 12:54:09 +00005844 mbedtls_free(local_output->buffer);
5845 local_output->buffer = NULL;
5846 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005847
5848 return PSA_SUCCESS;
5849}
5850
Gilles Peskinee59236f2018-01-27 23:32:46 +01005851#endif /* MBEDTLS_PSA_CRYPTO_C */