blob: 0010c66ab3e0a4a71679305e4202f21a21646aa3 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinee59236f2018-01-27 23:32:46 +01007 */
8
Gilles Peskinedb09ef62020-06-03 01:43:33 +02009#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010010
11#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030012
John Durkop07cc04a2020-11-16 22:08:34 -080013#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
14#include "check_crypto_config.h"
15#endif
16
Gilles Peskinee59236f2018-01-27 23:32:46 +010017#include "psa/crypto.h"
18
Ronald Crond6d28882020-12-14 14:56:02 +010019#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010020#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010021#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020022#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010023#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010024#include "psa_crypto_hash.h"
Steven Cooreman32d56942021-03-19 17:39:17 +010025#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010027#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020028#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020029#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020030#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010031#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010032/* Include internal declarations that are useful for implementing persistently
33 * stored keys. */
34#include "psa_crypto_storage.h"
35
Gilles Peskineb2b64d32020-12-14 16:43:58 +010036#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010037
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010038#include <stdlib.h>
39#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010041
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010042#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010043#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020044#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000045#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020046#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010047#include "mbedtls/blowfish.h"
48#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020049#include "mbedtls/chacha20.h"
50#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/cipher.h"
52#include "mbedtls/ccm.h"
53#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010054#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020055#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010056#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010057#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/error.h"
59#include "mbedtls/gcm.h"
60#include "mbedtls/md2.h"
61#include "mbedtls/md4.h"
62#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010063#include "mbedtls/md.h"
64#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010065#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010067#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000068#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/sha1.h"
72#include "mbedtls/sha256.h"
73#include "mbedtls/sha512.h"
74#include "mbedtls/xtea.h"
75
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020077
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010078/****************************************************************/
79/* Global data, support functions and library management */
80/****************************************************************/
81
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020083{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020085}
86
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010087/* Values for psa_global_data_t::rng_state */
88#define RNG_NOT_INITIALIZED 0
89#define RNG_INITIALIZED 1
90#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +010093 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010094 unsigned rng_state : 2;
Gilles Peskineb8006a62021-11-08 22:20:03 +010095 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +010096} psa_global_data_t;
97
98static psa_global_data_t global_data;
99
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100100#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
101mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
102 &global_data.rng.drbg;
103#endif
104
itayzafrir0adf0fc2018-09-06 16:24:41 +0300105#define GUARD_MODULE_INITIALIZED \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 if (global_data.initialized == 0) \
107 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300108
David Horstmannc0a2c302023-11-29 17:24:08 +0000109#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
David Horstmann926193a2023-12-13 15:55:25 +0000110
111/* Declare a local copy of an input buffer.
112 *
113 * Note: This macro must be called before any operations which may jump to
114 * the exit label, so that the local input copy object is safe to be freed.
115 *
116 * Assumptions:
117 * - input is the name of a pointer to the buffer to be copied
118 * - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope
119 */
120#define LOCAL_INPUT_DECLARE(input) \
121 psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT;
122
123/* Allocate a copy of the buffer input and create a pointer with the name
124 * input_copy_name that points to the start of the copy.
125 *
David Horstmann34980bd2023-11-29 17:07:13 +0000126 * Assumptions:
127 * - psa_status_t status exists
128 * - An exit label is declared
David Horstmann926193a2023-12-13 15:55:25 +0000129 * - input is the name of a pointer to the buffer to be copied
130 * - LOCAL_INPUT_DECLARE(input) has previously been called
131 * - input_copy_name is a name that is not used in the current scope
David Horstmann34980bd2023-11-29 17:07:13 +0000132 */
David Horstmann926193a2023-12-13 15:55:25 +0000133#define LOCAL_INPUT_ALLOC(input, length, input_copy_name) \
134 status = psa_crypto_local_input_alloc(input, length, \
135 &LOCAL_INPUT_COPY_OF_##input); \
David Horstmann34980bd2023-11-29 17:07:13 +0000136 if (status != PSA_SUCCESS) { \
137 goto exit; \
138 } \
David Horstmann926193a2023-12-13 15:55:25 +0000139 const uint8_t *input_copy_name = LOCAL_INPUT_COPY_OF_##input.buffer;
David Horstmann34980bd2023-11-29 17:07:13 +0000140
David Horstmann926193a2023-12-13 15:55:25 +0000141/* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC()
142 *
David Horstmann1f532132023-12-08 14:08:18 +0000143 * Assumptions:
David Horstmann926193a2023-12-13 15:55:25 +0000144 * - input_copy_name is the name of the input copy created by LOCAL_INPUT_ALLOC()
145 * - input is the name of the original buffer that was copied
David Horstmann1f532132023-12-08 14:08:18 +0000146 */
David Horstmann926193a2023-12-13 15:55:25 +0000147#define LOCAL_INPUT_FREE(input_copy_name, input) \
148 input_copy_name = NULL; \
149 psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input);
David Horstmann34980bd2023-11-29 17:07:13 +0000150
David Horstmann926193a2023-12-13 15:55:25 +0000151/* Declare a local copy of an output buffer.
152 *
153 * Note: This macro must be called before any operations which may jump to
154 * the exit label, so that the local output copy object is safe to be freed.
155 *
156 * Assumptions:
157 * - output is the name of a pointer to the buffer to be copied
158 * - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope
159 */
160#define LOCAL_OUTPUT_DECLARE(output) \
161 psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
162
163/* Allocate a copy of the buffer output and create a pointer with the name
164 * output_copy_name that points to the start of the copy.
165 *
David Horstmann34980bd2023-11-29 17:07:13 +0000166 * Assumptions:
167 * - psa_status_t status exists
168 * - An exit label is declared
David Horstmann926193a2023-12-13 15:55:25 +0000169 * - output is the name of a pointer to the buffer to be copied
170 * - LOCAL_OUTPUT_DECLARE(output) has previously been called
171 * - output_copy_name is a name that is not used in the current scope
David Horstmann34980bd2023-11-29 17:07:13 +0000172 */
David Horstmann926193a2023-12-13 15:55:25 +0000173#define LOCAL_OUTPUT_ALLOC(output, length, output_copy_name) \
174 status = psa_crypto_local_output_alloc(output, length, \
175 &LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmann34980bd2023-11-29 17:07:13 +0000176 if (status != PSA_SUCCESS) { \
177 goto exit; \
178 } \
David Horstmann926193a2023-12-13 15:55:25 +0000179 uint8_t *output_copy_name = LOCAL_OUTPUT_COPY_OF_##output.buffer;
David Horstmann34980bd2023-11-29 17:07:13 +0000180
David Horstmann926193a2023-12-13 15:55:25 +0000181/* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC()
David Horstmann1f532132023-12-08 14:08:18 +0000182 * after first copying back its contents to the original buffer.
David Horstmann926193a2023-12-13 15:55:25 +0000183 *
David Horstmann1f532132023-12-08 14:08:18 +0000184 * Assumptions:
David Horstmann1f532132023-12-08 14:08:18 +0000185 * - psa_status_t status exists
David Horstmann926193a2023-12-13 15:55:25 +0000186 * - input_copy_name is the name of the input copy created by LOCAL_INPUT_ALLOC()
187 * - input is the name of the original buffer that was copied
David Horstmann1f532132023-12-08 14:08:18 +0000188 */
David Horstmann926193a2023-12-13 15:55:25 +0000189#define LOCAL_OUTPUT_FREE(output_copy_name, output) \
190 output_copy_name = NULL; \
David Horstmann2b70a662023-12-13 14:09:08 +0000191 do { \
192 psa_status_t local_output_status; \
David Horstmann926193a2023-12-13 15:55:25 +0000193 local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmann2b70a662023-12-13 14:09:08 +0000194 if (local_output_status != PSA_SUCCESS) { \
195 /* Since this error case is an internal error, it's more serious than \
196 * any existing error code and so it's fine to overwrite the existing \
197 * status. */ \
198 status = local_output_status; \
199 } \
200 } while (0)
David Horstmannc0a2c302023-11-29 17:24:08 +0000201#else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmann926193a2023-12-13 15:55:25 +0000202#define LOCAL_INPUT_DECLARE(input)
203#define LOCAL_INPUT_ALLOC(input, length, input_copy_name) \
204 const uint8_t *input_copy_name = input;
205#define LOCAL_INPUT_FREE(input_copy_name, input) \
206 input_copy_name = NULL;
207#define LOCAL_OUTPUT_DECLARE(output)
208#define LOCAL_OUTPUT_ALLOC(output, length, output_copy_name) \
209 uint8_t *output_copy_name = output;
210#define LOCAL_OUTPUT_FREE(output_copy_name, output) \
211 output_copy_name = NULL;
212
David Horstmannc0a2c302023-11-29 17:24:08 +0000213#endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmann34980bd2023-11-29 17:07:13 +0000214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100216{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100217 /* Mbed TLS error codes can combine a high-level error code and a
218 * low-level error code. The low-level error usually reflects the
219 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 int low_level_ret = -(-ret & 0x007f);
221 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100222 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100224
225 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
226 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
227 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200232 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200234
Gilles Peskine9a944802018-06-21 09:35:35 +0200235 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
236 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
237 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
238 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
239 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200241 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200243 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200245
Jaeden Amero93e21112019-02-20 13:57:28 +0000246#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000247 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000248#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
249 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
250#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100255
Jaeden Amero93e21112019-02-20 13:57:28 +0000256#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000257 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000258#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
259 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
260#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100261 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100263 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100265
266 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100268 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100270 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100272
Gilles Peskine26869f22019-05-06 15:25:00 +0200273 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200275
276 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200278 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200280
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100283 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100285 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100287 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100289 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100291 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100297
298 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
302 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100303 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
304 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100307 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
308 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100312#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100313
314 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100316 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100318
Gilles Peskinee59236f2018-01-27 23:32:46 +0100319 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
320 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
321 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100323
324 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100326 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100328 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100330
Gilles Peskine82e57d12020-11-13 21:31:17 +0100331#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100333 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
334 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100335 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100337 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
338 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100340 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100342#endif
343
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200344 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
345 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
346 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200348
Gilles Peskinea5905292018-02-07 20:59:33 +0100349 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100351 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100353 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100355 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200357 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100358 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100359
Gilles Peskinef76aa772018-10-29 19:24:33 +0100360 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100362 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100364 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100366 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100368 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100370 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100372 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100374 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100376
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100377 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100379 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
380 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100381 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100382 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100384 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
385 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100387 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100389 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
390 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100392 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100394 case MBEDTLS_ERR_PK_INVALID_ALG:
395 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
396 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100398 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100400 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100402
Gilles Peskineff2d2002019-05-06 15:26:23 +0200403 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200405 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200407
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200408 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200410
Gilles Peskinea5905292018-02-07 20:59:33 +0100411 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100412 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100413 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100415 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100417 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100419 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
420 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100422 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100424 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100426 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100428 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100430 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100431 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100432
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200433 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
434 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
435 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200437
Gilles Peskinea5905292018-02-07 20:59:33 +0100438 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100440 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100442
itayzafrir5c753392018-05-08 11:18:38 +0300443 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300444 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300446 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300448 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300450 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
451 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300453 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100455 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300457 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100458 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100459
Janos Follatha13b9052019-11-22 12:48:59 +0000460 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300462
Gilles Peskinee59236f2018-01-27 23:32:46 +0100463 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100464 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100465 }
466}
467
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200468
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200469
470
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100471/****************************************************************/
472/* Key management */
473/****************************************************************/
474
John Durkop9814fa22020-11-04 12:28:15 -0800475#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800476 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100477 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
478 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
479 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
481 size_t bits,
482 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200483{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100485 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100487#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100488 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100490#endif
491#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100492 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100493 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100494#endif
495#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100496 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100497 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100498#endif
499#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100500 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100502#endif
503#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100504 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100505 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100506 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 if (bits_is_sloppy) {
508 return MBEDTLS_ECP_DP_SECP521R1;
509 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100510 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100511#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100512 }
513 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100514
Paul Elliott8ff510a2020-06-02 17:19:28 +0100515 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100517#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100518 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100520#endif
521#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100522 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100523 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100524#endif
525#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100526 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100527 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100528#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100529 }
530 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100531
Paul Elliott8ff510a2020-06-02 17:19:28 +0100532 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100534#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100535 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100536 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100537 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 if (bits_is_sloppy) {
539 return MBEDTLS_ECP_DP_CURVE25519;
540 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100541 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100542#endif
543#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100544 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100546#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100547 }
548 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100549
Paul Elliott8ff510a2020-06-02 17:19:28 +0100550 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100551 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100552#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100553 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100554 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100555#endif
556#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100557 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100558 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100559#endif
560#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100561 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100562 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100563#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100564 }
565 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200566 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100567
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100568 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100569 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200570}
John Durkop9814fa22020-11-04 12:28:15 -0800571#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100572 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
573 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
574 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
575 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100577static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
578 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200579{
580 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100581 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200582 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200583 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200584 case PSA_KEY_TYPE_DERIVE:
585 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100586#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200587 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100588 if (bits != 128 && bits != 192 && bits != 256) {
589 return PSA_ERROR_INVALID_ARGUMENT;
590 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200591 break;
592#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200593#if defined(PSA_WANT_KEY_TYPE_ARIA)
594 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100595 if (bits != 128 && bits != 192 && bits != 256) {
596 return PSA_ERROR_INVALID_ARGUMENT;
597 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200598 break;
599#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100600#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200601 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100602 if (bits != 128 && bits != 192 && bits != 256) {
603 return PSA_ERROR_INVALID_ARGUMENT;
604 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200605 break;
606#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100607#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200608 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100609 if (bits != 64 && bits != 128 && bits != 192) {
610 return PSA_ERROR_INVALID_ARGUMENT;
611 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200612 break;
613#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100614#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200615 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100616 if (bits < 8 || bits > 2048) {
617 return PSA_ERROR_INVALID_ARGUMENT;
618 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200619 break;
620#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100621#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200622 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100623 if (bits != 256) {
624 return PSA_ERROR_INVALID_ARGUMENT;
625 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200626 break;
627#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200628 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100629 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200630 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100631 if (bits % 8 != 0) {
632 return PSA_ERROR_INVALID_ARGUMENT;
633 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100635 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200636}
637
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100638/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100639 *
Steven Cooremancd640932021-03-08 12:00:27 +0100640 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
641 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100642 *
643 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100644 * \param[in] key_type The key type of the key to be used with the
645 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100646 *
647 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100648 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100649 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100650 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100651 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100652MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100653 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100654 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100655{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100656 if (PSA_ALG_IS_HMAC(algorithm)) {
657 if (key_type == PSA_KEY_TYPE_HMAC) {
658 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100659 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100660 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100661
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100662 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
663 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
664 * key. */
665 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
666 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
667 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
668 * the block length (larger than 1) for block ciphers. */
669 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
670 return PSA_SUCCESS;
671 }
672 }
673 }
674
675 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100676}
677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100678psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
679 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200680{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100681 if (slot->key.data != NULL) {
682 return PSA_ERROR_ALREADY_EXISTS;
683 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100685 slot->key.data = mbedtls_calloc(1, buffer_length);
686 if (slot->key.data == NULL) {
687 return PSA_ERROR_INSUFFICIENT_MEMORY;
688 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200689
Ronald Cronea0f8a62020-11-25 17:52:23 +0100690 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100691 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200692}
693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100694psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
695 const uint8_t *data,
696 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200697{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100698 psa_status_t status = psa_allocate_buffer_to_slot(slot,
699 data_length);
700 if (status != PSA_SUCCESS) {
701 return status;
702 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100704 memcpy(slot->key.data, data, data_length);
705 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200706}
707
Ronald Crona0fe59f2020-11-29 11:14:53 +0100708psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100709 const psa_key_attributes_t *attributes,
710 const uint8_t *data, size_t data_length,
711 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100712 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100713{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100714 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
715 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100716
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200717 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100718 if (data_length == 0) {
719 return PSA_ERROR_NOT_SUPPORTED;
720 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100722 if (key_type_is_raw_bytes(type)) {
723 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200724
Steven Cooreman75b74362020-07-28 14:30:13 +0200725 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726 if (data_length > SIZE_MAX / 8) {
727 return PSA_ERROR_NOT_SUPPORTED;
728 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200729
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200730 /* Enforce a size limit, and in particular ensure that the bit
731 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100732 if ((*bits) > PSA_MAX_KEY_BITS) {
733 return PSA_ERROR_NOT_SUPPORTED;
734 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200735
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100736 status = validate_unstructured_key_bit_size(type, *bits);
737 if (status != PSA_SUCCESS) {
738 return status;
739 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200740
Ronald Crondd04d422020-11-28 15:14:42 +0100741 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100742 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100743 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100744 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200745
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100746 return PSA_SUCCESS;
747 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800748#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100749 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
750 if (PSA_KEY_TYPE_IS_ECC(type)) {
751 return mbedtls_psa_ecp_import_key(attributes,
752 data, data_length,
753 key_buffer, key_buffer_size,
754 key_buffer_length,
755 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100756 }
John Durkop9814fa22020-11-04 12:28:15 -0800757#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
758 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700759#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100760 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
761 if (PSA_KEY_TYPE_IS_RSA(type)) {
762 return mbedtls_psa_rsa_import_key(attributes,
763 data, data_length,
764 key_buffer, key_buffer_size,
765 key_buffer_length,
766 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200767 }
John Durkop9814fa22020-11-04 12:28:15 -0800768#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
769 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100770 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100772 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100773}
774
Gilles Peskinef603c712019-01-19 13:40:11 +0100775/** Calculate the intersection of two algorithm usage policies.
776 *
777 * Return 0 (which allows no operation) on incompatibility.
778 */
779static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100780 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100781 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100782 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100783{
Gilles Peskine549ea862019-05-22 11:45:59 +0200784 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100785 if (alg1 == alg2) {
786 return alg1;
787 }
Steven Cooreman03488022021-02-18 12:07:20 +0100788 /* If the policies are from the same hash-and-sign family, check
789 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
791 PSA_ALG_IS_SIGN_HASH(alg2) &&
792 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
793 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
794 return alg2;
795 }
796 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
797 return alg1;
798 }
Steven Cooreman03488022021-02-18 12:07:20 +0100799 }
800 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100801 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100802 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100803 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
804 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
805 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
806 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
807 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100808 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100809
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100810 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100811 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
812 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
813 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
814 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100815 }
816 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100817 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
818 (alg1_len <= alg2_len)) {
819 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100820 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
822 (alg2_len <= alg1_len)) {
823 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100824 }
825 }
826 /* If the policies are from the same MAC family, check whether one
827 * of them is a minimum-MAC-length policy. Calculate the most
828 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100829 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
830 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
831 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100832 /* Validate the combination of key type and algorithm. Since the base
833 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100834 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
835 return 0;
836 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100837
Steven Cooreman31a876d2021-03-03 20:47:40 +0100838 /* Get the (exact or at-least) output lengths for both sides of the
839 * requested intersection. None of the currently supported algorithms
840 * have an output length dependent on the actual key size, so setting it
841 * to a bogus value of 0 is currently OK.
842 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100843 * Note that for at-least-this-length wildcard algorithms, the output
844 * length is set to the shortest allowed length, which allows us to
845 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100846 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
847 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100848 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100849
Steven Cooremana1d83222021-02-25 10:20:29 +0100850 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100851 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
852 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
853 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100854 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100855
856 /* If only one is an at-least-this-length policy, the intersection would
857 * be the other (fixed-length) policy as long as said fixed length is
858 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100859 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
860 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100861 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100862 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
863 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100864 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100865
Steven Cooremancd640932021-03-08 12:00:27 +0100866 /* If none of them are wildcards, check whether they define the same tag
867 * length. This is still possible here when one is default-length and
868 * the other specific-length. Ensure to always return the
869 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100870 if (alg1_len == alg2_len) {
871 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
872 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100873 }
874 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100875 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100876}
877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100878static int psa_key_algorithm_permits(psa_key_type_t key_type,
879 psa_algorithm_t policy_alg,
880 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200881{
Gilles Peskine549ea862019-05-22 11:45:59 +0200882 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100883 if (requested_alg == policy_alg) {
884 return 1;
885 }
Steven Cooreman03488022021-02-18 12:07:20 +0100886 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
887 * and requested_alg is the same hash-and-sign family with any hash,
888 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100889 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
890 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
891 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
892 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100893 }
894 /* If policy_alg is a wildcard AEAD algorithm of the same base as
895 * the requested algorithm, check the requested tag length to be
896 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100897 if (PSA_ALG_IS_AEAD(policy_alg) &&
898 PSA_ALG_IS_AEAD(requested_alg) &&
899 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
900 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
901 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
902 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
903 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100904 }
Steven Cooremancd640932021-03-08 12:00:27 +0100905 /* If policy_alg is a MAC algorithm of the same base as the requested
906 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100907 if (PSA_ALG_IS_MAC(policy_alg) &&
908 PSA_ALG_IS_MAC(requested_alg) &&
909 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
910 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100911 /* Validate the combination of key type and algorithm. Since the policy
912 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100913 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
914 return 0;
915 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100916
Steven Cooreman31a876d2021-03-03 20:47:40 +0100917 /* Get both the requested output length for the algorithm which is to be
918 * verified, and the default output length for the base algorithm.
919 * Note that none of the currently supported algorithms have an output
920 * length dependent on actual key size, so setting it to a bogus value
921 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100922 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100923 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100924 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100925 key_type, 0,
926 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100927
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100928 /* If the policy is default-length, only allow an algorithm with
929 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
931 return requested_output_length == default_output_length;
932 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100933
934 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100935 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100936 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
937 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
938 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100939 }
940
Steven Cooremancd640932021-03-08 12:00:27 +0100941 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
942 * check for the requested MAC length to be equal to or longer than the
943 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100944 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
945 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
946 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100947 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200948 }
Steven Cooremance48e852020-10-05 16:02:45 +0200949 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200950 * a key derivation with that key agreement should also be allowed. This
951 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100952 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
953 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
954 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
955 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200956 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100957 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100958 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200959}
960
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100961/** Test whether a policy permits an algorithm.
962 *
963 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100964 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100965 * \note This function requires providing the key type for which the policy is
966 * being validated, since some algorithm policy definitions (e.g. MAC)
967 * have different properties depending on what kind of cipher it is
968 * combined with.
969 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100970 * \retval PSA_SUCCESS When \p alg is a specific algorithm
971 * allowed by the \p policy.
972 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
973 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
974 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100975 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100976static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
977 psa_key_type_t key_type,
978 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100979{
Steven Cooremand788fab2021-02-25 11:29:17 +0100980 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100981 if (alg == 0) {
982 return PSA_ERROR_INVALID_ARGUMENT;
983 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100984
Steven Cooreman7e39f052021-02-23 14:18:32 +0100985 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100986 if (PSA_ALG_IS_WILDCARD(alg)) {
987 return PSA_ERROR_INVALID_ARGUMENT;
988 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100990 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
991 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
992 return PSA_SUCCESS;
993 } else {
994 return PSA_ERROR_NOT_PERMITTED;
995 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100996}
997
Gilles Peskinef603c712019-01-19 13:40:11 +0100998/** Restrict a key policy based on a constraint.
999 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001000 * \note This function requires providing the key type for which the policy is
1001 * being restricted, since some algorithm policy definitions (e.g. MAC)
1002 * have different properties depending on what kind of cipher it is
1003 * combined with.
1004 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001005 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001006 * \param[in,out] policy The policy to restrict.
1007 * \param[in] constraint The policy constraint to apply.
1008 *
1009 * \retval #PSA_SUCCESS
1010 * \c *policy contains the intersection of the original value of
1011 * \c *policy and \c *constraint.
1012 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001013 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001014 * \c *policy is unchanged.
1015 */
1016static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001017 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001018 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001019 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001020{
1021 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001022 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1023 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001024 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001025 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1026 constraint->alg2);
1027 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1028 return PSA_ERROR_INVALID_ARGUMENT;
1029 }
1030 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1031 return PSA_ERROR_INVALID_ARGUMENT;
1032 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001033 policy->usage &= constraint->usage;
1034 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001035 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001036 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001037}
1038
Ronald Cron5c522922020-11-14 16:35:34 +01001039/** Get the description of a key given its identifier and policy constraints
1040 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001041 *
Ronald Cron5c522922020-11-14 16:35:34 +01001042 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +01001043 * nonzero, the key must allow operations with this algorithm. If \p alg is
1044 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +01001045 *
Ronald Cron5c522922020-11-14 16:35:34 +01001046 * In case of a persistent key, the function loads the description of the key
1047 * into a key slot if not already done.
1048 *
1049 * On success, the returned key slot is locked. It is the responsibility of
1050 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001051 */
Ronald Cron5c522922020-11-14 16:35:34 +01001052static psa_status_t psa_get_and_lock_key_slot_with_policy(
1053 mbedtls_svc_key_id_t key,
1054 psa_key_slot_t **p_slot,
1055 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001056 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001057{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001058 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1059 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001061 status = psa_get_and_lock_key_slot(key, p_slot);
1062 if (status != PSA_SUCCESS) {
1063 return status;
1064 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001065 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001066
1067 /* Enforce that usage policy for the key slot contains all the flags
1068 * required by the usage parameter. There is one exception: public
1069 * keys can always be exported, so we treat public key objects as
1070 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001071 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001072 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001073 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001075 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001076 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001077 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001078 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001079
Shaun Case0e7791f2021-12-20 21:14:10 -08001080 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001081 if (alg != 0) {
1082 status = psa_key_policy_permits(&slot->attr.policy,
1083 slot->attr.type,
1084 alg);
1085 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001086 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001087 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001088 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001090 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001091
1092error:
1093 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001094 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001096 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001097}
Darryl Green940d72c2018-07-13 13:18:51 +01001098
Ronald Cron5c522922020-11-14 16:35:34 +01001099/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001100 *
1101 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001102 * available, as opposed to a key in a secure element and/or to be used
1103 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001104 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001105 * This is a temporary function that may be used instead of
1106 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1107 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001108 *
Ronald Cron5c522922020-11-14 16:35:34 +01001109 * On success, the returned key slot is locked. It is the responsibility of the
1110 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001111 */
Ronald Cron5c522922020-11-14 16:35:34 +01001112static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1113 mbedtls_svc_key_id_t key,
1114 psa_key_slot_t **p_slot,
1115 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001116 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001117{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001118 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1119 usage, alg);
1120 if (status != PSA_SUCCESS) {
1121 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001122 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001124 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1125 psa_unlock_key_slot(*p_slot);
1126 *p_slot = NULL;
1127 return PSA_ERROR_NOT_SUPPORTED;
1128 }
1129
1130 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001131}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001133psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001134{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001135 /* Data pointer will always be either a valid pointer or NULL in an
1136 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001137 if (slot->key.data != NULL) {
1138 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1139 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001141 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001142 slot->key.data = NULL;
1143 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001145 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001146}
1147
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001148/** Completely wipe a slot in memory, including its policy.
1149 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001150psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001151{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001152 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001153
1154 /*
1155 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001156 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001157 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1158 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001159 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001160 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001161 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001162#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001163 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001164#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001165 status = PSA_ERROR_CORRUPTION_DETECTED;
1166 }
1167
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001168 /* Multipart operations may still be using the key. This is safe
1169 * because all multipart operation objects are independent from
1170 * the key slot: if they need to access the key after the setup
1171 * phase, they have a copy of the key. Note that this means that
1172 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001173 /* At this point, key material and other type-specific content has
1174 * been wiped. Clear remaining metadata. We can call memset and not
1175 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001176 memset(slot, 0, sizeof(*slot));
1177 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001178}
1179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001180psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001181{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001182 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001183 psa_status_t status; /* status of the last operation */
1184 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001185#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1186 psa_se_drv_table_entry_t *driver;
1187#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001189 if (mbedtls_svc_key_id_is_null(key)) {
1190 return PSA_SUCCESS;
1191 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001192
Ronald Cronf2911112020-10-29 17:51:10 +01001193 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001194 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001195 * key, this will load the key description from persistent memory if not
1196 * done yet. We cannot avoid this loading as without it we don't know if
1197 * the key is operated by an SE or not and this information is needed by
1198 * the current implementation.
1199 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001200 status = psa_get_and_lock_key_slot(key, &slot);
1201 if (status != PSA_SUCCESS) {
1202 return status;
1203 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001204
Ronald Cronf2911112020-10-29 17:51:10 +01001205 /*
1206 * If the key slot containing the key description is under access by the
1207 * library (apart from the present access), the key cannot be destroyed
1208 * yet. For the time being, just return in error. Eventually (to be
1209 * implemented), the key should be destroyed when all accesses have
1210 * stopped.
1211 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001212 if (slot->lock_count > 1) {
1213 psa_unlock_key_slot(slot);
1214 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001215 }
1216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001217 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001218 /* Refuse the destruction of a read-only key (which may or may not work
1219 * if we attempt it, depending on whether the key is merely read-only
1220 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001221 * Just do the best we can, which is to wipe the copy in memory
1222 * (done in this function's cleanup code). */
1223 overall_status = PSA_ERROR_NOT_PERMITTED;
1224 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001225 }
1226
Gilles Peskine354f7672019-07-12 23:46:38 +02001227#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001228 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1229 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001230 /* For a key in a secure element, we need to do three things:
1231 * remove the key file in internal storage, destroy the
1232 * key inside the secure element, and update the driver's
1233 * persistent data. Start a transaction that will encompass these
1234 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001235 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001236 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001237 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001238 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001239 status = psa_crypto_save_transaction();
1240 if (status != PSA_SUCCESS) {
1241 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001242 /* We should still try to destroy the key in the secure
1243 * element and the key metadata in storage. This is especially
1244 * important if the error is that the storage is full.
1245 * But how to do it exactly without risking an inconsistent
1246 * state after a reset?
1247 * https://github.com/ARMmbed/mbed-crypto/issues/215
1248 */
1249 overall_status = status;
1250 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001251 }
1252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001253 status = psa_destroy_se_key(driver,
1254 psa_key_slot_get_slot_number(slot));
1255 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001256 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001257 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001258 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001259#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1260
Darryl Greend49a4992018-06-18 17:27:26 +01001261#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001262 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1263 status = psa_destroy_persistent_key(slot->attr.id);
1264 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001265 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001266 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001267
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001268 /* TODO: other slots may have a copy of the same key. We should
1269 * invalidate them.
1270 * https://github.com/ARMmbed/mbed-crypto/issues/214
1271 */
Darryl Greend49a4992018-06-18 17:27:26 +01001272 }
1273#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001274
Gilles Peskinefc762652019-07-22 19:30:34 +02001275#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276 if (driver != NULL) {
1277 status = psa_save_se_persistent_data(driver);
1278 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001279 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001280 }
1281 status = psa_crypto_stop_transaction();
1282 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001283 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001284 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001285 }
1286#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1287
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001288exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001289 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001290 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001291 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001292 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001293 }
1294 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001295}
1296
John Durkop07cc04a2020-11-16 22:08:34 -08001297#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001298 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001299static psa_status_t psa_get_rsa_public_exponent(
1300 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001301 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001302{
1303 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001304 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001305 uint8_t *buffer = NULL;
1306 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001307 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001309 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1310 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001311 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001312 }
1313 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001314 /* It's the default value, which is reported as an empty string,
1315 * so there's nothing to do. */
1316 goto exit;
1317 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001319 buflen = mbedtls_mpi_size(&mpi);
1320 buffer = mbedtls_calloc(1, buflen);
1321 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001322 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1323 goto exit;
1324 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001325 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1326 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001327 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001328 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001329 attributes->domain_parameters = buffer;
1330 attributes->domain_parameters_size = buflen;
1331
1332exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001333 mbedtls_mpi_free(&mpi);
1334 if (ret != 0) {
1335 mbedtls_free(buffer);
1336 }
1337 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001338}
John Durkop07cc04a2020-11-16 22:08:34 -08001339#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001340 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001341
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001342/** Retrieve all the publicly-accessible attributes of a key.
1343 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001344psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1345 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001346{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001348 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001349 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001351 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001353 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1354 if (status != PSA_SUCCESS) {
1355 return status;
1356 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001357
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001358 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001359 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1360 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001361
1362#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001363 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1364 psa_set_key_slot_number(attributes,
1365 psa_key_slot_get_slot_number(slot));
1366 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001367#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001369 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001370#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001371 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001372 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001373 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001374 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001375 * is not yet implemented.
1376 * https://github.com/ARMmbed/mbed-crypto/issues/216
1377 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001378 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001379 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001380
Ronald Cron90857082020-11-25 14:58:33 +01001381 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001382 slot->attr.type,
1383 slot->key.data,
1384 slot->key.bytes,
1385 &rsa);
1386 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001387 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001388 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001390 status = psa_get_rsa_public_exponent(rsa,
1391 attributes);
1392 mbedtls_rsa_free(rsa);
1393 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001394 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001395 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001396#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001397 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001398 default:
1399 /* Nothing else to do. */
1400 break;
1401 }
1402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001403 if (status != PSA_SUCCESS) {
1404 psa_reset_key_attributes(attributes);
1405 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001407 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001409 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001410}
1411
Gilles Peskinec8000c02019-08-02 20:15:51 +02001412#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1413psa_status_t psa_get_key_slot_number(
1414 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001415 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001416{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001417 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001418 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001419 return PSA_SUCCESS;
1420 } else {
1421 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001422 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001423}
1424#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001426static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1427 size_t key_buffer_size,
1428 uint8_t *data,
1429 size_t data_size,
1430 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001431{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001432 if (key_buffer_size > data_size) {
1433 return PSA_ERROR_BUFFER_TOO_SMALL;
1434 }
1435 memcpy(data, key_buffer, key_buffer_size);
1436 memset(data + key_buffer_size, 0,
1437 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001438 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001439 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001440}
1441
Ronald Cron7285cda2020-11-26 14:46:37 +01001442psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001443 const psa_key_attributes_t *attributes,
1444 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001445 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001446{
Ronald Crond18b5f82020-11-25 16:46:05 +01001447 psa_key_type_t type = attributes->core.type;
1448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001449 if (key_type_is_raw_bytes(type) ||
1450 PSA_KEY_TYPE_IS_RSA(type) ||
1451 PSA_KEY_TYPE_IS_ECC(type)) {
1452 return psa_export_key_buffer_internal(
1453 key_buffer, key_buffer_size,
1454 data, data_size, data_length);
1455 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001456 /* This shouldn't happen in the reference implementation, but
1457 it is valid for a special-purpose implementation to omit
1458 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001459 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001460 }
1461}
1462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001463psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1464 uint8_t *data,
1465 size_t data_size,
1466 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001467{
1468 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1469 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1470 psa_key_slot_t *slot;
1471
Ronald Crone907e552021-01-18 13:22:38 +01001472 /* Reject a zero-length output buffer now, since this can never be a
1473 * valid key representation. This way we know that data must be a valid
1474 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001475 if (data_size == 0) {
1476 return PSA_ERROR_BUFFER_TOO_SMALL;
1477 }
Ronald Crone907e552021-01-18 13:22:38 +01001478
Ronald Cron9486f9d2020-11-26 13:36:23 +01001479 /* Set the key to empty now, so that even when there are errors, we always
1480 * set data_length to a value between 0 and data_size. On error, setting
1481 * the key to empty is a good choice because an empty key representation is
1482 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001483 *data_length = 0;
1484
Ronald Cron9486f9d2020-11-26 13:36:23 +01001485 /* Export requires the EXPORT flag. There is an exception for public keys,
1486 * which don't require any flag, but
1487 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1488 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001489 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1490 PSA_KEY_USAGE_EXPORT, 0);
1491 if (status != PSA_SUCCESS) {
1492 return status;
1493 }
mohammad160306e79202018-03-28 13:17:44 +03001494
Ronald Crond18b5f82020-11-25 16:46:05 +01001495 psa_key_attributes_t attributes = {
1496 .core = slot->attr
1497 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001498 status = psa_driver_wrapper_export_key(&attributes,
1499 slot->key.data, slot->key.bytes,
1500 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001501
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001502 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001504 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001505}
1506
Ronald Cron7285cda2020-11-26 14:46:37 +01001507psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001508 const psa_key_attributes_t *attributes,
1509 const uint8_t *key_buffer,
1510 size_t key_buffer_size,
1511 uint8_t *data,
1512 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001513 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001514{
Ronald Crond18b5f82020-11-25 16:46:05 +01001515 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001517 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1518 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001519 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001520 return psa_export_key_buffer_internal(
1521 key_buffer, key_buffer_size,
1522 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001523 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001525 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001526#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001527 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1528 return mbedtls_psa_rsa_export_public_key(attributes,
1529 key_buffer,
1530 key_buffer_size,
1531 data,
1532 data_size,
1533 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001534#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001535 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001536 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001537#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1538 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001539 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001540#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001541 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1542 return mbedtls_psa_ecp_export_public_key(attributes,
1543 key_buffer,
1544 key_buffer_size,
1545 data,
1546 data_size,
1547 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001548#else
1549 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001550 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001551#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1552 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001553 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001554 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001555 /* This shouldn't happen in the reference implementation, but
1556 it is valid for a special-purpose implementation to omit
1557 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001558 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001559 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001560}
Ronald Crond18b5f82020-11-25 16:46:05 +01001561
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001562psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1563 uint8_t *data,
1564 size_t data_size,
1565 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001566{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001567 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001568 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001569 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001570 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001571
Ronald Crone907e552021-01-18 13:22:38 +01001572 /* Reject a zero-length output buffer now, since this can never be a
1573 * valid key representation. This way we know that data must be a valid
1574 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001575 if (data_size == 0) {
1576 return PSA_ERROR_BUFFER_TOO_SMALL;
1577 }
Ronald Crone907e552021-01-18 13:22:38 +01001578
Darryl Greendd8fb772018-11-07 16:00:44 +00001579 /* Set the key to empty now, so that even when there are errors, we always
1580 * set data_length to a value between 0 and data_size. On error, setting
1581 * the key to empty is a good choice because an empty key representation is
1582 * unlikely to be accepted anywhere. */
1583 *data_length = 0;
1584
1585 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001586 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1587 if (status != PSA_SUCCESS) {
1588 return status;
1589 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001591 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1592 status = PSA_ERROR_INVALID_ARGUMENT;
1593 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001594 }
1595
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001596 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001597 .core = slot->attr
1598 };
Ronald Cron67227982020-11-26 15:16:05 +01001599 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001600 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001601 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001602
1603exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001604 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001606 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001607}
1608
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001609MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1610 "One or more key attribute flag is listed as both external-only and dual-use")
1611MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1612 "One or more key attribute flag is listed as both internal-only and dual-use")
1613MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1614 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001615
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001616/** Validate that a key policy is internally well-formed.
1617 *
1618 * This function only rejects invalid policies. It does not validate the
1619 * consistency of the policy with respect to other attributes of the key
1620 * such as the key type.
1621 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001622static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001623{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001624 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1625 PSA_KEY_USAGE_COPY |
1626 PSA_KEY_USAGE_ENCRYPT |
1627 PSA_KEY_USAGE_DECRYPT |
1628 PSA_KEY_USAGE_SIGN_MESSAGE |
1629 PSA_KEY_USAGE_VERIFY_MESSAGE |
1630 PSA_KEY_USAGE_SIGN_HASH |
1631 PSA_KEY_USAGE_VERIFY_HASH |
1632 PSA_KEY_USAGE_DERIVE)) != 0) {
1633 return PSA_ERROR_INVALID_ARGUMENT;
1634 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001636 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001637}
1638
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001639/** Validate the internal consistency of key attributes.
1640 *
1641 * This function only rejects invalid attribute values. If does not
1642 * validate the consistency of the attributes with any key data that may
1643 * be involved in the creation of the key.
1644 *
1645 * Call this function early in the key creation process.
1646 *
1647 * \param[in] attributes Key attributes for the new key.
1648 * \param[out] p_drv On any return, the driver for the key, if any.
1649 * NULL for a transparent key.
1650 *
1651 */
1652static psa_status_t psa_validate_key_attributes(
1653 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001654 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001655{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001656 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001657 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1658 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001659
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001660 status = psa_validate_key_location(lifetime, p_drv);
1661 if (status != PSA_SUCCESS) {
1662 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001663 }
1664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001665 status = psa_validate_key_persistence(lifetime);
1666 if (status != PSA_SUCCESS) {
1667 return status;
1668 }
1669
1670 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1671 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1672 return PSA_ERROR_INVALID_ARGUMENT;
1673 }
1674 } else {
1675 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1676 return PSA_ERROR_INVALID_ARGUMENT;
1677 }
1678 }
1679
1680 status = psa_validate_key_policy(&attributes->core.policy);
1681 if (status != PSA_SUCCESS) {
1682 return status;
1683 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001684
1685 /* Refuse to create overly large keys.
1686 * Note that this doesn't trigger on import if the attributes don't
1687 * explicitly specify a size (so psa_get_key_bits returns 0), so
1688 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001689 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1690 return PSA_ERROR_NOT_SUPPORTED;
1691 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001692
Gilles Peskine91e8c332019-08-02 19:19:39 +02001693 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001694 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1695 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1696 return PSA_ERROR_INVALID_ARGUMENT;
1697 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001699 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001700}
1701
Gilles Peskine4747d192019-04-17 15:05:45 +02001702/** Prepare a key slot to receive key material.
1703 *
1704 * This function allocates a key slot and sets its metadata.
1705 *
1706 * If this function fails, call psa_fail_key_creation().
1707 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001708 * This function is intended to be used as follows:
1709 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001710 * it with the specified attributes, and in case of a volatile key assign it
1711 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001712 * -# Populate the slot with the key material.
1713 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1714 * In case of failure at any step, stop the sequence and call
1715 * psa_fail_key_creation().
1716 *
Ronald Cron5c522922020-11-14 16:35:34 +01001717 * On success, the key slot is locked. It is the responsibility of the caller
1718 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001719 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001720 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001721 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001722 * \param[out] p_slot On success, a pointer to the prepared slot.
1723 * \param[out] p_drv On any return, the driver for the key, if any.
1724 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001725 *
1726 * \retval #PSA_SUCCESS
1727 * The key slot is ready to receive key material.
1728 * \return If this function fails, the key slot is an invalid state.
1729 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001730 */
1731static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001732 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001733 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001734 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001735 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001736{
1737 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001738 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001739 psa_key_slot_t *slot;
1740
Gilles Peskinedf179142019-07-15 22:02:14 +02001741 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001742 *p_drv = NULL;
1743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001744 status = psa_validate_key_attributes(attributes, p_drv);
1745 if (status != PSA_SUCCESS) {
1746 return status;
1747 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001749 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1750 if (status != PSA_SUCCESS) {
1751 return status;
1752 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001753 slot = *p_slot;
1754
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001755 /* We're storing the declared bit-size of the key. It's up to each
1756 * creation mechanism to verify that this information is correct.
1757 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001758 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001759 * is optional (import, copy). In case of a volatile key, assign it the
1760 * volatile key identifier associated to the slot returned to contain its
1761 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001762
1763 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001764 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001765#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1766 slot->attr.id = volatile_key_id;
1767#else
1768 slot->attr.id.key_id = volatile_key_id;
1769#endif
1770 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001771
Gilles Peskine91e8c332019-08-02 19:19:39 +02001772 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001773 * external-only flags, query `attributes`. Thanks to the check
1774 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001775 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001776 * may have set. */
1777 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001778
Gilles Peskinecbaff462019-07-12 23:46:04 +02001779#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001780 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001781 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001782 * create the key file in internal storage, create the
1783 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001784 * persistent data. This is done by starting a transaction that will
1785 * encompass these three actions.
1786 * For registering a volatile key, we just need to find an appropriate
1787 * slot number inside the SE. Since the key is designated volatile, creating
1788 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001789 /* The first thing to do is to find a slot number for the new key.
1790 * We save the slot number in persistent storage as part of the
1791 * transaction data. It will be needed to recover if the power
1792 * fails during the key creation process, to clean up on the secure
1793 * element side after restarting. Obtaining a slot number from the
1794 * secure element driver updates its persistent state, but we do not yet
1795 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001796 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001797 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001798 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001799 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1800 &slot_number);
1801 if (status != PSA_SUCCESS) {
1802 return status;
1803 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001805 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1806 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001807 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001808 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001809 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001810 status = psa_crypto_save_transaction();
1811 if (status != PSA_SUCCESS) {
1812 (void) psa_crypto_stop_transaction();
1813 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001814 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001815 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001816
1817 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001818 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001819 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001821 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001822 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001823 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001824 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001825#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1826
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001827 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001828}
Gilles Peskine4747d192019-04-17 15:05:45 +02001829
1830/** Finalize the creation of a key once its key material has been set.
1831 *
1832 * This entails writing the key to persistent storage.
1833 *
1834 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001835 * See the documentation of psa_start_key_creation() for the intended use
1836 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001837 *
Ronald Cron5c522922020-11-14 16:35:34 +01001838 * If the finalization succeeds, the function unlocks the key slot (it was
1839 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1840 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001841 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001842 * \param[in,out] slot Pointer to the slot with key material.
1843 * \param[in] driver The secure element driver for the key,
1844 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001845 * \param[out] key On success, identifier of the key. Note that the
1846 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001847 *
1848 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001849 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001850 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1851 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1852 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1853 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1854 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1855 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001856 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001857 * \return If this function fails, the key slot is an invalid state.
1858 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001859 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001860static psa_status_t psa_finish_key_creation(
1861 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001862 psa_se_drv_table_entry_t *driver,
1863 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001864{
1865 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001866 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001867 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001868
1869#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001870 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001871#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001872 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001873 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001874 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001875 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001876
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001877 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1878 sizeof(data.slot_number),
1879 "Slot number size does not match psa_se_key_data_storage_t");
1880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001881 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1882 status = psa_save_persistent_key(&slot->attr,
1883 (uint8_t *) &data,
1884 sizeof(data));
1885 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001886#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1887 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001888 /* Key material is saved in export representation in the slot, so
1889 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001890 status = psa_save_persistent_key(&slot->attr,
1891 slot->key.data,
1892 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001893 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001894 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001895#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1896
Gilles Peskinecbaff462019-07-12 23:46:04 +02001897#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001898 /* Finish the transaction for a key creation. This does not
1899 * happen when registering an existing key. Detect this case
1900 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001901 * creation of a persistent key in a secure element requires a transaction,
1902 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001903 if (driver != NULL &&
1904 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1905 status = psa_save_se_persistent_data(driver);
1906 if (status != PSA_SUCCESS) {
1907 psa_destroy_persistent_key(slot->attr.id);
1908 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001909 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001910 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001911 }
1912#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1913
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001914 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001915 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001916 status = psa_unlock_key_slot(slot);
1917 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001918 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001919 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001920 }
Ronald Cron50972942020-11-14 11:28:25 +01001921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001922 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001923}
1924
1925/** Abort the creation of a key.
1926 *
1927 * You may call this function after calling psa_start_key_creation(),
1928 * or after psa_finish_key_creation() fails. In other circumstances, this
1929 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001930 * See the documentation of psa_start_key_creation() for the intended use
1931 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001932 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001933 * \param[in,out] slot Pointer to the slot with key material.
1934 * \param[in] driver The secure element driver for the key,
1935 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001936 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001937static void psa_fail_key_creation(psa_key_slot_t *slot,
1938 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001939{
Gilles Peskine011e4282019-06-26 18:34:38 +02001940 (void) driver;
1941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001942 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001943 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001944 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001945
1946#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001947 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001948 * element, and the failure happened later (when saving metadata
1949 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001950 * element.
1951 * https://github.com/ARMmbed/mbed-crypto/issues/217
1952 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001953
Gilles Peskined7729582019-08-05 15:55:54 +02001954 /* Abort the ongoing transaction if any (there may not be one if
1955 * the creation process failed before starting one, or if the
1956 * key creation is a registration of a key in a secure element).
1957 * Earlier functions must already have done what it takes to undo any
1958 * partial creation. All that's left is to update the transaction data
1959 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001960 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001961#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1962
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001963 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001964}
1965
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001966/** Validate optional attributes during key creation.
1967 *
1968 * Some key attributes are optional during key creation. If they are
1969 * specified in the attributes structure, check that they are consistent
1970 * with the data in the slot.
1971 *
1972 * This function should be called near the end of key creation, after
1973 * the slot in memory is fully populated but before saving persistent data.
1974 */
1975static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001976 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001977 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001978{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001979 if (attributes->core.type != 0) {
1980 if (attributes->core.type != slot->attr.type) {
1981 return PSA_ERROR_INVALID_ARGUMENT;
1982 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001983 }
1984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001985 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001986#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001987 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1988 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001989 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001990 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001991 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001992
Ronald Cron90857082020-11-25 14:58:33 +01001993 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001994 slot->attr.type,
1995 slot->key.data,
1996 slot->key.bytes,
1997 &rsa);
1998 if (status != PSA_SUCCESS) {
1999 return status;
2000 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002002 mbedtls_mpi_init(&actual);
2003 mbedtls_mpi_init(&required);
2004 ret = mbedtls_rsa_export(rsa,
2005 NULL, NULL, NULL, NULL, &actual);
2006 mbedtls_rsa_free(rsa);
2007 mbedtls_free(rsa);
2008 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002009 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002010 }
2011 ret = mbedtls_mpi_read_binary(&required,
2012 attributes->domain_parameters,
2013 attributes->domain_parameters_size);
2014 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002015 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002016 }
2017 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002018 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002019 }
2020rsa_exit:
2021 mbedtls_mpi_free(&actual);
2022 mbedtls_mpi_free(&required);
2023 if (ret != 0) {
2024 return mbedtls_to_psa_error(ret);
2025 }
2026 } else
John Durkop9814fa22020-11-04 12:28:15 -08002027#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2028 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002029 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002030 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002031 }
2032 }
2033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002034 if (attributes->core.bits != 0) {
2035 if (attributes->core.bits != slot->attr.bits) {
2036 return PSA_ERROR_INVALID_ARGUMENT;
2037 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002038 }
2039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002040 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041}
2042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002043psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2044 const uint8_t *data,
2045 size_t data_length,
2046 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002047{
2048 psa_status_t status;
2049 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002050 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002051 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002052
Ronald Cron81709fc2020-11-14 12:10:32 +01002053 *key = MBEDTLS_SVC_KEY_ID_INIT;
2054
Gilles Peskine0f84d622019-09-12 19:03:13 +02002055 /* Reject zero-length symmetric keys (including raw data key objects).
2056 * This also rejects any key which might be encoded as an empty string,
2057 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002058 if (data_length == 0) {
2059 return PSA_ERROR_INVALID_ARGUMENT;
2060 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002062 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2063 &slot, &driver);
2064 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002065 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002066 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002067
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002068 /* In the case of a transparent key or an opaque key stored in local
2069 * storage (thus not in the case of generating a key in a secure element
2070 * or cryptoprocessor with storage), we have to allocate a buffer to
2071 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002072 if (slot->key.data == NULL) {
2073 status = psa_allocate_buffer_to_slot(slot, data_length);
2074 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002075 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002076 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002077 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002078
2079 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002080 status = psa_driver_wrapper_import_key(attributes,
2081 data, data_length,
2082 slot->key.data,
2083 slot->key.bytes,
2084 &slot->key.bytes, &bits);
2085 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002086 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002087 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002089 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002090 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002091 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002092 status = PSA_ERROR_INVALID_ARGUMENT;
2093 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002094 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002096 status = psa_validate_optional_attributes(slot, attributes);
2097 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002098 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002099 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002102exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002103 if (status != PSA_SUCCESS) {
2104 psa_fail_key_creation(slot, driver);
2105 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002107 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002108}
2109
Gilles Peskined7729582019-08-05 15:55:54 +02002110#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2111psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002112 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002113{
2114 psa_status_t status;
2115 psa_key_slot_t *slot = NULL;
2116 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002118
2119 /* Leaving attributes unspecified is not currently supported.
2120 * It could make sense to query the key type and size from the
2121 * secure element, but not all secure elements support this
2122 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002123 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2124 return PSA_ERROR_NOT_SUPPORTED;
2125 }
2126 if (psa_get_key_bits(attributes) == 0) {
2127 return PSA_ERROR_NOT_SUPPORTED;
2128 }
Gilles Peskined7729582019-08-05 15:55:54 +02002129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002130 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2131 &slot, &driver);
2132 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002133 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002134 }
Gilles Peskined7729582019-08-05 15:55:54 +02002135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002136 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002137
2138exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002139 if (status != PSA_SUCCESS) {
2140 psa_fail_key_creation(slot, driver);
2141 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002142
Gilles Peskined7729582019-08-05 15:55:54 +02002143 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002144 psa_close_key(key);
2145 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002146}
2147#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002149static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2150 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002151{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152 psa_status_t status = psa_copy_key_material_into_slot(target,
2153 source->key.data,
2154 source->key.bytes);
2155 if (status != PSA_SUCCESS) {
2156 return status;
2157 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002158
Steven Cooreman398aee52020-10-13 14:35:45 +02002159 target->attr.type = source->attr.type;
2160 target->attr.bits = source->attr.bits;
2161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002162 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002163}
2164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002165psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2166 const psa_key_attributes_t *specified_attributes,
2167 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002168{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002169 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002170 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002171 psa_key_slot_t *source_slot = NULL;
2172 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002173 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002174 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002175
Ronald Cron81709fc2020-11-14 12:10:32 +01002176 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2177
Ronald Cron5c522922020-11-14 16:35:34 +01002178 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002179 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2180 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002181 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002182 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002184 status = psa_validate_optional_attributes(source_slot,
2185 specified_attributes);
2186 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002187 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002188 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002190 status = psa_restrict_key_policy(source_slot->attr.type,
2191 &actual_attributes.core.policy,
2192 &source_slot->attr.policy);
2193 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002194 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002195 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002197 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2198 &target_slot, &driver);
2199 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002200 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002201 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002202
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002203#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002204 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002205 /* Copying to a secure element is not implemented yet. */
2206 status = PSA_ERROR_NOT_SUPPORTED;
2207 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002208 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002209#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002211 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002212 /*
2213 * Copying through an opaque driver is not implemented yet, consider
2214 * a lifetime with an external location as an invalid parameter for
2215 * now.
2216 */
2217 status = PSA_ERROR_INVALID_ARGUMENT;
2218 goto exit;
2219 }
2220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002221 status = psa_copy_key_material(source_slot, target_slot);
2222 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002223 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002224 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002226 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002227exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002228 if (status != PSA_SUCCESS) {
2229 psa_fail_key_creation(target_slot, driver);
2230 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002232 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002234 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002235}
2236
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002237
2238
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002239/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002240/* Message digests */
2241/****************************************************************/
2242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002243psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002244{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002245 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002246 if (operation->id == 0) {
2247 return PSA_SUCCESS;
2248 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002250 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002251 operation->id = 0;
2252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002253 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254}
2255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002256psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2257 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002259 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002260
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002261 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002262 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002263 status = PSA_ERROR_BAD_STATE;
2264 goto exit;
2265 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002267 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002268 status = PSA_ERROR_INVALID_ARGUMENT;
2269 goto exit;
2270 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002271
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002272 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2273 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002274 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002276 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002277
2278exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002279 if (status != PSA_SUCCESS) {
2280 psa_hash_abort(operation);
2281 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002282
2283 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002284}
2285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002286psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2287 const uint8_t *input,
2288 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002289{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002290 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002292 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002293 status = PSA_ERROR_BAD_STATE;
2294 goto exit;
2295 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002296
Steven Cooremanc8288352021-03-04 14:02:19 +01002297 /* Don't require hash implementations to behave correctly on a
2298 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002299 if (input_length == 0) {
2300 return PSA_SUCCESS;
2301 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002303 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002304
2305exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002306 if (status != PSA_SUCCESS) {
2307 psa_hash_abort(operation);
2308 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002310 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002311}
2312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002313psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2314 uint8_t *hash,
2315 size_t hash_size,
2316 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002317{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002318 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002319 if (operation->id == 0) {
2320 return PSA_ERROR_BAD_STATE;
2321 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322
Steven Cooreman1e582352021-02-18 17:24:37 +01002323 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002324 operation, hash, hash_size, hash_length);
2325 psa_hash_abort(operation);
2326 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327}
2328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002329psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2330 const uint8_t *hash,
2331 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002333 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002335 psa_status_t status = psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002336 operation,
2337 actual_hash, sizeof(actual_hash),
2338 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002340 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002341 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002342 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002344 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002345 status = PSA_ERROR_INVALID_SIGNATURE;
2346 goto exit;
2347 }
2348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002349 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002350 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002351 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002352
2353exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002354 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2355 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002356 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002357 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002359 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002360}
2361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002362psa_status_t psa_hash_compute(psa_algorithm_t alg,
2363 const uint8_t *input, size_t input_length,
2364 uint8_t *hash, size_t hash_size,
2365 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002366{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002367 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002368 if (!PSA_ALG_IS_HASH(alg)) {
2369 return PSA_ERROR_INVALID_ARGUMENT;
2370 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002372 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2373 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002374}
2375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002376psa_status_t psa_hash_compare(psa_algorithm_t alg,
2377 const uint8_t *input, size_t input_length,
2378 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002379{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002380 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002381 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002383 if (!PSA_ALG_IS_HASH(alg)) {
2384 return PSA_ERROR_INVALID_ARGUMENT;
2385 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002386
Steven Cooreman1e582352021-02-18 17:24:37 +01002387 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002388 alg, input, input_length,
2389 actual_hash, sizeof(actual_hash),
2390 &actual_hash_length);
2391 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002392 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002393 }
2394 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002395 status = PSA_ERROR_INVALID_SIGNATURE;
2396 goto exit;
2397 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002398 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002399 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002400 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002401
2402exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2404 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002405}
2406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002407psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2408 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002409{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002410 if (source_operation->id == 0 ||
2411 target_operation->id != 0) {
2412 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002413 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002415 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2416 target_operation);
2417 if (status != PSA_SUCCESS) {
2418 psa_hash_abort(target_operation);
2419 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002421 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002422}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002423
2424
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002425/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002426/* MAC */
2427/****************************************************************/
2428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002429psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002430{
Steven Cooreman07897832021-03-19 18:28:56 +01002431 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002432 if (operation->id == 0) {
2433 return PSA_SUCCESS;
2434 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002436 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002437 operation->mac_size = 0;
2438 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002439 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002441 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002442}
2443
Ronald Cron1c650a12021-06-17 16:33:22 +02002444static psa_status_t psa_mac_finalize_alg_and_key_validation(
2445 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002446 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002447 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002449 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002450 psa_key_type_t key_type = psa_get_key_type(attributes);
2451 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002453 if (!PSA_ALG_IS_MAC(alg)) {
2454 return PSA_ERROR_INVALID_ARGUMENT;
2455 }
Steven Cooreman07897832021-03-19 18:28:56 +01002456
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002457 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002458 status = psa_mac_key_can_do(alg, key_type);
2459 if (status != PSA_SUCCESS) {
2460 return status;
2461 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002462
Steven Cooremana6474de2021-05-10 11:13:41 +02002463 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002464 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002466 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002467 /* A very short MAC is too short for security since it can be
2468 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2469 * so we make this our minimum, even though 32 bits is still
2470 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002471 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002472 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002474 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2475 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002476 /* It's impossible to "truncate" to a larger length than the full length
2477 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002478 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002479 }
2480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002481 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002482 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2483 * that is disabled in the compile-time configuration. The result can
2484 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2485 * configuration into account. In this case, force a return of
2486 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2487 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2488 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2489 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2490 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002491 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002492 }
2493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002494 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002495}
2496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002497static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2498 mbedtls_svc_key_id_t key,
2499 psa_algorithm_t alg,
2500 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002501{
2502 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2503 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002504 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002505 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002506
2507 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002508 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002509 status = PSA_ERROR_BAD_STATE;
2510 goto exit;
2511 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002512
2513 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002514 key,
2515 &slot,
2516 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2517 alg);
2518 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002519 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002520 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002521
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002522 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002523 .core = slot->attr
2524 };
2525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002526 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2527 &operation->mac_size);
2528 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002529 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002530 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002531
2532 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002533 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002534 if (is_sign) {
2535 status = psa_driver_wrapper_mac_sign_setup(operation,
2536 &attributes,
2537 slot->key.data,
2538 slot->key.bytes,
2539 alg);
2540 } else {
2541 status = psa_driver_wrapper_mac_verify_setup(operation,
2542 &attributes,
2543 slot->key.data,
2544 slot->key.bytes,
2545 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002547
Gilles Peskinefbfac682018-07-08 20:51:54 +02002548exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002549 if (status != PSA_SUCCESS) {
2550 psa_mac_abort(operation);
2551 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002553 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002555 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002556}
2557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002558psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2559 mbedtls_svc_key_id_t key,
2560 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002561{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002562 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002563}
2564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002565psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2566 mbedtls_svc_key_id_t key,
2567 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002568{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002569 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002570}
2571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002572psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2573 const uint8_t *input,
2574 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002576 if (operation->id == 0) {
2577 return PSA_ERROR_BAD_STATE;
2578 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002579
Steven Cooreman11743f92021-03-19 18:38:46 +01002580 /* Don't require hash implementations to behave correctly on a
2581 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002582 if (input_length == 0) {
2583 return PSA_SUCCESS;
2584 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002585
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002586 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2587 input, input_length);
2588 if (status != PSA_SUCCESS) {
2589 psa_mac_abort(operation);
2590 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002592 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002593}
2594
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002595psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2596 uint8_t *mac,
2597 size_t mac_size,
2598 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002599{
Steven Cooreman87885df2021-03-19 19:04:39 +01002600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2601 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002603 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002604 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002605 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002606 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002608 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002609 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002610 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002611 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002612
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002613 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2614 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002615 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002616 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002617 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002618 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002620 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002621 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002622 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002623 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002624
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002625 status = psa_driver_wrapper_mac_sign_finish(operation,
2626 mac, operation->mac_size,
2627 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002628
Dave Rodgman478ab542021-06-25 09:09:02 +01002629exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002630 /* In case of success, set the potential excess room in the output buffer
2631 * to an invalid value, to avoid potentially leaking a longer MAC.
2632 * In case of error, set the output length and content to a safe default,
2633 * such that in case the caller misses an error check, the output would be
2634 * an unachievable MAC.
2635 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002636 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002637 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002638 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002639 }
mohammad16036df908f2018-04-02 08:34:15 -07002640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002641 if (mac_size > operation->mac_size) {
2642 memset(&mac[operation->mac_size], '!',
2643 mac_size - operation->mac_size);
2644 }
Ronald Cron882eb782021-06-18 13:05:48 +02002645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002646 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002648 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002649}
2650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002651psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2652 const uint8_t *mac,
2653 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002654{
Steven Cooreman87885df2021-03-19 19:04:39 +01002655 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2656 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002658 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002659 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002660 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002661 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002663 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002664 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002665 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002666 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002668 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002669 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002670 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002671 }
2672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002673 status = psa_driver_wrapper_mac_verify_finish(operation,
2674 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002675
Dave Rodgman478ab542021-06-25 09:09:02 +01002676exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002677 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002679 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002680}
2681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002682static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2683 psa_algorithm_t alg,
2684 const uint8_t *input,
2685 size_t input_length,
2686 uint8_t *mac,
2687 size_t mac_size,
2688 size_t *mac_length,
2689 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002690{
2691 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002692 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002693 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002694 psa_key_slot_t *slot;
2695 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002696
Ronald Crondbb86462021-06-17 17:17:20 +02002697 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002698 key,
2699 &slot,
2700 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2701 alg);
2702 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002703 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002704 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002705
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002706 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002707 .core = slot->attr
2708 };
2709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002710 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2711 &operation_mac_size);
2712 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002713 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002714 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002716 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002717 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002718 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002719 }
2720
2721 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002722 &attributes,
2723 slot->key.data, slot->key.bytes,
2724 alg,
2725 input, input_length,
2726 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002727
2728exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002729 /* In case of success, set the potential excess room in the output buffer
2730 * to an invalid value, to avoid potentially leaking a longer MAC.
2731 * In case of error, set the output length and content to a safe default,
2732 * such that in case the caller misses an error check, the output would be
2733 * an unachievable MAC.
2734 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002735 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002736 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002737 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002738 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002739 if (mac_size > operation_mac_size) {
2740 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2741 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002743 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002745 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002746}
2747
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002748psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002749 psa_algorithm_t alg,
2750 const uint8_t *input,
2751 size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002752 uint8_t *mac,
2753 size_t mac_size,
2754 size_t *mac_length)
2755{
2756 return psa_mac_compute_internal(key, alg,
2757 input, input_length,
2758 mac, mac_size, mac_length, 1);
2759}
2760
2761psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2762 psa_algorithm_t alg,
2763 const uint8_t *input,
2764 size_t input_length,
2765 const uint8_t *mac,
2766 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002767{
2768 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002769 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2770 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002772 status = psa_mac_compute_internal(key, alg,
2773 input, input_length,
2774 actual_mac, sizeof(actual_mac),
2775 &actual_mac_length, 0);
2776 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002777 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002778 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002780 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002781 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002782 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002783 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002784 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002785 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002786 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002787 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002788
2789exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002790 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002792 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002793}
Gilles Peskine20035e32018-02-03 22:44:14 +01002794
Gilles Peskine20035e32018-02-03 22:44:14 +01002795/****************************************************************/
2796/* Asymmetric cryptography */
2797/****************************************************************/
2798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002799static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2800 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002801{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002802 if (input_is_message) {
2803 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2804 return PSA_ERROR_INVALID_ARGUMENT;
2805 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002807 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2808 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2809 return PSA_ERROR_INVALID_ARGUMENT;
2810 }
2811 }
2812 } else {
2813 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2814 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002815 }
2816 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002818 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002819}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002821static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2822 int input_is_message,
2823 psa_algorithm_t alg,
2824 const uint8_t *input,
2825 size_t input_length,
2826 uint8_t *signature,
2827 size_t signature_size,
2828 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002829{
2830 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2831 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002832 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002833 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002834
2835 *signature_length = 0;
2836
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002837 status = psa_sign_verify_check_alg(input_is_message, alg);
2838 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002839 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002840 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002841
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002842 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002843 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002844 * buffer can in principle be empty since it doesn't actually have
2845 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002846 if (signature_size == 0) {
2847 return PSA_ERROR_BUFFER_TOO_SMALL;
2848 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002849
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002850 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002851 key, &slot,
2852 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2853 PSA_KEY_USAGE_SIGN_HASH,
2854 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002855
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002856 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002857 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002858 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002859
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002860 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002861 status = PSA_ERROR_INVALID_ARGUMENT;
2862 goto exit;
2863 }
2864
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002865 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002866 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002867 };
2868
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002869 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002870 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002871 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002872 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002873 signature, signature_size, signature_length);
2874 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002875
2876 status = psa_driver_wrapper_sign_hash(
2877 &attributes, slot->key.data, slot->key.bytes,
2878 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002879 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002880 }
2881
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002882
2883exit:
2884 /* Fill the unused part of the output buffer (the whole buffer on error,
2885 * the trailing part on success) with something that isn't a valid signature
2886 * (barring an attack on the signature and deliberately-crafted input),
2887 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002888 if (status == PSA_SUCCESS) {
2889 memset(signature + *signature_length, '!',
2890 signature_size - *signature_length);
2891 } else {
2892 memset(signature, '!', signature_size);
2893 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002894 /* If signature_size is 0 then we have nothing to do. We must not call
2895 * memset because signature may be NULL in this case. */
2896
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002897 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002899 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002900}
2901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002902static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2903 int input_is_message,
2904 psa_algorithm_t alg,
2905 const uint8_t *input,
2906 size_t input_length,
2907 const uint8_t *signature,
2908 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002909{
2910 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2911 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2912 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002913
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002914 status = psa_sign_verify_check_alg(input_is_message, alg);
2915 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002916 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002917 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002918
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002919 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002920 key, &slot,
2921 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2922 PSA_KEY_USAGE_VERIFY_HASH,
2923 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002924
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002925 if (status != PSA_SUCCESS) {
2926 return status;
2927 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002928
2929 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002930 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002931 };
2932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002933 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002934 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002935 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002936 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002937 signature, signature_length);
2938 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002939 status = psa_driver_wrapper_verify_hash(
2940 &attributes, slot->key.data, slot->key.bytes,
2941 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002942 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002943 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002944
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002945 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002947 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002948
2949}
2950
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002951psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002952 const psa_key_attributes_t *attributes,
2953 const uint8_t *key_buffer,
2954 size_t key_buffer_size,
2955 psa_algorithm_t alg,
2956 const uint8_t *input,
2957 size_t input_length,
2958 uint8_t *signature,
2959 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002960 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002961{
2962 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002964 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002965 size_t hash_length;
2966 uint8_t hash[PSA_HASH_MAX_SIZE];
2967
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002968 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002969 PSA_ALG_SIGN_GET_HASH(alg),
2970 input, input_length,
2971 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002972
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002973 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002974 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002975 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002976
2977 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002978 attributes, key_buffer, key_buffer_size,
2979 alg, hash, hash_length,
2980 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002981 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002982
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002983 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002984}
2985
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002986psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2987 psa_algorithm_t alg,
2988 const uint8_t *input,
2989 size_t input_length,
2990 uint8_t *signature,
2991 size_t signature_size,
2992 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002993{
2994 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002995 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002996 signature, signature_size, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002997}
2998
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002999psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003000 const psa_key_attributes_t *attributes,
3001 const uint8_t *key_buffer,
3002 size_t key_buffer_size,
3003 psa_algorithm_t alg,
3004 const uint8_t *input,
3005 size_t input_length,
3006 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003007 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003008{
3009 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003010
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003011 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003012 size_t hash_length;
3013 uint8_t hash[PSA_HASH_MAX_SIZE];
3014
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003015 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003016 PSA_ALG_SIGN_GET_HASH(alg),
3017 input, input_length,
3018 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003020 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003021 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003022 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003023
3024 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003025 attributes, key_buffer, key_buffer_size,
3026 alg, hash, hash_length,
3027 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003028 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003030 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003031}
3032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003033psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3034 psa_algorithm_t alg,
3035 const uint8_t *input,
3036 size_t input_length,
3037 const uint8_t *signature,
3038 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003039{
3040 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003041 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003042 signature, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003043}
3044
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003045psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003046 const psa_key_attributes_t *attributes,
3047 const uint8_t *key_buffer, size_t key_buffer_size,
3048 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003049 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01003050{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003051 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3052 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3053 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003054#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003055 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3056 return mbedtls_psa_rsa_sign_hash(
3057 attributes,
3058 key_buffer, key_buffer_size,
3059 alg, hash, hash_length,
3060 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003061#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3062 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003063 } else {
3064 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003065 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003066 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3067 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003068#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003069 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3070 return mbedtls_psa_ecdsa_sign_hash(
3071 attributes,
3072 key_buffer, key_buffer_size,
3073 alg, hash, hash_length,
3074 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003075#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3076 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003077 } else {
3078 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003079 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003080 }
Ronald Cron4501c982021-03-19 20:09:32 +01003081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082 (void) key_buffer;
3083 (void) key_buffer_size;
3084 (void) hash;
3085 (void) hash_length;
3086 (void) signature;
3087 (void) signature_size;
3088 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003090 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003091}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003093psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3094 psa_algorithm_t alg,
3095 const uint8_t *hash,
3096 size_t hash_length,
3097 uint8_t *signature,
3098 size_t signature_size,
3099 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003100{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003101 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003102 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003103 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003104}
3105
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003106psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003107 const psa_key_attributes_t *attributes,
3108 const uint8_t *key_buffer, size_t key_buffer_size,
3109 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003110 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003111{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003112 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3113 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3114 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003115#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003116 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3117 return mbedtls_psa_rsa_verify_hash(
3118 attributes,
3119 key_buffer, key_buffer_size,
3120 alg, hash, hash_length,
3121 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003122#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3123 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003124 } else {
3125 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003126 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003127 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3128 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003129#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003130 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3131 return mbedtls_psa_ecdsa_verify_hash(
3132 attributes,
3133 key_buffer, key_buffer_size,
3134 alg, hash, hash_length,
3135 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003136#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3137 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003138 } else {
3139 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003140 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003141 }
Ronald Cron4501c982021-03-19 20:09:32 +01003142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003143 (void) key_buffer;
3144 (void) key_buffer_size;
3145 (void) hash;
3146 (void) hash_length;
3147 (void) signature;
3148 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003150 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003151}
3152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003153psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3154 psa_algorithm_t alg,
3155 const uint8_t *hash,
3156 size_t hash_length,
3157 const uint8_t *signature,
3158 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003159{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003160 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003161 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003162 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003163}
3164
John Durkop0e005192020-10-31 22:06:54 -07003165#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003166static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3167 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003168{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003169 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3170 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3171 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3172 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003173}
John Durkop0e005192020-10-31 22:06:54 -07003174#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003176psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3177 psa_algorithm_t alg,
3178 const uint8_t *input,
3179 size_t input_length,
3180 const uint8_t *salt,
3181 size_t salt_length,
3182 uint8_t *output,
3183 size_t output_size,
3184 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003185{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003186 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003187 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003188 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003189
Darryl Green5cc689a2018-07-24 15:34:10 +01003190 (void) input;
3191 (void) input_length;
3192 (void) salt;
3193 (void) output;
3194 (void) output_size;
3195
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003196 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003198 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3199 return PSA_ERROR_INVALID_ARGUMENT;
3200 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003201
Ronald Cron5c522922020-11-14 16:35:34 +01003202 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003203 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3204 if (status != PSA_SUCCESS) {
3205 return status;
3206 }
3207 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3208 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003209 status = PSA_ERROR_INVALID_ARGUMENT;
3210 goto exit;
3211 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003213 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003214#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003215 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003216 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003217 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3218 slot->key.data,
3219 slot->key.bytes,
3220 &rsa);
3221 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003222 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003223 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003225 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003226 status = PSA_ERROR_BUFFER_TOO_SMALL;
3227 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003228 }
Ronald Cron7207d572021-09-08 14:28:35 +02003229#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3230 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003231 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003232#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003233 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003234 mbedtls_rsa_pkcs1_encrypt(rsa,
3235 mbedtls_psa_get_random,
3236 MBEDTLS_PSA_RANDOM_STATE,
3237 MBEDTLS_RSA_PUBLIC,
3238 input_length,
3239 input,
3240 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003241#else
3242 status = PSA_ERROR_NOT_SUPPORTED;
3243#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003244 } else
3245 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003246#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003247 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003248 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003249 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3250 mbedtls_psa_get_random,
3251 MBEDTLS_PSA_RANDOM_STATE,
3252 MBEDTLS_RSA_PUBLIC,
3253 salt, salt_length,
3254 input_length,
3255 input,
3256 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003257#else
3258 status = PSA_ERROR_NOT_SUPPORTED;
3259#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003260 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003261 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003262 }
Ronald Cron7207d572021-09-08 14:28:35 +02003263#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003264 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003265rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003266 if (status == PSA_SUCCESS) {
3267 *output_length = mbedtls_rsa_get_len(rsa);
3268 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003270 mbedtls_rsa_free(rsa);
3271 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003272#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003273 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003274 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003275 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003276 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003277
3278exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003279 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003281 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003282}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003284psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3285 psa_algorithm_t alg,
3286 const uint8_t *input,
3287 size_t input_length,
3288 const uint8_t *salt,
3289 size_t salt_length,
3290 uint8_t *output,
3291 size_t output_size,
3292 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003293{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003294 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003295 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003296 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003297
Darryl Green5cc689a2018-07-24 15:34:10 +01003298 (void) input;
3299 (void) input_length;
3300 (void) salt;
3301 (void) output;
3302 (void) output_size;
3303
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003304 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003306 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3307 return PSA_ERROR_INVALID_ARGUMENT;
3308 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003309
Ronald Cron5c522922020-11-14 16:35:34 +01003310 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003311 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3312 if (status != PSA_SUCCESS) {
3313 return status;
3314 }
3315 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003316 status = PSA_ERROR_INVALID_ARGUMENT;
3317 goto exit;
3318 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003320 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003321#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003322 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003323 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003324 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3325 slot->key.data,
3326 slot->key.bytes,
3327 &rsa);
3328 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003329 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003330 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003332 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003333 status = PSA_ERROR_INVALID_ARGUMENT;
3334 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003335 }
Ronald Cron7207d572021-09-08 14:28:35 +02003336#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3337 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003339 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003340#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003341 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003342 mbedtls_rsa_pkcs1_decrypt(rsa,
3343 mbedtls_psa_get_random,
3344 MBEDTLS_PSA_RANDOM_STATE,
3345 MBEDTLS_RSA_PRIVATE,
3346 output_length,
3347 input,
3348 output,
3349 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003350#else
3351 status = PSA_ERROR_NOT_SUPPORTED;
3352#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003353 } else
3354 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003355#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003356 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003357 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003358 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3359 mbedtls_psa_get_random,
3360 MBEDTLS_PSA_RANDOM_STATE,
3361 MBEDTLS_RSA_PRIVATE,
3362 salt, salt_length,
3363 output_length,
3364 input,
3365 output,
3366 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003367#else
3368 status = PSA_ERROR_NOT_SUPPORTED;
3369#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003370 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003371 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003372 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003373
Ronald Cron7207d572021-09-08 14:28:35 +02003374#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003375 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003376rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003377 mbedtls_rsa_free(rsa);
3378 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003379#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3380 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003381 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003382 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003383 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003384
3385exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003386 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003388 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003389}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003390
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003391
3392
mohammad1603503973b2018-03-12 15:59:30 +02003393/****************************************************************/
3394/* Symmetric cryptography */
3395/****************************************************************/
3396
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003397static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3398 mbedtls_svc_key_id_t key,
3399 psa_algorithm_t alg,
3400 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003401{
3402 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3403 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003404 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003405 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003406 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3407 PSA_KEY_USAGE_ENCRYPT :
3408 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003409
3410 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003411 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003412 status = PSA_ERROR_BAD_STATE;
3413 goto exit;
3414 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003415
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003416 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003417 status = PSA_ERROR_INVALID_ARGUMENT;
3418 goto exit;
3419 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003421 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3422 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003423 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003424 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003425
Ronald Cron49fafa92021-03-10 08:34:23 +01003426 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003427 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003428 * so we only set it (in the driver wrapper) after resources have been
3429 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003430 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003431 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003432 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003433 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003434 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003435 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003436 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003437 }
3438 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003439
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003440 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003441 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003442 };
3443
Ronald Cronab99ac22020-10-01 16:38:28 +02003444 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003445 if (cipher_operation == MBEDTLS_ENCRYPT) {
3446 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3447 &attributes,
3448 slot->key.data,
3449 slot->key.bytes,
3450 alg);
3451 } else {
3452 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3453 &attributes,
3454 slot->key.data,
3455 slot->key.bytes,
3456 alg);
3457 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003458
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003459exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003460 if (status != PSA_SUCCESS) {
3461 psa_cipher_abort(operation);
3462 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003464 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003466 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003467}
3468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003469psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3470 mbedtls_svc_key_id_t key,
3471 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003472{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003473 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003474}
3475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003476psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3477 mbedtls_svc_key_id_t key,
3478 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003479{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003480 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003481}
3482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003483psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3484 uint8_t *iv,
3485 size_t iv_size,
3486 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003487{
Ronald Cron6d051732020-10-01 14:10:20 +02003488 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003489 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergey04eb7c02023-03-06 15:37:23 -07003490 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003492 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003493 status = PSA_ERROR_BAD_STATE;
3494 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003495 }
3496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003497 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003498 status = PSA_ERROR_BAD_STATE;
3499 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003500 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003501
Ronald Cronc423acb2021-07-05 12:31:44 +02003502 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003503 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003504 status = PSA_ERROR_BUFFER_TOO_SMALL;
3505 goto exit;
3506 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003507
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003508 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003509 status = PSA_ERROR_GENERIC_ERROR;
3510 goto exit;
3511 }
3512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003513 status = psa_generate_random(local_iv, default_iv_length);
3514 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003515 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003516 }
Ronald Cron5618a392021-03-26 09:52:26 +01003517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003518 status = psa_driver_wrapper_cipher_set_iv(operation,
3519 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003520
3521exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003522 if (status == PSA_SUCCESS) {
3523 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003524 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003525 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003526 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003527 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003528 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003529 }
Ronald Cron6d051732020-10-01 14:10:20 +02003530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003531 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003532}
3533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003534psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3535 const uint8_t *iv,
3536 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003537{
Ronald Cron6d051732020-10-01 14:10:20 +02003538 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003540 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003541 status = PSA_ERROR_BAD_STATE;
3542 goto exit;
3543 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003545 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003546 status = PSA_ERROR_BAD_STATE;
3547 goto exit;
3548 }
Ronald Crona0d68172021-03-26 10:15:08 +01003549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003550 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003551 status = PSA_ERROR_INVALID_ARGUMENT;
3552 goto exit;
3553 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003555 status = psa_driver_wrapper_cipher_set_iv(operation,
3556 iv,
3557 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003558
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003559exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003560 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003561 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003562 } else {
3563 psa_cipher_abort(operation);
3564 }
3565 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003566}
3567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003568psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3569 const uint8_t *input,
3570 size_t input_length,
3571 uint8_t *output,
3572 size_t output_size,
3573 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003574{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003575 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003577 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003578 status = PSA_ERROR_BAD_STATE;
3579 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003580 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003582 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003583 status = PSA_ERROR_BAD_STATE;
3584 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003585 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003587 status = psa_driver_wrapper_cipher_update(operation,
3588 input,
3589 input_length,
3590 output,
3591 output_size,
3592 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003593
3594exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003595 if (status != PSA_SUCCESS) {
3596 psa_cipher_abort(operation);
3597 }
Ronald Cron6d051732020-10-01 14:10:20 +02003598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003599 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003600}
3601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003602psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3603 uint8_t *output,
3604 size_t output_size,
3605 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003606{
David Saadab4ecc272019-02-14 13:48:10 +02003607 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003609 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003610 status = PSA_ERROR_BAD_STATE;
3611 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003612 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003614 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003615 status = PSA_ERROR_BAD_STATE;
3616 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003617 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003619 status = psa_driver_wrapper_cipher_finish(operation,
3620 output,
3621 output_size,
3622 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003623
3624exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003625 if (status == PSA_SUCCESS) {
3626 return psa_cipher_abort(operation);
3627 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003628 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003629 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003631 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003632 }
mohammad1603503973b2018-03-12 15:59:30 +02003633}
3634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003635psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003636{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003637 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003638 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003639 * in use. It's ok to call abort on such an object, and there's
3640 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003641 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003642 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003644 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003645
Ronald Cron49fafa92021-03-10 08:34:23 +01003646 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003647 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003648 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003650 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003651}
3652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003653psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3654 psa_algorithm_t alg,
David Horstmann926193a2023-12-13 15:55:25 +00003655 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003656 size_t input_length,
David Horstmann926193a2023-12-13 15:55:25 +00003657 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003658 size_t output_size,
3659 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003660{
3661 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003662 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003663 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003664 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003665 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3666 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003667
David Horstmann926193a2023-12-13 15:55:25 +00003668 LOCAL_INPUT_DECLARE(input_external);
3669 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3670 LOCAL_OUTPUT_DECLARE(output_external);
3671 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003673 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003674 status = PSA_ERROR_INVALID_ARGUMENT;
3675 goto exit;
3676 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003678 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3679 PSA_KEY_USAGE_ENCRYPT,
3680 alg);
3681 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003682 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003683 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003684
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003685 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003686 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003687 };
3688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003689 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3690 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003691 status = PSA_ERROR_GENERIC_ERROR;
3692 goto exit;
3693 }
3694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003695 if (default_iv_length > 0) {
3696 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003697 status = PSA_ERROR_BUFFER_TOO_SMALL;
3698 goto exit;
3699 }
3700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003701 status = psa_generate_random(local_iv, default_iv_length);
3702 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003703 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003704 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003705 }
3706
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003707 status = psa_driver_wrapper_cipher_encrypt(
3708 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003709 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003710 mbedtls_buffer_offset(output, default_iv_length),
3711 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003712
3713exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714 unlock_status = psa_unlock_key_slot(slot);
3715 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003716 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003717 }
Ronald Cron16377072021-07-08 19:00:07 +02003718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003719 if (status == PSA_SUCCESS) {
3720 if (default_iv_length > 0) {
3721 memcpy(output, local_iv, default_iv_length);
3722 }
3723 *output_length += default_iv_length;
3724 } else {
3725 *output_length = 0;
3726 }
3727
David Horstmann926193a2023-12-13 15:55:25 +00003728 LOCAL_INPUT_FREE(input, input_external);
3729 LOCAL_OUTPUT_FREE(output, output_external);
David Horstmannd2ad8862023-11-27 17:43:05 +00003730
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003731 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003732}
3733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003734psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3735 psa_algorithm_t alg,
3736 const uint8_t *input,
3737 size_t input_length,
3738 uint8_t *output,
3739 size_t output_size,
3740 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003741{
3742 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003743 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003744 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003745 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003746
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003747 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003748 status = PSA_ERROR_INVALID_ARGUMENT;
3749 goto exit;
3750 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003752 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3753 PSA_KEY_USAGE_DECRYPT,
3754 alg);
3755 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003756 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003757 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003758
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003759 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003760 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003761 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003762
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003763 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003764 status = PSA_ERROR_INVALID_ARGUMENT;
3765 goto exit;
3766 }
3767
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003768 status = psa_driver_wrapper_cipher_decrypt(
3769 &attributes, slot->key.data, slot->key.bytes,
3770 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003771 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003772
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003773exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003774 unlock_status = psa_unlock_key_slot(slot);
3775 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003776 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003777 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003779 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003780 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003781 }
Ronald Cron16377072021-07-08 19:00:07 +02003782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003783 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003784}
3785
3786
mohammad16035955c982018-04-26 00:53:03 +03003787/****************************************************************/
3788/* AEAD */
3789/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003790
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003791psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3792 psa_algorithm_t alg,
3793 const uint8_t *nonce,
3794 size_t nonce_length,
3795 const uint8_t *additional_data,
3796 size_t additional_data_length,
3797 const uint8_t *plaintext,
3798 size_t plaintext_length,
3799 uint8_t *ciphertext,
3800 size_t ciphertext_size,
3801 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003802{
Ronald Cron215633c2021-03-16 17:15:37 +01003803 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3804 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003805
mohammad1603f08a5502018-06-03 15:05:47 +03003806 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003807
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003808 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3809 return PSA_ERROR_NOT_SUPPORTED;
3810 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003811
Ronald Cron9a986162021-03-26 12:40:07 +01003812 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003813 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3814 if (status != PSA_SUCCESS) {
3815 return status;
3816 }
mohammad16035955c982018-04-26 00:53:03 +03003817
Ronald Cron215633c2021-03-16 17:15:37 +01003818 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003819 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003820 };
mohammad16035955c982018-04-26 00:53:03 +03003821
Ronald Cronde822812021-03-17 16:08:20 +01003822 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003823 &attributes, slot->key.data, slot->key.bytes,
3824 alg,
3825 nonce, nonce_length,
3826 additional_data, additional_data_length,
3827 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003828 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003830 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3831 memset(ciphertext, 0, ciphertext_size);
3832 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003834 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003836 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003837}
3838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003839psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3840 psa_algorithm_t alg,
3841 const uint8_t *nonce,
3842 size_t nonce_length,
3843 const uint8_t *additional_data,
3844 size_t additional_data_length,
3845 const uint8_t *ciphertext,
3846 size_t ciphertext_length,
3847 uint8_t *plaintext,
3848 size_t plaintext_size,
3849 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003850{
Ronald Cron215633c2021-03-16 17:15:37 +01003851 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3852 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003853
Gilles Peskineee652a32018-06-01 19:23:52 +02003854 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003855
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003856 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3857 return PSA_ERROR_NOT_SUPPORTED;
3858 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003859
Ronald Cron9a986162021-03-26 12:40:07 +01003860 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003861 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3862 if (status != PSA_SUCCESS) {
3863 return status;
3864 }
mohammad16035955c982018-04-26 00:53:03 +03003865
Ronald Cron215633c2021-03-16 17:15:37 +01003866 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003867 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003868 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003869
Ronald Cronde822812021-03-17 16:08:20 +01003870 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003871 &attributes, slot->key.data, slot->key.bytes,
3872 alg,
3873 nonce, nonce_length,
3874 additional_data, additional_data_length,
3875 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003876 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003878 if (status != PSA_SUCCESS && plaintext_size != 0) {
3879 memset(plaintext, 0, plaintext_size);
3880 }
mohammad160360a64d02018-06-03 17:20:42 +03003881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003882 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003884 return status;
mohammad16035955c982018-04-26 00:53:03 +03003885}
3886
Gilles Peskinee59236f2018-01-27 23:32:46 +01003887/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003888/* Generators */
3889/****************************************************************/
3890
John Durkop07cc04a2020-11-16 22:08:34 -08003891#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3892 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3893 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3894#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003895#endif /* At least one builtin KDF */
3896
3897#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3898 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3899 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3900static psa_status_t psa_key_derivation_start_hmac(
3901 psa_mac_operation_t *operation,
3902 psa_algorithm_t hash_alg,
3903 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003904 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02003905{
3906 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003908 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3909 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
3910 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02003911
Steven Cooreman15f0d922021-05-07 14:14:37 +02003912 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003913 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02003914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003915 status = psa_driver_wrapper_mac_sign_setup(operation,
3916 &attributes,
3917 hmac_key, hmac_key_length,
3918 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02003919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003920 psa_reset_key_attributes(&attributes);
3921 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02003922}
3923#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003924
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003925#define HKDF_STATE_INIT 0 /* no input yet */
3926#define HKDF_STATE_STARTED 1 /* got salt */
3927#define HKDF_STATE_KEYED 2 /* got key */
3928#define HKDF_STATE_OUTPUT 3 /* output started */
3929
Gilles Peskinecbe66502019-05-16 16:59:18 +02003930static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003931 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003932{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003933 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
3934 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
3935 } else {
3936 return operation->alg;
3937 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003938}
3939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003940psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003941{
3942 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003943 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
3944 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02003945 /* The object has (apparently) been initialized but it is not
3946 * in use. It's ok to call abort on such an object, and there's
3947 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003948 } else
John Durkopd0321952020-10-29 21:37:36 -07003949#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003950 if (PSA_ALG_IS_HKDF(kdf_alg)) {
3951 mbedtls_free(operation->ctx.hkdf.info);
3952 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
3953 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003954#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3955#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3956 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003957 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
3958 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
3959 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
3960 if (operation->ctx.tls12_prf.secret != NULL) {
3961 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
3962 operation->ctx.tls12_prf.secret_length);
3963 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003964 }
3965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003966 if (operation->ctx.tls12_prf.seed != NULL) {
3967 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
3968 operation->ctx.tls12_prf.seed_length);
3969 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01003970 }
3971
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003972 if (operation->ctx.tls12_prf.label != NULL) {
3973 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
3974 operation->ctx.tls12_prf.label_length);
3975 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01003976 }
3977
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003978 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003979
3980 /* We leave the fields Ai and output_block to be erased safely by the
3981 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003982 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003983#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3984 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003985 {
3986 status = PSA_ERROR_BAD_STATE;
3987 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003988 mbedtls_platform_zeroize(operation, sizeof(*operation));
3989 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003990}
3991
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003992psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003993 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003994{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003995 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003996 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003997 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003998 }
3999
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004000 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004001 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004002}
4003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004004psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4005 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004006{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004007 if (operation->alg == 0) {
4008 return PSA_ERROR_BAD_STATE;
4009 }
4010 if (capacity > operation->capacity) {
4011 return PSA_ERROR_INVALID_ARGUMENT;
4012 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004013 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004014 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004015}
4016
John Durkopd0321952020-10-29 21:37:36 -07004017#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004018/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004019 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004020static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
4021 psa_algorithm_t hash_alg,
4022 uint8_t *output,
4023 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004024{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004025 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02004026 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004027 psa_status_t status;
4028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004029 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
4030 return PSA_ERROR_BAD_STATE;
4031 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004032 hkdf->state = HKDF_STATE_OUTPUT;
4033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004034 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004035 /* Copy what remains of the current block */
4036 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004037 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004038 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004039 }
4040 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004041 output += n;
4042 output_length -= n;
4043 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004044 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004045 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004046 }
Gilles Peskined54931c2018-07-17 21:06:59 +02004047 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004048 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004049 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004050 * object was corrupted or if this function is called directly
4051 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004052 if (hkdf->block_number == 0xff) {
4053 return PSA_ERROR_BAD_STATE;
4054 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004055
4056 /* We need a new block */
4057 ++hkdf->block_number;
4058 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004060 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4061 hash_alg,
4062 hkdf->prk,
4063 hash_length);
4064 if (status != PSA_SUCCESS) {
4065 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004066 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004067
4068 if (hkdf->block_number != 1) {
4069 status = psa_mac_update(&hkdf->hmac,
4070 hkdf->output_block,
4071 hash_length);
4072 if (status != PSA_SUCCESS) {
4073 return status;
4074 }
4075 }
4076 status = psa_mac_update(&hkdf->hmac,
4077 hkdf->info,
4078 hkdf->info_length);
4079 if (status != PSA_SUCCESS) {
4080 return status;
4081 }
4082 status = psa_mac_update(&hkdf->hmac,
4083 &hkdf->block_number, 1);
4084 if (status != PSA_SUCCESS) {
4085 return status;
4086 }
4087 status = psa_mac_sign_finish(&hkdf->hmac,
4088 hkdf->output_block,
4089 sizeof(hkdf->output_block),
4090 &hmac_output_length);
4091 if (status != PSA_SUCCESS) {
4092 return status;
4093 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004094 }
4095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004096 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004097}
John Durkop07cc04a2020-11-16 22:08:34 -08004098#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004099
John Durkop07cc04a2020-11-16 22:08:34 -08004100#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4101 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004102static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4103 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004104 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004105{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004106 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4107 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004108 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4109 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004110 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004111
Janos Follath7742fee2019-06-17 12:58:10 +01004112 /* We can't be wanting more output after block 0xff, otherwise
4113 * the capacity check in psa_key_derivation_output_bytes() would have
4114 * prevented this call. It could happen only if the operation
4115 * object was corrupted or if this function is called directly
4116 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004117 if (tls12_prf->block_number == 0xff) {
4118 return PSA_ERROR_CORRUPTION_DETECTED;
4119 }
Janos Follath7742fee2019-06-17 12:58:10 +01004120
4121 /* We need a new block */
4122 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004123 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004124
4125 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4126 *
4127 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4128 *
4129 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4130 * HMAC_hash(secret, A(2) + seed) +
4131 * HMAC_hash(secret, A(3) + seed) + ...
4132 *
4133 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004134 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004135 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004136 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004137 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004138 * is currently extracted as `output_block` and where i is
4139 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004140 */
4141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004142 status = psa_key_derivation_start_hmac(&hmac,
4143 hash_alg,
4144 tls12_prf->secret,
4145 tls12_prf->secret_length);
4146 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004147 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004148 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004149
4150 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004151 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004152 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4153 * the variable seed and in this instance means it in the context of the
4154 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004155 status = psa_mac_update(&hmac,
4156 tls12_prf->label,
4157 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,
4162 tls12_prf->seed,
4163 tls12_prf->seed_length);
4164 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004165 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004166 }
4167 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004168 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004169 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4170 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004171 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004172 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004173 }
4174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004175 status = psa_mac_sign_finish(&hmac,
4176 tls12_prf->Ai, hash_length,
4177 &hmac_output_length);
4178 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004179 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004180 }
4181 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004182 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004183 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004184
4185 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004186 status = psa_key_derivation_start_hmac(&hmac,
4187 hash_alg,
4188 tls12_prf->secret,
4189 tls12_prf->secret_length);
4190 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004191 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004192 }
4193 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4194 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004195 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004196 }
4197 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4198 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004199 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004200 }
4201 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4202 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004203 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004204 }
4205 status = psa_mac_sign_finish(&hmac,
4206 tls12_prf->output_block, hash_length,
4207 &hmac_output_length);
4208 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004209 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004210 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004211
Janos Follath7742fee2019-06-17 12:58:10 +01004212
4213cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004214 cleanup_status = psa_mac_abort(&hmac);
4215 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004216 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004217 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004219 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004220}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004221
Janos Follath844eb0e2019-06-19 12:10:49 +01004222static psa_status_t psa_key_derivation_tls12_prf_read(
4223 psa_tls12_prf_key_derivation_t *tls12_prf,
4224 psa_algorithm_t alg,
4225 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004226 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004227{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004228 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4229 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004230 psa_status_t status;
4231 uint8_t offset, length;
4232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004233 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004234 case PSA_TLS12_PRF_STATE_LABEL_SET:
4235 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4236 break;
4237 case PSA_TLS12_PRF_STATE_OUTPUT:
4238 break;
4239 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004240 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004241 }
4242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004243 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004244 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004245 if (tls12_prf->left_in_block == 0) {
4246 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4247 alg);
4248 if (status != PSA_SUCCESS) {
4249 return status;
4250 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004251
4252 continue;
4253 }
4254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004255 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004256 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004257 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004258 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004259 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004260
4261 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004262 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004263 output += length;
4264 output_length -= length;
4265 tls12_prf->left_in_block -= length;
4266 }
4267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004268 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004269}
John Durkop07cc04a2020-11-16 22:08:34 -08004270#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4271 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004272
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004273psa_status_t psa_key_derivation_output_bytes(
4274 psa_key_derivation_operation_t *operation,
4275 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004276 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004277{
4278 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004279 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004281 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004282 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004283 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004284 }
4285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004286 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004287 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004288 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004289 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004290 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004291 goto exit;
4292 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004293 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004294 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004295 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004296 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4297 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004298 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004299 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004300 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004301 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004302 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004303
John Durkopd0321952020-10-29 21:37:36 -07004304#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004305 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4306 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4307 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4308 output, output_length);
4309 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004310#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4311#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4312 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004313 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4314 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4315 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4316 kdf_alg, output,
4317 output_length);
4318 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004319#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4320 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004321 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004322 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004323 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004324 }
4325
4326exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004327 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004328 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004329 * This allows us to differentiate between exhausted operations and
4330 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4331 * operations. */
4332 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004333 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004334 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004335 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004336 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004337 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004338}
4339
David Brown7807bf72021-02-09 16:28:23 -07004340#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004341static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004342{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004343 if (data_size >= 8) {
4344 mbedtls_des_key_set_parity(data);
4345 }
4346 if (data_size >= 16) {
4347 mbedtls_des_key_set_parity(data + 8);
4348 }
4349 if (data_size >= 24) {
4350 mbedtls_des_key_set_parity(data + 16);
4351 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004352}
David Brown7807bf72021-02-09 16:28:23 -07004353#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004354
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004355static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004356 psa_key_slot_t *slot,
4357 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004358 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004359{
4360 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004361 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004362 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004363 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004365 if (!key_type_is_raw_bytes(slot->attr.type)) {
4366 return PSA_ERROR_INVALID_ARGUMENT;
4367 }
4368 if (bits % 8 != 0) {
4369 return PSA_ERROR_INVALID_ARGUMENT;
4370 }
4371 data = mbedtls_calloc(1, bytes);
4372 if (data == NULL) {
4373 return PSA_ERROR_INSUFFICIENT_MEMORY;
4374 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004376 status = psa_key_derivation_output_bytes(operation, data, bytes);
4377 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004378 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004379 }
David Brown12ca5032021-02-11 11:02:00 -07004380#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004381 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4382 psa_des_set_key_parity(data, bytes);
4383 }
David Brown8107e312021-02-12 08:08:46 -07004384#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004386 status = psa_allocate_buffer_to_slot(slot, bytes);
4387 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004388 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004389 }
Ronald Crondd04d422020-11-28 15:14:42 +01004390
Ronald Cronbf33c932020-11-28 18:06:53 +01004391 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004392 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004393 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004394 };
4395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004396 status = psa_driver_wrapper_import_key(&attributes,
4397 data, bytes,
4398 slot->key.data,
4399 slot->key.bytes,
4400 &slot->key.bytes, &bits);
4401 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004402 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004403 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004404
4405exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004406 mbedtls_free(data);
4407 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004408}
4409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004410psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4411 psa_key_derivation_operation_t *operation,
4412 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004413{
4414 psa_status_t status;
4415 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004416 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004417
Ronald Cron81709fc2020-11-14 12:10:32 +01004418 *key = MBEDTLS_SVC_KEY_ID_INIT;
4419
Gilles Peskine0f84d622019-09-12 19:03:13 +02004420 /* Reject any attempt to create a zero-length key so that we don't
4421 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004422 if (psa_get_key_bits(attributes) == 0) {
4423 return PSA_ERROR_INVALID_ARGUMENT;
4424 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004426 if (operation->alg == PSA_ALG_NONE) {
4427 return PSA_ERROR_BAD_STATE;
4428 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004430 if (!operation->can_output_key) {
4431 return PSA_ERROR_NOT_PERMITTED;
4432 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004434 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4435 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004436#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004437 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004438 /* Deriving a key in a secure element is not implemented yet. */
4439 status = PSA_ERROR_NOT_SUPPORTED;
4440 }
4441#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004442 if (status == PSA_SUCCESS) {
4443 status = psa_generate_derived_key_internal(slot,
4444 attributes->core.bits,
4445 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004446 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004447 if (status == PSA_SUCCESS) {
4448 status = psa_finish_key_creation(slot, driver, key);
4449 }
4450 if (status != PSA_SUCCESS) {
4451 psa_fail_key_creation(slot, driver);
4452 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004454 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004455}
4456
Gilles Peskineeab56e42018-07-12 17:12:33 +02004457
4458
4459/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004460/* Key derivation */
4461/****************************************************************/
4462
Steven Cooreman932ffb72021-02-15 12:14:32 +01004463#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004464static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004465{
4466#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004467 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4468 return 1;
4469 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004470#endif
4471#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004472 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4473 return 1;
4474 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004475#endif
4476#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004477 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4478 return 1;
4479 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004480#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004481 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004482}
4483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004484static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004485{
4486 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004487 psa_status_t status = psa_hash_setup(&operation, alg);
4488 psa_hash_abort(&operation);
4489 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004490}
4491
Gilles Peskine969c5d62019-01-16 15:53:06 +01004492static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004493 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004494 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004495{
Janos Follath5fe19732019-06-20 15:09:30 +01004496 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4497 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004498 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004499
Gilles Peskine969c5d62019-01-16 15:53:06 +01004500 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004501 if (!is_kdf_alg_supported(kdf_alg)) {
4502 return PSA_ERROR_NOT_SUPPORTED;
4503 }
John Durkop07cc04a2020-11-16 22:08:34 -08004504
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004505 /* All currently supported key derivation algorithms are based on a
4506 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004507 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4508 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4509 if (hash_size == 0) {
4510 return PSA_ERROR_NOT_SUPPORTED;
4511 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004512
4513 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4514 * we might fail later, which is somewhat unfriendly and potentially
4515 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004516 psa_status_t status = psa_hash_try_support(hash_alg);
4517 if (status != PSA_SUCCESS) {
4518 return status;
4519 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004521 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4522 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4523 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4524 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004525 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004526
Gilles Peskinecdacf042021-04-29 21:10:00 +02004527 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004528 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004529}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004531static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004532{
4533#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004534 if (alg == PSA_ALG_ECDH) {
4535 return PSA_SUCCESS;
4536 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004537#endif
4538 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004539 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004540}
John Durkop07cc04a2020-11-16 22:08:34 -08004541#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004542
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004543psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4544 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004545{
4546 psa_status_t status;
4547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004548 if (operation->alg != 0) {
4549 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004550 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004552 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4553 return PSA_ERROR_INVALID_ARGUMENT;
4554 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4555#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4556 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4557 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4558 status = psa_key_agreement_try_support(ka_alg);
4559 if (status != PSA_SUCCESS) {
4560 return status;
4561 }
4562 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4563#else
4564 return PSA_ERROR_NOT_SUPPORTED;
4565#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4566 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4567#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4568 status = psa_key_derivation_setup_kdf(operation, alg);
4569#else
4570 return PSA_ERROR_NOT_SUPPORTED;
4571#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4572 } else {
4573 return PSA_ERROR_INVALID_ARGUMENT;
4574 }
4575
4576 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004577 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004578 }
4579 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004580}
4581
John Durkopd0321952020-10-29 21:37:36 -07004582#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004583static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4584 psa_algorithm_t hash_alg,
4585 psa_key_derivation_step_t step,
4586 const uint8_t *data,
4587 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004588{
4589 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004590 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004591 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004592 if (hkdf->state != HKDF_STATE_INIT) {
4593 return PSA_ERROR_BAD_STATE;
4594 } else {
4595 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4596 hash_alg,
4597 data, data_length);
4598 if (status != PSA_SUCCESS) {
4599 return status;
4600 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004601 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004602 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004603 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004604 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004605 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004606 if (hkdf->state == HKDF_STATE_INIT) {
4607 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4608 hash_alg,
4609 NULL, 0);
4610 if (status != PSA_SUCCESS) {
4611 return status;
4612 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004613 hkdf->state = HKDF_STATE_STARTED;
4614 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004615 if (hkdf->state != HKDF_STATE_STARTED) {
4616 return PSA_ERROR_BAD_STATE;
4617 }
4618 status = psa_mac_update(&hkdf->hmac,
4619 data, data_length);
4620 if (status != PSA_SUCCESS) {
4621 return status;
4622 }
4623 status = psa_mac_sign_finish(&hkdf->hmac,
4624 hkdf->prk,
4625 sizeof(hkdf->prk),
4626 &data_length);
4627 if (status != PSA_SUCCESS) {
4628 return status;
4629 }
4630 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004631 hkdf->block_number = 0;
4632 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004633 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004634 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004635 if (hkdf->state == HKDF_STATE_OUTPUT) {
4636 return PSA_ERROR_BAD_STATE;
4637 }
4638 if (hkdf->info_set) {
4639 return PSA_ERROR_BAD_STATE;
4640 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004641 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004642 if (data_length != 0) {
4643 hkdf->info = mbedtls_calloc(1, data_length);
4644 if (hkdf->info == NULL) {
4645 return PSA_ERROR_INSUFFICIENT_MEMORY;
4646 }
4647 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004648 }
4649 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004650 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004651 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004652 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004653 }
4654}
John Durkop07cc04a2020-11-16 22:08:34 -08004655#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004656
John Durkop07cc04a2020-11-16 22:08:34 -08004657#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4658 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004659static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4660 const uint8_t *data,
4661 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004662{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004663 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4664 return PSA_ERROR_BAD_STATE;
4665 }
Janos Follathf08e2652019-06-13 09:05:41 +01004666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004667 if (data_length != 0) {
4668 prf->seed = mbedtls_calloc(1, data_length);
4669 if (prf->seed == NULL) {
4670 return PSA_ERROR_INSUFFICIENT_MEMORY;
4671 }
Janos Follathf08e2652019-06-13 09:05:41 +01004672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004673 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004674 prf->seed_length = data_length;
4675 }
Janos Follathf08e2652019-06-13 09:05:41 +01004676
Gilles Peskine43f958b2020-12-13 14:55:14 +01004677 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004679 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004680}
4681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004682static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4683 const uint8_t *data,
4684 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004685{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004686 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4687 return PSA_ERROR_BAD_STATE;
4688 }
Janos Follath81550542019-06-13 14:26:34 +01004689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004690 if (data_length != 0) {
4691 prf->secret = mbedtls_calloc(1, data_length);
4692 if (prf->secret == NULL) {
4693 return PSA_ERROR_INSUFFICIENT_MEMORY;
4694 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004696 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004697 prf->secret_length = data_length;
4698 }
Janos Follath81550542019-06-13 14:26:34 +01004699
Gilles Peskine43f958b2020-12-13 14:55:14 +01004700 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004702 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004703}
4704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004705static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4706 const uint8_t *data,
4707 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004708{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004709 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4710 return PSA_ERROR_BAD_STATE;
4711 }
Janos Follath63028dd2019-06-13 09:15:47 +01004712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004713 if (data_length != 0) {
4714 prf->label = mbedtls_calloc(1, data_length);
4715 if (prf->label == NULL) {
4716 return PSA_ERROR_INSUFFICIENT_MEMORY;
4717 }
Janos Follath63028dd2019-06-13 09:15:47 +01004718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004719 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004720 prf->label_length = data_length;
4721 }
Janos Follath63028dd2019-06-13 09:15:47 +01004722
Gilles Peskine43f958b2020-12-13 14:55:14 +01004723 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004725 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01004726}
4727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004728static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
4729 psa_key_derivation_step_t step,
4730 const uint8_t *data,
4731 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01004732{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004733 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01004734 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004735 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01004736 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004737 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01004738 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004739 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01004740 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004741 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01004742 }
4743}
John Durkop07cc04a2020-11-16 22:08:34 -08004744#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4745 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4746
4747#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4748static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4749 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004750 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004751 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08004752{
4753 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004754 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08004755 uint8_t *cur = pms;
4756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004757 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
4758 return PSA_ERROR_INVALID_ARGUMENT;
4759 }
John Durkop07cc04a2020-11-16 22:08:34 -08004760
4761 /* Quoting RFC 4279, Section 2:
4762 *
4763 * The premaster secret is formed as follows: if the PSK is N octets
4764 * long, concatenate a uint16 with the value N, N zero octets, a second
4765 * uint16 with the value N, and the PSK itself.
4766 */
4767
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004768 *cur++ = MBEDTLS_BYTE_1(data_length);
4769 *cur++ = MBEDTLS_BYTE_0(data_length);
4770 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004771 cur += data_length;
4772 *cur++ = pms[0];
4773 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004774 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004775 cur += data_length;
4776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004777 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08004778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004779 mbedtls_platform_zeroize(pms, sizeof(pms));
4780 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08004781}
Janos Follath6660f0e2019-06-17 08:44:03 +01004782
4783static psa_status_t psa_tls12_prf_psk_to_ms_input(
4784 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004785 psa_key_derivation_step_t step,
4786 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004787 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01004788{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004789 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
4790 return psa_tls12_prf_psk_to_ms_set_key(prf,
4791 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01004792 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004794 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01004795}
John Durkop07cc04a2020-11-16 22:08:34 -08004796#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004797
Gilles Peskineb8965192019-09-24 16:21:10 +02004798/** Check whether the given key type is acceptable for the given
4799 * input step of a key derivation.
4800 *
4801 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4802 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4803 * Both secret and non-secret inputs can alternatively have the type
4804 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4805 * that the input was passed as a buffer rather than via a key object.
4806 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004807static int psa_key_derivation_check_input_type(
4808 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004809 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02004810{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004811 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004812 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004813 if (key_type == PSA_KEY_TYPE_DERIVE) {
4814 return PSA_SUCCESS;
4815 }
4816 if (key_type == PSA_KEY_TYPE_NONE) {
4817 return PSA_SUCCESS;
4818 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004819 break;
4820 case PSA_KEY_DERIVATION_INPUT_LABEL:
4821 case PSA_KEY_DERIVATION_INPUT_SALT:
4822 case PSA_KEY_DERIVATION_INPUT_INFO:
4823 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004824 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
4825 return PSA_SUCCESS;
4826 }
4827 if (key_type == PSA_KEY_TYPE_NONE) {
4828 return PSA_SUCCESS;
4829 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004830 break;
4831 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004832 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02004833}
4834
Janos Follathb80a94e2019-06-12 15:54:46 +01004835static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004836 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004837 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004838 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004839 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004840 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004841{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004842 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004843 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004845 status = psa_key_derivation_check_input_type(step, key_type);
4846 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004847 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004848 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004849
John Durkopd0321952020-10-29 21:37:36 -07004850#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004851 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4852 status = psa_hkdf_input(&operation->ctx.hkdf,
4853 PSA_ALG_HKDF_GET_HASH(kdf_alg),
4854 step, data, data_length);
4855 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004856#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4857#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004858 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4859 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
4860 step, data, data_length);
4861 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004862#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4863#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004864 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4865 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
4866 step, data, data_length);
4867 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004868#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004869 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004870 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004871 (void) data;
4872 (void) data_length;
4873 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004874 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004875 }
4876
Gilles Peskine224b0d62019-09-23 18:13:17 +02004877exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004878 if (status != PSA_SUCCESS) {
4879 psa_key_derivation_abort(operation);
4880 }
4881 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004882}
4883
Janos Follath51f4a0f2019-06-14 11:35:55 +01004884psa_status_t psa_key_derivation_input_bytes(
4885 psa_key_derivation_operation_t *operation,
4886 psa_key_derivation_step_t step,
4887 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004888 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004889{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004890 return psa_key_derivation_input_internal(operation, step,
4891 PSA_KEY_TYPE_NONE,
4892 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004893}
4894
Janos Follath51f4a0f2019-06-14 11:35:55 +01004895psa_status_t psa_key_derivation_input_key(
4896 psa_key_derivation_operation_t *operation,
4897 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004898 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004899{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004900 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004901 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004902 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004903
Ronald Cron5c522922020-11-14 16:35:34 +01004904 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004905 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4906 if (status != PSA_SUCCESS) {
4907 psa_key_derivation_abort(operation);
4908 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02004909 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004910
4911 /* Passing a key object as a SECRET input unlocks the permission
4912 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004913 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004914 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004915 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004916
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004917 status = psa_key_derivation_input_internal(operation,
4918 step, slot->attr.type,
4919 slot->key.data,
4920 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004922 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004924 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004925}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004926
4927
4928
4929/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004930/* Key agreement */
4931/****************************************************************/
4932
John Durkop6ba40d12020-11-10 08:50:04 -08004933#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004934static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
4935 size_t peer_key_length,
4936 const mbedtls_ecp_keypair *our_key,
4937 uint8_t *shared_secret,
4938 size_t shared_secret_size,
4939 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004940{
Steven Cooremand4867872020-08-05 16:31:39 +02004941 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004942 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004943 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004944 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004945 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
4946 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004947
Ronald Cron90857082020-11-25 14:58:33 +01004948 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004949 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
4950 bits,
4951 peer_key,
4952 peer_key_length,
4953 &their_key);
4954 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004955 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004956 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004957
Jaeden Amero97271b32019-01-10 19:38:51 +00004958 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004959 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
4960 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004961 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004962 }
Jaeden Amero97271b32019-01-10 19:38:51 +00004963 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004964 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
4965 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004966 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004967 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004968
Jaeden Amero97271b32019-01-10 19:38:51 +00004969 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004970 mbedtls_ecdh_calc_secret(&ecdh,
4971 shared_secret_length,
4972 shared_secret, shared_secret_size,
4973 mbedtls_psa_get_random,
4974 MBEDTLS_PSA_RANDOM_STATE));
4975 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004976 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004977 }
4978 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004979 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004980 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004981
4982exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004983 if (status != PSA_SUCCESS) {
4984 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
4985 }
4986 mbedtls_ecdh_free(&ecdh);
4987 mbedtls_ecp_keypair_free(their_key);
4988 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02004989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004990 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004991}
John Durkop6ba40d12020-11-10 08:50:04 -08004992#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004993
Gilles Peskine01d718c2018-09-18 12:01:02 +02004994#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004996static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
4997 psa_key_slot_t *private_key,
4998 const uint8_t *peer_key,
4999 size_t peer_key_length,
5000 uint8_t *shared_secret,
5001 size_t shared_secret_size,
5002 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005003{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005004 mbedtls_ecp_keypair *ecp = NULL;
5005 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01005006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005007 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08005008#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005009 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005010 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
5011 return PSA_ERROR_INVALID_ARGUMENT;
5012 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005013 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005014 private_key->attr.type,
5015 private_key->attr.bits,
5016 private_key->key.data,
5017 private_key->key.bytes,
5018 &ecp);
5019 if (status != PSA_SUCCESS) {
5020 return status;
5021 }
5022 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
5023 ecp,
5024 shared_secret, shared_secret_size,
5025 shared_secret_length);
5026 mbedtls_ecp_keypair_free(ecp);
5027 mbedtls_free(ecp);
5028 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08005029#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005030 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005031 (void) ecp;
5032 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01005033 (void) private_key;
5034 (void) peer_key;
5035 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005036 (void) shared_secret;
5037 (void) shared_secret_size;
5038 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005039 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005040 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005041}
5042
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005043/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005044 * to potentially free embedded data structures and wipe confidential data.
5045 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005046static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5047 psa_key_derivation_step_t step,
5048 psa_key_slot_t *private_key,
5049 const uint8_t *peer_key,
5050 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005051{
5052 psa_status_t status;
5053 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5054 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005055 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005056
5057 /* Step 1: run the secret agreement algorithm to generate the shared
5058 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005059 status = psa_key_agreement_raw_internal(ka_alg,
5060 private_key,
5061 peer_key, peer_key_length,
5062 shared_secret,
5063 sizeof(shared_secret),
5064 &shared_secret_length);
5065 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005066 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005067 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005068
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005069 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005070 * the shared secret. A shared secret is permitted wherever a key
5071 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005072 status = psa_key_derivation_input_internal(operation, step,
5073 PSA_KEY_TYPE_DERIVE,
5074 shared_secret,
5075 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005076exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005077 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5078 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005079}
5080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005081psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5082 psa_key_derivation_step_t step,
5083 mbedtls_svc_key_id_t private_key,
5084 const uint8_t *peer_key,
5085 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005086{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005087 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005088 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005089 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005091 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5092 return PSA_ERROR_INVALID_ARGUMENT;
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, operation->alg);
5096 if (status != PSA_SUCCESS) {
5097 return status;
5098 }
5099 status = psa_key_agreement_internal(operation, step,
5100 slot,
5101 peer_key, peer_key_length);
5102 if (status != PSA_SUCCESS) {
5103 psa_key_derivation_abort(operation);
5104 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005105 /* If a private key has been added as SECRET, we allow the derived
5106 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005107 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005108 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005109 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005110 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005112 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005114 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005115}
5116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005117psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5118 mbedtls_svc_key_id_t private_key,
5119 const uint8_t *peer_key,
5120 size_t peer_key_length,
5121 uint8_t *output,
5122 size_t output_size,
5123 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005124{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005125 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005126 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005127 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005128 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005130 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005131 status = PSA_ERROR_INVALID_ARGUMENT;
5132 goto exit;
5133 }
Ronald Cron5c522922020-11-14 16:35:34 +01005134 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005135 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5136 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005137 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005138 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005139
Gilles Peskine42313fb2022-04-14 00:17:15 +02005140 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5141 * for the output size. The PSA specification only guarantees that this
5142 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5143 * but it might be nice to allow smaller buffers if the output fits.
5144 * At the time of writing this comment, with only ECDH implemented,
5145 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5146 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5147 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005148 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005149 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5150 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005151 status = PSA_ERROR_BUFFER_TOO_SMALL;
5152 goto exit;
5153 }
5154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005155 status = psa_key_agreement_raw_internal(alg, slot,
5156 peer_key, peer_key_length,
5157 output, output_size,
5158 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005159
5160exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005161 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005162 /* If an error happens and is not handled properly, the output
5163 * may be used as a key to protect sensitive data. Arrange for such
5164 * a key to be random, which is likely to result in decryption or
5165 * verification errors. This is better than filling the buffer with
5166 * some constant data such as zeros, which would result in the data
5167 * being protected with a reproducible, easily knowable key.
5168 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005169 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005170 *output_length = output_size;
5171 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005173 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005175 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005176}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005177
5178
Gilles Peskine30524eb2020-11-13 17:02:26 +01005179
Gilles Peskine86a440b2018-11-12 18:39:40 +01005180/****************************************************************/
5181/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005182/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005183
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005184#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5185#include "mbedtls/entropy_poll.h"
5186#endif
5187
Gilles Peskine30524eb2020-11-13 17:02:26 +01005188/** Initialize the PSA random generator.
5189 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005190static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005191{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005192#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005193 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005194#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5195
Gilles Peskine30524eb2020-11-13 17:02:26 +01005196 /* Set default configuration if
5197 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005198 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005199 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005200 }
5201 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005202 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005203 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005205 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005206#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5207 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5208 /* The PSA entropy injection feature depends on using NV seed as an entropy
5209 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005210 mbedtls_entropy_add_source(&rng->entropy,
5211 mbedtls_nv_seed_poll, NULL,
5212 MBEDTLS_ENTROPY_BLOCK_SIZE,
5213 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005214#endif
5215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005216 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005217#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005218}
5219
5220/** Deinitialize the PSA random generator.
5221 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005222static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005223{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005224#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005225 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005226#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005227 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5228 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005229#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005230}
5231
5232/** Seed the PSA random generator.
5233 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005234static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005235{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005236#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5237 /* Do nothing: the external RNG seeds itself. */
5238 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005239 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005240#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005241 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005242 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5243 drbg_seed, sizeof(drbg_seed) - 1);
5244 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005245#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005246}
5247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005248psa_status_t psa_generate_random(uint8_t *output,
5249 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005250{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005251 GUARD_MODULE_INITIALIZED;
5252
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005253#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5254
5255 size_t output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005256 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
5257 output, output_size,
5258 &output_length);
5259 if (status != PSA_SUCCESS) {
5260 return status;
5261 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005262 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00005263 * for the external RNG interface. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005264 if (output_length != output_size) {
5265 return PSA_ERROR_INSUFFICIENT_ENTROPY;
5266 }
5267 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005268
5269#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5270
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005271 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005272 size_t request_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005273 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5274 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5275 output_size);
5276 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
5277 output, request_size);
5278 if (ret != 0) {
5279 return mbedtls_to_psa_error(ret);
5280 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01005281 output_size -= request_size;
5282 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005283 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005284 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005285#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005286}
5287
Gilles Peskine8814fc42020-12-14 15:33:44 +01005288/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005289 *
5290 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5291 * `psa_generate_random(...)`. The state parameter is ignored since the
5292 * PSA API doesn't support passing an explicit state.
5293 *
5294 * In the non-external case, psa_generate_random() calls an
5295 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5296 * and semantics as mbedtls_psa_get_random(). As an optimization,
5297 * instead of doing this back-and-forth between the PSA API and the
5298 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5299 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005300 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005301#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5302int mbedtls_psa_get_random(void *p_rng,
5303 unsigned char *output,
5304 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005305{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005306 /* This function takes a pointer to the RNG state because that's what
5307 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5308 * its own state internally and doesn't let the caller access that state.
5309 * So we just ignore the state parameter, and in practice we'll pass
5310 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005311 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005312 psa_status_t status = psa_generate_random(output, output_size);
5313 if (status == PSA_SUCCESS) {
5314 return 0;
5315 } else {
5316 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5317 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005318}
5319#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5320
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005321#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005322psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5323 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005324{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005325 if (global_data.initialized) {
5326 return PSA_ERROR_NOT_PERMITTED;
5327 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005329 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5330 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5331 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5332 return PSA_ERROR_INVALID_ARGUMENT;
5333 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005335 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005336}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005337#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005338
Ronald Cron3772afe2021-02-08 16:10:05 +01005339/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005340 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005341 * \param type The key type
5342 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005343 *
5344 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005345 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005346 * \retval #PSA_ERROR_INVALID_ARGUMENT
5347 * The size in bits of the key is not valid.
5348 * \retval #PSA_ERROR_NOT_SUPPORTED
5349 * The type and/or the size in bits of the key or the combination of
5350 * the two is not supported.
5351 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005352static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005353 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005354{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005355 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005357 if (key_type_is_raw_bytes(type)) {
5358 status = validate_unstructured_key_bit_size(type, bits);
5359 if (status != PSA_SUCCESS) {
5360 return status;
5361 }
5362 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005363#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005364 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5365 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5366 return PSA_ERROR_NOT_SUPPORTED;
5367 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005368
Ronald Cron01b2aba2020-10-05 09:42:02 +02005369 /* Accept only byte-aligned keys, for the same reasons as
5370 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005371 if (bits % 8 != 0) {
5372 return PSA_ERROR_NOT_SUPPORTED;
5373 }
5374 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005375#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005376
Ronald Cron5c4d3862020-12-07 11:07:24 +01005377#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005378 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005379 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005380 return PSA_SUCCESS;
5381 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005382#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005383 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005384 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005385 }
5386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005387 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005388}
5389
Ronald Cron55ed0592020-10-05 10:30:40 +02005390psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005391 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005392 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005393{
5394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005395 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005396
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005397 if ((attributes->domain_parameters == NULL) &&
5398 (attributes->domain_parameters_size != 0)) {
5399 return PSA_ERROR_INVALID_ARGUMENT;
5400 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005402 if (key_type_is_raw_bytes(type)) {
5403 status = psa_generate_random(key_buffer, key_buffer_size);
5404 if (status != PSA_SUCCESS) {
5405 return status;
5406 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005407
David Brown12ca5032021-02-11 11:02:00 -07005408#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005409 if (type == PSA_KEY_TYPE_DES) {
5410 psa_des_set_key_parity(key_buffer, key_buffer_size);
5411 }
David Brown8107e312021-02-12 08:08:46 -07005412#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005413 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005414
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005415#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5416 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005417 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5418 return mbedtls_psa_rsa_generate_key(attributes,
5419 key_buffer,
5420 key_buffer_size,
5421 key_buffer_length);
5422 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005423#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5424 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005425
John Durkop9814fa22020-11-04 12:28:15 -08005426#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005427 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5428 return mbedtls_psa_ecp_generate_key(attributes,
5429 key_buffer,
5430 key_buffer_size,
5431 key_buffer_length);
5432 } else
John Durkop9814fa22020-11-04 12:28:15 -08005433#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005434 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005435 (void) key_buffer_length;
5436 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005437 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005439 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005440}
Darryl Green0c6575a2018-11-07 16:05:30 +00005441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005442psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5443 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005444{
5445 psa_status_t status;
5446 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005447 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005448 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005449
Ronald Cron81709fc2020-11-14 12:10:32 +01005450 *key = MBEDTLS_SVC_KEY_ID_INIT;
5451
Gilles Peskine0f84d622019-09-12 19:03:13 +02005452 /* Reject any attempt to create a zero-length key so that we don't
5453 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005454 if (psa_get_key_bits(attributes) == 0) {
5455 return PSA_ERROR_INVALID_ARGUMENT;
5456 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005457
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005458 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005459 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5460 return PSA_ERROR_INVALID_ARGUMENT;
5461 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005463 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5464 &slot, &driver);
5465 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005466 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005467 }
Gilles Peskine11792082019-08-06 18:36:36 +02005468
Ronald Cron977c2472020-10-13 08:32:21 +02005469 /* In the case of a transparent key or an opaque key stored in local
5470 * storage (thus not in the case of generating a key in a secure element
5471 * or cryptoprocessor with storage), we have to allocate a buffer to
5472 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005473 if (slot->key.data == NULL) {
5474 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5475 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005476 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005477 attributes->core.type, attributes->core.bits);
5478 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005479 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005480 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005481
5482 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005483 attributes->core.type,
5484 attributes->core.bits);
5485 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005486 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005487 attributes, &key_buffer_size);
5488 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005489 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005490 }
Ronald Cron977c2472020-10-13 08:32:21 +02005491 }
Ronald Cron977c2472020-10-13 08:32:21 +02005492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005493 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5494 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005495 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005496 }
Ronald Cron977c2472020-10-13 08:32:21 +02005497 }
5498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005499 status = psa_driver_wrapper_generate_key(attributes,
5500 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005501
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005502 if (status != PSA_SUCCESS) {
5503 psa_remove_key_data_from_memory(slot);
5504 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005505
Gilles Peskine11792082019-08-06 18:36:36 +02005506exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005507 if (status == PSA_SUCCESS) {
5508 status = psa_finish_key_creation(slot, driver, key);
5509 }
5510 if (status != PSA_SUCCESS) {
5511 psa_fail_key_creation(slot, driver);
5512 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005514 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005515}
5516
Gilles Peskinee59236f2018-01-27 23:32:46 +01005517/****************************************************************/
5518/* Module setup */
5519/****************************************************************/
5520
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005521#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005522psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005523 void (* entropy_init)(mbedtls_entropy_context *ctx),
5524 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005525{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005526 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5527 return PSA_ERROR_BAD_STATE;
5528 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005529 global_data.rng.entropy_init = entropy_init;
5530 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005531 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005532}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005533#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005535void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005536{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005537 psa_wipe_all_key_slots();
5538 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5539 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005540 }
5541 /* Wipe all remaining data, including configuration.
5542 * In particular, this sets all state indicator to the value
5543 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005544 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005545
5546 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005547 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005548}
5549
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005550#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5551/** Recover a transaction that was interrupted by a power failure.
5552 *
5553 * This function is called during initialization, before psa_crypto_init()
5554 * returns. If this function returns a failure status, the initialization
5555 * fails.
5556 */
5557static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005558 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005559{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005560 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005561 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5562 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005563 /* TODO - fall through to the failure case until this
5564 * is implemented.
5565 * https://github.com/ARMmbed/mbed-crypto/issues/218
5566 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005567 default:
5568 /* We found an unsupported transaction in the storage.
5569 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005570 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005571 }
5572}
5573#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005575psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005576{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005577 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005578
Gilles Peskinec6b69072018-11-20 21:42:52 +01005579 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005580 if (global_data.initialized != 0) {
5581 return PSA_SUCCESS;
5582 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005583
Gilles Peskine30524eb2020-11-13 17:02:26 +01005584 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005585 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005586 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005587 status = mbedtls_psa_random_seed(&global_data.rng);
5588 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005589 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005590 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005591 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005593 status = psa_initialize_key_slots();
5594 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005595 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005596 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005597
Ronald Cron088d5d02021-04-10 16:57:30 +02005598 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005599 status = psa_driver_wrapper_init();
5600 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005601 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005602 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005603
Gilles Peskine4b734222019-07-24 15:56:31 +02005604#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005605 status = psa_crypto_load_transaction();
5606 if (status == PSA_SUCCESS) {
5607 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5608 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005609 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005610 }
5611 status = psa_crypto_stop_transaction();
5612 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005613 /* There's no transaction to complete. It's all good. */
5614 status = PSA_SUCCESS;
5615 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005616#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005617
Gilles Peskinec6b69072018-11-20 21:42:52 +01005618 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005619 global_data.initialized = 1;
5620
5621exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005622 if (status != PSA_SUCCESS) {
5623 mbedtls_psa_crypto_free();
5624 }
5625 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005626}
5627
David Horstmann114d8242023-12-07 18:39:17 +00005628/* Memory copying test hooks. These are called before input copy, after input
5629 * copy, before output copy and after output copy, respectively.
5630 * They are used by memory-poisoning tests to temporarily unpoison buffers
5631 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00005632#if defined(MBEDTLS_TEST_HOOKS)
5633void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5634void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5635void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5636void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5637#endif
5638
David Horstmanndf493552023-11-15 15:18:30 +00005639/** Copy from an input buffer to a local copy.
5640 *
5641 * \param[in] input Pointer to input buffer.
5642 * \param[in] input_len Length of the input buffer.
5643 * \param[out] input_copy Pointer to a local copy in which to store the input data.
5644 * \param[out] input_copy_len Length of the local copy buffer.
5645 * \return #PSA_SUCCESS, if the buffer was successfully
5646 * copied.
5647 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
5648 * copy is too small to hold contents of the
5649 * input buffer.
5650 */
5651MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00005652psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
5653 uint8_t *input_copy, size_t input_copy_len)
5654{
5655 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00005656 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00005657 }
5658
David Horstmann0760b152023-11-24 16:21:04 +00005659#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005660 if (psa_input_pre_copy_hook != NULL) {
5661 psa_input_pre_copy_hook(input, input_len);
5662 }
David Horstmann0760b152023-11-24 16:21:04 +00005663#endif
5664
David Horstmann660027f2023-11-15 17:33:47 +00005665 if (input_len > 0) {
5666 memcpy(input_copy, input, input_len);
5667 }
David Horstmann957f9802023-10-30 20:29:43 +00005668
David Horstmann0760b152023-11-24 16:21:04 +00005669#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005670 if (psa_input_post_copy_hook != NULL) {
5671 psa_input_post_copy_hook(input, input_len);
5672 }
David Horstmann0760b152023-11-24 16:21:04 +00005673#endif
5674
David Horstmann957f9802023-10-30 20:29:43 +00005675 return PSA_SUCCESS;
5676}
5677
David Horstmanndf493552023-11-15 15:18:30 +00005678/** Copy from a local output buffer into a user-supplied one.
5679 *
5680 * \param[in] output_copy Pointer to a local buffer containing the output.
5681 * \param[in] output_copy_len Length of the local buffer.
5682 * \param[out] output Pointer to user-supplied output buffer.
5683 * \param[out] output_len Length of the user-supplied output buffer.
5684 * \return #PSA_SUCCESS, if the buffer was successfully
5685 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00005686 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00005687 * user-supplied output buffer is too small to
5688 * hold the contents of the local buffer.
5689 */
5690MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00005691psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
5692 uint8_t *output, size_t output_len)
5693{
5694 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00005695 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00005696 }
David Horstmann660027f2023-11-15 17:33:47 +00005697
David Horstmann0760b152023-11-24 16:21:04 +00005698#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005699 if (psa_output_pre_copy_hook != NULL) {
5700 psa_output_pre_copy_hook(output, output_len);
5701 }
David Horstmann0760b152023-11-24 16:21:04 +00005702#endif
5703
David Horstmann660027f2023-11-15 17:33:47 +00005704 if (output_copy_len > 0) {
5705 memcpy(output, output_copy, output_copy_len);
5706 }
5707
David Horstmann0760b152023-11-24 16:21:04 +00005708#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005709 if (psa_output_post_copy_hook != NULL) {
5710 psa_output_post_copy_hook(output, output_len);
5711 }
David Horstmann0760b152023-11-24 16:21:04 +00005712#endif
5713
David Horstmann2bd296e2023-10-30 20:37:12 +00005714 return PSA_SUCCESS;
5715}
5716
David Horstmann81a0d572023-11-20 17:15:32 +00005717psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
5718 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005719{
5720 psa_status_t status;
5721
David Horstmann0d52c712023-11-23 15:50:37 +00005722 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005723
David Horstmann726bf052023-11-15 18:11:26 +00005724 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005725 return PSA_SUCCESS;
5726 }
5727
David Horstmann81a0d572023-11-20 17:15:32 +00005728 local_input->buffer = mbedtls_calloc(input_len, 1);
5729 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +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 Horstmann81a0d572023-11-20 17:15:32 +00005734 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005735
David Horstmann81a0d572023-11-20 17:15:32 +00005736 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005737
5738 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00005739 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005740 if (status != PSA_SUCCESS) {
5741 goto error;
5742 }
5743
5744 return PSA_SUCCESS;
5745
5746error:
David Horstmann81a0d572023-11-20 17:15:32 +00005747 mbedtls_free(local_input->buffer);
5748 local_input->buffer = NULL;
5749 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005750 return status;
5751}
5752
David Horstmann81a0d572023-11-20 17:15:32 +00005753void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00005754{
David Horstmann81a0d572023-11-20 17:15:32 +00005755 mbedtls_free(local_input->buffer);
5756 local_input->buffer = NULL;
5757 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00005758}
5759
David Horstmann1a76ab12023-11-20 12:54:09 +00005760psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
5761 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00005762{
David Horstmann0d52c712023-11-23 15:50:37 +00005763 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00005764
David Horstmann726bf052023-11-15 18:11:26 +00005765 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005766 return PSA_SUCCESS;
5767 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005768 local_output->buffer = mbedtls_calloc(output_len, 1);
5769 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005770 /* Since we dealt with the zero-length case above, we know that
5771 * a NULL return value means a failure of allocation. */
5772 return PSA_ERROR_INSUFFICIENT_MEMORY;
5773 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005774 local_output->length = output_len;
5775 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00005776
5777 return PSA_SUCCESS;
5778}
5779
David Horstmann1a76ab12023-11-20 12:54:09 +00005780psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00005781{
David Horstmann761761f2023-11-15 15:57:32 +00005782 psa_status_t status;
5783
David Horstmann1a76ab12023-11-20 12:54:09 +00005784 if (local_output->buffer == NULL) {
5785 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005786 return PSA_SUCCESS;
5787 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005788 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00005789 /* We have an internal copy but nothing to copy back to. */
5790 return PSA_ERROR_CORRUPTION_DETECTED;
5791 }
5792
David Horstmann1a76ab12023-11-20 12:54:09 +00005793 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
5794 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00005795 if (status != PSA_SUCCESS) {
5796 return status;
5797 }
David Horstmannaeeb2742023-11-08 17:44:18 +00005798
David Horstmann1a76ab12023-11-20 12:54:09 +00005799 mbedtls_free(local_output->buffer);
5800 local_output->buffer = NULL;
5801 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005802
5803 return PSA_SUCCESS;
5804}
5805
Gilles Peskinee59236f2018-01-27 23:32:46 +01005806#endif /* MBEDTLS_PSA_CRYPTO_C */