blob: 4e14cba1bb3c06820a982e0e31975b3f58facb17 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinee59236f2018-01-27 23:32:46 +01007 */
8
Gilles Peskinedb09ef62020-06-03 01:43:33 +02009#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010010
11#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030012
John Durkop07cc04a2020-11-16 22:08:34 -080013#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
14#include "check_crypto_config.h"
15#endif
16
Gilles Peskinee59236f2018-01-27 23:32:46 +010017#include "psa/crypto.h"
18
Ronald Crond6d28882020-12-14 14:56:02 +010019#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010020#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010021#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020022#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010023#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010024#include "psa_crypto_hash.h"
Steven Cooreman32d56942021-03-19 17:39:17 +010025#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010027#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020028#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020029#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020030#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010031#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010032/* Include internal declarations that are useful for implementing persistently
33 * stored keys. */
34#include "psa_crypto_storage.h"
35
Gilles Peskineb2b64d32020-12-14 16:43:58 +010036#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010037
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010038#include <stdlib.h>
39#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010041
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010042#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010043#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020044#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000045#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020046#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010047#include "mbedtls/blowfish.h"
48#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020049#include "mbedtls/chacha20.h"
50#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/cipher.h"
52#include "mbedtls/ccm.h"
53#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010054#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020055#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010056#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010057#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/error.h"
59#include "mbedtls/gcm.h"
60#include "mbedtls/md2.h"
61#include "mbedtls/md4.h"
62#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010063#include "mbedtls/md.h"
64#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010065#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010067#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000068#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/sha1.h"
72#include "mbedtls/sha256.h"
73#include "mbedtls/sha512.h"
74#include "mbedtls/xtea.h"
75
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020077
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010078/****************************************************************/
79/* Global data, support functions and library management */
80/****************************************************************/
81
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020083{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020085}
86
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010087/* Values for psa_global_data_t::rng_state */
88#define RNG_NOT_INITIALIZED 0
89#define RNG_INITIALIZED 1
90#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +010093 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010094 unsigned rng_state : 2;
Gilles Peskineb8006a62021-11-08 22:20:03 +010095 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +010096} psa_global_data_t;
97
98static psa_global_data_t global_data;
99
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100100#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
101mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
102 &global_data.rng.drbg;
103#endif
104
itayzafrir0adf0fc2018-09-06 16:24:41 +0300105#define GUARD_MODULE_INITIALIZED \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 if (global_data.initialized == 0) \
107 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300108
David Horstmannc0a2c302023-11-29 17:24:08 +0000109#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
David Horstmann34980bd2023-11-29 17:07:13 +0000110/* Substitute an input buffer for a local copy of itself.
111 * Assumptions:
112 * - psa_status_t status exists
113 * - An exit label is declared
David Horstmann1f532132023-12-08 14:08:18 +0000114 * - The name <input>_copy is not used for the given value of <input>
David Horstmann34980bd2023-11-29 17:07:13 +0000115 */
116#define SWAP_FOR_LOCAL_INPUT(input, length) \
117 psa_crypto_local_input_t input ## _copy = PSA_CRYPTO_LOCAL_INPUT_INIT; \
118 status = psa_crypto_local_input_alloc(input, length, &input ## _copy); \
119 if (status != PSA_SUCCESS) { \
120 goto exit; \
121 } \
122 input = input ## _copy.buffer;
123
David Horstmann1f532132023-12-08 14:08:18 +0000124/* Free the substituted input buffer copy created by SWAP_FOR_LOCAL_INPUT().
125 * Note that this does not restore the pointer to the original buffer.
126 * Assumptions:
127 * - psa_crypto_local_input_t <input>_copy exists, for the given value of
128 * <input>
129 * - <input>_copy was previously allocated by psa_crypto_local_input_alloc()
130 * - <input> points to <input>_copy.buffer
131 */
David Horstmann34980bd2023-11-29 17:07:13 +0000132#define FREE_LOCAL_INPUT(input) \
David Horstmann1f532132023-12-08 14:08:18 +0000133 input = NULL; \
David Horstmann34980bd2023-11-29 17:07:13 +0000134 psa_crypto_local_input_free(&input ## _copy);
135
136/* Substitute an output buffer for a local copy of itself.
137 * Assumptions:
138 * - psa_status_t status exists
139 * - An exit label is declared
David Horstmann1f532132023-12-08 14:08:18 +0000140 * - The name <output>_copy is not used for the given value of <output>
David Horstmann34980bd2023-11-29 17:07:13 +0000141 */
142#define SWAP_FOR_LOCAL_OUTPUT(output, length) \
143 psa_crypto_local_output_t output ## _copy = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
144 status = psa_crypto_local_output_alloc(output, length, &output ## _copy); \
145 if (status != PSA_SUCCESS) { \
146 goto exit; \
147 } \
148 output = output ## _copy.buffer;
149
David Horstmann1f532132023-12-08 14:08:18 +0000150/* Free the substituted output buffer copy created by SWAP_FOR_LOCAL_OUTPUT()
151 * after first copying back its contents to the original buffer.
152 * Note that this does not restore the pointer to the original buffer.
153 * Assumptions:
154 * - psa_crypto_local_output_t <output>_copy exists, for the given value of
155 * <output>
156 * - <output>_copy was previously allocated by psa_crypto_local_output_alloc()
157 * - <output> points to <output>_copy.buffer
158 * - psa_status_t status exists
159 */
David Horstmann34980bd2023-11-29 17:07:13 +0000160#define FREE_LOCAL_OUTPUT(output) \
David Horstmann1f532132023-12-08 14:08:18 +0000161 output = NULL; \
David Horstmann34980bd2023-11-29 17:07:13 +0000162 psa_status_t local_output_free_status; \
163 local_output_free_status = psa_crypto_local_output_free(&output ## _copy); \
164 if (local_output_free_status != PSA_SUCCESS) { \
David Horstmann1f532132023-12-08 14:08:18 +0000165 /* Since this error case is an internal error, it's more serious than \
166 * any existing error code and so it's fine to overwrite the existing \
167 * status. */ \
David Horstmann34980bd2023-11-29 17:07:13 +0000168 status = local_output_free_status; \
169 }
David Horstmannc0a2c302023-11-29 17:24:08 +0000170#else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
171#define SWAP_FOR_LOCAL_INPUT(input, length)
172#define FREE_LOCAL_INPUT(input)
173#define SWAP_FOR_LOCAL_OUTPUT(output, length)
174#define FREE_LOCAL_OUTPUT(output)
175#endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmann34980bd2023-11-29 17:07:13 +0000176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100178{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100179 /* Mbed TLS error codes can combine a high-level error code and a
180 * low-level error code. The low-level error usually reflects the
181 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 int low_level_ret = -(-ret & 0x007f);
183 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100184 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100186
187 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
188 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
189 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200194 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200196
Gilles Peskine9a944802018-06-21 09:35:35 +0200197 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
198 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
199 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
200 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
201 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200203 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200205 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200207
Jaeden Amero93e21112019-02-20 13:57:28 +0000208#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000209 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000210#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
211 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
212#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100217
Jaeden Amero93e21112019-02-20 13:57:28 +0000218#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000219 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000220#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
221 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
222#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100223 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100227
228 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100232 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100234
Gilles Peskine26869f22019-05-06 15:25:00 +0200235 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200237
238 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200240 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200242
Gilles Peskinea5905292018-02-07 20:59:33 +0100243 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100245 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100255 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100257 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100259
260 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
264 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100265 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
266 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
270 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100272 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100274#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100275
276 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100278 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100280
Gilles Peskinee59236f2018-01-27 23:32:46 +0100281 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
282 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
283 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100285
286 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100290 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292
Gilles Peskine82e57d12020-11-13 21:31:17 +0100293#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100295 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
296 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100297 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100299 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
300 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100302 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100304#endif
305
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200306 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
307 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
308 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200310
Gilles Peskinea5905292018-02-07 20:59:33 +0100311 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200319 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100321
Gilles Peskinef76aa772018-10-29 19:24:33 +0100322 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100324 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100326 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100328 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100330 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100332 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100334 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100336 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100338
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100339 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100341 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
342 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100344 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100346 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
347 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100349 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100351 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
352 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100354 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356 case MBEDTLS_ERR_PK_INVALID_ALG:
357 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
358 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100360 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100362 case MBEDTLS_ERR_PK_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 Peskineff2d2002019-05-06 15:26:23 +0200365 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200367 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200369
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200370 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200372
Gilles Peskinea5905292018-02-07 20:59:33 +0100373 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100375 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100377 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100379 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100381 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
382 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100384 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100386 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100388 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100390 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100392 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100394
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200395 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
396 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
397 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200399
Gilles Peskinea5905292018-02-07 20:59:33 +0100400 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100402 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100404
itayzafrir5c753392018-05-08 11:18:38 +0300405 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300406 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300408 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300410 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300412 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
413 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300415 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100417 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300419 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100421
Janos Follatha13b9052019-11-22 12:48:59 +0000422 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300424
Gilles Peskinee59236f2018-01-27 23:32:46 +0100425 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100427 }
428}
429
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200430
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200431
432
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100433/****************************************************************/
434/* Key management */
435/****************************************************************/
436
John Durkop9814fa22020-11-04 12:28:15 -0800437#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800438 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100439 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
440 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
441 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
443 size_t bits,
444 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200445{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100447 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100449#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100450 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100452#endif
453#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100454 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100456#endif
457#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100458 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100460#endif
461#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100462 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100464#endif
465#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100466 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100467 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100468 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 if (bits_is_sloppy) {
470 return MBEDTLS_ECP_DP_SECP521R1;
471 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100472 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100473#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100474 }
475 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100476
Paul Elliott8ff510a2020-06-02 17:19:28 +0100477 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100479#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100480 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100481 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100482#endif
483#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100484 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100486#endif
487#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100488 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100490#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100491 }
492 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100493
Paul Elliott8ff510a2020-06-02 17:19:28 +0100494 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100496#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100497 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100498 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100499 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100500 if (bits_is_sloppy) {
501 return MBEDTLS_ECP_DP_CURVE25519;
502 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100503 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100504#endif
505#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100506 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100508#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100509 }
510 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100511
Paul Elliott8ff510a2020-06-02 17:19:28 +0100512 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100513 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100514#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100515 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100517#endif
518#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100519 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100521#endif
522#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100523 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100525#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100526 }
527 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200528 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100529
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100530 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100531 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200532}
John Durkop9814fa22020-11-04 12:28:15 -0800533#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100534 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
535 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
536 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
537 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100539static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
540 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200541{
542 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100543 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200544 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200545 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200546 case PSA_KEY_TYPE_DERIVE:
547 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100548#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200549 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 if (bits != 128 && bits != 192 && bits != 256) {
551 return PSA_ERROR_INVALID_ARGUMENT;
552 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200553 break;
554#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200555#if defined(PSA_WANT_KEY_TYPE_ARIA)
556 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100557 if (bits != 128 && bits != 192 && bits != 256) {
558 return PSA_ERROR_INVALID_ARGUMENT;
559 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200560 break;
561#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100562#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200563 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100564 if (bits != 128 && bits != 192 && bits != 256) {
565 return PSA_ERROR_INVALID_ARGUMENT;
566 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200567 break;
568#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100569#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200570 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100571 if (bits != 64 && bits != 128 && bits != 192) {
572 return PSA_ERROR_INVALID_ARGUMENT;
573 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200574 break;
575#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100576#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200577 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100578 if (bits < 8 || bits > 2048) {
579 return PSA_ERROR_INVALID_ARGUMENT;
580 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200581 break;
582#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100583#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200584 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585 if (bits != 256) {
586 return PSA_ERROR_INVALID_ARGUMENT;
587 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200588 break;
589#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200590 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100591 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200592 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 if (bits % 8 != 0) {
594 return PSA_ERROR_INVALID_ARGUMENT;
595 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100597 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200598}
599
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100600/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100601 *
Steven Cooremancd640932021-03-08 12:00:27 +0100602 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
603 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100604 *
605 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100606 * \param[in] key_type The key type of the key to be used with the
607 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100608 *
609 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100610 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100611 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100612 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100613 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100614MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100615 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100616 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100617{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100618 if (PSA_ALG_IS_HMAC(algorithm)) {
619 if (key_type == PSA_KEY_TYPE_HMAC) {
620 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100621 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100622 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
625 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
626 * key. */
627 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
628 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
629 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
630 * the block length (larger than 1) for block ciphers. */
631 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
632 return PSA_SUCCESS;
633 }
634 }
635 }
636
637 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100638}
639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
641 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200642{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100643 if (slot->key.data != NULL) {
644 return PSA_ERROR_ALREADY_EXISTS;
645 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100647 slot->key.data = mbedtls_calloc(1, buffer_length);
648 if (slot->key.data == NULL) {
649 return PSA_ERROR_INSUFFICIENT_MEMORY;
650 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200651
Ronald Cronea0f8a62020-11-25 17:52:23 +0100652 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100653 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200654}
655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100656psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
657 const uint8_t *data,
658 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200659{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100660 psa_status_t status = psa_allocate_buffer_to_slot(slot,
661 data_length);
662 if (status != PSA_SUCCESS) {
663 return status;
664 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100666 memcpy(slot->key.data, data, data_length);
667 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200668}
669
Ronald Crona0fe59f2020-11-29 11:14:53 +0100670psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100671 const psa_key_attributes_t *attributes,
672 const uint8_t *data, size_t data_length,
673 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100674 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
677 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200679 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100680 if (data_length == 0) {
681 return PSA_ERROR_NOT_SUPPORTED;
682 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100684 if (key_type_is_raw_bytes(type)) {
685 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200686
Steven Cooreman75b74362020-07-28 14:30:13 +0200687 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100688 if (data_length > SIZE_MAX / 8) {
689 return PSA_ERROR_NOT_SUPPORTED;
690 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200691
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200692 /* Enforce a size limit, and in particular ensure that the bit
693 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100694 if ((*bits) > PSA_MAX_KEY_BITS) {
695 return PSA_ERROR_NOT_SUPPORTED;
696 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100698 status = validate_unstructured_key_bit_size(type, *bits);
699 if (status != PSA_SUCCESS) {
700 return status;
701 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200702
Ronald Crondd04d422020-11-28 15:14:42 +0100703 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100704 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100705 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100706 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100708 return PSA_SUCCESS;
709 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800710#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100711 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
712 if (PSA_KEY_TYPE_IS_ECC(type)) {
713 return mbedtls_psa_ecp_import_key(attributes,
714 data, data_length,
715 key_buffer, key_buffer_size,
716 key_buffer_length,
717 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100718 }
John Durkop9814fa22020-11-04 12:28:15 -0800719#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
720 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700721#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100722 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
723 if (PSA_KEY_TYPE_IS_RSA(type)) {
724 return mbedtls_psa_rsa_import_key(attributes,
725 data, data_length,
726 key_buffer, key_buffer_size,
727 key_buffer_length,
728 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200729 }
John Durkop9814fa22020-11-04 12:28:15 -0800730#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
731 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100732 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100734 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100735}
736
Gilles Peskinef603c712019-01-19 13:40:11 +0100737/** Calculate the intersection of two algorithm usage policies.
738 *
739 * Return 0 (which allows no operation) on incompatibility.
740 */
741static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100742 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100743 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100744 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100745{
Gilles Peskine549ea862019-05-22 11:45:59 +0200746 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100747 if (alg1 == alg2) {
748 return alg1;
749 }
Steven Cooreman03488022021-02-18 12:07:20 +0100750 /* If the policies are from the same hash-and-sign family, check
751 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100752 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
753 PSA_ALG_IS_SIGN_HASH(alg2) &&
754 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
755 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
756 return alg2;
757 }
758 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
759 return alg1;
760 }
Steven Cooreman03488022021-02-18 12:07:20 +0100761 }
762 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100763 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100764 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100765 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
766 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
767 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
768 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
769 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100770 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100771
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100772 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100773 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
774 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
775 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
776 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100777 }
778 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100779 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
780 (alg1_len <= alg2_len)) {
781 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100782 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100783 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
784 (alg2_len <= alg1_len)) {
785 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100786 }
787 }
788 /* If the policies are from the same MAC family, check whether one
789 * of them is a minimum-MAC-length policy. Calculate the most
790 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100791 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
792 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
793 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100794 /* Validate the combination of key type and algorithm. Since the base
795 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100796 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
797 return 0;
798 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100799
Steven Cooreman31a876d2021-03-03 20:47:40 +0100800 /* Get the (exact or at-least) output lengths for both sides of the
801 * requested intersection. None of the currently supported algorithms
802 * have an output length dependent on the actual key size, so setting it
803 * to a bogus value of 0 is currently OK.
804 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100805 * Note that for at-least-this-length wildcard algorithms, the output
806 * length is set to the shortest allowed length, which allows us to
807 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100808 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
809 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100810 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100811
Steven Cooremana1d83222021-02-25 10:20:29 +0100812 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100813 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
814 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
815 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100816 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100817
818 /* If only one is an at-least-this-length policy, the intersection would
819 * be the other (fixed-length) policy as long as said fixed length is
820 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
822 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100823 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100824 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
825 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100826 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100827
Steven Cooremancd640932021-03-08 12:00:27 +0100828 /* If none of them are wildcards, check whether they define the same tag
829 * length. This is still possible here when one is default-length and
830 * the other specific-length. Ensure to always return the
831 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100832 if (alg1_len == alg2_len) {
833 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
834 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100835 }
836 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100837 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100838}
839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100840static int psa_key_algorithm_permits(psa_key_type_t key_type,
841 psa_algorithm_t policy_alg,
842 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200843{
Gilles Peskine549ea862019-05-22 11:45:59 +0200844 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100845 if (requested_alg == policy_alg) {
846 return 1;
847 }
Steven Cooreman03488022021-02-18 12:07:20 +0100848 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
849 * and requested_alg is the same hash-and-sign family with any hash,
850 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100851 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
852 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
853 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
854 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100855 }
856 /* If policy_alg is a wildcard AEAD algorithm of the same base as
857 * the requested algorithm, check the requested tag length to be
858 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100859 if (PSA_ALG_IS_AEAD(policy_alg) &&
860 PSA_ALG_IS_AEAD(requested_alg) &&
861 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
862 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
863 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
864 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
865 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100866 }
Steven Cooremancd640932021-03-08 12:00:27 +0100867 /* If policy_alg is a MAC algorithm of the same base as the requested
868 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100869 if (PSA_ALG_IS_MAC(policy_alg) &&
870 PSA_ALG_IS_MAC(requested_alg) &&
871 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
872 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100873 /* Validate the combination of key type and algorithm. Since the policy
874 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100875 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
876 return 0;
877 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100878
Steven Cooreman31a876d2021-03-03 20:47:40 +0100879 /* Get both the requested output length for the algorithm which is to be
880 * verified, and the default output length for the base algorithm.
881 * Note that none of the currently supported algorithms have an output
882 * length dependent on actual key size, so setting it to a bogus value
883 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100884 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100885 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100886 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100887 key_type, 0,
888 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100889
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100890 /* If the policy is default-length, only allow an algorithm with
891 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100892 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
893 return requested_output_length == default_output_length;
894 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100895
896 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100897 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100898 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
899 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
900 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100901 }
902
Steven Cooremancd640932021-03-08 12:00:27 +0100903 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
904 * check for the requested MAC length to be equal to or longer than the
905 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100906 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
907 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
908 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100909 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200910 }
Steven Cooremance48e852020-10-05 16:02:45 +0200911 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200912 * a key derivation with that key agreement should also be allowed. This
913 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100914 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
915 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
916 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
917 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200918 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100919 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100920 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200921}
922
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100923/** Test whether a policy permits an algorithm.
924 *
925 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100926 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100927 * \note This function requires providing the key type for which the policy is
928 * being validated, since some algorithm policy definitions (e.g. MAC)
929 * have different properties depending on what kind of cipher it is
930 * combined with.
931 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100932 * \retval PSA_SUCCESS When \p alg is a specific algorithm
933 * allowed by the \p policy.
934 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
935 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
936 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100937 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100938static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
939 psa_key_type_t key_type,
940 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100941{
Steven Cooremand788fab2021-02-25 11:29:17 +0100942 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100943 if (alg == 0) {
944 return PSA_ERROR_INVALID_ARGUMENT;
945 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100946
Steven Cooreman7e39f052021-02-23 14:18:32 +0100947 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100948 if (PSA_ALG_IS_WILDCARD(alg)) {
949 return PSA_ERROR_INVALID_ARGUMENT;
950 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100952 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
953 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
954 return PSA_SUCCESS;
955 } else {
956 return PSA_ERROR_NOT_PERMITTED;
957 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100958}
959
Gilles Peskinef603c712019-01-19 13:40:11 +0100960/** Restrict a key policy based on a constraint.
961 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100962 * \note This function requires providing the key type for which the policy is
963 * being restricted, since some algorithm policy definitions (e.g. MAC)
964 * have different properties depending on what kind of cipher it is
965 * combined with.
966 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100967 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100968 * \param[in,out] policy The policy to restrict.
969 * \param[in] constraint The policy constraint to apply.
970 *
971 * \retval #PSA_SUCCESS
972 * \c *policy contains the intersection of the original value of
973 * \c *policy and \c *constraint.
974 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100975 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100976 * \c *policy is unchanged.
977 */
978static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100979 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100980 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100981 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100982{
983 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100984 psa_key_policy_algorithm_intersection(key_type, policy->alg,
985 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200986 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100987 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
988 constraint->alg2);
989 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
990 return PSA_ERROR_INVALID_ARGUMENT;
991 }
992 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
993 return PSA_ERROR_INVALID_ARGUMENT;
994 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100995 policy->usage &= constraint->usage;
996 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200997 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100998 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100999}
1000
Ronald Cron5c522922020-11-14 16:35:34 +01001001/** Get the description of a key given its identifier and policy constraints
1002 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001003 *
Ronald Cron5c522922020-11-14 16:35:34 +01001004 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +01001005 * nonzero, the key must allow operations with this algorithm. If \p alg is
1006 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +01001007 *
Ronald Cron5c522922020-11-14 16:35:34 +01001008 * In case of a persistent key, the function loads the description of the key
1009 * into a key slot if not already done.
1010 *
1011 * On success, the returned key slot is locked. It is the responsibility of
1012 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001013 */
Ronald Cron5c522922020-11-14 16:35:34 +01001014static psa_status_t psa_get_and_lock_key_slot_with_policy(
1015 mbedtls_svc_key_id_t key,
1016 psa_key_slot_t **p_slot,
1017 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001018 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001019{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001020 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1021 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001022
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001023 status = psa_get_and_lock_key_slot(key, p_slot);
1024 if (status != PSA_SUCCESS) {
1025 return status;
1026 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001027 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001028
1029 /* Enforce that usage policy for the key slot contains all the flags
1030 * required by the usage parameter. There is one exception: public
1031 * keys can always be exported, so we treat public key objects as
1032 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001033 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001034 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001035 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001037 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001038 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001039 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001040 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001041
Shaun Case0e7791f2021-12-20 21:14:10 -08001042 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001043 if (alg != 0) {
1044 status = psa_key_policy_permits(&slot->attr.policy,
1045 slot->attr.type,
1046 alg);
1047 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001048 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001049 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001050 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001052 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001053
1054error:
1055 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001056 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001058 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001059}
Darryl Green940d72c2018-07-13 13:18:51 +01001060
Ronald Cron5c522922020-11-14 16:35:34 +01001061/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001062 *
1063 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001064 * available, as opposed to a key in a secure element and/or to be used
1065 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001066 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001067 * This is a temporary function that may be used instead of
1068 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1069 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001070 *
Ronald Cron5c522922020-11-14 16:35:34 +01001071 * On success, the returned key slot is locked. It is the responsibility of the
1072 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001073 */
Ronald Cron5c522922020-11-14 16:35:34 +01001074static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1075 mbedtls_svc_key_id_t key,
1076 psa_key_slot_t **p_slot,
1077 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001078 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001079{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001080 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1081 usage, alg);
1082 if (status != PSA_SUCCESS) {
1083 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001084 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001086 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1087 psa_unlock_key_slot(*p_slot);
1088 *p_slot = NULL;
1089 return PSA_ERROR_NOT_SUPPORTED;
1090 }
1091
1092 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001093}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001095psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001096{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001097 /* Data pointer will always be either a valid pointer or NULL in an
1098 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001099 if (slot->key.data != NULL) {
1100 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1101 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001103 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001104 slot->key.data = NULL;
1105 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001107 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001108}
1109
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001110/** Completely wipe a slot in memory, including its policy.
1111 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001112psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001113{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001114 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001115
1116 /*
1117 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001118 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001119 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1120 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001121 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001122 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001123 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001124#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001125 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001126#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001127 status = PSA_ERROR_CORRUPTION_DETECTED;
1128 }
1129
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001130 /* Multipart operations may still be using the key. This is safe
1131 * because all multipart operation objects are independent from
1132 * the key slot: if they need to access the key after the setup
1133 * phase, they have a copy of the key. Note that this means that
1134 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001135 /* At this point, key material and other type-specific content has
1136 * been wiped. Clear remaining metadata. We can call memset and not
1137 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001138 memset(slot, 0, sizeof(*slot));
1139 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001140}
1141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001142psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001143{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001144 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001145 psa_status_t status; /* status of the last operation */
1146 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001147#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1148 psa_se_drv_table_entry_t *driver;
1149#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001151 if (mbedtls_svc_key_id_is_null(key)) {
1152 return PSA_SUCCESS;
1153 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001154
Ronald Cronf2911112020-10-29 17:51:10 +01001155 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001156 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001157 * key, this will load the key description from persistent memory if not
1158 * done yet. We cannot avoid this loading as without it we don't know if
1159 * the key is operated by an SE or not and this information is needed by
1160 * the current implementation.
1161 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001162 status = psa_get_and_lock_key_slot(key, &slot);
1163 if (status != PSA_SUCCESS) {
1164 return status;
1165 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001166
Ronald Cronf2911112020-10-29 17:51:10 +01001167 /*
1168 * If the key slot containing the key description is under access by the
1169 * library (apart from the present access), the key cannot be destroyed
1170 * yet. For the time being, just return in error. Eventually (to be
1171 * implemented), the key should be destroyed when all accesses have
1172 * stopped.
1173 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001174 if (slot->lock_count > 1) {
1175 psa_unlock_key_slot(slot);
1176 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001177 }
1178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001179 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001180 /* Refuse the destruction of a read-only key (which may or may not work
1181 * if we attempt it, depending on whether the key is merely read-only
1182 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001183 * Just do the best we can, which is to wipe the copy in memory
1184 * (done in this function's cleanup code). */
1185 overall_status = PSA_ERROR_NOT_PERMITTED;
1186 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001187 }
1188
Gilles Peskine354f7672019-07-12 23:46:38 +02001189#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001190 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1191 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001192 /* For a key in a secure element, we need to do three things:
1193 * remove the key file in internal storage, destroy the
1194 * key inside the secure element, and update the driver's
1195 * persistent data. Start a transaction that will encompass these
1196 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001197 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001198 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001199 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001200 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001201 status = psa_crypto_save_transaction();
1202 if (status != PSA_SUCCESS) {
1203 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001204 /* We should still try to destroy the key in the secure
1205 * element and the key metadata in storage. This is especially
1206 * important if the error is that the storage is full.
1207 * But how to do it exactly without risking an inconsistent
1208 * state after a reset?
1209 * https://github.com/ARMmbed/mbed-crypto/issues/215
1210 */
1211 overall_status = status;
1212 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001213 }
1214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001215 status = psa_destroy_se_key(driver,
1216 psa_key_slot_get_slot_number(slot));
1217 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001218 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001219 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001220 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001221#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1222
Darryl Greend49a4992018-06-18 17:27:26 +01001223#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001224 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1225 status = psa_destroy_persistent_key(slot->attr.id);
1226 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001227 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001228 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001229
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001230 /* TODO: other slots may have a copy of the same key. We should
1231 * invalidate them.
1232 * https://github.com/ARMmbed/mbed-crypto/issues/214
1233 */
Darryl Greend49a4992018-06-18 17:27:26 +01001234 }
1235#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001236
Gilles Peskinefc762652019-07-22 19:30:34 +02001237#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001238 if (driver != NULL) {
1239 status = psa_save_se_persistent_data(driver);
1240 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001241 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001242 }
1243 status = psa_crypto_stop_transaction();
1244 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001245 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001246 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001247 }
1248#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1249
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001250exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001251 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001252 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001253 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001254 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001255 }
1256 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001257}
1258
John Durkop07cc04a2020-11-16 22:08:34 -08001259#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001260 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001261static psa_status_t psa_get_rsa_public_exponent(
1262 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001263 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001264{
1265 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001266 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001267 uint8_t *buffer = NULL;
1268 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001269 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001270
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001271 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1272 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001273 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001274 }
1275 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001276 /* It's the default value, which is reported as an empty string,
1277 * so there's nothing to do. */
1278 goto exit;
1279 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001281 buflen = mbedtls_mpi_size(&mpi);
1282 buffer = mbedtls_calloc(1, buflen);
1283 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001284 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1285 goto exit;
1286 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001287 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1288 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001289 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001290 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001291 attributes->domain_parameters = buffer;
1292 attributes->domain_parameters_size = buflen;
1293
1294exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001295 mbedtls_mpi_free(&mpi);
1296 if (ret != 0) {
1297 mbedtls_free(buffer);
1298 }
1299 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001300}
John Durkop07cc04a2020-11-16 22:08:34 -08001301#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001302 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001303
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001304/** Retrieve all the publicly-accessible attributes of a key.
1305 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001306psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1307 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001308{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001309 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001310 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001311 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001313 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001315 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1316 if (status != PSA_SUCCESS) {
1317 return status;
1318 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001319
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001320 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001321 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1322 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001323
1324#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001325 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1326 psa_set_key_slot_number(attributes,
1327 psa_key_slot_get_slot_number(slot));
1328 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001329#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001331 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001332#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001333 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001334 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001335 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001336 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001337 * is not yet implemented.
1338 * https://github.com/ARMmbed/mbed-crypto/issues/216
1339 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001340 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001341 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001342
Ronald Cron90857082020-11-25 14:58:33 +01001343 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001344 slot->attr.type,
1345 slot->key.data,
1346 slot->key.bytes,
1347 &rsa);
1348 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001349 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001350 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001352 status = psa_get_rsa_public_exponent(rsa,
1353 attributes);
1354 mbedtls_rsa_free(rsa);
1355 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001356 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001357 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001358#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001359 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001360 default:
1361 /* Nothing else to do. */
1362 break;
1363 }
1364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001365 if (status != PSA_SUCCESS) {
1366 psa_reset_key_attributes(attributes);
1367 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001369 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001371 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001372}
1373
Gilles Peskinec8000c02019-08-02 20:15:51 +02001374#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1375psa_status_t psa_get_key_slot_number(
1376 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001377 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001378{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001379 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001380 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001381 return PSA_SUCCESS;
1382 } else {
1383 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001384 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001385}
1386#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001388static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1389 size_t key_buffer_size,
1390 uint8_t *data,
1391 size_t data_size,
1392 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001393{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001394 if (key_buffer_size > data_size) {
1395 return PSA_ERROR_BUFFER_TOO_SMALL;
1396 }
1397 memcpy(data, key_buffer, key_buffer_size);
1398 memset(data + key_buffer_size, 0,
1399 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001400 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001401 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001402}
1403
Ronald Cron7285cda2020-11-26 14:46:37 +01001404psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001405 const psa_key_attributes_t *attributes,
1406 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001407 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001408{
Ronald Crond18b5f82020-11-25 16:46:05 +01001409 psa_key_type_t type = attributes->core.type;
1410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001411 if (key_type_is_raw_bytes(type) ||
1412 PSA_KEY_TYPE_IS_RSA(type) ||
1413 PSA_KEY_TYPE_IS_ECC(type)) {
1414 return psa_export_key_buffer_internal(
1415 key_buffer, key_buffer_size,
1416 data, data_size, data_length);
1417 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001418 /* This shouldn't happen in the reference implementation, but
1419 it is valid for a special-purpose implementation to omit
1420 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001421 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001422 }
1423}
1424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001425psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1426 uint8_t *data,
1427 size_t data_size,
1428 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001429{
1430 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1431 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1432 psa_key_slot_t *slot;
1433
Ronald Crone907e552021-01-18 13:22:38 +01001434 /* Reject a zero-length output buffer now, since this can never be a
1435 * valid key representation. This way we know that data must be a valid
1436 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001437 if (data_size == 0) {
1438 return PSA_ERROR_BUFFER_TOO_SMALL;
1439 }
Ronald Crone907e552021-01-18 13:22:38 +01001440
Ronald Cron9486f9d2020-11-26 13:36:23 +01001441 /* Set the key to empty now, so that even when there are errors, we always
1442 * set data_length to a value between 0 and data_size. On error, setting
1443 * the key to empty is a good choice because an empty key representation is
1444 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001445 *data_length = 0;
1446
Ronald Cron9486f9d2020-11-26 13:36:23 +01001447 /* Export requires the EXPORT flag. There is an exception for public keys,
1448 * which don't require any flag, but
1449 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1450 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001451 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1452 PSA_KEY_USAGE_EXPORT, 0);
1453 if (status != PSA_SUCCESS) {
1454 return status;
1455 }
mohammad160306e79202018-03-28 13:17:44 +03001456
Ronald Crond18b5f82020-11-25 16:46:05 +01001457 psa_key_attributes_t attributes = {
1458 .core = slot->attr
1459 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001460 status = psa_driver_wrapper_export_key(&attributes,
1461 slot->key.data, slot->key.bytes,
1462 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001464 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001466 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001467}
1468
Ronald Cron7285cda2020-11-26 14:46:37 +01001469psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001470 const psa_key_attributes_t *attributes,
1471 const uint8_t *key_buffer,
1472 size_t key_buffer_size,
1473 uint8_t *data,
1474 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001475 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001476{
Ronald Crond18b5f82020-11-25 16:46:05 +01001477 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001479 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1480 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001481 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001482 return psa_export_key_buffer_internal(
1483 key_buffer, key_buffer_size,
1484 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001485 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001487 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001488#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001489 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1490 return mbedtls_psa_rsa_export_public_key(attributes,
1491 key_buffer,
1492 key_buffer_size,
1493 data,
1494 data_size,
1495 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001496#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001497 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001498 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001499#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1500 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001501 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001502#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001503 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1504 return mbedtls_psa_ecp_export_public_key(attributes,
1505 key_buffer,
1506 key_buffer_size,
1507 data,
1508 data_size,
1509 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001510#else
1511 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001512 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001513#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1514 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001515 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001516 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001517 /* This shouldn't happen in the reference implementation, but
1518 it is valid for a special-purpose implementation to omit
1519 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001520 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001521 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001522}
Ronald Crond18b5f82020-11-25 16:46:05 +01001523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001524psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1525 uint8_t *data,
1526 size_t data_size,
1527 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001528{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001529 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001530 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001531 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001532 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001533
Ronald Crone907e552021-01-18 13:22:38 +01001534 /* Reject a zero-length output buffer now, since this can never be a
1535 * valid key representation. This way we know that data must be a valid
1536 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001537 if (data_size == 0) {
1538 return PSA_ERROR_BUFFER_TOO_SMALL;
1539 }
Ronald Crone907e552021-01-18 13:22:38 +01001540
Darryl Greendd8fb772018-11-07 16:00:44 +00001541 /* Set the key to empty now, so that even when there are errors, we always
1542 * set data_length to a value between 0 and data_size. On error, setting
1543 * the key to empty is a good choice because an empty key representation is
1544 * unlikely to be accepted anywhere. */
1545 *data_length = 0;
1546
1547 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001548 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1549 if (status != PSA_SUCCESS) {
1550 return status;
1551 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001553 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1554 status = PSA_ERROR_INVALID_ARGUMENT;
1555 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001556 }
1557
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001558 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001559 .core = slot->attr
1560 };
Ronald Cron67227982020-11-26 15:16:05 +01001561 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001562 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001563 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001564
1565exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001566 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001568 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001569}
1570
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001571MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1572 "One or more key attribute flag is listed as both external-only and dual-use")
1573MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1574 "One or more key attribute flag is listed as both internal-only and dual-use")
1575MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1576 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001577
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001578/** Validate that a key policy is internally well-formed.
1579 *
1580 * This function only rejects invalid policies. It does not validate the
1581 * consistency of the policy with respect to other attributes of the key
1582 * such as the key type.
1583 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001584static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001585{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001586 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1587 PSA_KEY_USAGE_COPY |
1588 PSA_KEY_USAGE_ENCRYPT |
1589 PSA_KEY_USAGE_DECRYPT |
1590 PSA_KEY_USAGE_SIGN_MESSAGE |
1591 PSA_KEY_USAGE_VERIFY_MESSAGE |
1592 PSA_KEY_USAGE_SIGN_HASH |
1593 PSA_KEY_USAGE_VERIFY_HASH |
1594 PSA_KEY_USAGE_DERIVE)) != 0) {
1595 return PSA_ERROR_INVALID_ARGUMENT;
1596 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001597
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001598 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001599}
1600
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001601/** Validate the internal consistency of key attributes.
1602 *
1603 * This function only rejects invalid attribute values. If does not
1604 * validate the consistency of the attributes with any key data that may
1605 * be involved in the creation of the key.
1606 *
1607 * Call this function early in the key creation process.
1608 *
1609 * \param[in] attributes Key attributes for the new key.
1610 * \param[out] p_drv On any return, the driver for the key, if any.
1611 * NULL for a transparent key.
1612 *
1613 */
1614static psa_status_t psa_validate_key_attributes(
1615 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001616 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001617{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001618 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001619 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1620 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001622 status = psa_validate_key_location(lifetime, p_drv);
1623 if (status != PSA_SUCCESS) {
1624 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001625 }
1626
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001627 status = psa_validate_key_persistence(lifetime);
1628 if (status != PSA_SUCCESS) {
1629 return status;
1630 }
1631
1632 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1633 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1634 return PSA_ERROR_INVALID_ARGUMENT;
1635 }
1636 } else {
1637 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1638 return PSA_ERROR_INVALID_ARGUMENT;
1639 }
1640 }
1641
1642 status = psa_validate_key_policy(&attributes->core.policy);
1643 if (status != PSA_SUCCESS) {
1644 return status;
1645 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001646
1647 /* Refuse to create overly large keys.
1648 * Note that this doesn't trigger on import if the attributes don't
1649 * explicitly specify a size (so psa_get_key_bits returns 0), so
1650 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001651 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1652 return PSA_ERROR_NOT_SUPPORTED;
1653 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001654
Gilles Peskine91e8c332019-08-02 19:19:39 +02001655 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001656 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1657 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1658 return PSA_ERROR_INVALID_ARGUMENT;
1659 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001661 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001662}
1663
Gilles Peskine4747d192019-04-17 15:05:45 +02001664/** Prepare a key slot to receive key material.
1665 *
1666 * This function allocates a key slot and sets its metadata.
1667 *
1668 * If this function fails, call psa_fail_key_creation().
1669 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001670 * This function is intended to be used as follows:
1671 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001672 * it with the specified attributes, and in case of a volatile key assign it
1673 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001674 * -# Populate the slot with the key material.
1675 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1676 * In case of failure at any step, stop the sequence and call
1677 * psa_fail_key_creation().
1678 *
Ronald Cron5c522922020-11-14 16:35:34 +01001679 * On success, the key slot is locked. It is the responsibility of the caller
1680 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001681 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001682 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001683 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001684 * \param[out] p_slot On success, a pointer to the prepared slot.
1685 * \param[out] p_drv On any return, the driver for the key, if any.
1686 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001687 *
1688 * \retval #PSA_SUCCESS
1689 * The key slot is ready to receive key material.
1690 * \return If this function fails, the key slot is an invalid state.
1691 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001692 */
1693static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001694 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001695 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001696 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001697 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001698{
1699 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001700 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001701 psa_key_slot_t *slot;
1702
Gilles Peskinedf179142019-07-15 22:02:14 +02001703 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001704 *p_drv = NULL;
1705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001706 status = psa_validate_key_attributes(attributes, p_drv);
1707 if (status != PSA_SUCCESS) {
1708 return status;
1709 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001711 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1712 if (status != PSA_SUCCESS) {
1713 return status;
1714 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001715 slot = *p_slot;
1716
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001717 /* We're storing the declared bit-size of the key. It's up to each
1718 * creation mechanism to verify that this information is correct.
1719 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001720 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001721 * is optional (import, copy). In case of a volatile key, assign it the
1722 * volatile key identifier associated to the slot returned to contain its
1723 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001724
1725 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001726 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001727#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1728 slot->attr.id = volatile_key_id;
1729#else
1730 slot->attr.id.key_id = volatile_key_id;
1731#endif
1732 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001733
Gilles Peskine91e8c332019-08-02 19:19:39 +02001734 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001735 * external-only flags, query `attributes`. Thanks to the check
1736 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001737 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001738 * may have set. */
1739 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001740
Gilles Peskinecbaff462019-07-12 23:46:04 +02001741#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001742 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001743 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001744 * create the key file in internal storage, create the
1745 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001746 * persistent data. This is done by starting a transaction that will
1747 * encompass these three actions.
1748 * For registering a volatile key, we just need to find an appropriate
1749 * slot number inside the SE. Since the key is designated volatile, creating
1750 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001751 /* The first thing to do is to find a slot number for the new key.
1752 * We save the slot number in persistent storage as part of the
1753 * transaction data. It will be needed to recover if the power
1754 * fails during the key creation process, to clean up on the secure
1755 * element side after restarting. Obtaining a slot number from the
1756 * secure element driver updates its persistent state, but we do not yet
1757 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001758 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001759 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001760 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001761 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1762 &slot_number);
1763 if (status != PSA_SUCCESS) {
1764 return status;
1765 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001767 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1768 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001769 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001770 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001771 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001772 status = psa_crypto_save_transaction();
1773 if (status != PSA_SUCCESS) {
1774 (void) psa_crypto_stop_transaction();
1775 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001776 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001777 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001778
1779 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001780 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001781 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001783 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001784 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001785 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001786 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001787#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1788
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001789 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001790}
Gilles Peskine4747d192019-04-17 15:05:45 +02001791
1792/** Finalize the creation of a key once its key material has been set.
1793 *
1794 * This entails writing the key to persistent storage.
1795 *
1796 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001797 * See the documentation of psa_start_key_creation() for the intended use
1798 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001799 *
Ronald Cron5c522922020-11-14 16:35:34 +01001800 * If the finalization succeeds, the function unlocks the key slot (it was
1801 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1802 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001803 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001804 * \param[in,out] slot Pointer to the slot with key material.
1805 * \param[in] driver The secure element driver for the key,
1806 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001807 * \param[out] key On success, identifier of the key. Note that the
1808 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001809 *
1810 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001811 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001812 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1813 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1814 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1815 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1816 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1817 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001818 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001819 * \return If this function fails, the key slot is an invalid state.
1820 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001821 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001822static psa_status_t psa_finish_key_creation(
1823 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001824 psa_se_drv_table_entry_t *driver,
1825 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001826{
1827 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001828 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001829 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001830
1831#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001832 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001833#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001834 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001835 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001836 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001837 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001838
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001839 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1840 sizeof(data.slot_number),
1841 "Slot number size does not match psa_se_key_data_storage_t");
1842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001843 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1844 status = psa_save_persistent_key(&slot->attr,
1845 (uint8_t *) &data,
1846 sizeof(data));
1847 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001848#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1849 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001850 /* Key material is saved in export representation in the slot, so
1851 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001852 status = psa_save_persistent_key(&slot->attr,
1853 slot->key.data,
1854 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001855 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001856 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001857#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1858
Gilles Peskinecbaff462019-07-12 23:46:04 +02001859#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001860 /* Finish the transaction for a key creation. This does not
1861 * happen when registering an existing key. Detect this case
1862 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001863 * creation of a persistent key in a secure element requires a transaction,
1864 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001865 if (driver != NULL &&
1866 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1867 status = psa_save_se_persistent_data(driver);
1868 if (status != PSA_SUCCESS) {
1869 psa_destroy_persistent_key(slot->attr.id);
1870 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001871 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001872 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001873 }
1874#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001876 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001877 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001878 status = psa_unlock_key_slot(slot);
1879 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001880 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001881 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001882 }
Ronald Cron50972942020-11-14 11:28:25 +01001883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001884 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001885}
1886
1887/** Abort the creation of a key.
1888 *
1889 * You may call this function after calling psa_start_key_creation(),
1890 * or after psa_finish_key_creation() fails. In other circumstances, this
1891 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001892 * See the documentation of psa_start_key_creation() for the intended use
1893 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001894 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001895 * \param[in,out] slot Pointer to the slot with key material.
1896 * \param[in] driver The secure element driver for the key,
1897 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001898 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001899static void psa_fail_key_creation(psa_key_slot_t *slot,
1900 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001901{
Gilles Peskine011e4282019-06-26 18:34:38 +02001902 (void) driver;
1903
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001904 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001905 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001906 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001907
1908#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001909 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001910 * element, and the failure happened later (when saving metadata
1911 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001912 * element.
1913 * https://github.com/ARMmbed/mbed-crypto/issues/217
1914 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001915
Gilles Peskined7729582019-08-05 15:55:54 +02001916 /* Abort the ongoing transaction if any (there may not be one if
1917 * the creation process failed before starting one, or if the
1918 * key creation is a registration of a key in a secure element).
1919 * Earlier functions must already have done what it takes to undo any
1920 * partial creation. All that's left is to update the transaction data
1921 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001922 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001923#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1924
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001925 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001926}
1927
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001928/** Validate optional attributes during key creation.
1929 *
1930 * Some key attributes are optional during key creation. If they are
1931 * specified in the attributes structure, check that they are consistent
1932 * with the data in the slot.
1933 *
1934 * This function should be called near the end of key creation, after
1935 * the slot in memory is fully populated but before saving persistent data.
1936 */
1937static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001938 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001939 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001940{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001941 if (attributes->core.type != 0) {
1942 if (attributes->core.type != slot->attr.type) {
1943 return PSA_ERROR_INVALID_ARGUMENT;
1944 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001945 }
1946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001947 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001948#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001949 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1950 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001951 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001952 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001953 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001954
Ronald Cron90857082020-11-25 14:58:33 +01001955 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001956 slot->attr.type,
1957 slot->key.data,
1958 slot->key.bytes,
1959 &rsa);
1960 if (status != PSA_SUCCESS) {
1961 return status;
1962 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001964 mbedtls_mpi_init(&actual);
1965 mbedtls_mpi_init(&required);
1966 ret = mbedtls_rsa_export(rsa,
1967 NULL, NULL, NULL, NULL, &actual);
1968 mbedtls_rsa_free(rsa);
1969 mbedtls_free(rsa);
1970 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001971 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001972 }
1973 ret = mbedtls_mpi_read_binary(&required,
1974 attributes->domain_parameters,
1975 attributes->domain_parameters_size);
1976 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001977 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001978 }
1979 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001980 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001981 }
1982rsa_exit:
1983 mbedtls_mpi_free(&actual);
1984 mbedtls_mpi_free(&required);
1985 if (ret != 0) {
1986 return mbedtls_to_psa_error(ret);
1987 }
1988 } else
John Durkop9814fa22020-11-04 12:28:15 -08001989#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1990 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001991 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001992 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001993 }
1994 }
1995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001996 if (attributes->core.bits != 0) {
1997 if (attributes->core.bits != slot->attr.bits) {
1998 return PSA_ERROR_INVALID_ARGUMENT;
1999 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002000 }
2001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002002 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002003}
2004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002005psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2006 const uint8_t *data,
2007 size_t data_length,
2008 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002009{
2010 psa_status_t status;
2011 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002012 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002013 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002014
Ronald Cron81709fc2020-11-14 12:10:32 +01002015 *key = MBEDTLS_SVC_KEY_ID_INIT;
2016
Gilles Peskine0f84d622019-09-12 19:03:13 +02002017 /* Reject zero-length symmetric keys (including raw data key objects).
2018 * This also rejects any key which might be encoded as an empty string,
2019 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002020 if (data_length == 0) {
2021 return PSA_ERROR_INVALID_ARGUMENT;
2022 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002023
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002024 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2025 &slot, &driver);
2026 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002027 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002029
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002030 /* In the case of a transparent key or an opaque key stored in local
2031 * storage (thus not in the case of generating a key in a secure element
2032 * or cryptoprocessor with storage), we have to allocate a buffer to
2033 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002034 if (slot->key.data == NULL) {
2035 status = psa_allocate_buffer_to_slot(slot, data_length);
2036 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002037 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002038 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002039 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002040
2041 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002042 status = psa_driver_wrapper_import_key(attributes,
2043 data, data_length,
2044 slot->key.data,
2045 slot->key.bytes,
2046 &slot->key.bytes, &bits);
2047 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002048 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002049 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002051 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002052 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002053 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002054 status = PSA_ERROR_INVALID_ARGUMENT;
2055 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002056 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002058 status = psa_validate_optional_attributes(slot, attributes);
2059 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002060 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002061 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002063 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002064exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002065 if (status != PSA_SUCCESS) {
2066 psa_fail_key_creation(slot, driver);
2067 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002069 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002070}
2071
Gilles Peskined7729582019-08-05 15:55:54 +02002072#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2073psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002074 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002075{
2076 psa_status_t status;
2077 psa_key_slot_t *slot = NULL;
2078 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002079 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002080
2081 /* Leaving attributes unspecified is not currently supported.
2082 * It could make sense to query the key type and size from the
2083 * secure element, but not all secure elements support this
2084 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002085 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2086 return PSA_ERROR_NOT_SUPPORTED;
2087 }
2088 if (psa_get_key_bits(attributes) == 0) {
2089 return PSA_ERROR_NOT_SUPPORTED;
2090 }
Gilles Peskined7729582019-08-05 15:55:54 +02002091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002092 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2093 &slot, &driver);
2094 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002095 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002096 }
Gilles Peskined7729582019-08-05 15:55:54 +02002097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002098 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002099
2100exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 if (status != PSA_SUCCESS) {
2102 psa_fail_key_creation(slot, driver);
2103 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002104
Gilles Peskined7729582019-08-05 15:55:54 +02002105 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002106 psa_close_key(key);
2107 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002108}
2109#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002111static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2112 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002113{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002114 psa_status_t status = psa_copy_key_material_into_slot(target,
2115 source->key.data,
2116 source->key.bytes);
2117 if (status != PSA_SUCCESS) {
2118 return status;
2119 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002120
Steven Cooreman398aee52020-10-13 14:35:45 +02002121 target->attr.type = source->attr.type;
2122 target->attr.bits = source->attr.bits;
2123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002124 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002125}
2126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002127psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2128 const psa_key_attributes_t *specified_attributes,
2129 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002130{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002131 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002132 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002133 psa_key_slot_t *source_slot = NULL;
2134 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002135 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002136 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002137
Ronald Cron81709fc2020-11-14 12:10:32 +01002138 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2139
Ronald Cron5c522922020-11-14 16:35:34 +01002140 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002141 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2142 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002143 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002144 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002146 status = psa_validate_optional_attributes(source_slot,
2147 specified_attributes);
2148 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002149 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002150 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152 status = psa_restrict_key_policy(source_slot->attr.type,
2153 &actual_attributes.core.policy,
2154 &source_slot->attr.policy);
2155 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002156 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002157 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002159 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2160 &target_slot, &driver);
2161 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002162 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002163 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002164
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002165#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002166 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002167 /* Copying to a secure element is not implemented yet. */
2168 status = PSA_ERROR_NOT_SUPPORTED;
2169 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002170 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002171#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002173 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002174 /*
2175 * Copying through an opaque driver is not implemented yet, consider
2176 * a lifetime with an external location as an invalid parameter for
2177 * now.
2178 */
2179 status = PSA_ERROR_INVALID_ARGUMENT;
2180 goto exit;
2181 }
2182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002183 status = psa_copy_key_material(source_slot, target_slot);
2184 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002185 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002186 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002188 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002189exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002190 if (status != PSA_SUCCESS) {
2191 psa_fail_key_creation(target_slot, driver);
2192 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002194 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002196 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002197}
2198
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002199
2200
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002201/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002202/* Message digests */
2203/****************************************************************/
2204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002205psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002206{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002207 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002208 if (operation->id == 0) {
2209 return PSA_SUCCESS;
2210 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002212 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002213 operation->id = 0;
2214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002215 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002216}
2217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002218psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2219 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002220{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002221 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002222
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002223 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002224 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002225 status = PSA_ERROR_BAD_STATE;
2226 goto exit;
2227 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002229 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002230 status = PSA_ERROR_INVALID_ARGUMENT;
2231 goto exit;
2232 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002233
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002234 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2235 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002236 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002238 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002239
2240exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002241 if (status != PSA_SUCCESS) {
2242 psa_hash_abort(operation);
2243 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002244
2245 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002246}
2247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002248psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2249 const uint8_t *input,
2250 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002251{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002252 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002254 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002255 status = PSA_ERROR_BAD_STATE;
2256 goto exit;
2257 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002258
Steven Cooremanc8288352021-03-04 14:02:19 +01002259 /* Don't require hash implementations to behave correctly on a
2260 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002261 if (input_length == 0) {
2262 return PSA_SUCCESS;
2263 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002265 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002266
2267exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002268 if (status != PSA_SUCCESS) {
2269 psa_hash_abort(operation);
2270 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002272 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002273}
2274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002275psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2276 uint8_t *hash,
2277 size_t hash_size,
2278 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002279{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002280 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002281 if (operation->id == 0) {
2282 return PSA_ERROR_BAD_STATE;
2283 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002284
Steven Cooreman1e582352021-02-18 17:24:37 +01002285 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002286 operation, hash, hash_size, hash_length);
2287 psa_hash_abort(operation);
2288 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002289}
2290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002291psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2292 const uint8_t *hash,
2293 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002294{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002295 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002296 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002297 psa_status_t status = psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002298 operation,
2299 actual_hash, sizeof(actual_hash),
2300 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002302 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002303 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002304 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002306 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002307 status = PSA_ERROR_INVALID_SIGNATURE;
2308 goto exit;
2309 }
2310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002311 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002312 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002313 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002314
2315exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002316 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2317 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002318 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002319 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002321 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322}
2323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002324psa_status_t psa_hash_compute(psa_algorithm_t alg,
2325 const uint8_t *input, size_t input_length,
2326 uint8_t *hash, size_t hash_size,
2327 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002328{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002329 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002330 if (!PSA_ALG_IS_HASH(alg)) {
2331 return PSA_ERROR_INVALID_ARGUMENT;
2332 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002334 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2335 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002336}
2337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002338psa_status_t psa_hash_compare(psa_algorithm_t alg,
2339 const uint8_t *input, size_t input_length,
2340 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002341{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002342 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002343 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002345 if (!PSA_ALG_IS_HASH(alg)) {
2346 return PSA_ERROR_INVALID_ARGUMENT;
2347 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002348
Steven Cooreman1e582352021-02-18 17:24:37 +01002349 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002350 alg, input, input_length,
2351 actual_hash, sizeof(actual_hash),
2352 &actual_hash_length);
2353 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002354 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002355 }
2356 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002357 status = PSA_ERROR_INVALID_SIGNATURE;
2358 goto exit;
2359 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002360 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002361 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002362 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002363
2364exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002365 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2366 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002367}
2368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002369psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2370 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002371{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002372 if (source_operation->id == 0 ||
2373 target_operation->id != 0) {
2374 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002375 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002377 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2378 target_operation);
2379 if (status != PSA_SUCCESS) {
2380 psa_hash_abort(target_operation);
2381 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002383 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002384}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002385
2386
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002387/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002388/* MAC */
2389/****************************************************************/
2390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002391psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002392{
Steven Cooreman07897832021-03-19 18:28:56 +01002393 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002394 if (operation->id == 0) {
2395 return PSA_SUCCESS;
2396 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002398 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002399 operation->mac_size = 0;
2400 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002401 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002404}
2405
Ronald Cron1c650a12021-06-17 16:33:22 +02002406static psa_status_t psa_mac_finalize_alg_and_key_validation(
2407 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002408 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002409 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002410{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002411 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002412 psa_key_type_t key_type = psa_get_key_type(attributes);
2413 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002415 if (!PSA_ALG_IS_MAC(alg)) {
2416 return PSA_ERROR_INVALID_ARGUMENT;
2417 }
Steven Cooreman07897832021-03-19 18:28:56 +01002418
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002419 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002420 status = psa_mac_key_can_do(alg, key_type);
2421 if (status != PSA_SUCCESS) {
2422 return status;
2423 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002424
Steven Cooremana6474de2021-05-10 11:13:41 +02002425 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002426 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002428 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002429 /* A very short MAC is too short for security since it can be
2430 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2431 * so we make this our minimum, even though 32 bits is still
2432 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002433 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002434 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002436 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2437 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002438 /* It's impossible to "truncate" to a larger length than the full length
2439 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002440 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002441 }
2442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002443 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002444 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2445 * that is disabled in the compile-time configuration. The result can
2446 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2447 * configuration into account. In this case, force a return of
2448 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2449 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2450 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2451 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2452 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002453 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002454 }
2455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002456 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002457}
2458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002459static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2460 mbedtls_svc_key_id_t key,
2461 psa_algorithm_t alg,
2462 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002463{
2464 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2465 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002466 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002467 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002468
2469 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002470 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002471 status = PSA_ERROR_BAD_STATE;
2472 goto exit;
2473 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002474
2475 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002476 key,
2477 &slot,
2478 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2479 alg);
2480 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002481 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002482 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002483
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002484 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002485 .core = slot->attr
2486 };
2487
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002488 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2489 &operation->mac_size);
2490 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002491 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002492 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002493
2494 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002495 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002496 if (is_sign) {
2497 status = psa_driver_wrapper_mac_sign_setup(operation,
2498 &attributes,
2499 slot->key.data,
2500 slot->key.bytes,
2501 alg);
2502 } else {
2503 status = psa_driver_wrapper_mac_verify_setup(operation,
2504 &attributes,
2505 slot->key.data,
2506 slot->key.bytes,
2507 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002508 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002509
Gilles Peskinefbfac682018-07-08 20:51:54 +02002510exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002511 if (status != PSA_SUCCESS) {
2512 psa_mac_abort(operation);
2513 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002514
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002515 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002517 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518}
2519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002520psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2521 mbedtls_svc_key_id_t key,
2522 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002523{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002524 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002525}
2526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002527psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2528 mbedtls_svc_key_id_t key,
2529 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002530{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002531 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002532}
2533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002534psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2535 const uint8_t *input,
2536 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002538 if (operation->id == 0) {
2539 return PSA_ERROR_BAD_STATE;
2540 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002541
Steven Cooreman11743f92021-03-19 18:38:46 +01002542 /* Don't require hash implementations to behave correctly on a
2543 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002544 if (input_length == 0) {
2545 return PSA_SUCCESS;
2546 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002548 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2549 input, input_length);
2550 if (status != PSA_SUCCESS) {
2551 psa_mac_abort(operation);
2552 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002554 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002555}
2556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002557psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2558 uint8_t *mac,
2559 size_t mac_size,
2560 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002561{
Steven Cooreman87885df2021-03-19 19:04:39 +01002562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2563 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002565 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002566 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002567 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002568 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002570 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002571 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002572 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002573 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002574
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002575 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2576 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002577 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002578 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002579 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002580 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002582 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002583 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002584 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002585 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002587 status = psa_driver_wrapper_mac_sign_finish(operation,
2588 mac, operation->mac_size,
2589 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002590
Dave Rodgman478ab542021-06-25 09:09:02 +01002591exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002592 /* In case of success, set the potential excess room in the output buffer
2593 * to an invalid value, to avoid potentially leaking a longer MAC.
2594 * In case of error, set the output length and content to a safe default,
2595 * such that in case the caller misses an error check, the output would be
2596 * an unachievable MAC.
2597 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002598 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002599 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002600 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002601 }
mohammad16036df908f2018-04-02 08:34:15 -07002602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002603 if (mac_size > operation->mac_size) {
2604 memset(&mac[operation->mac_size], '!',
2605 mac_size - operation->mac_size);
2606 }
Ronald Cron882eb782021-06-18 13:05:48 +02002607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002608 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002610 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002611}
2612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002613psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2614 const uint8_t *mac,
2615 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002616{
Steven Cooreman87885df2021-03-19 19:04:39 +01002617 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2618 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002620 if (operation->id == 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 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002624
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002625 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002626 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002627 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002628 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002630 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002631 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002632 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002633 }
2634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002635 status = psa_driver_wrapper_mac_verify_finish(operation,
2636 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002637
Dave Rodgman478ab542021-06-25 09:09:02 +01002638exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002639 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002641 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002642}
2643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002644static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2645 psa_algorithm_t alg,
2646 const uint8_t *input,
2647 size_t input_length,
2648 uint8_t *mac,
2649 size_t mac_size,
2650 size_t *mac_length,
2651 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002652{
2653 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002654 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002655 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002656 psa_key_slot_t *slot;
2657 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658
Ronald Crondbb86462021-06-17 17:17:20 +02002659 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002660 key,
2661 &slot,
2662 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2663 alg);
2664 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002665 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002666 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002667
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002668 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002669 .core = slot->attr
2670 };
2671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002672 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2673 &operation_mac_size);
2674 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002675 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002676 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002678 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002679 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002680 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002681 }
2682
2683 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002684 &attributes,
2685 slot->key.data, slot->key.bytes,
2686 alg,
2687 input, input_length,
2688 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002689
2690exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002691 /* In case of success, set the potential excess room in the output buffer
2692 * to an invalid value, to avoid potentially leaking a longer MAC.
2693 * In case of error, set the output length and content to a safe default,
2694 * such that in case the caller misses an error check, the output would be
2695 * an unachievable MAC.
2696 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002697 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002698 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002699 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002700 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002701 if (mac_size > operation_mac_size) {
2702 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2703 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002705 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002707 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002708}
2709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002710psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002711 psa_algorithm_t alg,
2712 const uint8_t *input,
2713 size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002714 uint8_t *mac,
2715 size_t mac_size,
2716 size_t *mac_length)
2717{
2718 return psa_mac_compute_internal(key, alg,
2719 input, input_length,
2720 mac, mac_size, mac_length, 1);
2721}
2722
2723psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2724 psa_algorithm_t alg,
2725 const uint8_t *input,
2726 size_t input_length,
2727 const uint8_t *mac,
2728 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002729{
2730 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002731 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2732 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002734 status = psa_mac_compute_internal(key, alg,
2735 input, input_length,
2736 actual_mac, sizeof(actual_mac),
2737 &actual_mac_length, 0);
2738 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002739 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002740 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002741
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002742 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002743 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002744 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002745 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002746 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002747 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002748 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002749 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002750
2751exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002752 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002754 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002755}
Gilles Peskine20035e32018-02-03 22:44:14 +01002756
Gilles Peskine20035e32018-02-03 22:44:14 +01002757/****************************************************************/
2758/* Asymmetric cryptography */
2759/****************************************************************/
2760
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002761static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2762 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002763{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002764 if (input_is_message) {
2765 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2766 return PSA_ERROR_INVALID_ARGUMENT;
2767 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002769 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2770 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2771 return PSA_ERROR_INVALID_ARGUMENT;
2772 }
2773 }
2774 } else {
2775 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2776 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002777 }
2778 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002780 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002781}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002783static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2784 int input_is_message,
2785 psa_algorithm_t alg,
2786 const uint8_t *input,
2787 size_t input_length,
2788 uint8_t *signature,
2789 size_t signature_size,
2790 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002791{
2792 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2793 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002794 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002795 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002796
2797 *signature_length = 0;
2798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002799 status = psa_sign_verify_check_alg(input_is_message, alg);
2800 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002801 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002802 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002803
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002804 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002805 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002806 * buffer can in principle be empty since it doesn't actually have
2807 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002808 if (signature_size == 0) {
2809 return PSA_ERROR_BUFFER_TOO_SMALL;
2810 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002811
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002812 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002813 key, &slot,
2814 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2815 PSA_KEY_USAGE_SIGN_HASH,
2816 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002818 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002819 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002820 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002822 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002823 status = PSA_ERROR_INVALID_ARGUMENT;
2824 goto exit;
2825 }
2826
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002827 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002828 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002829 };
2830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002831 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002832 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002833 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002834 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002835 signature, signature_size, signature_length);
2836 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002837
2838 status = psa_driver_wrapper_sign_hash(
2839 &attributes, slot->key.data, slot->key.bytes,
2840 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002841 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002842 }
2843
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002844
2845exit:
2846 /* Fill the unused part of the output buffer (the whole buffer on error,
2847 * the trailing part on success) with something that isn't a valid signature
2848 * (barring an attack on the signature and deliberately-crafted input),
2849 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002850 if (status == PSA_SUCCESS) {
2851 memset(signature + *signature_length, '!',
2852 signature_size - *signature_length);
2853 } else {
2854 memset(signature, '!', signature_size);
2855 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002856 /* If signature_size is 0 then we have nothing to do. We must not call
2857 * memset because signature may be NULL in this case. */
2858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002859 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002861 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002862}
2863
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002864static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2865 int input_is_message,
2866 psa_algorithm_t alg,
2867 const uint8_t *input,
2868 size_t input_length,
2869 const uint8_t *signature,
2870 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002871{
2872 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2873 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2874 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002876 status = psa_sign_verify_check_alg(input_is_message, alg);
2877 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002878 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002879 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002880
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002881 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002882 key, &slot,
2883 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2884 PSA_KEY_USAGE_VERIFY_HASH,
2885 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002887 if (status != PSA_SUCCESS) {
2888 return status;
2889 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002890
2891 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002892 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002893 };
2894
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002895 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002896 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002897 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002898 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002899 signature, signature_length);
2900 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002901 status = psa_driver_wrapper_verify_hash(
2902 &attributes, slot->key.data, slot->key.bytes,
2903 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002904 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002905 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002907 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002909 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002910
2911}
2912
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002913psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002914 const psa_key_attributes_t *attributes,
2915 const uint8_t *key_buffer,
2916 size_t key_buffer_size,
2917 psa_algorithm_t alg,
2918 const uint8_t *input,
2919 size_t input_length,
2920 uint8_t *signature,
2921 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002923{
2924 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002926 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002927 size_t hash_length;
2928 uint8_t hash[PSA_HASH_MAX_SIZE];
2929
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002930 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002931 PSA_ALG_SIGN_GET_HASH(alg),
2932 input, input_length,
2933 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002934
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002935 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002936 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002937 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002938
2939 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002940 attributes, key_buffer, key_buffer_size,
2941 alg, hash, hash_length,
2942 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002943 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002944
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002945 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002946}
2947
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002948psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2949 psa_algorithm_t alg,
2950 const uint8_t *input,
2951 size_t input_length,
2952 uint8_t *signature,
2953 size_t signature_size,
2954 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002955{
2956 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002957 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002958 signature, signature_size, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002959}
2960
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002961psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002962 const psa_key_attributes_t *attributes,
2963 const uint8_t *key_buffer,
2964 size_t key_buffer_size,
2965 psa_algorithm_t alg,
2966 const uint8_t *input,
2967 size_t input_length,
2968 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002969 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002970{
2971 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002972
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002973 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002974 size_t hash_length;
2975 uint8_t hash[PSA_HASH_MAX_SIZE];
2976
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002977 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002978 PSA_ALG_SIGN_GET_HASH(alg),
2979 input, input_length,
2980 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002982 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002983 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002984 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002985
2986 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002987 attributes, key_buffer, key_buffer_size,
2988 alg, hash, hash_length,
2989 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002990 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002991
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002992 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002993}
2994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002995psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2996 psa_algorithm_t alg,
2997 const uint8_t *input,
2998 size_t input_length,
2999 const uint8_t *signature,
3000 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003001{
3002 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003003 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003004 signature, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003005}
3006
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003007psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003008 const psa_key_attributes_t *attributes,
3009 const uint8_t *key_buffer, size_t key_buffer_size,
3010 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003011 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01003012{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003013 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3014 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3015 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003016#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003017 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3018 return mbedtls_psa_rsa_sign_hash(
3019 attributes,
3020 key_buffer, key_buffer_size,
3021 alg, hash, hash_length,
3022 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003023#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3024 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003025 } else {
3026 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003027 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003028 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3029 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003030#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003031 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3032 return mbedtls_psa_ecdsa_sign_hash(
3033 attributes,
3034 key_buffer, key_buffer_size,
3035 alg, hash, hash_length,
3036 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003037#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3038 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003039 } else {
3040 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003041 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003042 }
Ronald Cron4501c982021-03-19 20:09:32 +01003043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003044 (void) key_buffer;
3045 (void) key_buffer_size;
3046 (void) hash;
3047 (void) hash_length;
3048 (void) signature;
3049 (void) signature_size;
3050 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003052 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003053}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003055psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3056 psa_algorithm_t alg,
3057 const uint8_t *hash,
3058 size_t hash_length,
3059 uint8_t *signature,
3060 size_t signature_size,
3061 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003062{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003063 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003064 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003065 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003066}
3067
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003068psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003069 const psa_key_attributes_t *attributes,
3070 const uint8_t *key_buffer, size_t key_buffer_size,
3071 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003072 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003073{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003074 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3075 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3076 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003077#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003078 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3079 return mbedtls_psa_rsa_verify_hash(
3080 attributes,
3081 key_buffer, key_buffer_size,
3082 alg, hash, hash_length,
3083 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003084#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3085 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003086 } else {
3087 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003088 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003089 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3090 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003091#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003092 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3093 return mbedtls_psa_ecdsa_verify_hash(
3094 attributes,
3095 key_buffer, key_buffer_size,
3096 alg, hash, hash_length,
3097 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003098#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3099 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003100 } else {
3101 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003102 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003103 }
Ronald Cron4501c982021-03-19 20:09:32 +01003104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003105 (void) key_buffer;
3106 (void) key_buffer_size;
3107 (void) hash;
3108 (void) hash_length;
3109 (void) signature;
3110 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003112 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003113}
3114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003115psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3116 psa_algorithm_t alg,
3117 const uint8_t *hash,
3118 size_t hash_length,
3119 const uint8_t *signature,
3120 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003121{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003122 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003123 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003124 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003125}
3126
John Durkop0e005192020-10-31 22:06:54 -07003127#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003128static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3129 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003130{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003131 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3132 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3133 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3134 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003135}
John Durkop0e005192020-10-31 22:06:54 -07003136#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003138psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3139 psa_algorithm_t alg,
3140 const uint8_t *input,
3141 size_t input_length,
3142 const uint8_t *salt,
3143 size_t salt_length,
3144 uint8_t *output,
3145 size_t output_size,
3146 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003148 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003149 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003150 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003151
Darryl Green5cc689a2018-07-24 15:34:10 +01003152 (void) input;
3153 (void) input_length;
3154 (void) salt;
3155 (void) output;
3156 (void) output_size;
3157
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003158 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003160 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3161 return PSA_ERROR_INVALID_ARGUMENT;
3162 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003163
Ronald Cron5c522922020-11-14 16:35:34 +01003164 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003165 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3166 if (status != PSA_SUCCESS) {
3167 return status;
3168 }
3169 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3170 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003171 status = PSA_ERROR_INVALID_ARGUMENT;
3172 goto exit;
3173 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003175 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003176#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003177 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003178 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003179 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3180 slot->key.data,
3181 slot->key.bytes,
3182 &rsa);
3183 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003184 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003185 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003187 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003188 status = PSA_ERROR_BUFFER_TOO_SMALL;
3189 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003190 }
Ronald Cron7207d572021-09-08 14:28:35 +02003191#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3192 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003193 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003194#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003195 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003196 mbedtls_rsa_pkcs1_encrypt(rsa,
3197 mbedtls_psa_get_random,
3198 MBEDTLS_PSA_RANDOM_STATE,
3199 MBEDTLS_RSA_PUBLIC,
3200 input_length,
3201 input,
3202 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003203#else
3204 status = PSA_ERROR_NOT_SUPPORTED;
3205#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003206 } else
3207 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003208#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003209 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003210 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003211 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3212 mbedtls_psa_get_random,
3213 MBEDTLS_PSA_RANDOM_STATE,
3214 MBEDTLS_RSA_PUBLIC,
3215 salt, salt_length,
3216 input_length,
3217 input,
3218 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003219#else
3220 status = PSA_ERROR_NOT_SUPPORTED;
3221#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003222 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003223 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003224 }
Ronald Cron7207d572021-09-08 14:28:35 +02003225#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003226 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003227rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003228 if (status == PSA_SUCCESS) {
3229 *output_length = mbedtls_rsa_get_len(rsa);
3230 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003232 mbedtls_rsa_free(rsa);
3233 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003234#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003235 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003236 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003237 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003239
3240exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003241 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003243 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003244}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003246psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3247 psa_algorithm_t alg,
3248 const uint8_t *input,
3249 size_t input_length,
3250 const uint8_t *salt,
3251 size_t salt_length,
3252 uint8_t *output,
3253 size_t output_size,
3254 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003255{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003256 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003257 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003258 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003259
Darryl Green5cc689a2018-07-24 15:34:10 +01003260 (void) input;
3261 (void) input_length;
3262 (void) salt;
3263 (void) output;
3264 (void) output_size;
3265
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003266 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003268 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3269 return PSA_ERROR_INVALID_ARGUMENT;
3270 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003271
Ronald Cron5c522922020-11-14 16:35:34 +01003272 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003273 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3274 if (status != PSA_SUCCESS) {
3275 return status;
3276 }
3277 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003278 status = PSA_ERROR_INVALID_ARGUMENT;
3279 goto exit;
3280 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003282 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003283#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003284 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003285 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003286 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3287 slot->key.data,
3288 slot->key.bytes,
3289 &rsa);
3290 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003291 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003292 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003294 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003295 status = PSA_ERROR_INVALID_ARGUMENT;
3296 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003297 }
Ronald Cron7207d572021-09-08 14:28:35 +02003298#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3299 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003301 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003302#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003303 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003304 mbedtls_rsa_pkcs1_decrypt(rsa,
3305 mbedtls_psa_get_random,
3306 MBEDTLS_PSA_RANDOM_STATE,
3307 MBEDTLS_RSA_PRIVATE,
3308 output_length,
3309 input,
3310 output,
3311 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003312#else
3313 status = PSA_ERROR_NOT_SUPPORTED;
3314#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003315 } else
3316 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003317#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003318 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003319 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003320 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3321 mbedtls_psa_get_random,
3322 MBEDTLS_PSA_RANDOM_STATE,
3323 MBEDTLS_RSA_PRIVATE,
3324 salt, salt_length,
3325 output_length,
3326 input,
3327 output,
3328 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003329#else
3330 status = PSA_ERROR_NOT_SUPPORTED;
3331#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003332 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003333 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003334 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003335
Ronald Cron7207d572021-09-08 14:28:35 +02003336#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003337 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003338rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003339 mbedtls_rsa_free(rsa);
3340 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003341#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3342 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003343 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003344 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003345 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003346
3347exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003348 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003350 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003351}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003352
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003353
3354
mohammad1603503973b2018-03-12 15:59:30 +02003355/****************************************************************/
3356/* Symmetric cryptography */
3357/****************************************************************/
3358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003359static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3360 mbedtls_svc_key_id_t key,
3361 psa_algorithm_t alg,
3362 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003363{
3364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3365 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003366 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003367 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003368 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3369 PSA_KEY_USAGE_ENCRYPT :
3370 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003371
3372 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003373 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003374 status = PSA_ERROR_BAD_STATE;
3375 goto exit;
3376 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003378 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003379 status = PSA_ERROR_INVALID_ARGUMENT;
3380 goto exit;
3381 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003383 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3384 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003385 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003386 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003387
Ronald Cron49fafa92021-03-10 08:34:23 +01003388 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003389 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003390 * so we only set it (in the driver wrapper) after resources have been
3391 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003392 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003393 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003394 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003395 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003396 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003397 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003398 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003399 }
3400 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003401
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003402 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003403 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003404 };
3405
Ronald Cronab99ac22020-10-01 16:38:28 +02003406 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003407 if (cipher_operation == MBEDTLS_ENCRYPT) {
3408 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3409 &attributes,
3410 slot->key.data,
3411 slot->key.bytes,
3412 alg);
3413 } else {
3414 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3415 &attributes,
3416 slot->key.data,
3417 slot->key.bytes,
3418 alg);
3419 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003420
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003421exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003422 if (status != PSA_SUCCESS) {
3423 psa_cipher_abort(operation);
3424 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003426 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003428 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003429}
3430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003431psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3432 mbedtls_svc_key_id_t key,
3433 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003434{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003435 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003436}
3437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003438psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3439 mbedtls_svc_key_id_t key,
3440 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003441{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003442 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003443}
3444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003445psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3446 uint8_t *iv,
3447 size_t iv_size,
3448 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003449{
Ronald Cron6d051732020-10-01 14:10:20 +02003450 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003451 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergey04eb7c02023-03-06 15:37:23 -07003452 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003454 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003455 status = PSA_ERROR_BAD_STATE;
3456 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003457 }
3458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003459 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003460 status = PSA_ERROR_BAD_STATE;
3461 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003462 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003463
Ronald Cronc423acb2021-07-05 12:31:44 +02003464 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003465 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003466 status = PSA_ERROR_BUFFER_TOO_SMALL;
3467 goto exit;
3468 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003470 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003471 status = PSA_ERROR_GENERIC_ERROR;
3472 goto exit;
3473 }
3474
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003475 status = psa_generate_random(local_iv, default_iv_length);
3476 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003477 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003478 }
Ronald Cron5618a392021-03-26 09:52:26 +01003479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003480 status = psa_driver_wrapper_cipher_set_iv(operation,
3481 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003482
3483exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003484 if (status == PSA_SUCCESS) {
3485 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003486 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003487 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003488 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003489 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003490 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003491 }
Ronald Cron6d051732020-10-01 14:10:20 +02003492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003493 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003494}
3495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003496psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3497 const uint8_t *iv,
3498 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003499{
Ronald Cron6d051732020-10-01 14:10:20 +02003500 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003501
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003502 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003503 status = PSA_ERROR_BAD_STATE;
3504 goto exit;
3505 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003507 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003508 status = PSA_ERROR_BAD_STATE;
3509 goto exit;
3510 }
Ronald Crona0d68172021-03-26 10:15:08 +01003511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003512 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003513 status = PSA_ERROR_INVALID_ARGUMENT;
3514 goto exit;
3515 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003517 status = psa_driver_wrapper_cipher_set_iv(operation,
3518 iv,
3519 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003520
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003521exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003522 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003523 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003524 } else {
3525 psa_cipher_abort(operation);
3526 }
3527 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003528}
3529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003530psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3531 const uint8_t *input,
3532 size_t input_length,
3533 uint8_t *output,
3534 size_t output_size,
3535 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003536{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003537 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003539 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003540 status = PSA_ERROR_BAD_STATE;
3541 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003542 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003544 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003545 status = PSA_ERROR_BAD_STATE;
3546 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003547 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003549 status = psa_driver_wrapper_cipher_update(operation,
3550 input,
3551 input_length,
3552 output,
3553 output_size,
3554 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003555
3556exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003557 if (status != PSA_SUCCESS) {
3558 psa_cipher_abort(operation);
3559 }
Ronald Cron6d051732020-10-01 14:10:20 +02003560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003561 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003562}
3563
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003564psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3565 uint8_t *output,
3566 size_t output_size,
3567 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003568{
David Saadab4ecc272019-02-14 13:48:10 +02003569 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003571 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003572 status = PSA_ERROR_BAD_STATE;
3573 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003574 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003576 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003577 status = PSA_ERROR_BAD_STATE;
3578 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003579 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003581 status = psa_driver_wrapper_cipher_finish(operation,
3582 output,
3583 output_size,
3584 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003585
3586exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003587 if (status == PSA_SUCCESS) {
3588 return psa_cipher_abort(operation);
3589 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003590 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003591 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003593 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003594 }
mohammad1603503973b2018-03-12 15:59:30 +02003595}
3596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003597psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003598{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003599 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003600 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003601 * in use. It's ok to call abort on such an object, and there's
3602 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003603 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003604 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003606 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003607
Ronald Cron49fafa92021-03-10 08:34:23 +01003608 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003609 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003610 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003612 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003613}
3614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003615psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3616 psa_algorithm_t alg,
3617 const uint8_t *input,
3618 size_t input_length,
3619 uint8_t *output,
3620 size_t output_size,
3621 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003622{
3623 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003624 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003625 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003626 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003627 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3628 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003629
David Horstmann34980bd2023-11-29 17:07:13 +00003630 SWAP_FOR_LOCAL_INPUT(input, input_length);
3631 SWAP_FOR_LOCAL_OUTPUT(output, output_size);
David Horstmannd2ad8862023-11-27 17:43:05 +00003632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003633 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003634 status = PSA_ERROR_INVALID_ARGUMENT;
3635 goto exit;
3636 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003638 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3639 PSA_KEY_USAGE_ENCRYPT,
3640 alg);
3641 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003642 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003643 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003644
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003645 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003646 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003647 };
3648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003649 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3650 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003651 status = PSA_ERROR_GENERIC_ERROR;
3652 goto exit;
3653 }
3654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003655 if (default_iv_length > 0) {
3656 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003657 status = PSA_ERROR_BUFFER_TOO_SMALL;
3658 goto exit;
3659 }
3660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003661 status = psa_generate_random(local_iv, default_iv_length);
3662 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003663 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003664 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003665 }
3666
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003667 status = psa_driver_wrapper_cipher_encrypt(
3668 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003669 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003670 mbedtls_buffer_offset(output, default_iv_length),
3671 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003672
3673exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003674 unlock_status = psa_unlock_key_slot(slot);
3675 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003676 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003677 }
Ronald Cron16377072021-07-08 19:00:07 +02003678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003679 if (status == PSA_SUCCESS) {
3680 if (default_iv_length > 0) {
3681 memcpy(output, local_iv, default_iv_length);
3682 }
3683 *output_length += default_iv_length;
3684 } else {
3685 *output_length = 0;
3686 }
3687
David Horstmann34980bd2023-11-29 17:07:13 +00003688 FREE_LOCAL_INPUT(input);
3689 FREE_LOCAL_OUTPUT(output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003691 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003692}
3693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003694psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3695 psa_algorithm_t alg,
3696 const uint8_t *input,
3697 size_t input_length,
3698 uint8_t *output,
3699 size_t output_size,
3700 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003701{
3702 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003703 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003704 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003705 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003707 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003708 status = PSA_ERROR_INVALID_ARGUMENT;
3709 goto exit;
3710 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003712 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3713 PSA_KEY_USAGE_DECRYPT,
3714 alg);
3715 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003716 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003717 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003718
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003719 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003720 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003721 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003723 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003724 status = PSA_ERROR_INVALID_ARGUMENT;
3725 goto exit;
3726 }
3727
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003728 status = psa_driver_wrapper_cipher_decrypt(
3729 &attributes, slot->key.data, slot->key.bytes,
3730 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003731 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003732
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003733exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003734 unlock_status = psa_unlock_key_slot(slot);
3735 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003736 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003737 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003739 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003740 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003741 }
Ronald Cron16377072021-07-08 19:00:07 +02003742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003743 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003744}
3745
3746
mohammad16035955c982018-04-26 00:53:03 +03003747/****************************************************************/
3748/* AEAD */
3749/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003751psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3752 psa_algorithm_t alg,
3753 const uint8_t *nonce,
3754 size_t nonce_length,
3755 const uint8_t *additional_data,
3756 size_t additional_data_length,
3757 const uint8_t *plaintext,
3758 size_t plaintext_length,
3759 uint8_t *ciphertext,
3760 size_t ciphertext_size,
3761 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003762{
Ronald Cron215633c2021-03-16 17:15:37 +01003763 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3764 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003765
mohammad1603f08a5502018-06-03 15:05:47 +03003766 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003767
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003768 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3769 return PSA_ERROR_NOT_SUPPORTED;
3770 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003771
Ronald Cron9a986162021-03-26 12:40:07 +01003772 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003773 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3774 if (status != PSA_SUCCESS) {
3775 return status;
3776 }
mohammad16035955c982018-04-26 00:53:03 +03003777
Ronald Cron215633c2021-03-16 17:15:37 +01003778 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003779 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003780 };
mohammad16035955c982018-04-26 00:53:03 +03003781
Ronald Cronde822812021-03-17 16:08:20 +01003782 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003783 &attributes, slot->key.data, slot->key.bytes,
3784 alg,
3785 nonce, nonce_length,
3786 additional_data, additional_data_length,
3787 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003788 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003790 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3791 memset(ciphertext, 0, ciphertext_size);
3792 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003794 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003796 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003797}
3798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003799psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3800 psa_algorithm_t alg,
3801 const uint8_t *nonce,
3802 size_t nonce_length,
3803 const uint8_t *additional_data,
3804 size_t additional_data_length,
3805 const uint8_t *ciphertext,
3806 size_t ciphertext_length,
3807 uint8_t *plaintext,
3808 size_t plaintext_size,
3809 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003810{
Ronald Cron215633c2021-03-16 17:15:37 +01003811 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3812 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003813
Gilles Peskineee652a32018-06-01 19:23:52 +02003814 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003816 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3817 return PSA_ERROR_NOT_SUPPORTED;
3818 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003819
Ronald Cron9a986162021-03-26 12:40:07 +01003820 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003821 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3822 if (status != PSA_SUCCESS) {
3823 return status;
3824 }
mohammad16035955c982018-04-26 00:53:03 +03003825
Ronald Cron215633c2021-03-16 17:15:37 +01003826 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003827 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003828 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003829
Ronald Cronde822812021-03-17 16:08:20 +01003830 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003831 &attributes, slot->key.data, slot->key.bytes,
3832 alg,
3833 nonce, nonce_length,
3834 additional_data, additional_data_length,
3835 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003836 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003838 if (status != PSA_SUCCESS && plaintext_size != 0) {
3839 memset(plaintext, 0, plaintext_size);
3840 }
mohammad160360a64d02018-06-03 17:20:42 +03003841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003842 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003843
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003844 return status;
mohammad16035955c982018-04-26 00:53:03 +03003845}
3846
Gilles Peskinee59236f2018-01-27 23:32:46 +01003847/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003848/* Generators */
3849/****************************************************************/
3850
John Durkop07cc04a2020-11-16 22:08:34 -08003851#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3852 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3853 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3854#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003855#endif /* At least one builtin KDF */
3856
3857#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3858 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3859 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3860static psa_status_t psa_key_derivation_start_hmac(
3861 psa_mac_operation_t *operation,
3862 psa_algorithm_t hash_alg,
3863 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003864 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02003865{
3866 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003868 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3869 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
3870 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02003871
Steven Cooreman15f0d922021-05-07 14:14:37 +02003872 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003873 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02003874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003875 status = psa_driver_wrapper_mac_sign_setup(operation,
3876 &attributes,
3877 hmac_key, hmac_key_length,
3878 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02003879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003880 psa_reset_key_attributes(&attributes);
3881 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02003882}
3883#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003884
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003885#define HKDF_STATE_INIT 0 /* no input yet */
3886#define HKDF_STATE_STARTED 1 /* got salt */
3887#define HKDF_STATE_KEYED 2 /* got key */
3888#define HKDF_STATE_OUTPUT 3 /* output started */
3889
Gilles Peskinecbe66502019-05-16 16:59:18 +02003890static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003891 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003892{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003893 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
3894 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
3895 } else {
3896 return operation->alg;
3897 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003898}
3899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003900psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003901{
3902 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003903 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
3904 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02003905 /* The object has (apparently) been initialized but it is not
3906 * in use. It's ok to call abort on such an object, and there's
3907 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003908 } else
John Durkopd0321952020-10-29 21:37:36 -07003909#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003910 if (PSA_ALG_IS_HKDF(kdf_alg)) {
3911 mbedtls_free(operation->ctx.hkdf.info);
3912 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
3913 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003914#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3915#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3916 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003917 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
3918 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
3919 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
3920 if (operation->ctx.tls12_prf.secret != NULL) {
3921 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
3922 operation->ctx.tls12_prf.secret_length);
3923 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003924 }
3925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003926 if (operation->ctx.tls12_prf.seed != NULL) {
3927 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
3928 operation->ctx.tls12_prf.seed_length);
3929 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01003930 }
3931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003932 if (operation->ctx.tls12_prf.label != NULL) {
3933 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
3934 operation->ctx.tls12_prf.label_length);
3935 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01003936 }
3937
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003938 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003939
3940 /* We leave the fields Ai and output_block to be erased safely by the
3941 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003942 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003943#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3944 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003945 {
3946 status = PSA_ERROR_BAD_STATE;
3947 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003948 mbedtls_platform_zeroize(operation, sizeof(*operation));
3949 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003950}
3951
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003952psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003953 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003954{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003955 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003956 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003957 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003958 }
3959
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003960 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003961 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003962}
3963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003964psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
3965 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003966{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003967 if (operation->alg == 0) {
3968 return PSA_ERROR_BAD_STATE;
3969 }
3970 if (capacity > operation->capacity) {
3971 return PSA_ERROR_INVALID_ARGUMENT;
3972 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003973 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003974 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003975}
3976
John Durkopd0321952020-10-29 21:37:36 -07003977#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003978/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003979 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003980static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
3981 psa_algorithm_t hash_alg,
3982 uint8_t *output,
3983 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003984{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003985 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02003986 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003987 psa_status_t status;
3988
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003989 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
3990 return PSA_ERROR_BAD_STATE;
3991 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003992 hkdf->state = HKDF_STATE_OUTPUT;
3993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003994 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003995 /* Copy what remains of the current block */
3996 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003997 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003998 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003999 }
4000 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004001 output += n;
4002 output_length -= n;
4003 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004004 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004005 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004006 }
Gilles Peskined54931c2018-07-17 21:06:59 +02004007 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004008 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004009 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004010 * object was corrupted or if this function is called directly
4011 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004012 if (hkdf->block_number == 0xff) {
4013 return PSA_ERROR_BAD_STATE;
4014 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004015
4016 /* We need a new block */
4017 ++hkdf->block_number;
4018 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004020 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4021 hash_alg,
4022 hkdf->prk,
4023 hash_length);
4024 if (status != PSA_SUCCESS) {
4025 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004026 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004027
4028 if (hkdf->block_number != 1) {
4029 status = psa_mac_update(&hkdf->hmac,
4030 hkdf->output_block,
4031 hash_length);
4032 if (status != PSA_SUCCESS) {
4033 return status;
4034 }
4035 }
4036 status = psa_mac_update(&hkdf->hmac,
4037 hkdf->info,
4038 hkdf->info_length);
4039 if (status != PSA_SUCCESS) {
4040 return status;
4041 }
4042 status = psa_mac_update(&hkdf->hmac,
4043 &hkdf->block_number, 1);
4044 if (status != PSA_SUCCESS) {
4045 return status;
4046 }
4047 status = psa_mac_sign_finish(&hkdf->hmac,
4048 hkdf->output_block,
4049 sizeof(hkdf->output_block),
4050 &hmac_output_length);
4051 if (status != PSA_SUCCESS) {
4052 return status;
4053 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004054 }
4055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004056 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004057}
John Durkop07cc04a2020-11-16 22:08:34 -08004058#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004059
John Durkop07cc04a2020-11-16 22:08:34 -08004060#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4061 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004062static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4063 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004064 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004065{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004066 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4067 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004068 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4069 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004070 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004071
Janos Follath7742fee2019-06-17 12:58:10 +01004072 /* We can't be wanting more output after block 0xff, otherwise
4073 * the capacity check in psa_key_derivation_output_bytes() would have
4074 * prevented this call. It could happen only if the operation
4075 * object was corrupted or if this function is called directly
4076 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004077 if (tls12_prf->block_number == 0xff) {
4078 return PSA_ERROR_CORRUPTION_DETECTED;
4079 }
Janos Follath7742fee2019-06-17 12:58:10 +01004080
4081 /* We need a new block */
4082 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004083 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004084
4085 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4086 *
4087 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4088 *
4089 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4090 * HMAC_hash(secret, A(2) + seed) +
4091 * HMAC_hash(secret, A(3) + seed) + ...
4092 *
4093 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004094 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004095 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004096 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004097 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004098 * is currently extracted as `output_block` and where i is
4099 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004100 */
4101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004102 status = psa_key_derivation_start_hmac(&hmac,
4103 hash_alg,
4104 tls12_prf->secret,
4105 tls12_prf->secret_length);
4106 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004107 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004108 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004109
4110 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004111 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004112 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4113 * the variable seed and in this instance means it in the context of the
4114 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004115 status = psa_mac_update(&hmac,
4116 tls12_prf->label,
4117 tls12_prf->label_length);
4118 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004119 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004120 }
4121 status = psa_mac_update(&hmac,
4122 tls12_prf->seed,
4123 tls12_prf->seed_length);
4124 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004125 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004126 }
4127 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004128 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004129 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4130 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004131 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004132 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004133 }
4134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004135 status = psa_mac_sign_finish(&hmac,
4136 tls12_prf->Ai, hash_length,
4137 &hmac_output_length);
4138 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004139 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004140 }
4141 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004142 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004143 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004144
4145 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004146 status = psa_key_derivation_start_hmac(&hmac,
4147 hash_alg,
4148 tls12_prf->secret,
4149 tls12_prf->secret_length);
4150 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004151 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004152 }
4153 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4154 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004155 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004156 }
4157 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4158 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004159 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004160 }
4161 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4162 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004163 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004164 }
4165 status = psa_mac_sign_finish(&hmac,
4166 tls12_prf->output_block, hash_length,
4167 &hmac_output_length);
4168 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004169 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004170 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004171
Janos Follath7742fee2019-06-17 12:58:10 +01004172
4173cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004174 cleanup_status = psa_mac_abort(&hmac);
4175 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004176 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004177 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004179 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004180}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004181
Janos Follath844eb0e2019-06-19 12:10:49 +01004182static psa_status_t psa_key_derivation_tls12_prf_read(
4183 psa_tls12_prf_key_derivation_t *tls12_prf,
4184 psa_algorithm_t alg,
4185 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004186 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004187{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004188 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4189 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004190 psa_status_t status;
4191 uint8_t offset, length;
4192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004193 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004194 case PSA_TLS12_PRF_STATE_LABEL_SET:
4195 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4196 break;
4197 case PSA_TLS12_PRF_STATE_OUTPUT:
4198 break;
4199 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004200 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004201 }
4202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004203 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004204 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004205 if (tls12_prf->left_in_block == 0) {
4206 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4207 alg);
4208 if (status != PSA_SUCCESS) {
4209 return status;
4210 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004211
4212 continue;
4213 }
4214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004215 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004216 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004217 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004218 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004219 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004220
4221 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004222 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004223 output += length;
4224 output_length -= length;
4225 tls12_prf->left_in_block -= length;
4226 }
4227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004228 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004229}
John Durkop07cc04a2020-11-16 22:08:34 -08004230#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4231 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004232
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004233psa_status_t psa_key_derivation_output_bytes(
4234 psa_key_derivation_operation_t *operation,
4235 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004236 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004237{
4238 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004239 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004241 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004242 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004243 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004244 }
4245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004246 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004247 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004248 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004249 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004250 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004251 goto exit;
4252 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004253 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004254 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004255 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004256 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4257 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004258 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004259 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004260 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004261 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004262 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004263
John Durkopd0321952020-10-29 21:37:36 -07004264#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004265 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4266 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4267 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4268 output, output_length);
4269 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004270#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4271#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4272 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004273 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4274 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4275 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4276 kdf_alg, output,
4277 output_length);
4278 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004279#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4280 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004281 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004282 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004283 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004284 }
4285
4286exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004287 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004288 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004289 * This allows us to differentiate between exhausted operations and
4290 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4291 * operations. */
4292 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004293 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004294 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004295 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004296 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004297 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004298}
4299
David Brown7807bf72021-02-09 16:28:23 -07004300#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004301static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004302{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004303 if (data_size >= 8) {
4304 mbedtls_des_key_set_parity(data);
4305 }
4306 if (data_size >= 16) {
4307 mbedtls_des_key_set_parity(data + 8);
4308 }
4309 if (data_size >= 24) {
4310 mbedtls_des_key_set_parity(data + 16);
4311 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004312}
David Brown7807bf72021-02-09 16:28:23 -07004313#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004314
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004315static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004316 psa_key_slot_t *slot,
4317 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004318 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004319{
4320 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004321 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004322 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004323 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004325 if (!key_type_is_raw_bytes(slot->attr.type)) {
4326 return PSA_ERROR_INVALID_ARGUMENT;
4327 }
4328 if (bits % 8 != 0) {
4329 return PSA_ERROR_INVALID_ARGUMENT;
4330 }
4331 data = mbedtls_calloc(1, bytes);
4332 if (data == NULL) {
4333 return PSA_ERROR_INSUFFICIENT_MEMORY;
4334 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004336 status = psa_key_derivation_output_bytes(operation, data, bytes);
4337 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004338 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004339 }
David Brown12ca5032021-02-11 11:02:00 -07004340#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004341 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4342 psa_des_set_key_parity(data, bytes);
4343 }
David Brown8107e312021-02-12 08:08:46 -07004344#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004346 status = psa_allocate_buffer_to_slot(slot, bytes);
4347 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004348 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004349 }
Ronald Crondd04d422020-11-28 15:14:42 +01004350
Ronald Cronbf33c932020-11-28 18:06:53 +01004351 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004352 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004353 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004354 };
4355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004356 status = psa_driver_wrapper_import_key(&attributes,
4357 data, bytes,
4358 slot->key.data,
4359 slot->key.bytes,
4360 &slot->key.bytes, &bits);
4361 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004362 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004363 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004364
4365exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004366 mbedtls_free(data);
4367 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004368}
4369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004370psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4371 psa_key_derivation_operation_t *operation,
4372 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004373{
4374 psa_status_t status;
4375 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004376 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004377
Ronald Cron81709fc2020-11-14 12:10:32 +01004378 *key = MBEDTLS_SVC_KEY_ID_INIT;
4379
Gilles Peskine0f84d622019-09-12 19:03:13 +02004380 /* Reject any attempt to create a zero-length key so that we don't
4381 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004382 if (psa_get_key_bits(attributes) == 0) {
4383 return PSA_ERROR_INVALID_ARGUMENT;
4384 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004386 if (operation->alg == PSA_ALG_NONE) {
4387 return PSA_ERROR_BAD_STATE;
4388 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004390 if (!operation->can_output_key) {
4391 return PSA_ERROR_NOT_PERMITTED;
4392 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004394 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4395 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004396#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004397 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004398 /* Deriving a key in a secure element is not implemented yet. */
4399 status = PSA_ERROR_NOT_SUPPORTED;
4400 }
4401#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004402 if (status == PSA_SUCCESS) {
4403 status = psa_generate_derived_key_internal(slot,
4404 attributes->core.bits,
4405 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004406 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004407 if (status == PSA_SUCCESS) {
4408 status = psa_finish_key_creation(slot, driver, key);
4409 }
4410 if (status != PSA_SUCCESS) {
4411 psa_fail_key_creation(slot, driver);
4412 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004414 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004415}
4416
Gilles Peskineeab56e42018-07-12 17:12:33 +02004417
4418
4419/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004420/* Key derivation */
4421/****************************************************************/
4422
Steven Cooreman932ffb72021-02-15 12:14:32 +01004423#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004424static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004425{
4426#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004427 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4428 return 1;
4429 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004430#endif
4431#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004432 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4433 return 1;
4434 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004435#endif
4436#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004437 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4438 return 1;
4439 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004440#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004441 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004442}
4443
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004444static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004445{
4446 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004447 psa_status_t status = psa_hash_setup(&operation, alg);
4448 psa_hash_abort(&operation);
4449 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004450}
4451
Gilles Peskine969c5d62019-01-16 15:53:06 +01004452static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004453 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004454 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004455{
Janos Follath5fe19732019-06-20 15:09:30 +01004456 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4457 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004458 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004459
Gilles Peskine969c5d62019-01-16 15:53:06 +01004460 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004461 if (!is_kdf_alg_supported(kdf_alg)) {
4462 return PSA_ERROR_NOT_SUPPORTED;
4463 }
John Durkop07cc04a2020-11-16 22:08:34 -08004464
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004465 /* All currently supported key derivation algorithms are based on a
4466 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004467 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4468 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4469 if (hash_size == 0) {
4470 return PSA_ERROR_NOT_SUPPORTED;
4471 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004472
4473 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4474 * we might fail later, which is somewhat unfriendly and potentially
4475 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004476 psa_status_t status = psa_hash_try_support(hash_alg);
4477 if (status != PSA_SUCCESS) {
4478 return status;
4479 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004481 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4482 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4483 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4484 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004485 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004486
Gilles Peskinecdacf042021-04-29 21:10:00 +02004487 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004488 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004489}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004491static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004492{
4493#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004494 if (alg == PSA_ALG_ECDH) {
4495 return PSA_SUCCESS;
4496 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004497#endif
4498 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004499 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004500}
John Durkop07cc04a2020-11-16 22:08:34 -08004501#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004502
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004503psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4504 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004505{
4506 psa_status_t status;
4507
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004508 if (operation->alg != 0) {
4509 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004510 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004512 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4513 return PSA_ERROR_INVALID_ARGUMENT;
4514 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4515#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4516 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4517 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4518 status = psa_key_agreement_try_support(ka_alg);
4519 if (status != PSA_SUCCESS) {
4520 return status;
4521 }
4522 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4523#else
4524 return PSA_ERROR_NOT_SUPPORTED;
4525#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4526 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4527#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4528 status = psa_key_derivation_setup_kdf(operation, alg);
4529#else
4530 return PSA_ERROR_NOT_SUPPORTED;
4531#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4532 } else {
4533 return PSA_ERROR_INVALID_ARGUMENT;
4534 }
4535
4536 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004537 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004538 }
4539 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004540}
4541
John Durkopd0321952020-10-29 21:37:36 -07004542#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004543static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4544 psa_algorithm_t hash_alg,
4545 psa_key_derivation_step_t step,
4546 const uint8_t *data,
4547 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004548{
4549 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004550 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004551 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004552 if (hkdf->state != HKDF_STATE_INIT) {
4553 return PSA_ERROR_BAD_STATE;
4554 } else {
4555 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4556 hash_alg,
4557 data, data_length);
4558 if (status != PSA_SUCCESS) {
4559 return status;
4560 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004561 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004562 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004563 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004564 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004565 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004566 if (hkdf->state == HKDF_STATE_INIT) {
4567 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4568 hash_alg,
4569 NULL, 0);
4570 if (status != PSA_SUCCESS) {
4571 return status;
4572 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004573 hkdf->state = HKDF_STATE_STARTED;
4574 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004575 if (hkdf->state != HKDF_STATE_STARTED) {
4576 return PSA_ERROR_BAD_STATE;
4577 }
4578 status = psa_mac_update(&hkdf->hmac,
4579 data, data_length);
4580 if (status != PSA_SUCCESS) {
4581 return status;
4582 }
4583 status = psa_mac_sign_finish(&hkdf->hmac,
4584 hkdf->prk,
4585 sizeof(hkdf->prk),
4586 &data_length);
4587 if (status != PSA_SUCCESS) {
4588 return status;
4589 }
4590 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004591 hkdf->block_number = 0;
4592 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004593 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004594 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004595 if (hkdf->state == HKDF_STATE_OUTPUT) {
4596 return PSA_ERROR_BAD_STATE;
4597 }
4598 if (hkdf->info_set) {
4599 return PSA_ERROR_BAD_STATE;
4600 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004601 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004602 if (data_length != 0) {
4603 hkdf->info = mbedtls_calloc(1, data_length);
4604 if (hkdf->info == NULL) {
4605 return PSA_ERROR_INSUFFICIENT_MEMORY;
4606 }
4607 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004608 }
4609 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004610 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004611 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004612 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004613 }
4614}
John Durkop07cc04a2020-11-16 22:08:34 -08004615#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004616
John Durkop07cc04a2020-11-16 22:08:34 -08004617#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4618 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004619static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4620 const uint8_t *data,
4621 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004622{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4624 return PSA_ERROR_BAD_STATE;
4625 }
Janos Follathf08e2652019-06-13 09:05:41 +01004626
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004627 if (data_length != 0) {
4628 prf->seed = mbedtls_calloc(1, data_length);
4629 if (prf->seed == NULL) {
4630 return PSA_ERROR_INSUFFICIENT_MEMORY;
4631 }
Janos Follathf08e2652019-06-13 09:05:41 +01004632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004633 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004634 prf->seed_length = data_length;
4635 }
Janos Follathf08e2652019-06-13 09:05:41 +01004636
Gilles Peskine43f958b2020-12-13 14:55:14 +01004637 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004639 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004640}
4641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004642static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4643 const uint8_t *data,
4644 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004645{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004646 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4647 return PSA_ERROR_BAD_STATE;
4648 }
Janos Follath81550542019-06-13 14:26:34 +01004649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004650 if (data_length != 0) {
4651 prf->secret = mbedtls_calloc(1, data_length);
4652 if (prf->secret == NULL) {
4653 return PSA_ERROR_INSUFFICIENT_MEMORY;
4654 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004656 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004657 prf->secret_length = data_length;
4658 }
Janos Follath81550542019-06-13 14:26:34 +01004659
Gilles Peskine43f958b2020-12-13 14:55:14 +01004660 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004661
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004662 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004663}
4664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004665static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4666 const uint8_t *data,
4667 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004668{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004669 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4670 return PSA_ERROR_BAD_STATE;
4671 }
Janos Follath63028dd2019-06-13 09:15:47 +01004672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004673 if (data_length != 0) {
4674 prf->label = mbedtls_calloc(1, data_length);
4675 if (prf->label == NULL) {
4676 return PSA_ERROR_INSUFFICIENT_MEMORY;
4677 }
Janos Follath63028dd2019-06-13 09:15:47 +01004678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004679 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004680 prf->label_length = data_length;
4681 }
Janos Follath63028dd2019-06-13 09:15:47 +01004682
Gilles Peskine43f958b2020-12-13 14:55:14 +01004683 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004685 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01004686}
4687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004688static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
4689 psa_key_derivation_step_t step,
4690 const uint8_t *data,
4691 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01004692{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004693 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01004694 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004695 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01004696 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004697 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01004698 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004699 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01004700 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004701 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01004702 }
4703}
John Durkop07cc04a2020-11-16 22:08:34 -08004704#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4705 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4706
4707#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4708static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4709 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004710 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004711 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08004712{
4713 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004714 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08004715 uint8_t *cur = pms;
4716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004717 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
4718 return PSA_ERROR_INVALID_ARGUMENT;
4719 }
John Durkop07cc04a2020-11-16 22:08:34 -08004720
4721 /* Quoting RFC 4279, Section 2:
4722 *
4723 * The premaster secret is formed as follows: if the PSK is N octets
4724 * long, concatenate a uint16 with the value N, N zero octets, a second
4725 * uint16 with the value N, and the PSK itself.
4726 */
4727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004728 *cur++ = MBEDTLS_BYTE_1(data_length);
4729 *cur++ = MBEDTLS_BYTE_0(data_length);
4730 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004731 cur += data_length;
4732 *cur++ = pms[0];
4733 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004734 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004735 cur += data_length;
4736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004737 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08004738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004739 mbedtls_platform_zeroize(pms, sizeof(pms));
4740 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08004741}
Janos Follath6660f0e2019-06-17 08:44:03 +01004742
4743static psa_status_t psa_tls12_prf_psk_to_ms_input(
4744 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004745 psa_key_derivation_step_t step,
4746 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004747 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01004748{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004749 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
4750 return psa_tls12_prf_psk_to_ms_set_key(prf,
4751 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01004752 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004754 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01004755}
John Durkop07cc04a2020-11-16 22:08:34 -08004756#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004757
Gilles Peskineb8965192019-09-24 16:21:10 +02004758/** Check whether the given key type is acceptable for the given
4759 * input step of a key derivation.
4760 *
4761 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4762 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4763 * Both secret and non-secret inputs can alternatively have the type
4764 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4765 * that the input was passed as a buffer rather than via a key object.
4766 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004767static int psa_key_derivation_check_input_type(
4768 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004769 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02004770{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004771 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004772 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004773 if (key_type == PSA_KEY_TYPE_DERIVE) {
4774 return PSA_SUCCESS;
4775 }
4776 if (key_type == PSA_KEY_TYPE_NONE) {
4777 return PSA_SUCCESS;
4778 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004779 break;
4780 case PSA_KEY_DERIVATION_INPUT_LABEL:
4781 case PSA_KEY_DERIVATION_INPUT_SALT:
4782 case PSA_KEY_DERIVATION_INPUT_INFO:
4783 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004784 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
4785 return PSA_SUCCESS;
4786 }
4787 if (key_type == PSA_KEY_TYPE_NONE) {
4788 return PSA_SUCCESS;
4789 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004790 break;
4791 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004792 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02004793}
4794
Janos Follathb80a94e2019-06-12 15:54:46 +01004795static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004796 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004797 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004798 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004799 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004800 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004801{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004802 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004803 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004805 status = psa_key_derivation_check_input_type(step, key_type);
4806 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004807 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004808 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004809
John Durkopd0321952020-10-29 21:37:36 -07004810#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004811 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4812 status = psa_hkdf_input(&operation->ctx.hkdf,
4813 PSA_ALG_HKDF_GET_HASH(kdf_alg),
4814 step, data, data_length);
4815 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004816#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4817#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004818 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4819 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
4820 step, data, data_length);
4821 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004822#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4823#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004824 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4825 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
4826 step, data, data_length);
4827 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004828#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004829 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004831 (void) data;
4832 (void) data_length;
4833 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004834 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004835 }
4836
Gilles Peskine224b0d62019-09-23 18:13:17 +02004837exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004838 if (status != PSA_SUCCESS) {
4839 psa_key_derivation_abort(operation);
4840 }
4841 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004842}
4843
Janos Follath51f4a0f2019-06-14 11:35:55 +01004844psa_status_t psa_key_derivation_input_bytes(
4845 psa_key_derivation_operation_t *operation,
4846 psa_key_derivation_step_t step,
4847 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004848 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004849{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004850 return psa_key_derivation_input_internal(operation, step,
4851 PSA_KEY_TYPE_NONE,
4852 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004853}
4854
Janos Follath51f4a0f2019-06-14 11:35:55 +01004855psa_status_t psa_key_derivation_input_key(
4856 psa_key_derivation_operation_t *operation,
4857 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004858 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004859{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004860 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004861 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004862 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004863
Ronald Cron5c522922020-11-14 16:35:34 +01004864 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004865 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4866 if (status != PSA_SUCCESS) {
4867 psa_key_derivation_abort(operation);
4868 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02004869 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004870
4871 /* Passing a key object as a SECRET input unlocks the permission
4872 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004873 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004874 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004875 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004877 status = psa_key_derivation_input_internal(operation,
4878 step, slot->attr.type,
4879 slot->key.data,
4880 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004882 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004884 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004885}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004886
4887
4888
4889/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004890/* Key agreement */
4891/****************************************************************/
4892
John Durkop6ba40d12020-11-10 08:50:04 -08004893#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004894static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
4895 size_t peer_key_length,
4896 const mbedtls_ecp_keypair *our_key,
4897 uint8_t *shared_secret,
4898 size_t shared_secret_size,
4899 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004900{
Steven Cooremand4867872020-08-05 16:31:39 +02004901 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004902 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004903 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004904 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004905 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
4906 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004907
Ronald Cron90857082020-11-25 14:58:33 +01004908 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004909 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
4910 bits,
4911 peer_key,
4912 peer_key_length,
4913 &their_key);
4914 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004915 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004916 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004917
Jaeden Amero97271b32019-01-10 19:38:51 +00004918 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004919 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
4920 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004921 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004922 }
Jaeden Amero97271b32019-01-10 19:38:51 +00004923 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004924 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
4925 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004926 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004927 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004928
Jaeden Amero97271b32019-01-10 19:38:51 +00004929 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004930 mbedtls_ecdh_calc_secret(&ecdh,
4931 shared_secret_length,
4932 shared_secret, shared_secret_size,
4933 mbedtls_psa_get_random,
4934 MBEDTLS_PSA_RANDOM_STATE));
4935 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004936 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004937 }
4938 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004939 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004940 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004941
4942exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004943 if (status != PSA_SUCCESS) {
4944 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
4945 }
4946 mbedtls_ecdh_free(&ecdh);
4947 mbedtls_ecp_keypair_free(their_key);
4948 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02004949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004950 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004951}
John Durkop6ba40d12020-11-10 08:50:04 -08004952#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004953
Gilles Peskine01d718c2018-09-18 12:01:02 +02004954#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004956static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
4957 psa_key_slot_t *private_key,
4958 const uint8_t *peer_key,
4959 size_t peer_key_length,
4960 uint8_t *shared_secret,
4961 size_t shared_secret_size,
4962 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004963{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004964 mbedtls_ecp_keypair *ecp = NULL;
4965 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01004966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004967 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08004968#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004969 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004970 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
4971 return PSA_ERROR_INVALID_ARGUMENT;
4972 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004973 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004974 private_key->attr.type,
4975 private_key->attr.bits,
4976 private_key->key.data,
4977 private_key->key.bytes,
4978 &ecp);
4979 if (status != PSA_SUCCESS) {
4980 return status;
4981 }
4982 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
4983 ecp,
4984 shared_secret, shared_secret_size,
4985 shared_secret_length);
4986 mbedtls_ecp_keypair_free(ecp);
4987 mbedtls_free(ecp);
4988 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08004989#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004990 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004991 (void) ecp;
4992 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01004993 (void) private_key;
4994 (void) peer_key;
4995 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004996 (void) shared_secret;
4997 (void) shared_secret_size;
4998 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004999 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005000 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005001}
5002
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005003/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005004 * to potentially free embedded data structures and wipe confidential data.
5005 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005006static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5007 psa_key_derivation_step_t step,
5008 psa_key_slot_t *private_key,
5009 const uint8_t *peer_key,
5010 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005011{
5012 psa_status_t status;
5013 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5014 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005015 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005016
5017 /* Step 1: run the secret agreement algorithm to generate the shared
5018 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005019 status = psa_key_agreement_raw_internal(ka_alg,
5020 private_key,
5021 peer_key, peer_key_length,
5022 shared_secret,
5023 sizeof(shared_secret),
5024 &shared_secret_length);
5025 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005026 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005027 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005028
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005029 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005030 * the shared secret. A shared secret is permitted wherever a key
5031 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005032 status = psa_key_derivation_input_internal(operation, step,
5033 PSA_KEY_TYPE_DERIVE,
5034 shared_secret,
5035 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005036exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005037 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5038 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005039}
5040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005041psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5042 psa_key_derivation_step_t step,
5043 mbedtls_svc_key_id_t private_key,
5044 const uint8_t *peer_key,
5045 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005046{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005047 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005048 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005049 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005051 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5052 return PSA_ERROR_INVALID_ARGUMENT;
5053 }
Ronald Cron5c522922020-11-14 16:35:34 +01005054 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005055 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5056 if (status != PSA_SUCCESS) {
5057 return status;
5058 }
5059 status = psa_key_agreement_internal(operation, step,
5060 slot,
5061 peer_key, peer_key_length);
5062 if (status != PSA_SUCCESS) {
5063 psa_key_derivation_abort(operation);
5064 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005065 /* If a private key has been added as SECRET, we allow the derived
5066 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005067 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005068 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005069 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005070 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005072 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005074 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005075}
5076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005077psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5078 mbedtls_svc_key_id_t private_key,
5079 const uint8_t *peer_key,
5080 size_t peer_key_length,
5081 uint8_t *output,
5082 size_t output_size,
5083 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005084{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005085 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005086 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005087 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005088 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005090 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005091 status = PSA_ERROR_INVALID_ARGUMENT;
5092 goto exit;
5093 }
Ronald Cron5c522922020-11-14 16:35:34 +01005094 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005095 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5096 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005097 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005098 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005099
Gilles Peskine42313fb2022-04-14 00:17:15 +02005100 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5101 * for the output size. The PSA specification only guarantees that this
5102 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5103 * but it might be nice to allow smaller buffers if the output fits.
5104 * At the time of writing this comment, with only ECDH implemented,
5105 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5106 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5107 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005108 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005109 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5110 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005111 status = PSA_ERROR_BUFFER_TOO_SMALL;
5112 goto exit;
5113 }
5114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005115 status = psa_key_agreement_raw_internal(alg, slot,
5116 peer_key, peer_key_length,
5117 output, output_size,
5118 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005119
5120exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005121 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005122 /* If an error happens and is not handled properly, the output
5123 * may be used as a key to protect sensitive data. Arrange for such
5124 * a key to be random, which is likely to result in decryption or
5125 * verification errors. This is better than filling the buffer with
5126 * some constant data such as zeros, which would result in the data
5127 * being protected with a reproducible, easily knowable key.
5128 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005129 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005130 *output_length = output_size;
5131 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005133 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005135 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005136}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005137
5138
Gilles Peskine30524eb2020-11-13 17:02:26 +01005139
Gilles Peskine86a440b2018-11-12 18:39:40 +01005140/****************************************************************/
5141/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005142/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005143
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005144#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5145#include "mbedtls/entropy_poll.h"
5146#endif
5147
Gilles Peskine30524eb2020-11-13 17:02:26 +01005148/** Initialize the PSA random generator.
5149 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005150static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005151{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005152#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005153 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005154#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5155
Gilles Peskine30524eb2020-11-13 17:02:26 +01005156 /* Set default configuration if
5157 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005158 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005159 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005160 }
5161 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005162 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005163 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005165 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005166#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5167 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5168 /* The PSA entropy injection feature depends on using NV seed as an entropy
5169 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005170 mbedtls_entropy_add_source(&rng->entropy,
5171 mbedtls_nv_seed_poll, NULL,
5172 MBEDTLS_ENTROPY_BLOCK_SIZE,
5173 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005174#endif
5175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005176 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005177#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005178}
5179
5180/** Deinitialize the PSA random generator.
5181 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005182static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005183{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005184#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005185 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005186#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005187 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5188 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005189#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005190}
5191
5192/** Seed the PSA random generator.
5193 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005194static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005195{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005196#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5197 /* Do nothing: the external RNG seeds itself. */
5198 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005199 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005200#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005201 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005202 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5203 drbg_seed, sizeof(drbg_seed) - 1);
5204 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005205#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005206}
5207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005208psa_status_t psa_generate_random(uint8_t *output,
5209 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005210{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005211 GUARD_MODULE_INITIALIZED;
5212
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005213#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5214
5215 size_t output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005216 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
5217 output, output_size,
5218 &output_length);
5219 if (status != PSA_SUCCESS) {
5220 return status;
5221 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005222 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00005223 * for the external RNG interface. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005224 if (output_length != output_size) {
5225 return PSA_ERROR_INSUFFICIENT_ENTROPY;
5226 }
5227 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005228
5229#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005231 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005232 size_t request_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005233 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5234 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5235 output_size);
5236 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
5237 output, request_size);
5238 if (ret != 0) {
5239 return mbedtls_to_psa_error(ret);
5240 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01005241 output_size -= request_size;
5242 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005243 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005244 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005245#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005246}
5247
Gilles Peskine8814fc42020-12-14 15:33:44 +01005248/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005249 *
5250 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5251 * `psa_generate_random(...)`. The state parameter is ignored since the
5252 * PSA API doesn't support passing an explicit state.
5253 *
5254 * In the non-external case, psa_generate_random() calls an
5255 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5256 * and semantics as mbedtls_psa_get_random(). As an optimization,
5257 * instead of doing this back-and-forth between the PSA API and the
5258 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5259 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005260 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005261#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5262int mbedtls_psa_get_random(void *p_rng,
5263 unsigned char *output,
5264 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005265{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005266 /* This function takes a pointer to the RNG state because that's what
5267 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5268 * its own state internally and doesn't let the caller access that state.
5269 * So we just ignore the state parameter, and in practice we'll pass
5270 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005271 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005272 psa_status_t status = psa_generate_random(output, output_size);
5273 if (status == PSA_SUCCESS) {
5274 return 0;
5275 } else {
5276 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5277 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005278}
5279#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5280
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005281#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005282psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5283 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005284{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005285 if (global_data.initialized) {
5286 return PSA_ERROR_NOT_PERMITTED;
5287 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005289 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5290 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5291 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5292 return PSA_ERROR_INVALID_ARGUMENT;
5293 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005295 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005296}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005297#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005298
Ronald Cron3772afe2021-02-08 16:10:05 +01005299/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005300 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005301 * \param type The key type
5302 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005303 *
5304 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005305 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005306 * \retval #PSA_ERROR_INVALID_ARGUMENT
5307 * The size in bits of the key is not valid.
5308 * \retval #PSA_ERROR_NOT_SUPPORTED
5309 * The type and/or the size in bits of the key or the combination of
5310 * the two is not supported.
5311 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005312static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005313 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005314{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005315 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005317 if (key_type_is_raw_bytes(type)) {
5318 status = validate_unstructured_key_bit_size(type, bits);
5319 if (status != PSA_SUCCESS) {
5320 return status;
5321 }
5322 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005323#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005324 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5325 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5326 return PSA_ERROR_NOT_SUPPORTED;
5327 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005328
Ronald Cron01b2aba2020-10-05 09:42:02 +02005329 /* Accept only byte-aligned keys, for the same reasons as
5330 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005331 if (bits % 8 != 0) {
5332 return PSA_ERROR_NOT_SUPPORTED;
5333 }
5334 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005335#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005336
Ronald Cron5c4d3862020-12-07 11:07:24 +01005337#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005338 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005339 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005340 return PSA_SUCCESS;
5341 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005342#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005343 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005344 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005345 }
5346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005347 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005348}
5349
Ronald Cron55ed0592020-10-05 10:30:40 +02005350psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005351 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005352 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005353{
5354 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005355 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005357 if ((attributes->domain_parameters == NULL) &&
5358 (attributes->domain_parameters_size != 0)) {
5359 return PSA_ERROR_INVALID_ARGUMENT;
5360 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005362 if (key_type_is_raw_bytes(type)) {
5363 status = psa_generate_random(key_buffer, key_buffer_size);
5364 if (status != PSA_SUCCESS) {
5365 return status;
5366 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005367
David Brown12ca5032021-02-11 11:02:00 -07005368#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005369 if (type == PSA_KEY_TYPE_DES) {
5370 psa_des_set_key_parity(key_buffer, key_buffer_size);
5371 }
David Brown8107e312021-02-12 08:08:46 -07005372#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005373 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005374
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005375#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5376 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005377 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5378 return mbedtls_psa_rsa_generate_key(attributes,
5379 key_buffer,
5380 key_buffer_size,
5381 key_buffer_length);
5382 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005383#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5384 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005385
John Durkop9814fa22020-11-04 12:28:15 -08005386#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005387 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5388 return mbedtls_psa_ecp_generate_key(attributes,
5389 key_buffer,
5390 key_buffer_size,
5391 key_buffer_length);
5392 } else
John Durkop9814fa22020-11-04 12:28:15 -08005393#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005394 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005395 (void) key_buffer_length;
5396 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005397 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005399 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005400}
Darryl Green0c6575a2018-11-07 16:05:30 +00005401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005402psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5403 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005404{
5405 psa_status_t status;
5406 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005407 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005408 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005409
Ronald Cron81709fc2020-11-14 12:10:32 +01005410 *key = MBEDTLS_SVC_KEY_ID_INIT;
5411
Gilles Peskine0f84d622019-09-12 19:03:13 +02005412 /* Reject any attempt to create a zero-length key so that we don't
5413 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005414 if (psa_get_key_bits(attributes) == 0) {
5415 return PSA_ERROR_INVALID_ARGUMENT;
5416 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005417
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005418 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005419 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5420 return PSA_ERROR_INVALID_ARGUMENT;
5421 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005423 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5424 &slot, &driver);
5425 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005426 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005427 }
Gilles Peskine11792082019-08-06 18:36:36 +02005428
Ronald Cron977c2472020-10-13 08:32:21 +02005429 /* In the case of a transparent key or an opaque key stored in local
5430 * storage (thus not in the case of generating a key in a secure element
5431 * or cryptoprocessor with storage), we have to allocate a buffer to
5432 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005433 if (slot->key.data == NULL) {
5434 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5435 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005436 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005437 attributes->core.type, attributes->core.bits);
5438 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005439 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005440 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005441
5442 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005443 attributes->core.type,
5444 attributes->core.bits);
5445 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005446 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005447 attributes, &key_buffer_size);
5448 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005449 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005450 }
Ronald Cron977c2472020-10-13 08:32:21 +02005451 }
Ronald Cron977c2472020-10-13 08:32:21 +02005452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005453 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5454 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005455 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005456 }
Ronald Cron977c2472020-10-13 08:32:21 +02005457 }
5458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005459 status = psa_driver_wrapper_generate_key(attributes,
5460 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005462 if (status != PSA_SUCCESS) {
5463 psa_remove_key_data_from_memory(slot);
5464 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005465
Gilles Peskine11792082019-08-06 18:36:36 +02005466exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005467 if (status == PSA_SUCCESS) {
5468 status = psa_finish_key_creation(slot, driver, key);
5469 }
5470 if (status != PSA_SUCCESS) {
5471 psa_fail_key_creation(slot, driver);
5472 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005474 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005475}
5476
Gilles Peskinee59236f2018-01-27 23:32:46 +01005477/****************************************************************/
5478/* Module setup */
5479/****************************************************************/
5480
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005481#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005482psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005483 void (* entropy_init)(mbedtls_entropy_context *ctx),
5484 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005485{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005486 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5487 return PSA_ERROR_BAD_STATE;
5488 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005489 global_data.rng.entropy_init = entropy_init;
5490 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005491 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005492}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005493#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005495void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005496{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005497 psa_wipe_all_key_slots();
5498 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5499 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005500 }
5501 /* Wipe all remaining data, including configuration.
5502 * In particular, this sets all state indicator to the value
5503 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005504 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005505
5506 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005507 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005508}
5509
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005510#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5511/** Recover a transaction that was interrupted by a power failure.
5512 *
5513 * This function is called during initialization, before psa_crypto_init()
5514 * returns. If this function returns a failure status, the initialization
5515 * fails.
5516 */
5517static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005518 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005519{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005520 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005521 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5522 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005523 /* TODO - fall through to the failure case until this
5524 * is implemented.
5525 * https://github.com/ARMmbed/mbed-crypto/issues/218
5526 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005527 default:
5528 /* We found an unsupported transaction in the storage.
5529 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005530 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005531 }
5532}
5533#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005535psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005536{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005537 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005538
Gilles Peskinec6b69072018-11-20 21:42:52 +01005539 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005540 if (global_data.initialized != 0) {
5541 return PSA_SUCCESS;
5542 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005543
Gilles Peskine30524eb2020-11-13 17:02:26 +01005544 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005545 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005546 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005547 status = mbedtls_psa_random_seed(&global_data.rng);
5548 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005549 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005550 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005551 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005553 status = psa_initialize_key_slots();
5554 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005555 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005556 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005557
Ronald Cron088d5d02021-04-10 16:57:30 +02005558 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005559 status = psa_driver_wrapper_init();
5560 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005561 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005562 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005563
Gilles Peskine4b734222019-07-24 15:56:31 +02005564#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005565 status = psa_crypto_load_transaction();
5566 if (status == PSA_SUCCESS) {
5567 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5568 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005569 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005570 }
5571 status = psa_crypto_stop_transaction();
5572 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005573 /* There's no transaction to complete. It's all good. */
5574 status = PSA_SUCCESS;
5575 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005576#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005577
Gilles Peskinec6b69072018-11-20 21:42:52 +01005578 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005579 global_data.initialized = 1;
5580
5581exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005582 if (status != PSA_SUCCESS) {
5583 mbedtls_psa_crypto_free();
5584 }
5585 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005586}
5587
David Horstmann114d8242023-12-07 18:39:17 +00005588/* Memory copying test hooks. These are called before input copy, after input
5589 * copy, before output copy and after output copy, respectively.
5590 * They are used by memory-poisoning tests to temporarily unpoison buffers
5591 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00005592#if defined(MBEDTLS_TEST_HOOKS)
5593void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5594void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5595void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5596void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5597#endif
5598
David Horstmanndf493552023-11-15 15:18:30 +00005599/** Copy from an input buffer to a local copy.
5600 *
5601 * \param[in] input Pointer to input buffer.
5602 * \param[in] input_len Length of the input buffer.
5603 * \param[out] input_copy Pointer to a local copy in which to store the input data.
5604 * \param[out] input_copy_len Length of the local copy buffer.
5605 * \return #PSA_SUCCESS, if the buffer was successfully
5606 * copied.
5607 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
5608 * copy is too small to hold contents of the
5609 * input buffer.
5610 */
5611MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00005612psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
5613 uint8_t *input_copy, size_t input_copy_len)
5614{
5615 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00005616 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00005617 }
5618
David Horstmann0760b152023-11-24 16:21:04 +00005619#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005620 if (psa_input_pre_copy_hook != NULL) {
5621 psa_input_pre_copy_hook(input, input_len);
5622 }
David Horstmann0760b152023-11-24 16:21:04 +00005623#endif
5624
David Horstmann660027f2023-11-15 17:33:47 +00005625 if (input_len > 0) {
5626 memcpy(input_copy, input, input_len);
5627 }
David Horstmann957f9802023-10-30 20:29:43 +00005628
David Horstmann0760b152023-11-24 16:21:04 +00005629#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005630 if (psa_input_post_copy_hook != NULL) {
5631 psa_input_post_copy_hook(input, input_len);
5632 }
David Horstmann0760b152023-11-24 16:21:04 +00005633#endif
5634
David Horstmann957f9802023-10-30 20:29:43 +00005635 return PSA_SUCCESS;
5636}
5637
David Horstmanndf493552023-11-15 15:18:30 +00005638/** Copy from a local output buffer into a user-supplied one.
5639 *
5640 * \param[in] output_copy Pointer to a local buffer containing the output.
5641 * \param[in] output_copy_len Length of the local buffer.
5642 * \param[out] output Pointer to user-supplied output buffer.
5643 * \param[out] output_len Length of the user-supplied output buffer.
5644 * \return #PSA_SUCCESS, if the buffer was successfully
5645 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00005646 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00005647 * user-supplied output buffer is too small to
5648 * hold the contents of the local buffer.
5649 */
5650MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00005651psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
5652 uint8_t *output, size_t output_len)
5653{
5654 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00005655 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00005656 }
David Horstmann660027f2023-11-15 17:33:47 +00005657
David Horstmann0760b152023-11-24 16:21:04 +00005658#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005659 if (psa_output_pre_copy_hook != NULL) {
5660 psa_output_pre_copy_hook(output, output_len);
5661 }
David Horstmann0760b152023-11-24 16:21:04 +00005662#endif
5663
David Horstmann660027f2023-11-15 17:33:47 +00005664 if (output_copy_len > 0) {
5665 memcpy(output, output_copy, output_copy_len);
5666 }
5667
David Horstmann0760b152023-11-24 16:21:04 +00005668#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005669 if (psa_output_post_copy_hook != NULL) {
5670 psa_output_post_copy_hook(output, output_len);
5671 }
David Horstmann0760b152023-11-24 16:21:04 +00005672#endif
5673
David Horstmann2bd296e2023-10-30 20:37:12 +00005674 return PSA_SUCCESS;
5675}
5676
David Horstmann81a0d572023-11-20 17:15:32 +00005677psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
5678 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005679{
5680 psa_status_t status;
5681
David Horstmann0d52c712023-11-23 15:50:37 +00005682 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005683
David Horstmann726bf052023-11-15 18:11:26 +00005684 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005685 return PSA_SUCCESS;
5686 }
5687
David Horstmann81a0d572023-11-20 17:15:32 +00005688 local_input->buffer = mbedtls_calloc(input_len, 1);
5689 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005690 /* Since we dealt with the zero-length case above, we know that
5691 * a NULL return value means a failure of allocation. */
5692 return PSA_ERROR_INSUFFICIENT_MEMORY;
5693 }
David Horstmann81a0d572023-11-20 17:15:32 +00005694 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005695
David Horstmann81a0d572023-11-20 17:15:32 +00005696 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005697
5698 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00005699 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005700 if (status != PSA_SUCCESS) {
5701 goto error;
5702 }
5703
5704 return PSA_SUCCESS;
5705
5706error:
David Horstmann81a0d572023-11-20 17:15:32 +00005707 mbedtls_free(local_input->buffer);
5708 local_input->buffer = NULL;
5709 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005710 return status;
5711}
5712
David Horstmann81a0d572023-11-20 17:15:32 +00005713void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00005714{
David Horstmann81a0d572023-11-20 17:15:32 +00005715 mbedtls_free(local_input->buffer);
5716 local_input->buffer = NULL;
5717 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00005718}
5719
David Horstmann1a76ab12023-11-20 12:54:09 +00005720psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
5721 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00005722{
David Horstmann0d52c712023-11-23 15:50:37 +00005723 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00005724
David Horstmann726bf052023-11-15 18:11:26 +00005725 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005726 return PSA_SUCCESS;
5727 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005728 local_output->buffer = mbedtls_calloc(output_len, 1);
5729 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005730 /* Since we dealt with the zero-length case above, we know that
5731 * a NULL return value means a failure of allocation. */
5732 return PSA_ERROR_INSUFFICIENT_MEMORY;
5733 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005734 local_output->length = output_len;
5735 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00005736
5737 return PSA_SUCCESS;
5738}
5739
David Horstmann1a76ab12023-11-20 12:54:09 +00005740psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00005741{
David Horstmann761761f2023-11-15 15:57:32 +00005742 psa_status_t status;
5743
David Horstmann1a76ab12023-11-20 12:54:09 +00005744 if (local_output->buffer == NULL) {
5745 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005746 return PSA_SUCCESS;
5747 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005748 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00005749 /* We have an internal copy but nothing to copy back to. */
5750 return PSA_ERROR_CORRUPTION_DETECTED;
5751 }
5752
David Horstmann1a76ab12023-11-20 12:54:09 +00005753 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
5754 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00005755 if (status != PSA_SUCCESS) {
5756 return status;
5757 }
David Horstmannaeeb2742023-11-08 17:44:18 +00005758
David Horstmann1a76ab12023-11-20 12:54:09 +00005759 mbedtls_free(local_output->buffer);
5760 local_output->buffer = NULL;
5761 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005762
5763 return PSA_SUCCESS;
5764}
5765
Gilles Peskinee59236f2018-01-27 23:32:46 +01005766#endif /* MBEDTLS_PSA_CRYPTO_C */