blob: a0a002a088aa632350bf6d7db31f404c6e9c0ea0 [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 Rodgman16799db2023-11-02 19:47:20 +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"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010010#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010011
12#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030013
John Durkop07cc04a2020-11-16 22:08:34 -080014#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
15#include "check_crypto_config.h"
16#endif
17
Gilles Peskinee59236f2018-01-27 23:32:46 +010018#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010019#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010020
Ronald Crond6d28882020-12-14 14:56:02 +010021#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010022#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010023#include "psa_crypto_invasive.h"
Xiaokang Qiane9c39c42023-08-09 08:50:19 +000024#include "psa_crypto_driver_wrappers.h"
Xiaokang Qianfe9666b2023-09-11 10:36:20 +000025#include "psa_crypto_driver_wrappers_no_static.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010027#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010028#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010029#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010031#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020033#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020034#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskineb2b64d32020-12-14 16:43:58 +010040#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010041
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include <stdlib.h>
43#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010046#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020047#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000048#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020049#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010050#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020051#include "mbedtls/chacha20.h"
52#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/cipher.h"
54#include "mbedtls/ccm.h"
55#include "mbedtls/cmac.h"
Dave Rodgman78701152023-08-29 14:20:18 +010056#include "mbedtls/constant_time.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020058#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010059#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010060#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/error.h"
62#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/md5.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010064#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000065#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010066#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000067#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/sha1.h"
71#include "mbedtls/sha256.h"
72#include "mbedtls/sha512.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010073#include "mbedtls/psa_util.h"
Paul Elliott600472b2024-02-23 17:26:58 +000074#include "mbedtls/threading.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020076#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
77 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
78 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020079#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020080#endif
81
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010082/****************************************************************/
83/* Global data, support functions and library management */
84/****************************************************************/
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020087{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020089}
90
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010091/* Values for psa_global_data_t::rng_state */
92#define RNG_NOT_INITIALIZED 0
93#define RNG_INITIALIZED 1
94#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010095
Paul Elliott0db6a902024-03-15 13:48:20 +000096/* IDs for PSA crypto subsystems. Starts at 1 to catch potential uninitialized
97 * variables as arguments. */
Paul Elliott47cee8e2024-03-08 21:38:02 +000098typedef enum {
Paul Elliott0db6a902024-03-15 13:48:20 +000099 PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS = 1,
Paul Elliott47cee8e2024-03-08 21:38:02 +0000100 PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS,
101 PSA_CRYPTO_SUBSYSTEM_RNG,
102 PSA_CRYPTO_SUBSYSTEM_TRANSACTION,
103} mbedtls_psa_crypto_subsystem;
104
Paul Elliottb24e36d2024-03-15 16:25:48 +0000105/* Initialization flags for global_data::initialized */
Paul Elliott47cee8e2024-03-08 21:38:02 +0000106#define PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED 0x01
107#define PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED 0x02
108#define PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED 0x04
109
110#define PSA_CRYPTO_SUBSYSTEM_ALL_INITIALISED ( \
111 PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED | \
112 PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED | \
113 PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED)
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +0100116 uint8_t initialized;
117 uint8_t rng_state;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100118 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119} psa_global_data_t;
120
121static psa_global_data_t global_data;
122
Paul Elliott600472b2024-02-23 17:26:58 +0000123static uint8_t psa_get_initialized(void)
124{
125 uint8_t initialized;
126
127#if defined(MBEDTLS_THREADING_C)
Paul Elliott47cee8e2024-03-08 21:38:02 +0000128 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
129#endif /* defined(MBEDTLS_THREADING_C) */
130
131 initialized = global_data.rng_state == RNG_SEEDED;
132
133#if defined(MBEDTLS_THREADING_C)
134 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
135#endif /* defined(MBEDTLS_THREADING_C) */
136
137#if defined(MBEDTLS_THREADING_C)
Paul Elliott600472b2024-02-23 17:26:58 +0000138 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
139#endif /* defined(MBEDTLS_THREADING_C) */
140
Paul Elliott47cee8e2024-03-08 21:38:02 +0000141 initialized =
142 (initialized && (global_data.initialized == PSA_CRYPTO_SUBSYSTEM_ALL_INITIALISED));
Paul Elliott600472b2024-02-23 17:26:58 +0000143
144#if defined(MBEDTLS_THREADING_C)
145 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
146#endif /* defined(MBEDTLS_THREADING_C) */
147
148 return initialized;
149}
150
Paul Elliott35816522024-02-23 17:47:42 +0000151static uint8_t psa_get_drivers_initialized(void)
152{
153 uint8_t initialized;
154
155#if defined(MBEDTLS_THREADING_C)
156 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
157#endif /* defined(MBEDTLS_THREADING_C) */
158
Paul Elliott47cee8e2024-03-08 21:38:02 +0000159 initialized = (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED) != 0;
Paul Elliott35816522024-02-23 17:47:42 +0000160
161#if defined(MBEDTLS_THREADING_C)
162 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
163#endif /* defined(MBEDTLS_THREADING_C) */
164
165 return initialized;
166}
167
itayzafrir0adf0fc2018-09-06 16:24:41 +0300168#define GUARD_MODULE_INITIALIZED \
Paul Elliott600472b2024-02-23 17:26:58 +0000169 if (psa_get_initialized() == 0) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300171
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100172int psa_can_do_hash(psa_algorithm_t hash_alg)
173{
174 (void) hash_alg;
Paul Elliott35816522024-02-23 17:47:42 +0000175 return psa_get_drivers_initialized();
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100176}
Valerio Settic6f004f2023-12-12 11:27:36 +0100177
Valerio Setti1fff4f22023-12-28 14:19:34 +0100178int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
Valerio Settic6f004f2023-12-12 11:27:36 +0100179{
Valerio Setti1fff4f22023-12-28 14:19:34 +0100180 (void) key_type;
Valerio Settic6f004f2023-12-12 11:27:36 +0100181 (void) cipher_alg;
Paul Elliott35816522024-02-23 17:47:42 +0000182 return psa_get_drivers_initialized();
Valerio Settic6f004f2023-12-12 11:27:36 +0100183}
184
185
Valerio Settia55f0422023-07-10 15:34:41 +0200186#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200187 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200188 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200189static int psa_is_dh_key_size_valid(size_t bits)
190{
Valerio Setti504a1022024-01-17 15:19:30 +0100191 switch (bits) {
192#if defined(PSA_WANT_DH_RFC7919_2048)
193 case 2048:
194 return 1;
195#endif /* PSA_WANT_DH_RFC7919_2048 */
196#if defined(PSA_WANT_DH_RFC7919_3072)
197 case 3072:
198 return 1;
199#endif /* PSA_WANT_DH_RFC7919_3072 */
200#if defined(PSA_WANT_DH_RFC7919_4096)
201 case 4096:
202 return 1;
203#endif /* PSA_WANT_DH_RFC7919_4096 */
204#if defined(PSA_WANT_DH_RFC7919_6144)
205 case 6144:
206 return 1;
207#endif /* PSA_WANT_DH_RFC7919_6144 */
208#if defined(PSA_WANT_DH_RFC7919_8192)
209 case 8192:
210 return 1;
211#endif /* PSA_WANT_DH_RFC7919_8192 */
212 default:
213 return 0;
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200214 }
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200215}
Valerio Settia55f0422023-07-10 15:34:41 +0200216#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200217 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200218 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100221{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100222 /* Mbed TLS error codes can combine a high-level error code and a
223 * low-level error code. The low-level error usually reflects the
224 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 int low_level_ret = -(-ret & 0x007f);
226 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100227 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100230#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100231 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
232 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100234 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
235 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100236#endif
237
238#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200239 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
240 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
241 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
242 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
243 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200245 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200247 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100249#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200250
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100251#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000252 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100255#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100256
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100257#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100258 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100260 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100262#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100263
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100264#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200265 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100267#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200268
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100269#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200270 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200272 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100274#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200275
Dave Rodgman509b5672023-08-16 19:26:23 +0100276#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100279 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100283 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100285 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100287 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100289 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100291#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
294 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100295 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
296 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100299 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
300 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100304#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100305
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100306#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100307 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100309#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100310
Gilles Peskinee59236f2018-01-27 23:32:46 +0100311 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
312 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
313 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100316#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200319 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100321 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100323#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100324
Gilles Peskine82e57d12020-11-13 21:31:17 +0100325#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100327 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
328 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100329 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100331 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
332 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100334 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100336#endif
337
Dave Rodgmandea266f2023-08-31 11:52:43 +0100338#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100339 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100341 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100343 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100345#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100346 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100348#endif
349#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100350
Dave Rodgman509b5672023-08-16 19:26:23 +0100351#if defined(MBEDTLS_BIGNUM_C)
352#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100353 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100355#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100356 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100358 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100360 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100362 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100364 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100366 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100368 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100370#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100371
Dave Rodgman509b5672023-08-16 19:26:23 +0100372#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100373 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100375 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
376 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100378#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
379 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100380 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100382#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100383 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
384 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100386 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100388 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
389 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100391 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100393 case MBEDTLS_ERR_PK_INVALID_ALG:
394 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
395 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100397 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200399 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100401#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100402
Gilles Peskineff2d2002019-05-06 15:26:23 +0200403 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200405 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200407
Dave Rodgman509b5672023-08-16 19:26:23 +0100408#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100409 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100411 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100413 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100415 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100417 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
418 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100420 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100422 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100424 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100426#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200427
Dave Rodgman1dab4452023-09-01 09:59:51 +0100428#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300429 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300430 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300432 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300434 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300436 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
437 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300439 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100441 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100443
Dave Rodgman509b5672023-08-16 19:26:23 +0100444#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000445 case MBEDTLS_ERR_ECP_IN_PROGRESS:
446 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100447#endif
448#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000449
Janos Follatha13b9052019-11-22 12:48:59 +0000450 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300452
Gilles Peskinee59236f2018-01-27 23:32:46 +0100453 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100455 }
456}
457
Paul Elliottdc42ca82023-02-24 18:11:59 +0000458/**
459 * \brief For output buffers which contain "tags"
460 * (outputs that may be checked for validity like
461 * hashes, MACs and signatures), fill the unused
462 * part of the output buffer (the whole buffer on
463 * error, the trailing part on success) with
464 * something that isn't a valid tag (barring an
465 * attack on the tag and deliberately-crafted
466 * input), in case the caller doesn't check the
467 * return status properly.
468 *
469 * \param output_buffer Pointer to buffer to wipe. May not be NULL
470 * unless \p output_buffer_size is zero.
471 * \param status Status of function called to generate
472 * output_buffer originally
473 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
474 * could be NULL.
475 * \param output_buffer_length Length of data written to output_buffer, must be
476 * less than \p output_buffer_size
477 */
478static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000479 size_t output_buffer_size, size_t output_buffer_length)
480{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000481 size_t offset = 0;
482
483 if (output_buffer_size == 0) {
484 /* If output_buffer_size is 0 then we have nothing to do. We must not
485 call memset because output_buffer may be NULL in this case */
486 return;
487 }
488
489 if (status == PSA_SUCCESS) {
490 offset = output_buffer_length;
491 }
492
493 memset(output_buffer + offset, '!', output_buffer_size - offset);
494}
495
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
498 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499{
500 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200502 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200503 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200504 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200505 case PSA_KEY_TYPE_PASSWORD:
506 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200507 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100508#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200509 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (bits != 128 && bits != 192 && bits != 256) {
511 return PSA_ERROR_INVALID_ARGUMENT;
512 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200513 break;
514#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200515#if defined(PSA_WANT_KEY_TYPE_ARIA)
516 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (bits != 128 && bits != 192 && bits != 256) {
518 return PSA_ERROR_INVALID_ARGUMENT;
519 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200520 break;
521#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100522#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200523 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (bits != 128 && bits != 192 && bits != 256) {
525 return PSA_ERROR_INVALID_ARGUMENT;
526 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200527 break;
528#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100529#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200530 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (bits != 64 && bits != 128 && bits != 192) {
532 return PSA_ERROR_INVALID_ARGUMENT;
533 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200534 break;
535#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100536#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200537 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if (bits != 256) {
539 return PSA_ERROR_INVALID_ARGUMENT;
540 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200541 break;
542#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200543 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200545 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (bits % 8 != 0) {
547 return PSA_ERROR_INVALID_ARGUMENT;
548 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200551}
552
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100553/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 *
Steven Cooremancd640932021-03-08 12:00:27 +0100555 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
556 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100557 *
558 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100559 * \param[in] key_type The key type of the key to be used with the
560 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100561 *
562 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100563 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100564 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100565 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100566 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100567MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100568 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100570{
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if (PSA_ALG_IS_HMAC(algorithm)) {
572 if (key_type == PSA_KEY_TYPE_HMAC) {
573 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100574 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100575 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
578 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
579 * key. */
580 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
581 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
582 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
583 * the block length (larger than 1) for block ciphers. */
584 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
585 return PSA_SUCCESS;
586 }
587 }
588 }
589
590 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100591}
592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
594 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200595{
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (slot->key.data != NULL) {
597 return PSA_ERROR_ALREADY_EXISTS;
598 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 slot->key.data = mbedtls_calloc(1, buffer_length);
601 if (slot->key.data == NULL) {
602 return PSA_ERROR_INSUFFICIENT_MEMORY;
603 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200604
Ronald Cronea0f8a62020-11-25 17:52:23 +0100605 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200607}
608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
610 const uint8_t *data,
611 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200612{
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 psa_status_t status = psa_allocate_buffer_to_slot(slot,
614 data_length);
615 if (status != PSA_SUCCESS) {
616 return status;
617 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 memcpy(slot->key.data, data, data_length);
620 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200621}
622
Ronald Crona0fe59f2020-11-29 11:14:53 +0100623psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100624 const psa_key_attributes_t *attributes,
625 const uint8_t *data, size_t data_length,
626 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100628{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100629 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100630 psa_key_type_t type = attributes->type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100631
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200632 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (data_length == 0) {
634 return PSA_ERROR_NOT_SUPPORTED;
635 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 if (key_type_is_raw_bytes(type)) {
638 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200639
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100640 status = psa_validate_unstructured_key_bit_size(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 *bits);
642 if (status != PSA_SUCCESS) {
643 return status;
644 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200645
Ronald Crondd04d422020-11-28 15:14:42 +0100646 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100648 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 return PSA_SUCCESS;
652 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200653#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200654 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100655 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200656 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Valerio Setti504a1022024-01-17 15:19:30 +0100657 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100658 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200659 return mbedtls_psa_ffdh_import_key(attributes,
660 data, data_length,
661 key_buffer, key_buffer_size,
662 key_buffer_length,
663 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100664 }
Valerio Settia55f0422023-07-10 15:34:41 +0200665#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200666 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200667#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
669 if (PSA_KEY_TYPE_IS_ECC(type)) {
670 return mbedtls_psa_ecp_import_key(attributes,
671 data, data_length,
672 key_buffer, key_buffer_size,
673 key_buffer_length,
674 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100675 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200676#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800677 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200678#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
679 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
681 if (PSA_KEY_TYPE_IS_RSA(type)) {
682 return mbedtls_psa_rsa_import_key(attributes,
683 data, data_length,
684 key_buffer, key_buffer_size,
685 key_buffer_length,
686 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200687 }
Valerio Settife478902023-07-25 12:27:19 +0200688#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
689 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800690 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100691 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100694}
695
Gilles Peskinef603c712019-01-19 13:40:11 +0100696/** Calculate the intersection of two algorithm usage policies.
697 *
698 * Return 0 (which allows no operation) on incompatibility.
699 */
700static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100701 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100702 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100704{
Gilles Peskine549ea862019-05-22 11:45:59 +0200705 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (alg1 == alg2) {
707 return alg1;
708 }
Steven Cooreman03488022021-02-18 12:07:20 +0100709 /* If the policies are from the same hash-and-sign family, check
710 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
712 PSA_ALG_IS_SIGN_HASH(alg2) &&
713 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
714 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
715 return alg2;
716 }
717 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
718 return alg1;
719 }
Steven Cooreman03488022021-02-18 12:07:20 +0100720 }
721 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100722 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100723 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
725 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
726 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
727 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
728 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100729 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100730
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100731 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
733 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
734 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
735 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100736 }
737 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
739 (alg1_len <= alg2_len)) {
740 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100741 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
743 (alg2_len <= alg1_len)) {
744 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100745 }
746 }
747 /* If the policies are from the same MAC family, check whether one
748 * of them is a minimum-MAC-length policy. Calculate the most
749 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
751 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
752 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100753 /* Validate the combination of key type and algorithm. Since the base
754 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
756 return 0;
757 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100758
Steven Cooreman31a876d2021-03-03 20:47:40 +0100759 /* Get the (exact or at-least) output lengths for both sides of the
760 * requested intersection. None of the currently supported algorithms
761 * have an output length dependent on the actual key size, so setting it
762 * to a bogus value of 0 is currently OK.
763 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100764 * Note that for at-least-this-length wildcard algorithms, the output
765 * length is set to the shortest allowed length, which allows us to
766 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
768 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100769 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100770
Steven Cooremana1d83222021-02-25 10:20:29 +0100771 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
773 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
774 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100775 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100776
777 /* If only one is an at-least-this-length policy, the intersection would
778 * be the other (fixed-length) policy as long as said fixed length is
779 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
781 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100782 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
784 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100785 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100786
Steven Cooremancd640932021-03-08 12:00:27 +0100787 /* If none of them are wildcards, check whether they define the same tag
788 * length. This is still possible here when one is default-length and
789 * the other specific-length. Ensure to always return the
790 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (alg1_len == alg2_len) {
792 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
793 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100794 }
795 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100797}
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799static int psa_key_algorithm_permits(psa_key_type_t key_type,
800 psa_algorithm_t policy_alg,
801 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200802{
Gilles Peskine549ea862019-05-22 11:45:59 +0200803 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (requested_alg == policy_alg) {
805 return 1;
806 }
Steven Cooreman03488022021-02-18 12:07:20 +0100807 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
808 * and requested_alg is the same hash-and-sign family with any hash,
809 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
811 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
812 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
813 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100814 }
815 /* If policy_alg is a wildcard AEAD algorithm of the same base as
816 * the requested algorithm, check the requested tag length to be
817 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if (PSA_ALG_IS_AEAD(policy_alg) &&
819 PSA_ALG_IS_AEAD(requested_alg) &&
820 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
821 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
822 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
823 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
824 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100825 }
Steven Cooremancd640932021-03-08 12:00:27 +0100826 /* If policy_alg is a MAC algorithm of the same base as the requested
827 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (PSA_ALG_IS_MAC(policy_alg) &&
829 PSA_ALG_IS_MAC(requested_alg) &&
830 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
831 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100832 /* Validate the combination of key type and algorithm. Since the policy
833 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
835 return 0;
836 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100837
Steven Cooreman31a876d2021-03-03 20:47:40 +0100838 /* Get both the requested output length for the algorithm which is to be
839 * verified, and the default output length for the base algorithm.
840 * Note that none of the currently supported algorithms have an output
841 * length dependent on actual key size, so setting it to a bogus value
842 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100843 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100845 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 key_type, 0,
847 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100848
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100849 /* If the policy is default-length, only allow an algorithm with
850 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
852 return requested_output_length == default_output_length;
853 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100854
855 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100856 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
858 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
859 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100860 }
861
Steven Cooremancd640932021-03-08 12:00:27 +0100862 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
863 * check for the requested MAC length to be equal to or longer than the
864 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
866 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
867 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100868 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200869 }
Steven Cooremance48e852020-10-05 16:02:45 +0200870 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200871 * a key derivation with that key agreement should also be allowed. This
872 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
874 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
875 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
876 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200877 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100878 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200880}
881
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100882/** Test whether a policy permits an algorithm.
883 *
884 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100885 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100886 * \note This function requires providing the key type for which the policy is
887 * being validated, since some algorithm policy definitions (e.g. MAC)
888 * have different properties depending on what kind of cipher it is
889 * combined with.
890 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100891 * \retval PSA_SUCCESS When \p alg is a specific algorithm
892 * allowed by the \p policy.
893 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
894 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
895 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100896 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100897static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
898 psa_key_type_t key_type,
899 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100900{
Steven Cooremand788fab2021-02-25 11:29:17 +0100901 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (alg == 0) {
903 return PSA_ERROR_INVALID_ARGUMENT;
904 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100905
Steven Cooreman7e39f052021-02-23 14:18:32 +0100906 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (PSA_ALG_IS_WILDCARD(alg)) {
908 return PSA_ERROR_INVALID_ARGUMENT;
909 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
912 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
913 return PSA_SUCCESS;
914 } else {
915 return PSA_ERROR_NOT_PERMITTED;
916 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100917}
918
Gilles Peskinef603c712019-01-19 13:40:11 +0100919/** Restrict a key policy based on a constraint.
920 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100921 * \note This function requires providing the key type for which the policy is
922 * being restricted, since some algorithm policy definitions (e.g. MAC)
923 * have different properties depending on what kind of cipher it is
924 * combined with.
925 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100926 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100927 * \param[in,out] policy The policy to restrict.
928 * \param[in] constraint The policy constraint to apply.
929 *
930 * \retval #PSA_SUCCESS
931 * \c *policy contains the intersection of the original value of
932 * \c *policy and \c *constraint.
933 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100934 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100935 * \c *policy is unchanged.
936 */
937static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100938 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100939 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100941{
942 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 psa_key_policy_algorithm_intersection(key_type, policy->alg,
944 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200945 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
947 constraint->alg2);
948 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
949 return PSA_ERROR_INVALID_ARGUMENT;
950 }
951 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
952 return PSA_ERROR_INVALID_ARGUMENT;
953 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100954 policy->usage &= constraint->usage;
955 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200956 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100958}
959
Przemek Stekiel061f6942022-11-30 10:51:35 +0100960/** Get the description of a key given its identifier and policy constraints
961 * and lock it.
962 *
963 * The key must have allow all the usage flags set in \p usage. If \p alg is
964 * nonzero, the key must allow operations with this algorithm. If \p alg is
965 * zero, the algorithm is not checked.
966 *
967 * In case of a persistent key, the function loads the description of the key
968 * into a key slot if not already done.
969 *
Ryan Everett098c6652024-01-03 13:03:36 +0000970 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +0000971 * It is the responsibility of the caller to then unregister
972 * once they have finished reading the contents of the slot.
973 * The caller unregisters by calling psa_unregister_read() or
974 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
975 * if and only if the caller already holds the global key slot mutex
976 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
977 * the unregister with mutex lock and unlock operations.
Przemek Stekiel061f6942022-11-30 10:51:35 +0100978 */
979static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100980 mbedtls_svc_key_id_t key,
981 psa_key_slot_t **p_slot,
982 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100984{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200985 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100986 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 status = psa_get_and_lock_key_slot(key, p_slot);
989 if (status != PSA_SUCCESS) {
990 return status;
991 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200992 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100993
994 /* Enforce that usage policy for the key slot contains all the flags
995 * required by the usage parameter. There is one exception: public
996 * keys can always be exported, so we treat public key objects as
997 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100999 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001003 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001004 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001005 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001006
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001007 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 if (alg != 0) {
1009 status = psa_key_policy_permits(&slot->attr.policy,
1010 slot->attr.type,
1011 alg);
1012 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001013 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001015 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001018
1019error:
1020 *p_slot = NULL;
Ryan Everettfb792ca2024-01-31 13:40:05 +00001021 psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001024}
Darryl Green940d72c2018-07-13 13:18:51 +01001025
Ronald Cron5c522922020-11-14 16:35:34 +01001026/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001027 *
1028 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001029 * available, as opposed to a key in a secure element and/or to be used
1030 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001031 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001032 * This is a temporary function that may be used instead of
1033 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1034 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001035 *
Ryan Everett098c6652024-01-03 13:03:36 +00001036 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +00001037 * It is the responsibility of the caller to then unregister
1038 * once they have finished reading the contents of the slot.
1039 * The caller unregisters by calling psa_unregister_read() or
1040 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
1041 * if and only if the caller already holds the global key slot mutex
1042 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
1043 * psa_unregister_read() with mutex lock and unlock operations.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001044 */
Ronald Cron5c522922020-11-14 16:35:34 +01001045static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1046 mbedtls_svc_key_id_t key,
1047 psa_key_slot_t **p_slot,
1048 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001050{
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1052 usage, alg);
1053 if (status != PSA_SUCCESS) {
1054 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001055 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everettfb792ca2024-01-31 13:40:05 +00001058 psa_unregister_read_under_mutex(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 *p_slot = NULL;
1060 return PSA_ERROR_NOT_SUPPORTED;
1061 }
1062
1063 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001064}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001067{
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001069 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001071
Ronald Cronea0f8a62020-11-25 17:52:23 +01001072 slot->key.data = NULL;
1073 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001076}
1077
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001078/** Completely wipe a slot in memory, including its policy.
1079 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001081{
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 /*
1085 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +00001086 * do our best to report an unexpected amount of registered readers or
1087 * an unexpected state.
1088 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1089 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1091 * function is called as part of the execution of a test suite, the
1092 * execution of the test suite is stopped in error if the assertion fails.
1093 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001094 switch (slot->state) {
1095 case PSA_SLOT_FULL:
1096 /* In this state psa_wipe_key_slot() must only be called if the
1097 * caller is the last reader. */
1098 case PSA_SLOT_PENDING_DELETION:
1099 /* In this state psa_wipe_key_slot() must only be called if the
1100 * caller is the last reader. */
1101 if (slot->registered_readers != 1) {
1102 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1);
1103 status = PSA_ERROR_CORRUPTION_DETECTED;
1104 }
1105 break;
1106 case PSA_SLOT_FILLING:
1107 /* In this state registered_readers must be 0. */
1108 if (slot->registered_readers != 0) {
1109 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0);
1110 status = PSA_ERROR_CORRUPTION_DETECTED;
1111 }
1112 break;
1113 case PSA_SLOT_EMPTY:
1114 /* The slot is already empty, it cannot be wiped. */
1115 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1116 status = PSA_ERROR_CORRUPTION_DETECTED;
1117 break;
1118 default:
1119 /* The slot's state is invalid. */
1120 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001121 }
1122
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001123 /* Multipart operations may still be using the key. This is safe
1124 * because all multipart operation objects are independent from
1125 * the key slot: if they need to access the key after the setup
1126 * phase, they have a copy of the key. Note that this means that
1127 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001128 /* At this point, key material and other type-specific content has
1129 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001130 * zeroize because the metadata is not particularly sensitive.
1131 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 memset(slot, 0, sizeof(*slot));
1133 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001134}
1135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001137{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001138 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001139 psa_status_t status; /* status of the last operation */
1140 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001141#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1142 psa_se_drv_table_entry_t *driver;
1143#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (mbedtls_svc_key_id_is_null(key)) {
1146 return PSA_SUCCESS;
1147 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001148
Ronald Cronf2911112020-10-29 17:51:10 +01001149 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001150 * Get the description of the key in a key slot, and register to read it.
1151 * In the case of a persistent key, this will load the key description
1152 * from persistent memory if not done yet.
1153 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001154 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001155 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 status = psa_get_and_lock_key_slot(key, &slot);
1157 if (status != PSA_SUCCESS) {
1158 return status;
1159 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001160
Ryan Everettc053d962024-01-25 17:56:32 +00001161#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001162 /* We cannot unlock between setting the state to PENDING_DELETION
1163 * and destroying the key in storage, as otherwise another thread
1164 * could load the key into a new slot and the key will not be
1165 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001166 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1167 &mbedtls_threading_key_slot_mutex));
Ryan Everettd868b742024-03-08 18:35:09 +00001168
1169 if (slot->state == PSA_SLOT_PENDING_DELETION) {
1170 /* Another thread has destroyed the key between us locking the slot
1171 * and us gaining the mutex. Unregister from the slot,
1172 * and report that the key does not exist. */
1173 status = psa_unregister_read(slot);
1174
1175 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1176 &mbedtls_threading_key_slot_mutex));
1177 return (status == PSA_SUCCESS) ? PSA_ERROR_INVALID_HANDLE : status;
1178 }
Ryan Everettc053d962024-01-25 17:56:32 +00001179#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001180 /* Set the key slot containing the key description's state to
1181 * PENDING_DELETION. This stops new operations from registering
1182 * to read the slot. Current readers can safely continue to access
1183 * the key within the slot; the last registered reader will
1184 * automatically wipe the slot when they call psa_unregister_read().
1185 * If the key is persistent, we can now delete the copy of the key
1186 * from memory. If the key is opaque, we require the driver to
1187 * deal with the deletion. */
Ryan Everettd868b742024-03-08 18:35:09 +00001188 overall_status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1189 PSA_SLOT_PENDING_DELETION);
Ryan Everettc053d962024-01-25 17:56:32 +00001190
Ryan Everettd868b742024-03-08 18:35:09 +00001191 if (overall_status != PSA_SUCCESS) {
Ryan Everettc053d962024-01-25 17:56:32 +00001192 goto exit;
1193 }
Ronald Cronf2911112020-10-29 17:51:10 +01001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001196 /* Refuse the destruction of a read-only key (which may or may not work
1197 * if we attempt it, depending on whether the key is merely read-only
1198 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001199 * Just do the best we can, which is to wipe the copy in memory
1200 * (done in this function's cleanup code). */
1201 overall_status = PSA_ERROR_NOT_PERMITTED;
1202 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001203 }
1204
Gilles Peskine354f7672019-07-12 23:46:38 +02001205#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1207 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001208 /* For a key in a secure element, we need to do three things:
1209 * remove the key file in internal storage, destroy the
1210 * key inside the secure element, and update the driver's
1211 * persistent data. Start a transaction that will encompass these
1212 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001214 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001216 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 status = psa_crypto_save_transaction();
1218 if (status != PSA_SUCCESS) {
1219 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001220 /* We should still try to destroy the key in the secure
1221 * element and the key metadata in storage. This is especially
1222 * important if the error is that the storage is full.
1223 * But how to do it exactly without risking an inconsistent
1224 * state after a reset?
1225 * https://github.com/ARMmbed/mbed-crypto/issues/215
1226 */
1227 overall_status = status;
1228 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001229 }
1230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 status = psa_destroy_se_key(driver,
1232 psa_key_slot_get_slot_number(slot));
1233 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001234 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001236 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001237#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1238
Darryl Greend49a4992018-06-18 17:27:26 +01001239#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001241 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001242 * The slot will still hold a copy of the key until the last reader
1243 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 status = psa_destroy_persistent_key(slot->attr.id);
1245 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001246 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 }
Darryl Greend49a4992018-06-18 17:27:26 +01001248 }
1249#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001250
Gilles Peskinefc762652019-07-22 19:30:34 +02001251#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if (driver != NULL) {
1253 status = psa_save_se_persistent_data(driver);
1254 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001255 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 }
1257 status = psa_crypto_stop_transaction();
1258 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001259 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001261 }
1262#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1263
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001264exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001265 /* Unregister from reading the slot. If we are the last active reader
1266 * then this will wipe the slot. */
1267 status = psa_unregister_read(slot);
1268 /* Prioritize CORRUPTION_DETECTED from unregistering over
1269 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001271 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 }
Ryan Everettc053d962024-01-25 17:56:32 +00001273
1274#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001275 /* Don't overwrite existing errors if the unlock fails. */
1276 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001277 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1278 &mbedtls_threading_key_slot_mutex));
1279#endif
1280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001282}
1283
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001284/** Retrieve all the publicly-accessible attributes of a key.
1285 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001286psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1287 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001288{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001289 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001290 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1295 if (status != PSA_SUCCESS) {
1296 return status;
1297 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001298
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001299 *attributes = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001300
1301#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1303 psa_set_key_slot_number(attributes,
1304 psa_key_slot_get_slot_number(slot));
1305 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001306#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001307
Gilles Peskine97c0b2f2024-02-16 00:49:46 +01001308 return psa_unregister_read_under_mutex(slot);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001309}
1310
Gilles Peskinec8000c02019-08-02 20:15:51 +02001311#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1312psa_status_t psa_get_key_slot_number(
1313 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001315{
Gilles Peskine972539c2024-02-28 01:49:45 +01001316 if (attributes->has_slot_number) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001317 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 return PSA_SUCCESS;
1319 } else {
1320 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001321 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001322}
1323#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1326 size_t key_buffer_size,
1327 uint8_t *data,
1328 size_t data_size,
1329 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001330{
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if (key_buffer_size > data_size) {
1332 return PSA_ERROR_BUFFER_TOO_SMALL;
1333 }
1334 memcpy(data, key_buffer, key_buffer_size);
1335 memset(data + key_buffer_size, 0,
1336 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001337 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001339}
1340
Ronald Cron7285cda2020-11-26 14:46:37 +01001341psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001342 const psa_key_attributes_t *attributes,
1343 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001345{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001346 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 if (key_type_is_raw_bytes(type) ||
1349 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001350 PSA_KEY_TYPE_IS_ECC(type) ||
1351 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 return psa_export_key_buffer_internal(
1353 key_buffer, key_buffer_size,
1354 data, data_size, data_length);
1355 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001356 /* This shouldn't happen in the reference implementation, but
1357 it is valid for a special-purpose implementation to omit
1358 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001360 }
1361}
1362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1364 uint8_t *data,
1365 size_t data_size,
1366 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001367{
1368 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1369 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1370 psa_key_slot_t *slot;
1371
Ronald Crone907e552021-01-18 13:22:38 +01001372 /* Reject a zero-length output buffer now, since this can never be a
1373 * valid key representation. This way we know that data must be a valid
1374 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 if (data_size == 0) {
1376 return PSA_ERROR_BUFFER_TOO_SMALL;
1377 }
Ronald Crone907e552021-01-18 13:22:38 +01001378
Ronald Cron9486f9d2020-11-26 13:36:23 +01001379 /* Set the key to empty now, so that even when there are errors, we always
1380 * set data_length to a value between 0 and data_size. On error, setting
1381 * the key to empty is a good choice because an empty key representation is
1382 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001383 *data_length = 0;
1384
Ronald Cron9486f9d2020-11-26 13:36:23 +01001385 /* Export requires the EXPORT flag. There is an exception for public keys,
1386 * which don't require any flag, but
1387 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1390 PSA_KEY_USAGE_EXPORT, 0);
1391 if (status != PSA_SUCCESS) {
1392 return status;
1393 }
mohammad160306e79202018-03-28 13:17:44 +03001394
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001395 status = psa_driver_wrapper_export_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 slot->key.data, slot->key.bytes,
1397 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001398
Ryan Everetta103ec92024-01-31 13:59:57 +00001399 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001402}
1403
Ronald Cron7285cda2020-11-26 14:46:37 +01001404psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001405 const psa_key_attributes_t *attributes,
1406 const uint8_t *key_buffer,
1407 size_t key_buffer_size,
1408 uint8_t *data,
1409 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001411{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001412 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001413
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001414 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001415 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1416 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001417 /* Exporting public -> public */
1418 return psa_export_key_buffer_internal(
1419 key_buffer, key_buffer_size,
1420 data, data_size, data_length);
1421 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001422#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001423 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1424 return mbedtls_psa_rsa_export_public_key(attributes,
1425 key_buffer,
1426 key_buffer_size,
1427 data,
1428 data_size,
1429 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001430#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001431 /* We don't know how to convert a private RSA key to public. */
1432 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001433#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001434 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001435 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001436#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001437 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1438 return mbedtls_psa_ecp_export_public_key(attributes,
1439 key_buffer,
1440 key_buffer_size,
1441 data,
1442 data_size,
1443 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001444#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001445 /* We don't know how to convert a private ECC key to public */
1446 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001447#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001448 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001449 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001450#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001451 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001452 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001453 key_buffer,
1454 key_buffer_size,
1455 data, data_size,
1456 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001457#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001458 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001459#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001460 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001462 (void) key_buffer;
1463 (void) key_buffer_size;
1464 (void) data;
1465 (void) data_size;
1466 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001468 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001469}
Ronald Crond18b5f82020-11-25 16:46:05 +01001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1472 uint8_t *data,
1473 size_t data_size,
1474 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001475{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001476 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001477 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001478 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001479
Ronald Crone907e552021-01-18 13:22:38 +01001480 /* Reject a zero-length output buffer now, since this can never be a
1481 * valid key representation. This way we know that data must be a valid
1482 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (data_size == 0) {
1484 return PSA_ERROR_BUFFER_TOO_SMALL;
1485 }
Ronald Crone907e552021-01-18 13:22:38 +01001486
Darryl Greendd8fb772018-11-07 16:00:44 +00001487 /* Set the key to empty now, so that even when there are errors, we always
1488 * set data_length to a value between 0 and data_size. On error, setting
1489 * the key to empty is a good choice because an empty key representation is
1490 * unlikely to be accepted anywhere. */
1491 *data_length = 0;
1492
1493 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1495 if (status != PSA_SUCCESS) {
1496 return status;
1497 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1500 status = PSA_ERROR_INVALID_ARGUMENT;
1501 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001502 }
1503
Ronald Cron67227982020-11-26 15:16:05 +01001504 status = psa_driver_wrapper_export_public_key(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001505 &slot->attr, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001507
1508exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001509 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001512}
1513
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001514/** Validate that a key policy is internally well-formed.
1515 *
1516 * This function only rejects invalid policies. It does not validate the
1517 * consistency of the policy with respect to other attributes of the key
1518 * such as the key type.
1519 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001521{
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1523 PSA_KEY_USAGE_COPY |
1524 PSA_KEY_USAGE_ENCRYPT |
1525 PSA_KEY_USAGE_DECRYPT |
1526 PSA_KEY_USAGE_SIGN_MESSAGE |
1527 PSA_KEY_USAGE_VERIFY_MESSAGE |
1528 PSA_KEY_USAGE_SIGN_HASH |
1529 PSA_KEY_USAGE_VERIFY_HASH |
1530 PSA_KEY_USAGE_VERIFY_DERIVATION |
1531 PSA_KEY_USAGE_DERIVE)) != 0) {
1532 return PSA_ERROR_INVALID_ARGUMENT;
1533 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001536}
1537
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001538/** Validate the internal consistency of key attributes.
1539 *
1540 * This function only rejects invalid attribute values. If does not
1541 * validate the consistency of the attributes with any key data that may
1542 * be involved in the creation of the key.
1543 *
1544 * Call this function early in the key creation process.
1545 *
1546 * \param[in] attributes Key attributes for the new key.
1547 * \param[out] p_drv On any return, the driver for the key, if any.
1548 * NULL for a transparent key.
1549 *
1550 */
1551static psa_status_t psa_validate_key_attributes(
1552 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001554{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001555 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1557 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 status = psa_validate_key_location(lifetime, p_drv);
1560 if (status != PSA_SUCCESS) {
1561 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001562 }
1563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 status = psa_validate_key_persistence(lifetime);
1565 if (status != PSA_SUCCESS) {
1566 return status;
1567 }
1568
1569 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1570 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1571 return PSA_ERROR_INVALID_ARGUMENT;
1572 }
1573 } else {
1574 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1575 return PSA_ERROR_INVALID_ARGUMENT;
1576 }
1577 }
1578
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001579 status = psa_validate_key_policy(&attributes->policy);
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if (status != PSA_SUCCESS) {
1581 return status;
1582 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001583
1584 /* Refuse to create overly large keys.
1585 * Note that this doesn't trigger on import if the attributes don't
1586 * explicitly specify a size (so psa_get_key_bits returns 0), so
1587 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1589 return PSA_ERROR_NOT_SUPPORTED;
1590 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001593}
1594
Gilles Peskine4747d192019-04-17 15:05:45 +02001595/** Prepare a key slot to receive key material.
1596 *
1597 * This function allocates a key slot and sets its metadata.
1598 *
1599 * If this function fails, call psa_fail_key_creation().
1600 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001601 * This function is intended to be used as follows:
1602 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001603 * it with the specified attributes, and in case of a volatile key assign it
1604 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001605 * -# Populate the slot with the key material.
1606 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1607 * In case of failure at any step, stop the sequence and call
1608 * psa_fail_key_creation().
1609 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001610 * On success, the key slot's state is PSA_SLOT_FILLING.
1611 * It is the responsibility of the caller to change the slot's state to
1612 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001613 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001614 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001615 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001616 * \param[out] p_slot On success, a pointer to the prepared slot.
1617 * \param[out] p_drv On any return, the driver for the key, if any.
1618 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001619 *
1620 * \retval #PSA_SUCCESS
1621 * The key slot is ready to receive key material.
1622 * \return If this function fails, the key slot is an invalid state.
1623 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 */
1625static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001626 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001627 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001628 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001630{
1631 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001632 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001633 psa_key_slot_t *slot;
1634
Gilles Peskinedf179142019-07-15 22:02:14 +02001635 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001636 *p_drv = NULL;
1637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 status = psa_validate_key_attributes(attributes, p_drv);
1639 if (status != PSA_SUCCESS) {
1640 return status;
1641 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001642
Ryan Everett3d8118d2024-01-30 16:58:47 +00001643#if defined(MBEDTLS_THREADING_C)
1644 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1645 &mbedtls_threading_key_slot_mutex));
1646#endif
Ryan Everettb69118e2024-01-02 15:54:32 +00001647 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001648#if defined(MBEDTLS_THREADING_C)
1649 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1650 &mbedtls_threading_key_slot_mutex));
1651#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 if (status != PSA_SUCCESS) {
1653 return status;
1654 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001655 slot = *p_slot;
1656
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001657 /* We're storing the declared bit-size of the key. It's up to each
1658 * creation mechanism to verify that this information is correct.
1659 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001660 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001661 * is optional (import, copy). In case of a volatile key, assign it the
1662 * volatile key identifier associated to the slot returned to contain its
1663 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001664
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001665 slot->attr = *attributes;
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001667#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1668 slot->attr.id = volatile_key_id;
1669#else
1670 slot->attr.id.key_id = volatile_key_id;
1671#endif
1672 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001673
Gilles Peskinecbaff462019-07-12 23:46:04 +02001674#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001675 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001676 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001677 * create the key file in internal storage, create the
1678 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001679 * persistent data. This is done by starting a transaction that will
1680 * encompass these three actions.
1681 * For registering a volatile key, we just need to find an appropriate
1682 * slot number inside the SE. Since the key is designated volatile, creating
1683 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001684 /* The first thing to do is to find a slot number for the new key.
1685 * We save the slot number in persistent storage as part of the
1686 * transaction data. It will be needed to recover if the power
1687 * fails during the key creation process, to clean up on the secure
1688 * element side after restarting. Obtaining a slot number from the
1689 * secure element driver updates its persistent state, but we do not yet
1690 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001691 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001693 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1695 &slot_number);
1696 if (status != PSA_SUCCESS) {
1697 return status;
1698 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001699
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001700 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001702 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001703 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001704 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 status = psa_crypto_save_transaction();
1706 if (status != PSA_SUCCESS) {
1707 (void) psa_crypto_stop_transaction();
1708 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001709 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001710 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001711
1712 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001714 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001717 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001719 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001720#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1721
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001723}
Gilles Peskine4747d192019-04-17 15:05:45 +02001724
1725/** Finalize the creation of a key once its key material has been set.
1726 *
1727 * This entails writing the key to persistent storage.
1728 *
1729 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001730 * See the documentation of psa_start_key_creation() for the intended use
1731 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001732 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001733 * If the finalization succeeds, the function sets the key slot's state to
1734 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1735 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001736 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001737 * \param[in,out] slot Pointer to the slot with key material.
1738 * \param[in] driver The secure element driver for the key,
1739 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001740 * \param[out] key On success, identifier of the key. Note that the
1741 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001742 *
1743 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001744 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001745 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1746 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1747 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1748 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1749 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1750 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001751 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001752 * \return If this function fails, the key slot is an invalid state.
1753 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001754 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001755static psa_status_t psa_finish_key_creation(
1756 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001757 psa_se_drv_table_entry_t *driver,
1758 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001759{
1760 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001761 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001762 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001763
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001764#if defined(MBEDTLS_THREADING_C)
1765 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1766 &mbedtls_threading_key_slot_mutex));
1767#endif
1768
Gilles Peskine4747d192019-04-17 15:05:45 +02001769#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001771#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001773 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001774 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001776
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001777 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1778 sizeof(data.slot_number),
1779 "Slot number size does not match psa_se_key_data_storage_t");
1780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1782 status = psa_save_persistent_key(&slot->attr,
1783 (uint8_t *) &data,
1784 sizeof(data));
1785 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001786#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1787 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001788 /* Key material is saved in export representation in the slot, so
1789 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 status = psa_save_persistent_key(&slot->attr,
1791 slot->key.data,
1792 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001793 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001794 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001795#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1796
Gilles Peskinecbaff462019-07-12 23:46:04 +02001797#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001798 /* Finish the transaction for a key creation. This does not
1799 * happen when registering an existing key. Detect this case
1800 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001801 * creation of a persistent key in a secure element requires a transaction,
1802 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if (driver != NULL &&
1804 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1805 status = psa_save_se_persistent_data(driver);
1806 if (status != PSA_SUCCESS) {
1807 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001808
1809#if defined(MBEDTLS_THREADING_C)
1810 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1811 &mbedtls_threading_key_slot_mutex));
1812#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001814 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001816 }
1817#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001820 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001821 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1822 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001824 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001826 }
Ronald Cron50972942020-11-14 11:28:25 +01001827
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001828#if defined(MBEDTLS_THREADING_C)
1829 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1830 &mbedtls_threading_key_slot_mutex));
1831#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001833}
1834
1835/** Abort the creation of a key.
1836 *
1837 * You may call this function after calling psa_start_key_creation(),
1838 * or after psa_finish_key_creation() fails. In other circumstances, this
1839 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001840 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001841 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001842 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001843 * \param[in,out] slot Pointer to the slot with key material.
1844 * \param[in] driver The secure element driver for the key,
1845 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001846 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001847static void psa_fail_key_creation(psa_key_slot_t *slot,
1848 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001849{
Gilles Peskine011e4282019-06-26 18:34:38 +02001850 (void) driver;
1851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001853 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001855
Ryan Everettb7101442024-01-23 20:09:49 +00001856#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00001857 /* If the lock operation fails we still wipe the slot.
1858 * Operations will no longer work after a failed lock,
1859 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00001860 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
1861#endif
1862
Gilles Peskine011e4282019-06-26 18:34:38 +02001863#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001864 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001865 * element, and the failure happened later (when saving metadata
1866 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001867 * element.
1868 * https://github.com/ARMmbed/mbed-crypto/issues/217
1869 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001870
Gilles Peskined7729582019-08-05 15:55:54 +02001871 /* Abort the ongoing transaction if any (there may not be one if
1872 * the creation process failed before starting one, or if the
1873 * key creation is a registration of a key in a secure element).
1874 * Earlier functions must already have done what it takes to undo any
1875 * partial creation. All that's left is to update the transaction data
1876 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001878#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00001881
1882#if defined(MBEDTLS_THREADING_C)
1883 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
1884#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02001885}
1886
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001887/** Validate optional attributes during key creation.
1888 *
1889 * Some key attributes are optional during key creation. If they are
1890 * specified in the attributes structure, check that they are consistent
1891 * with the data in the slot.
1892 *
1893 * This function should be called near the end of key creation, after
1894 * the slot in memory is fully populated but before saving persistent data.
1895 */
1896static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001897 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001899{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001900 if (attributes->type != 0) {
1901 if (attributes->type != slot->attr.type) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 return PSA_ERROR_INVALID_ARGUMENT;
1903 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001904 }
1905
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001906 if (attributes->bits != 0) {
1907 if (attributes->bits != slot->attr.bits) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 return PSA_ERROR_INVALID_ARGUMENT;
1909 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001910 }
1911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001913}
1914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1916 const uint8_t *data,
1917 size_t data_length,
1918 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001919{
1920 psa_status_t status;
1921 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001922 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001923 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301924 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001925
Ronald Cron81709fc2020-11-14 12:10:32 +01001926 *key = MBEDTLS_SVC_KEY_ID_INIT;
1927
Gilles Peskine0f84d622019-09-12 19:03:13 +02001928 /* Reject zero-length symmetric keys (including raw data key objects).
1929 * This also rejects any key which might be encoded as an empty string,
1930 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 if (data_length == 0) {
1932 return PSA_ERROR_INVALID_ARGUMENT;
1933 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001934
Archana449608b2021-09-08 15:36:05 +05301935 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if (data_length > SIZE_MAX / 8) {
1937 return PSA_ERROR_NOT_SUPPORTED;
1938 }
Archana6ed4bda2021-08-04 10:47:15 +05301939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1941 &slot, &driver);
1942 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001943 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001945
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001946 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301947 * storage ( thus not in the case of importing a key in a secure element
1948 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1949 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001951 if (psa_key_lifetime_is_external(attributes->lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301952 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 attributes, data, data_length, &storage_size);
1954 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301955 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 }
Archanad8a83dc2021-06-14 10:04:16 +05301957 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 status = psa_allocate_buffer_to_slot(slot, storage_size);
1959 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001960 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001962 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001963
1964 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 status = psa_driver_wrapper_import_key(attributes,
1966 data, data_length,
1967 slot->key.data,
1968 slot->key.bytes,
1969 &slot->key.bytes, &bits);
1970 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001971 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001973
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001975 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001977 status = PSA_ERROR_INVALID_ARGUMENT;
1978 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001979 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001980
Archana6ed4bda2021-08-04 10:47:15 +05301981 /* Enforce a size limit, and in particular ensure that the bit
1982 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301984 status = PSA_ERROR_NOT_SUPPORTED;
1985 goto exit;
1986 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 status = psa_validate_optional_attributes(slot, attributes);
1988 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001989 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001993exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 if (status != PSA_SUCCESS) {
1995 psa_fail_key_creation(slot, driver);
1996 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001997
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001999}
2000
Gilles Peskined7729582019-08-05 15:55:54 +02002001#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2002psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002004{
2005 psa_status_t status;
2006 psa_key_slot_t *slot = NULL;
2007 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002008 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002009
2010 /* Leaving attributes unspecified is not currently supported.
2011 * It could make sense to query the key type and size from the
2012 * secure element, but not all secure elements support this
2013 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2015 return PSA_ERROR_NOT_SUPPORTED;
2016 }
2017 if (psa_get_key_bits(attributes) == 0) {
2018 return PSA_ERROR_NOT_SUPPORTED;
2019 }
Gilles Peskined7729582019-08-05 15:55:54 +02002020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2022 &slot, &driver);
2023 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002024 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 }
Gilles Peskined7729582019-08-05 15:55:54 +02002026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002028
2029exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 if (status != PSA_SUCCESS) {
2031 psa_fail_key_creation(slot, driver);
2032 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002033
Gilles Peskined7729582019-08-05 15:55:54 +02002034 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 psa_close_key(key);
2036 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002037}
2038#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2041 const psa_key_attributes_t *specified_attributes,
2042 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002043{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002044 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002045 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002046 psa_key_slot_t *source_slot = NULL;
2047 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002048 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002049 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302050 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002051
Ronald Cron81709fc2020-11-14 12:10:32 +01002052 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2053
Archana8a180362021-07-05 02:18:48 +05302054 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2056 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002057 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 status = psa_validate_optional_attributes(source_slot,
2061 specified_attributes);
2062 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002063 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002065
Archana9d17bf42021-09-10 06:22:44 +05302066 /* The target key type and number of bits have been validated by
2067 * psa_validate_optional_attributes() to be either equal to zero or
2068 * equal to the ones of the source key. So it is safe to inherit
2069 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302070 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002071 actual_attributes.bits = source_slot->attr.bits;
2072 actual_attributes.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302073
2074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 status = psa_restrict_key_policy(source_slot->attr.type,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002076 &actual_attributes.policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 &source_slot->attr.policy);
2078 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002079 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2083 &target_slot, &driver);
2084 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002085 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 }
2087 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2088 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302089 /*
Archana9d17bf42021-09-10 06:22:44 +05302090 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302091 * the source key would need to be exported as plaintext and re-imported
2092 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302093 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302094 * appropriate API invocations from the application, if needed.
2095 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002096 status = PSA_ERROR_NOT_SUPPORTED;
2097 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002098 }
Archana8a180362021-07-05 02:18:48 +05302099 /*
2100 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302101 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302102 * - For opaque keys this translates to an invocation of the drivers'
2103 * copy_key entry point through the dispatch layer.
2104 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002105 if (psa_key_lifetime_is_external(actual_attributes.lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2107 &storage_size);
2108 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302109 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 }
Archana374fe5b2021-09-08 15:50:28 +05302111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2113 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302114 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 }
Archana374fe5b2021-09-08 15:50:28 +05302116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 status = psa_driver_wrapper_copy_key(&actual_attributes,
2118 source_slot->key.data,
2119 source_slot->key.bytes,
2120 target_slot->key.data,
2121 target_slot->key.bytes,
2122 &target_slot->key.bytes);
2123 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 }
2126 } else {
2127 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302128 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 source_slot->key.bytes);
2130 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302131 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 }
Archana8a180362021-07-05 02:18:48 +05302133 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002135exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 if (status != PSA_SUCCESS) {
2137 psa_fail_key_creation(target_slot, driver);
2138 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002139
Ryan Everetta103ec92024-01-31 13:59:57 +00002140 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002143}
2144
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002145
2146
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002147/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002148/* Message digests */
2149/****************************************************************/
2150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002152{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002153 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (operation->id == 0) {
2155 return PSA_SUCCESS;
2156 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002159 operation->id = 0;
2160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002162}
2163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2165 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002166{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002167 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002168
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002169 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002171 status = PSA_ERROR_BAD_STATE;
2172 goto exit;
2173 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002176 status = PSA_ERROR_INVALID_ARGUMENT;
2177 goto exit;
2178 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002179
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002180 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2181 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002185
2186exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 if (status != PSA_SUCCESS) {
2188 psa_hash_abort(operation);
2189 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002190
2191 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002192}
2193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2195 const uint8_t *input,
2196 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002197{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002198 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002201 status = PSA_ERROR_BAD_STATE;
2202 goto exit;
2203 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002204
Steven Cooremanc8288352021-03-04 14:02:19 +01002205 /* Don't require hash implementations to behave correctly on a
2206 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (input_length == 0) {
2208 return PSA_SUCCESS;
2209 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002212
2213exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 if (status != PSA_SUCCESS) {
2215 psa_hash_abort(operation);
2216 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002219}
2220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2222 uint8_t *hash,
2223 size_t hash_size,
2224 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002225{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002226 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if (operation->id == 0) {
2228 return PSA_ERROR_BAD_STATE;
2229 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002230
Steven Cooreman1e582352021-02-18 17:24:37 +01002231 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 operation, hash, hash_size, hash_length);
2233 psa_hash_abort(operation);
2234 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002235}
2236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2238 const uint8_t *hash,
2239 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002240{
Ronald Cron69a63422021-10-18 09:47:58 +02002241 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002243 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 operation,
2245 actual_hash, sizeof(actual_hash),
2246 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002249 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002253 status = PSA_ERROR_INVALID_SIGNATURE;
2254 goto exit;
2255 }
2256
Dave Rodgman78701152023-08-29 14:20:18 +01002257 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002258 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002260
2261exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2263 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002264 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002268}
2269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270psa_status_t psa_hash_compute(psa_algorithm_t alg,
2271 const uint8_t *input, size_t input_length,
2272 uint8_t *hash, size_t hash_size,
2273 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002274{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002275 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 if (!PSA_ALG_IS_HASH(alg)) {
2277 return PSA_ERROR_INVALID_ARGUMENT;
2278 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2281 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002282}
2283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284psa_status_t psa_hash_compare(psa_algorithm_t alg,
2285 const uint8_t *input, size_t input_length,
2286 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002287{
Ronald Cron69a63422021-10-18 09:47:58 +02002288 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002289 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if (!PSA_ALG_IS_HASH(alg)) {
2292 return PSA_ERROR_INVALID_ARGUMENT;
2293 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002294
Steven Cooreman1e582352021-02-18 17:24:37 +01002295 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 alg, input, input_length,
2297 actual_hash, sizeof(actual_hash),
2298 &actual_hash_length);
2299 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002300 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 }
2302 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002303 status = PSA_ERROR_INVALID_SIGNATURE;
2304 goto exit;
2305 }
Dave Rodgman78701152023-08-29 14:20:18 +01002306 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002307 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002309
2310exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2312 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002313}
2314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2316 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002317{
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 if (source_operation->id == 0 ||
2319 target_operation->id != 0) {
2320 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002321 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2324 target_operation);
2325 if (status != PSA_SUCCESS) {
2326 psa_hash_abort(target_operation);
2327 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002330}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331
2332
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002333/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002334/* MAC */
2335/****************************************************************/
2336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002338{
Steven Cooremane6804192021-03-19 18:28:56 +01002339 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (operation->id == 0) {
2341 return PSA_SUCCESS;
2342 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002345 operation->mac_size = 0;
2346 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002347 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002350}
2351
Ronald Cron2dff3b22021-06-17 16:33:22 +02002352static psa_status_t psa_mac_finalize_alg_and_key_validation(
2353 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002354 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002356{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002357 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 psa_key_type_t key_type = psa_get_key_type(attributes);
2359 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 if (!PSA_ALG_IS_MAC(alg)) {
2362 return PSA_ERROR_INVALID_ARGUMENT;
2363 }
Steven Cooremane6804192021-03-19 18:28:56 +01002364
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002365 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 status = psa_mac_key_can_do(alg, key_type);
2367 if (status != PSA_SUCCESS) {
2368 return status;
2369 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002370
Steven Cooremand1a68f12021-05-10 11:13:41 +02002371 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002375 /* A very short MAC is too short for security since it can be
2376 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2377 * so we make this our minimum, even though 32 bits is still
2378 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002380 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2383 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002384 /* It's impossible to "truncate" to a larger length than the full length
2385 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002387 }
2388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002390 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2391 * that is disabled in the compile-time configuration. The result can
2392 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2393 * configuration into account. In this case, force a return of
2394 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2395 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2396 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2397 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2398 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002400 }
2401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002403}
2404
Gilles Peskine449bd832023-01-11 14:50:10 +01002405static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2406 mbedtls_svc_key_id_t key,
2407 psa_algorithm_t alg,
2408 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002409{
2410 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2411 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002412 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002413
2414 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002416 status = PSA_ERROR_BAD_STATE;
2417 goto exit;
2418 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002419
2420 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 key,
2422 &slot,
2423 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2424 alg);
2425 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002426 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002428
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002429 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 &operation->mac_size);
2431 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002434
2435 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002436 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 if (is_sign) {
2438 status = psa_driver_wrapper_mac_sign_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002439 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 slot->key.data,
2441 slot->key.bytes,
2442 alg);
2443 } else {
2444 status = psa_driver_wrapper_mac_verify_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002445 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 slot->key.data,
2447 slot->key.bytes,
2448 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002449 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002450
Gilles Peskinefbfac682018-07-08 20:51:54 +02002451exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 if (status != PSA_SUCCESS) {
2453 psa_mac_abort(operation);
2454 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002455
Ryan Everettfb9857f2024-02-14 12:16:41 +00002456 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459}
2460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2462 mbedtls_svc_key_id_t key,
2463 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002464{
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002466}
2467
Gilles Peskine449bd832023-01-11 14:50:10 +01002468psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2469 mbedtls_svc_key_id_t key,
2470 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002471{
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002473}
2474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2476 const uint8_t *input,
2477 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002478{
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 if (operation->id == 0) {
2480 return PSA_ERROR_BAD_STATE;
2481 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002482
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002483 /* Don't require hash implementations to behave correctly on a
2484 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 if (input_length == 0) {
2486 return PSA_SUCCESS;
2487 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2490 input, input_length);
2491 if (status != PSA_SUCCESS) {
2492 psa_mac_abort(operation);
2493 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002496}
2497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2499 uint8_t *mac,
2500 size_t mac_size,
2501 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002502{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002503 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2504 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002507 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002508 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002509 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002512 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002513 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002514 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002515
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002516 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2517 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002519 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002520 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002521 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002524 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002525 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002526 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 status = psa_driver_wrapper_mac_sign_finish(operation,
2529 mac, operation->mac_size,
2530 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002531
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002532exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002533 /* In case of success, set the potential excess room in the output buffer
2534 * to an invalid value, to avoid potentially leaking a longer MAC.
2535 * In case of error, set the output length and content to a safe default,
2536 * such that in case the caller misses an error check, the output would be
2537 * an unachievable MAC.
2538 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002540 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002541 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002542 }
mohammad16036df908f2018-04-02 08:34:15 -07002543
Paul Elliottdc42ca82023-02-24 18:11:59 +00002544 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002549}
2550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2552 const uint8_t *mac,
2553 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002554{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002555 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2556 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002559 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002560 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002561 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002564 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002565 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002566 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002569 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002570 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002571 }
2572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 status = psa_driver_wrapper_mac_verify_finish(operation,
2574 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002575
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580}
2581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2583 psa_algorithm_t alg,
2584 const uint8_t *input,
2585 size_t input_length,
2586 uint8_t *mac,
2587 size_t mac_size,
2588 size_t *mac_length,
2589 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002590{
2591 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002592 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2593 psa_key_slot_t *slot;
2594 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595
Ronald Cron51131b52021-06-17 17:17:20 +02002596 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 key,
2598 &slot,
2599 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2600 alg);
2601 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002602 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002604
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002605 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 &operation_mac_size);
2607 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002608 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002612 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002613 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002614 }
2615
2616 status = psa_driver_wrapper_mac_compute(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002617 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 slot->key.data, slot->key.bytes,
2619 alg,
2620 input, input_length,
2621 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002622
2623exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002624 /* In case of success, set the potential excess room in the output buffer
2625 * to an invalid value, to avoid potentially leaking a longer MAC.
2626 * In case of error, set the output length and content to a safe default,
2627 * such that in case the caller misses an error check, the output would be
2628 * an unachievable MAC.
2629 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002631 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002632 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002633 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002634
2635 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002636
Ryan Everetta103ec92024-01-31 13:59:57 +00002637 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002640}
2641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002643 psa_algorithm_t alg,
2644 const uint8_t *input,
2645 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 uint8_t *mac,
2647 size_t mac_size,
2648 size_t *mac_length)
2649{
2650 return psa_mac_compute_internal(key, alg,
2651 input, input_length,
2652 mac, mac_size, mac_length, 1);
2653}
2654
2655psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2656 psa_algorithm_t alg,
2657 const uint8_t *input,
2658 size_t input_length,
2659 const uint8_t *mac,
2660 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002661{
2662 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002663 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2664 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 status = psa_mac_compute_internal(key, alg,
2667 input, input_length,
2668 actual_mac, sizeof(actual_mac),
2669 &actual_mac_length, 0);
2670 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002671 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002675 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002676 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002677 }
Dave Rodgman78701152023-08-29 14:20:18 +01002678 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002679 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002680 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002681 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002682
2683exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002687}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002688
Gilles Peskinee59236f2018-01-27 23:32:46 +01002689/****************************************************************/
2690/* Asymmetric cryptography */
2691/****************************************************************/
2692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2694 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002695{
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 if (input_is_message) {
2697 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2698 return PSA_ERROR_INVALID_ARGUMENT;
2699 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2702 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2703 return PSA_ERROR_INVALID_ARGUMENT;
2704 }
2705 }
2706 } else {
2707 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2708 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002709 }
2710 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002713}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2716 int input_is_message,
2717 psa_algorithm_t alg,
2718 const uint8_t *input,
2719 size_t input_length,
2720 uint8_t *signature,
2721 size_t signature_size,
2722 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002723{
2724 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2725 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2726 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002727
2728 *signature_length = 0;
2729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 status = psa_sign_verify_check_alg(input_is_message, alg);
2731 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002732 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002734
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002735 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002736 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002737 * buffer can in principle be empty since it doesn't actually have
2738 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 if (signature_size == 0) {
2740 return PSA_ERROR_BUFFER_TOO_SMALL;
2741 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002742
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002743 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 key, &slot,
2745 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2746 PSA_KEY_USAGE_SIGN_HASH,
2747 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002750 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002754 status = PSA_ERROR_INVALID_ARGUMENT;
2755 goto exit;
2756 }
2757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002759 status = psa_driver_wrapper_sign_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002760 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002761 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 signature, signature_size, signature_length);
2763 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002764
2765 status = psa_driver_wrapper_sign_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002766 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002767 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002769 }
2770
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002771
2772exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002773 psa_wipe_tag_output_buffer(signature, status, signature_size,
2774 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002775
Ryan Everetta103ec92024-01-31 13:59:57 +00002776 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002779}
2780
Gilles Peskine449bd832023-01-11 14:50:10 +01002781static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2782 int input_is_message,
2783 psa_algorithm_t alg,
2784 const uint8_t *input,
2785 size_t input_length,
2786 const uint8_t *signature,
2787 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002788{
2789 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2790 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2791 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 status = psa_sign_verify_check_alg(input_is_message, alg);
2794 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002795 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002797
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002798 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 key, &slot,
2800 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2801 PSA_KEY_USAGE_VERIFY_HASH,
2802 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 if (status != PSA_SUCCESS) {
2805 return status;
2806 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002807
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002809 status = psa_driver_wrapper_verify_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002810 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002811 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 signature, signature_length);
2813 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002814 status = psa_driver_wrapper_verify_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002815 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002816 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002818 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002819
Ryan Everetta103ec92024-01-31 13:59:57 +00002820 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002821
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002823
2824}
2825
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002826psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002827 const psa_key_attributes_t *attributes,
2828 const uint8_t *key_buffer,
2829 size_t key_buffer_size,
2830 psa_algorithm_t alg,
2831 const uint8_t *input,
2832 size_t input_length,
2833 uint8_t *signature,
2834 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002836{
2837 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002840 size_t hash_length;
2841 uint8_t hash[PSA_HASH_MAX_SIZE];
2842
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002843 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 PSA_ALG_SIGN_GET_HASH(alg),
2845 input, input_length,
2846 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002847
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002849 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002851
2852 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 attributes, key_buffer, key_buffer_size,
2854 alg, hash, hash_length,
2855 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002856 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002859}
2860
Gilles Peskine449bd832023-01-11 14:50:10 +01002861psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2862 psa_algorithm_t alg,
2863 const uint8_t *input,
2864 size_t input_length,
2865 uint8_t *signature,
2866 size_t signature_size,
2867 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002868{
2869 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002870 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002872}
2873
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002874psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002875 const psa_key_attributes_t *attributes,
2876 const uint8_t *key_buffer,
2877 size_t key_buffer_size,
2878 psa_algorithm_t alg,
2879 const uint8_t *input,
2880 size_t input_length,
2881 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002883{
2884 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002887 size_t hash_length;
2888 uint8_t hash[PSA_HASH_MAX_SIZE];
2889
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002890 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 PSA_ALG_SIGN_GET_HASH(alg),
2892 input, input_length,
2893 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002894
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002896 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002898
2899 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 attributes, key_buffer, key_buffer_size,
2901 alg, hash, hash_length,
2902 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002903 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002904
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002906}
2907
Gilles Peskine449bd832023-01-11 14:50:10 +01002908psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2909 psa_algorithm_t alg,
2910 const uint8_t *input,
2911 size_t input_length,
2912 const uint8_t *signature,
2913 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002914{
2915 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002916 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002918}
2919
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002920psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002921 const psa_key_attributes_t *attributes,
2922 const uint8_t *key_buffer, size_t key_buffer_size,
2923 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002925{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002926 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2928 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002929#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2931 return mbedtls_psa_rsa_sign_hash(
2932 attributes,
2933 key_buffer, key_buffer_size,
2934 alg, hash, hash_length,
2935 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002936#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2937 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 } else {
2939 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002940 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002941 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002943#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2945 return mbedtls_psa_ecdsa_sign_hash(
2946 attributes,
2947 key_buffer, key_buffer_size,
2948 alg, hash, hash_length,
2949 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002950#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2951 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 } else {
2953 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002954 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002955 }
Ronald Cron4501c982021-03-19 20:09:32 +01002956
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 (void) key_buffer;
2958 (void) key_buffer_size;
2959 (void) hash;
2960 (void) hash_length;
2961 (void) signature;
2962 (void) signature_size;
2963 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002966}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002967
Gilles Peskine449bd832023-01-11 14:50:10 +01002968psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2969 psa_algorithm_t alg,
2970 const uint8_t *hash,
2971 size_t hash_length,
2972 uint8_t *signature,
2973 size_t signature_size,
2974 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002975{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002976 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002977 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002979}
2980
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002981psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002982 const psa_key_attributes_t *attributes,
2983 const uint8_t *key_buffer, size_t key_buffer_size,
2984 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002986{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002987 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2989 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002990#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2992 return mbedtls_psa_rsa_verify_hash(
2993 attributes,
2994 key_buffer, key_buffer_size,
2995 alg, hash, hash_length,
2996 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002997#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2998 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 } else {
3000 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003001 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003002 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003004#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3006 return mbedtls_psa_ecdsa_verify_hash(
3007 attributes,
3008 key_buffer, key_buffer_size,
3009 alg, hash, hash_length,
3010 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003011#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3012 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 } else {
3014 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003015 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003016 }
Ronald Cron4501c982021-03-19 20:09:32 +01003017
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 (void) key_buffer;
3019 (void) key_buffer_size;
3020 (void) hash;
3021 (void) hash_length;
3022 (void) signature;
3023 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003026}
3027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3029 psa_algorithm_t alg,
3030 const uint8_t *hash,
3031 size_t hash_length,
3032 const uint8_t *signature,
3033 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003034{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003035 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003036 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003038}
3039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3041 psa_algorithm_t alg,
3042 const uint8_t *input,
3043 size_t input_length,
3044 const uint8_t *salt,
3045 size_t salt_length,
3046 uint8_t *output,
3047 size_t output_size,
3048 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003049{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003050 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003051 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003052 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003053
Darryl Green5cc689a2018-07-24 15:34:10 +01003054 (void) input;
3055 (void) input_length;
3056 (void) salt;
3057 (void) output;
3058 (void) output_size;
3059
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003060 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003061
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3063 return PSA_ERROR_INVALID_ARGUMENT;
3064 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003065
Valerio Setti5bb454a2024-01-15 10:43:16 +01003066 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3068 if (status != PSA_SUCCESS) {
3069 return status;
3070 }
3071 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3072 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003073 status = PSA_ERROR_INVALID_ARGUMENT;
3074 goto exit;
3075 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003076
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003077 status = psa_driver_wrapper_asymmetric_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003078 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003079 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003081exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003082 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003085}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003086
Gilles Peskine449bd832023-01-11 14:50:10 +01003087psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3088 psa_algorithm_t alg,
3089 const uint8_t *input,
3090 size_t input_length,
3091 const uint8_t *salt,
3092 size_t salt_length,
3093 uint8_t *output,
3094 size_t output_size,
3095 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003096{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003097 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003098 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003099 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003100
Darryl Green5cc689a2018-07-24 15:34:10 +01003101 (void) input;
3102 (void) input_length;
3103 (void) salt;
3104 (void) output;
3105 (void) output_size;
3106
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003107 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003108
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3110 return PSA_ERROR_INVALID_ARGUMENT;
3111 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003112
Valerio Setti5bb454a2024-01-15 10:43:16 +01003113 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3115 if (status != PSA_SUCCESS) {
3116 return status;
3117 }
3118 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003119 status = PSA_ERROR_INVALID_ARGUMENT;
3120 goto exit;
3121 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003122
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003123 status = psa_driver_wrapper_asymmetric_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003124 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003125 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003127
3128exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003129 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003132}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003133
Paul Elliott9fe12f62022-11-30 19:16:02 +00003134/****************************************************************/
3135/* Asymmetric interruptible cryptography */
3136/****************************************************************/
3137
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003138static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3139
Paul Elliott9fe12f62022-11-30 19:16:02 +00003140void psa_interruptible_set_max_ops(uint32_t max_ops)
3141{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003142 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003143}
3144
3145uint32_t psa_interruptible_get_max_ops(void)
3146{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003147 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003148}
3149
Paul Elliott9fe12f62022-11-30 19:16:02 +00003150uint32_t psa_sign_hash_get_num_ops(
3151 const psa_sign_hash_interruptible_operation_t *operation)
3152{
Paul Elliott296ede92022-12-15 17:00:30 +00003153 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003154}
3155
3156uint32_t psa_verify_hash_get_num_ops(
3157 const psa_verify_hash_interruptible_operation_t *operation)
3158{
Paul Elliott296ede92022-12-15 17:00:30 +00003159 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003160}
3161
Paul Elliott59ad9452022-12-18 15:09:02 +00003162static psa_status_t psa_sign_hash_abort_internal(
3163 psa_sign_hash_interruptible_operation_t *operation)
3164{
3165 if (operation->id == 0) {
3166 /* The object has (apparently) been initialized but it is not (yet)
3167 * in use. It's ok to call abort on such an object, and there's
3168 * nothing to do. */
3169 return PSA_SUCCESS;
3170 }
3171
3172 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3173
3174 status = psa_driver_wrapper_sign_hash_abort(operation);
3175
3176 operation->id = 0;
3177
Paul Elliotte6145dc2023-02-07 12:51:21 +00003178 /* Do not clear either the error_occurred or num_ops elements here as they
3179 * only want to be cleared by the application calling abort, not by abort
3180 * being called at completion of an operation. */
3181
Paul Elliott59ad9452022-12-18 15:09:02 +00003182 return status;
3183}
3184
Paul Elliott9fe12f62022-11-30 19:16:02 +00003185psa_status_t psa_sign_hash_start(
3186 psa_sign_hash_interruptible_operation_t *operation,
3187 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3188 const uint8_t *hash, size_t hash_length)
3189{
3190 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3191 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3192 psa_key_slot_t *slot;
3193
Paul Elliottc9774412023-02-06 15:14:07 +00003194 /* Check that start has not been previously called, or operation has not
3195 * previously errored. */
3196 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003197 return PSA_ERROR_BAD_STATE;
3198 }
3199
Paul Elliott9fe12f62022-11-30 19:16:02 +00003200 status = psa_sign_verify_check_alg(0, alg);
3201 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003202 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003203 return status;
3204 }
3205
3206 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3207 PSA_KEY_USAGE_SIGN_HASH,
3208 alg);
3209
3210 if (status != PSA_SUCCESS) {
3211 goto exit;
3212 }
3213
3214 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3215 status = PSA_ERROR_INVALID_ARGUMENT;
3216 goto exit;
3217 }
3218
Paul Elliott296ede92022-12-15 17:00:30 +00003219 /* Ensure ops count gets reset, in case of operation re-use. */
3220 operation->num_ops = 0;
3221
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003222 status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003223 slot->key.data,
3224 slot->key.bytes, alg,
3225 hash, hash_length);
3226exit:
3227
3228 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003229 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003230 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003231 }
3232
Ryan Everettdcc03d52024-02-14 15:44:13 +00003233 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003234
Paul Elliottc9774412023-02-06 15:14:07 +00003235 if (unlock_status != PSA_SUCCESS) {
3236 operation->error_occurred = 1;
3237 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003238
Paul Elliottc9774412023-02-06 15:14:07 +00003239 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003240}
3241
3242
3243psa_status_t psa_sign_hash_complete(
3244 psa_sign_hash_interruptible_operation_t *operation,
3245 uint8_t *signature, size_t signature_size,
3246 size_t *signature_length)
3247{
3248 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3249
3250 *signature_length = 0;
3251
Paul Elliottc9774412023-02-06 15:14:07 +00003252 /* Check that start has been called first, and that operation has not
3253 * previously errored. */
3254 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003255 status = PSA_ERROR_BAD_STATE;
3256 goto exit;
3257 }
3258
Paul Elliott096abc42023-02-03 18:33:23 +00003259 /* Immediately reject a zero-length signature buffer. This guarantees that
3260 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003261 if (signature_size == 0) {
3262 status = PSA_ERROR_BUFFER_TOO_SMALL;
3263 goto exit;
3264 }
3265
3266 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3267 signature_size,
3268 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003269
Paul Elliott296ede92022-12-15 17:00:30 +00003270 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003271 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003272
Paul Elliottebe225c2023-02-10 14:32:53 +00003273exit:
3274
Paul Elliott59200a22023-02-21 15:07:40 +00003275 psa_wipe_tag_output_buffer(signature, status, signature_size,
3276 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003277
Paul Elliott53bb3122023-02-10 14:22:22 +00003278 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003279 if (status != PSA_SUCCESS) {
3280 operation->error_occurred = 1;
3281 }
3282
Paul Elliott59ad9452022-12-18 15:09:02 +00003283 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003284 }
3285
3286 return status;
3287}
3288
3289psa_status_t psa_sign_hash_abort(
3290 psa_sign_hash_interruptible_operation_t *operation)
3291{
Paul Elliott59ad9452022-12-18 15:09:02 +00003292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3293
3294 status = psa_sign_hash_abort_internal(operation);
3295
3296 /* We clear the number of ops done here, so that it is not cleared when
3297 * the operation fails or succeeds, only on manual abort. */
3298 operation->num_ops = 0;
3299
Paul Elliottc9774412023-02-06 15:14:07 +00003300 /* Likewise, failure state. */
3301 operation->error_occurred = 0;
3302
Paul Elliott59ad9452022-12-18 15:09:02 +00003303 return status;
3304}
3305
3306static psa_status_t psa_verify_hash_abort_internal(
3307 psa_verify_hash_interruptible_operation_t *operation)
3308{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003309 if (operation->id == 0) {
3310 /* The object has (apparently) been initialized but it is not (yet)
3311 * in use. It's ok to call abort on such an object, and there's
3312 * nothing to do. */
3313 return PSA_SUCCESS;
3314 }
3315
Paul Elliott59ad9452022-12-18 15:09:02 +00003316 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3317
3318 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003319
3320 operation->id = 0;
3321
Paul Elliotte6145dc2023-02-07 12:51:21 +00003322 /* Do not clear either the error_occurred or num_ops elements here as they
3323 * only want to be cleared by the application calling abort, not by abort
3324 * being called at completion of an operation. */
3325
Paul Elliott59ad9452022-12-18 15:09:02 +00003326 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003327}
3328
3329psa_status_t psa_verify_hash_start(
3330 psa_verify_hash_interruptible_operation_t *operation,
3331 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3332 const uint8_t *hash, size_t hash_length,
3333 const uint8_t *signature, size_t signature_length)
3334{
3335 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3336 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3337 psa_key_slot_t *slot;
3338
Paul Elliottc9774412023-02-06 15:14:07 +00003339 /* Check that start has not been previously called, or operation has not
3340 * previously errored. */
3341 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003342 return PSA_ERROR_BAD_STATE;
3343 }
3344
3345 status = psa_sign_verify_check_alg(0, alg);
3346 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003347 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003348 return status;
3349 }
3350
3351 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3352 PSA_KEY_USAGE_VERIFY_HASH,
3353 alg);
3354
3355 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003356 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003357 return status;
3358 }
3359
Paul Elliott296ede92022-12-15 17:00:30 +00003360 /* Ensure ops count gets reset, in case of operation re-use. */
3361 operation->num_ops = 0;
3362
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003363 status = psa_driver_wrapper_verify_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003364 slot->key.data,
3365 slot->key.bytes,
3366 alg, hash, hash_length,
3367 signature, signature_length);
3368
3369 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003370 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003371 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003372 }
3373
Ryan Everett291267f2024-02-14 15:59:15 +00003374 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003375
Paul Elliottc9774412023-02-06 15:14:07 +00003376 if (unlock_status != PSA_SUCCESS) {
3377 operation->error_occurred = 1;
3378 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003379
Paul Elliottc9774412023-02-06 15:14:07 +00003380 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003381}
3382
3383psa_status_t psa_verify_hash_complete(
3384 psa_verify_hash_interruptible_operation_t *operation)
3385{
3386 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3387
Paul Elliottc9774412023-02-06 15:14:07 +00003388 /* Check that start has been called first, and that operation has not
3389 * previously errored. */
3390 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003391 status = PSA_ERROR_BAD_STATE;
3392 goto exit;
3393 }
3394
3395 status = psa_driver_wrapper_verify_hash_complete(operation);
3396
Paul Elliott296ede92022-12-15 17:00:30 +00003397 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003398 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003399 operation);
3400
Paul Elliottebe225c2023-02-10 14:32:53 +00003401exit:
3402
Paul Elliott9fe12f62022-11-30 19:16:02 +00003403 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003404 if (status != PSA_SUCCESS) {
3405 operation->error_occurred = 1;
3406 }
3407
Paul Elliott59ad9452022-12-18 15:09:02 +00003408 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003409 }
3410
3411 return status;
3412}
3413
3414psa_status_t psa_verify_hash_abort(
3415 psa_verify_hash_interruptible_operation_t *operation)
3416{
Paul Elliott59ad9452022-12-18 15:09:02 +00003417 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003418
Paul Elliott59ad9452022-12-18 15:09:02 +00003419 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003420
Paul Elliott59ad9452022-12-18 15:09:02 +00003421 /* We clear the number of ops done here, so that it is not cleared when
3422 * the operation fails or succeeds, only on manual abort. */
3423 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003424
Paul Elliottc9774412023-02-06 15:14:07 +00003425 /* Likewise, failure state. */
3426 operation->error_occurred = 0;
3427
Paul Elliott59ad9452022-12-18 15:09:02 +00003428 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003429}
3430
Paul Elliott588f8ed2022-12-02 18:10:26 +00003431/****************************************************************/
3432/* Asymmetric interruptible cryptography internal */
3433/* implementations */
3434/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003435
Paul Elliott588f8ed2022-12-02 18:10:26 +00003436void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3437{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003438
Paul Elliott588f8ed2022-12-02 18:10:26 +00003439#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3440 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3441 defined(MBEDTLS_ECP_RESTARTABLE)
3442
3443 /* Internal implementation uses zero to indicate infinite number max ops,
3444 * therefore avoid this value, and set to minimum possible. */
3445 if (max_ops == 0) {
3446 max_ops = 1;
3447 }
3448
Paul Elliott588f8ed2022-12-02 18:10:26 +00003449 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003450#else
3451 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003452#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3453 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3454 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3455}
3456
Paul Elliott588f8ed2022-12-02 18:10:26 +00003457uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003458 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003459{
3460#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3461 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3462 defined(MBEDTLS_ECP_RESTARTABLE)
3463
Paul Elliott93d9ca82023-02-15 18:14:21 +00003464 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003465#else
3466 (void) operation;
3467 return 0;
3468#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3469 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3470 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3471}
3472
3473uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003474 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003475{
3476 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3477 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3478 defined(MBEDTLS_ECP_RESTARTABLE)
3479
Paul Elliott93d9ca82023-02-15 18:14:21 +00003480 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003481#else
3482 (void) operation;
3483 return 0;
3484#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3485 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3486 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3487}
3488
3489psa_status_t mbedtls_psa_sign_hash_start(
3490 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3491 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3492 size_t key_buffer_size, psa_algorithm_t alg,
3493 const uint8_t *hash, size_t hash_length)
3494{
3495 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003496 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003497
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003498 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003499 return PSA_ERROR_NOT_SUPPORTED;
3500 }
3501
3502 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003503 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003504 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003505
3506#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003507 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3508 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003509
Paul Elliotta1c94092023-02-15 16:38:04 +00003510 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3511
Paul Elliott93d9ca82023-02-15 18:14:21 +00003512 /* Ensure num_ops is zero'ed in case of context re-use. */
3513 operation->num_ops = 0;
3514
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003515 status = mbedtls_psa_ecp_load_representation(attributes->type,
3516 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003517 key_buffer,
3518 key_buffer_size,
3519 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003520
Paul Elliott068fe072023-01-16 13:59:15 +00003521 if (status != PSA_SUCCESS) {
3522 return status;
3523 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003524
Paul Elliott1bc59df2023-02-05 13:41:57 +00003525 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003526 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003527
Paul Elliott068fe072023-01-16 13:59:15 +00003528 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003529 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003530 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003531
Paul Elliott0290a762023-02-09 14:30:24 +00003532 /* We only need to store the same length of hash as the private key size
3533 * here, it would be truncated by the internal implementation anyway. */
3534 required_hash_length = (hash_length < operation->coordinate_bytes ?
3535 hash_length : operation->coordinate_bytes);
3536
Paul Elliottba70ad42023-02-15 18:23:53 +00003537 if (required_hash_length > sizeof(operation->hash)) {
3538 /* Shouldn't happen, but better safe than sorry. */
3539 return PSA_ERROR_CORRUPTION_DETECTED;
3540 }
3541
Paul Elliott0290a762023-02-09 14:30:24 +00003542 memcpy(operation->hash, hash, required_hash_length);
3543 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003544
3545 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003546
3547#else
Paul Elliott068fe072023-01-16 13:59:15 +00003548 (void) operation;
3549 (void) key_buffer;
3550 (void) key_buffer_size;
3551 (void) alg;
3552 (void) hash;
3553 (void) hash_length;
3554 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003555 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003556
Paul Elliott068fe072023-01-16 13:59:15 +00003557 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003558#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3559 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3560 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003561}
3562
3563psa_status_t mbedtls_psa_sign_hash_complete(
3564 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3565 uint8_t *signature, size_t signature_size,
3566 size_t *signature_length)
3567{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003568#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3569 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3570 defined(MBEDTLS_ECP_RESTARTABLE)
3571
Paul Elliotta1c94092023-02-15 16:38:04 +00003572 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003573 mbedtls_mpi r;
3574 mbedtls_mpi s;
3575
Paul Elliott46845252023-02-03 14:59:11 +00003576 mbedtls_mpi_init(&r);
3577 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003578
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003579 /* Ensure max_ops is set to the current value (or default). */
3580 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3581
Paul Elliotta1c94092023-02-15 16:38:04 +00003582 if (signature_size < 2 * operation->coordinate_bytes) {
3583 status = PSA_ERROR_BUFFER_TOO_SMALL;
3584 goto exit;
3585 }
3586
Paul Elliott588f8ed2022-12-02 18:10:26 +00003587 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003588
Paul Elliott588f8ed2022-12-02 18:10:26 +00003589#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3590 status = mbedtls_to_psa_error(
3591 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003592 &r,
3593 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003594 &operation->ctx->d,
3595 operation->hash,
3596 operation->hash_length,
3597 operation->md_alg,
3598 mbedtls_psa_get_random,
3599 MBEDTLS_PSA_RANDOM_STATE,
3600 &operation->restart_ctx));
3601#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003602 status = PSA_ERROR_NOT_SUPPORTED;
3603 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003604#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3605 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003606 status = mbedtls_to_psa_error(
3607 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003608 &r,
3609 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003610 &operation->ctx->d,
3611 operation->hash,
3612 operation->hash_length,
3613 mbedtls_psa_get_random,
3614 MBEDTLS_PSA_RANDOM_STATE,
3615 mbedtls_psa_get_random,
3616 MBEDTLS_PSA_RANDOM_STATE,
3617 &operation->restart_ctx));
3618 }
3619
Paul Elliottf8e5b562023-02-19 18:43:45 +00003620 /* Hide the fact that the restart context only holds a delta of number of
3621 * ops done during the last operation, not an absolute value. */
3622 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3623
Paul Elliott724bd252023-02-08 12:35:08 +00003624 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003625 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003626 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003627 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003628 operation->coordinate_bytes)
3629 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003630
3631 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003632 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003633 }
3634
3635 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003636 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003637 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003638 operation->coordinate_bytes,
3639 operation->coordinate_bytes)
3640 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003641
3642 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003643 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003644 }
3645
Paul Elliott1bc59df2023-02-05 13:41:57 +00003646 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003647
Paul Elliott724bd252023-02-08 12:35:08 +00003648 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003649 }
Paul Elliott724bd252023-02-08 12:35:08 +00003650
3651exit:
3652
3653 mbedtls_mpi_free(&r);
3654 mbedtls_mpi_free(&s);
3655 return status;
3656
Paul Elliott588f8ed2022-12-02 18:10:26 +00003657 #else
3658
3659 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003660 (void) signature;
3661 (void) signature_size;
3662 (void) signature_length;
3663
3664 return PSA_ERROR_NOT_SUPPORTED;
3665
3666#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3667 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3668 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3669}
3670
3671psa_status_t mbedtls_psa_sign_hash_abort(
3672 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3673{
3674
3675#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3676 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3677 defined(MBEDTLS_ECP_RESTARTABLE)
3678
3679 if (operation->ctx) {
3680 mbedtls_ecdsa_free(operation->ctx);
3681 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003682 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003683 }
3684
3685 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3686
Paul Elliott93d9ca82023-02-15 18:14:21 +00003687 operation->num_ops = 0;
3688
Paul Elliott588f8ed2022-12-02 18:10:26 +00003689 return PSA_SUCCESS;
3690
3691#else
3692
3693 (void) operation;
3694
3695 return PSA_ERROR_NOT_SUPPORTED;
3696
3697#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3698 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3699 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3700}
3701
3702psa_status_t mbedtls_psa_verify_hash_start(
3703 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3704 const psa_key_attributes_t *attributes,
3705 const uint8_t *key_buffer, size_t key_buffer_size,
3706 psa_algorithm_t alg,
3707 const uint8_t *hash, size_t hash_length,
3708 const uint8_t *signature, size_t signature_length)
3709{
3710 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003711 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003712 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003713
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003714 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003715 return PSA_ERROR_NOT_SUPPORTED;
3716 }
3717
3718 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003719 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003720 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003721
3722#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003723 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3724 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725
Paul Elliotta1c94092023-02-15 16:38:04 +00003726 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3727 mbedtls_mpi_init(&operation->r);
3728 mbedtls_mpi_init(&operation->s);
3729
Paul Elliott93d9ca82023-02-15 18:14:21 +00003730 /* Ensure num_ops is zero'ed in case of context re-use. */
3731 operation->num_ops = 0;
3732
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003733 status = mbedtls_psa_ecp_load_representation(attributes->type,
3734 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003735 key_buffer,
3736 key_buffer_size,
3737 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003738
Paul Elliott068fe072023-01-16 13:59:15 +00003739 if (status != PSA_SUCCESS) {
3740 return status;
3741 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003742
Paul Elliottc569fc22023-02-10 13:02:54 +00003743 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003744
Paul Elliott1bc59df2023-02-05 13:41:57 +00003745 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003746 return PSA_ERROR_INVALID_SIGNATURE;
3747 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003748
Paul Elliott068fe072023-01-16 13:59:15 +00003749 status = mbedtls_to_psa_error(
3750 mbedtls_mpi_read_binary(&operation->r,
3751 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003752 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003753
Paul Elliott068fe072023-01-16 13:59:15 +00003754 if (status != PSA_SUCCESS) {
3755 return status;
3756 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003757
Paul Elliott068fe072023-01-16 13:59:15 +00003758 status = mbedtls_to_psa_error(
3759 mbedtls_mpi_read_binary(&operation->s,
3760 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003761 coordinate_bytes,
3762 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003763
Paul Elliott068fe072023-01-16 13:59:15 +00003764 if (status != PSA_SUCCESS) {
3765 return status;
3766 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767
Paul Elliott2c9843f2023-02-15 17:32:42 +00003768 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003769
Paul Elliott2c9843f2023-02-15 17:32:42 +00003770 if (status != PSA_SUCCESS) {
3771 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003772 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773
Paul Elliott0290a762023-02-09 14:30:24 +00003774 /* We only need to store the same length of hash as the private key size
3775 * here, it would be truncated by the internal implementation anyway. */
3776 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3777 coordinate_bytes);
3778
Paul Elliottba70ad42023-02-15 18:23:53 +00003779 if (required_hash_length > sizeof(operation->hash)) {
3780 /* Shouldn't happen, but better safe than sorry. */
3781 return PSA_ERROR_CORRUPTION_DETECTED;
3782 }
3783
Paul Elliott0290a762023-02-09 14:30:24 +00003784 memcpy(operation->hash, hash, required_hash_length);
3785 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003786
3787 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003788#else
Paul Elliott068fe072023-01-16 13:59:15 +00003789 (void) operation;
3790 (void) key_buffer;
3791 (void) key_buffer_size;
3792 (void) alg;
3793 (void) hash;
3794 (void) hash_length;
3795 (void) signature;
3796 (void) signature_length;
3797 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003798 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003799 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003800
Paul Elliott068fe072023-01-16 13:59:15 +00003801 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3803 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3804 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805}
3806
3807psa_status_t mbedtls_psa_verify_hash_complete(
3808 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3809{
3810
3811#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3812 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3813 defined(MBEDTLS_ECP_RESTARTABLE)
3814
Paul Elliottf8e5b562023-02-19 18:43:45 +00003815 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3816
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003817 /* Ensure max_ops is set to the current value (or default). */
3818 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3819
Paul Elliottf8e5b562023-02-19 18:43:45 +00003820 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003821 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3822 operation->hash,
3823 operation->hash_length,
3824 &operation->ctx->Q,
3825 &operation->r,
3826 &operation->s,
3827 &operation->restart_ctx));
3828
Paul Elliottf8e5b562023-02-19 18:43:45 +00003829 /* Hide the fact that the restart context only holds a delta of number of
3830 * ops done during the last operation, not an absolute value. */
3831 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3832
3833 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834#else
3835 (void) operation;
3836
3837 return PSA_ERROR_NOT_SUPPORTED;
3838
3839#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3840 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3841 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3842}
3843
3844psa_status_t mbedtls_psa_verify_hash_abort(
3845 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3846{
3847
3848#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3849 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3850 defined(MBEDTLS_ECP_RESTARTABLE)
3851
3852 if (operation->ctx) {
3853 mbedtls_ecdsa_free(operation->ctx);
3854 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003855 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003856 }
3857
3858 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3859
Paul Elliott93d9ca82023-02-15 18:14:21 +00003860 operation->num_ops = 0;
3861
Paul Elliott588f8ed2022-12-02 18:10:26 +00003862 mbedtls_mpi_free(&operation->r);
3863 mbedtls_mpi_free(&operation->s);
3864
3865 return PSA_SUCCESS;
3866
3867#else
3868 (void) operation;
3869
3870 return PSA_ERROR_NOT_SUPPORTED;
3871
3872#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3873 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3874 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3875}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003876
mohammad1603503973b2018-03-12 15:59:30 +02003877/****************************************************************/
3878/* Symmetric cryptography */
3879/****************************************************************/
3880
Gilles Peskine449bd832023-01-11 14:50:10 +01003881static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3882 mbedtls_svc_key_id_t key,
3883 psa_algorithm_t alg,
3884 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003885{
3886 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3887 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003888 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003889 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3890 PSA_KEY_USAGE_ENCRYPT :
3891 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003892
3893 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003894 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003895 status = PSA_ERROR_BAD_STATE;
3896 goto exit;
3897 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003898
Gilles Peskine449bd832023-01-11 14:50:10 +01003899 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003900 status = PSA_ERROR_INVALID_ARGUMENT;
3901 goto exit;
3902 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003903
Gilles Peskine449bd832023-01-11 14:50:10 +01003904 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3905 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003906 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003907 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003908
Ronald Cron49fafa92021-03-10 08:34:23 +01003909 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003910 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003911 * so we only set it (in the driver wrapper) after resources have been
3912 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003913 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003914 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003915 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003916 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003917 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003918 }
3919 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003920
3921 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003922 if (cipher_operation == MBEDTLS_ENCRYPT) {
3923 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003924 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003925 slot->key.data,
3926 slot->key.bytes,
3927 alg);
3928 } else {
3929 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003930 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003931 slot->key.data,
3932 slot->key.bytes,
3933 alg);
3934 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003935
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003936exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 if (status != PSA_SUCCESS) {
3938 psa_cipher_abort(operation);
3939 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003940
Ryan Everettc0053cc2024-02-14 16:27:13 +00003941 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003942
Gilles Peskine449bd832023-01-11 14:50:10 +01003943 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003944}
3945
Gilles Peskine449bd832023-01-11 14:50:10 +01003946psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3947 mbedtls_svc_key_id_t key,
3948 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003949{
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003951}
3952
Gilles Peskine449bd832023-01-11 14:50:10 +01003953psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3954 mbedtls_svc_key_id_t key,
3955 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003956{
Gilles Peskine449bd832023-01-11 14:50:10 +01003957 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003958}
3959
Gilles Peskine449bd832023-01-11 14:50:10 +01003960psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3961 uint8_t *iv,
3962 size_t iv_size,
3963 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003964{
Ronald Cron6d051732020-10-01 14:10:20 +02003965 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003966 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07003967 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003968
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003970 status = PSA_ERROR_BAD_STATE;
3971 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003972 }
3973
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003975 status = PSA_ERROR_BAD_STATE;
3976 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003977 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003978
Ronald Cron2fb90522021-07-05 12:31:44 +02003979 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003980 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003981 status = PSA_ERROR_BUFFER_TOO_SMALL;
3982 goto exit;
3983 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003984
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02003986 status = PSA_ERROR_GENERIC_ERROR;
3987 goto exit;
3988 }
3989
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 status = psa_generate_random(local_iv, default_iv_length);
3991 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003992 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 }
Ronald Cron5618a392021-03-26 09:52:26 +01003994
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 status = psa_driver_wrapper_cipher_set_iv(operation,
3996 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003997
3998exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003999 if (status == PSA_SUCCESS) {
4000 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004001 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004002 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004004 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004006 }
Ronald Cron6d051732020-10-01 14:10:20 +02004007
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004009}
4010
Gilles Peskine449bd832023-01-11 14:50:10 +01004011psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4012 const uint8_t *iv,
4013 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004014{
Ronald Cron6d051732020-10-01 14:10:20 +02004015 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004016
Gilles Peskine449bd832023-01-11 14:50:10 +01004017 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004018 status = PSA_ERROR_BAD_STATE;
4019 goto exit;
4020 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004021
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004023 status = PSA_ERROR_BAD_STATE;
4024 goto exit;
4025 }
Ronald Crona0d68172021-03-26 10:15:08 +01004026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004028 status = PSA_ERROR_INVALID_ARGUMENT;
4029 goto exit;
4030 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 status = psa_driver_wrapper_cipher_set_iv(operation,
4033 iv,
4034 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004035
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004036exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004038 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 } else {
4040 psa_cipher_abort(operation);
4041 }
4042 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004043}
4044
Gilles Peskine449bd832023-01-11 14:50:10 +01004045psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4046 const uint8_t *input,
4047 size_t input_length,
4048 uint8_t *output,
4049 size_t output_size,
4050 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004051{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004052 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004053
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004055 status = PSA_ERROR_BAD_STATE;
4056 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004057 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004058
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004060 status = PSA_ERROR_BAD_STATE;
4061 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004062 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004063
Gilles Peskine449bd832023-01-11 14:50:10 +01004064 status = psa_driver_wrapper_cipher_update(operation,
4065 input,
4066 input_length,
4067 output,
4068 output_size,
4069 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004070
4071exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 if (status != PSA_SUCCESS) {
4073 psa_cipher_abort(operation);
4074 }
Ronald Cron6d051732020-10-01 14:10:20 +02004075
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004077}
4078
Gilles Peskine449bd832023-01-11 14:50:10 +01004079psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4080 uint8_t *output,
4081 size_t output_size,
4082 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004083{
David Saadab4ecc272019-02-14 13:48:10 +02004084 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004085
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004087 status = PSA_ERROR_BAD_STATE;
4088 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004089 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004090
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004092 status = PSA_ERROR_BAD_STATE;
4093 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004094 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004095
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 status = psa_driver_wrapper_cipher_finish(operation,
4097 output,
4098 output_size,
4099 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004100
4101exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 if (status == PSA_SUCCESS) {
4103 return psa_cipher_abort(operation);
4104 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004105 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004107
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004109 }
mohammad1603503973b2018-03-12 15:59:30 +02004110}
4111
Gilles Peskine449bd832023-01-11 14:50:10 +01004112psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004113{
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004115 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004116 * in use. It's ok to call abort on such an object, and there's
4117 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004119 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004120
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004122
Ronald Cron49fafa92021-03-10 08:34:23 +01004123 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004124 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004125 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004126
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004128}
4129
Gilles Peskine449bd832023-01-11 14:50:10 +01004130psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4131 psa_algorithm_t alg,
4132 const uint8_t *input,
4133 size_t input_length,
4134 uint8_t *output,
4135 size_t output_size,
4136 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004137{
4138 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004139 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004140 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004141 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4142 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004143
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004145 status = PSA_ERROR_INVALID_ARGUMENT;
4146 goto exit;
4147 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004148
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4150 PSA_KEY_USAGE_ENCRYPT,
4151 alg);
4152 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004153 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004155
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4157 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004158 status = PSA_ERROR_GENERIC_ERROR;
4159 goto exit;
4160 }
4161
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 if (default_iv_length > 0) {
4163 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004164 status = PSA_ERROR_BUFFER_TOO_SMALL;
4165 goto exit;
4166 }
4167
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 status = psa_generate_random(local_iv, default_iv_length);
4169 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004170 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004172 }
4173
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004174 status = psa_driver_wrapper_cipher_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004175 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004176 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004177 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004179
4180exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004181 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004183 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004184 }
Ronald Cron23919522021-07-08 19:00:07 +02004185
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 if (status == PSA_SUCCESS) {
4187 if (default_iv_length > 0) {
4188 memcpy(output, local_iv, default_iv_length);
4189 }
4190 *output_length += default_iv_length;
4191 } else {
4192 *output_length = 0;
4193 }
4194
4195 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004196}
4197
Gilles Peskine449bd832023-01-11 14:50:10 +01004198psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4199 psa_algorithm_t alg,
4200 const uint8_t *input,
4201 size_t input_length,
4202 uint8_t *output,
4203 size_t output_size,
4204 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004205{
4206 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004207 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004208 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004209
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004211 status = PSA_ERROR_INVALID_ARGUMENT;
4212 goto exit;
4213 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004214
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4216 PSA_KEY_USAGE_DECRYPT,
4217 alg);
4218 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004219 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4223 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004224 status = PSA_ERROR_INVALID_ARGUMENT;
4225 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004227 status = PSA_ERROR_INVALID_ARGUMENT;
4228 goto exit;
4229 }
4230
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004231 status = psa_driver_wrapper_cipher_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004232 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004233 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004235
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004236exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004237 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004239 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004241
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004243 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004244 }
Ronald Cron23919522021-07-08 19:00:07 +02004245
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004247}
4248
4249
mohammad16035955c982018-04-26 00:53:03 +03004250/****************************************************************/
4251/* AEAD */
4252/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004253
Paul Elliottbaff51c2021-09-28 17:44:45 +01004254/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004255static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004256{
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004258}
4259
4260/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004261static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4262 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004263{
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004265
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004267#if defined(PSA_WANT_ALG_GCM)
4268 case PSA_ALG_GCM:
4269 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 * arbitrarily large nonces. Please note that we do not generally
4271 * recommend the usage of nonces of greater length than
4272 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4273 * size, which can then lead to collisions if you encrypt a very
4274 * large number of messages.*/
4275 if (nonce_length != 0) {
4276 return PSA_SUCCESS;
4277 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004278 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004279#endif /* PSA_WANT_ALG_GCM */
4280#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004281 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 if (nonce_length >= 7 && nonce_length <= 13) {
4283 return PSA_SUCCESS;
4284 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004285 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004286#endif /* PSA_WANT_ALG_CCM */
4287#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004288 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004289 if (nonce_length == 12) {
4290 return PSA_SUCCESS;
4291 } else if (nonce_length == 8) {
4292 return PSA_ERROR_NOT_SUPPORTED;
4293 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004294 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004295#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004296 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004297 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004299 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004300
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004302}
4303
Gilles Peskine449bd832023-01-11 14:50:10 +01004304static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004305{
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4307 return PSA_ERROR_INVALID_ARGUMENT;
4308 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004309
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004311}
4312
Gilles Peskine449bd832023-01-11 14:50:10 +01004313psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4314 psa_algorithm_t alg,
4315 const uint8_t *nonce,
4316 size_t nonce_length,
4317 const uint8_t *additional_data,
4318 size_t additional_data_length,
4319 const uint8_t *plaintext,
4320 size_t plaintext_length,
4321 uint8_t *ciphertext,
4322 size_t ciphertext_size,
4323 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004324{
Ronald Cron215633c2021-03-16 17:15:37 +01004325 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4326 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004327
mohammad1603f08a5502018-06-03 15:05:47 +03004328 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004329
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 status = psa_aead_check_algorithm(alg);
4331 if (status != PSA_SUCCESS) {
4332 return status;
4333 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004334
Ronald Cron9a986162021-03-26 12:40:07 +01004335 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4337 if (status != PSA_SUCCESS) {
4338 return status;
4339 }
mohammad16035955c982018-04-26 00:53:03 +03004340
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 status = psa_aead_check_nonce_length(alg, nonce_length);
4342 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004343 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004344 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004345
Ronald Cronde822812021-03-17 16:08:20 +01004346 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004347 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004348 alg,
4349 nonce, nonce_length,
4350 additional_data, additional_data_length,
4351 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004352 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004353
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4355 memset(ciphertext, 0, ciphertext_size);
4356 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004357
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004358exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004359 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004360
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004362}
4363
Gilles Peskine449bd832023-01-11 14:50:10 +01004364psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4365 psa_algorithm_t alg,
4366 const uint8_t *nonce,
4367 size_t nonce_length,
4368 const uint8_t *additional_data,
4369 size_t additional_data_length,
4370 const uint8_t *ciphertext,
4371 size_t ciphertext_length,
4372 uint8_t *plaintext,
4373 size_t plaintext_size,
4374 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004375{
Ronald Cron215633c2021-03-16 17:15:37 +01004376 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4377 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004378
Gilles Peskineee652a32018-06-01 19:23:52 +02004379 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004380
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 status = psa_aead_check_algorithm(alg);
4382 if (status != PSA_SUCCESS) {
4383 return status;
4384 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004385
Ronald Cron9a986162021-03-26 12:40:07 +01004386 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4388 if (status != PSA_SUCCESS) {
4389 return status;
4390 }
mohammad16035955c982018-04-26 00:53:03 +03004391
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 status = psa_aead_check_nonce_length(alg, nonce_length);
4393 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004394 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004396
Ronald Cronde822812021-03-17 16:08:20 +01004397 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004398 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004399 alg,
4400 nonce, nonce_length,
4401 additional_data, additional_data_length,
4402 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (status != PSA_SUCCESS && plaintext_size != 0) {
4406 memset(plaintext, 0, plaintext_size);
4407 }
mohammad160360a64d02018-06-03 17:20:42 +03004408
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004409exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004410 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004411
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 return status;
mohammad16035955c982018-04-26 00:53:03 +03004413}
4414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4416{
4417 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004418
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004420#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004422 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4424 return PSA_ERROR_INVALID_ARGUMENT;
4425 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004426 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004427#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004428
Przemek Stekiel86679c72022-10-06 17:06:56 +02004429#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004431 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4433 return PSA_ERROR_INVALID_ARGUMENT;
4434 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004435 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004436#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004437
Przemek Stekiel86679c72022-10-06 17:06:56 +02004438#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004440 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 if (tag_len != 16) {
4442 return PSA_ERROR_INVALID_ARGUMENT;
4443 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004444 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004445#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004446
4447 default:
4448 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004450 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004452}
4453
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004454/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004455static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4456 int is_encrypt,
4457 mbedtls_svc_key_id_t key,
4458 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004459{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004460 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4461 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4462 psa_key_slot_t *slot = NULL;
4463 psa_key_usage_t key_usage = 0;
4464
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 status = psa_aead_check_algorithm(alg);
4466 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004467 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004469
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004471 status = PSA_ERROR_BAD_STATE;
4472 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004473 }
4474
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 if (operation->nonce_set || operation->lengths_set ||
4476 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004477 status = PSA_ERROR_BAD_STATE;
4478 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004479 }
4480
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004482 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004484 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004486
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4488 alg);
4489 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004490 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004492
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004494 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004496
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 if (is_encrypt) {
4498 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004499 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 slot->key.data,
4501 slot->key.bytes,
4502 alg);
4503 } else {
4504 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004505 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 slot->key.data,
4507 slot->key.bytes,
4508 alg);
4509 }
4510 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004511 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004513
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004514 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004515
4516exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00004517 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004518
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004520 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004522 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 } else {
4524 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004525 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004526
Gilles Peskine449bd832023-01-11 14:50:10 +01004527 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004528}
4529
Paul Elliott302ff6b2021-04-20 18:10:30 +01004530/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004531psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4532 mbedtls_svc_key_id_t key,
4533 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004534{
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004536}
4537
4538/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004539psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4540 mbedtls_svc_key_id_t key,
4541 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004542{
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004544}
4545
4546/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004547psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4548 uint8_t *nonce,
4549 size_t nonce_size,
4550 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004551{
4552 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004553 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004554 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004555
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004556 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004557
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004559 status = PSA_ERROR_BAD_STATE;
4560 goto exit;
4561 }
4562
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004564 status = PSA_ERROR_BAD_STATE;
4565 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004566 }
4567
Paul Elliotte193ea82021-10-01 13:00:16 +01004568 /* For CCM, this size may not be correct according to the PSA
4569 * specification. The PSA Crypto 1.0.1 specification states:
4570 *
4571 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4572 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4573 *
4574 * However this restriction that L has to be the smallest integer is not
4575 * applied in practice, and it is not implementable here since the
4576 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4578 operation->alg);
4579 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004580 status = PSA_ERROR_BUFFER_TOO_SMALL;
4581 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004582 }
4583
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 status = psa_generate_random(local_nonce, required_nonce_size);
4585 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004586 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004590
Paul Elliott39dc6b82021-05-11 19:16:09 +01004591exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 if (status == PSA_SUCCESS) {
4593 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004594 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 } else {
4596 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004597 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004600}
4601
4602/* Set the nonce for a multipart authenticated encryption or decryption
4603 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004604psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4605 const uint8_t *nonce,
4606 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004607{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4609
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004611 status = PSA_ERROR_BAD_STATE;
4612 goto exit;
4613 }
4614
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004616 status = PSA_ERROR_BAD_STATE;
4617 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004618 }
4619
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4621 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004622 status = PSA_ERROR_INVALID_ARGUMENT;
4623 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004624 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004625
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4627 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004628
Paul Elliott39dc6b82021-05-11 19:16:09 +01004629exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004631 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 } else {
4633 psa_aead_abort(operation);
4634 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004635
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004637}
4638
4639/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004640psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4641 size_t ad_length,
4642 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004643{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004644 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4645
Gilles Peskine449bd832023-01-11 14:50:10 +01004646 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004647 status = PSA_ERROR_BAD_STATE;
4648 goto exit;
4649 }
4650
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 if (operation->lengths_set || operation->ad_started ||
4652 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004653 status = PSA_ERROR_BAD_STATE;
4654 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004655 }
4656
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004658#if defined(PSA_WANT_ALG_GCM)
4659 case PSA_ALG_GCM:
4660 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 * bits. Without the guard this code will generate warnings on 32bit
4662 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004663#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 if (((uint64_t) ad_length) >> 61 != 0 ||
4665 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004666 status = PSA_ERROR_INVALID_ARGUMENT;
4667 goto exit;
4668 }
Paul Elliott325d3742021-09-27 17:56:28 +01004669#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004670 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004671#endif /* PSA_WANT_ALG_GCM */
4672#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004673 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004675 status = PSA_ERROR_INVALID_ARGUMENT;
4676 goto exit;
4677 }
4678 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004679#endif /* PSA_WANT_ALG_CCM */
4680#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004681 case PSA_ALG_CHACHA20_POLY1305:
4682 /* No length restrictions for ChaChaPoly. */
4683 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004684#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004685 default:
4686 break;
4687 }
Paul Elliott325d3742021-09-27 17:56:28 +01004688
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4690 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004691
Paul Elliott39dc6b82021-05-11 19:16:09 +01004692exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004694 operation->ad_remaining = ad_length;
4695 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004696 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 } else {
4698 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004699 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004700
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004702}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004703
4704/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004705psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4706 const uint8_t *input,
4707 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004708{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004709 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4710
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004712 status = PSA_ERROR_BAD_STATE;
4713 goto exit;
4714 }
4715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004717 status = PSA_ERROR_BAD_STATE;
4718 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004719 }
4720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 if (operation->lengths_set) {
4722 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004723 status = PSA_ERROR_INVALID_ARGUMENT;
4724 goto exit;
4725 }
4726
4727 operation->ad_remaining -= input_length;
4728 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004729#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004731 status = PSA_ERROR_BAD_STATE;
4732 goto exit;
4733 }
4734#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004735
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 status = psa_driver_wrapper_aead_update_ad(operation, input,
4737 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004738
Paul Elliott39dc6b82021-05-11 19:16:09 +01004739exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004741 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 } else {
4743 psa_aead_abort(operation);
4744 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004745
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004747}
4748
4749/* Encrypt or decrypt a message fragment in an active multipart AEAD
4750 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004751psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4752 const uint8_t *input,
4753 size_t input_length,
4754 uint8_t *output,
4755 size_t output_size,
4756 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004757{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004758 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004759
4760 *output_length = 0;
4761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004763 status = PSA_ERROR_BAD_STATE;
4764 goto exit;
4765 }
4766
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004768 status = PSA_ERROR_BAD_STATE;
4769 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004770 }
4771
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004773 /* Additional data length was supplied, but not all the additional
4774 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004776 status = PSA_ERROR_INVALID_ARGUMENT;
4777 goto exit;
4778 }
4779
4780 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004782 status = PSA_ERROR_INVALID_ARGUMENT;
4783 goto exit;
4784 }
4785
4786 operation->body_remaining -= input_length;
4787 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004788#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004790 status = PSA_ERROR_BAD_STATE;
4791 goto exit;
4792 }
4793#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4796 output, output_size,
4797 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004798
Paul Elliott39dc6b82021-05-11 19:16:09 +01004799exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004801 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 } else {
4803 psa_aead_abort(operation);
4804 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004805
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004807}
4808
Gilles Peskine449bd832023-01-11 14:50:10 +01004809static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004810{
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 if (operation->id == 0 || !operation->nonce_set) {
4812 return PSA_ERROR_BAD_STATE;
4813 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4816 operation->body_remaining != 0)) {
4817 return PSA_ERROR_INVALID_ARGUMENT;
4818 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004819
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004821}
4822
Paul Elliott302ff6b2021-04-20 18:10:30 +01004823/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004824psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4825 uint8_t *ciphertext,
4826 size_t ciphertext_size,
4827 size_t *ciphertext_length,
4828 uint8_t *tag,
4829 size_t tag_size,
4830 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004831{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004832 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4833
Paul Elliott302ff6b2021-04-20 18:10:30 +01004834 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004835 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 status = psa_aead_final_checks(operation);
4838 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004839 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004843 status = PSA_ERROR_BAD_STATE;
4844 goto exit;
4845 }
4846
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4848 ciphertext_size,
4849 ciphertext_length,
4850 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004851
Paul Elliott39dc6b82021-05-11 19:16:09 +01004852exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004853
4854
Paul Elliottf47b0952021-05-21 18:02:33 +01004855 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004856 * the zero tag size, make sure the tag is set to something implausible.
4857 * Even if the operation succeeds, make sure we clear the rest of the
4858 * buffer to prevent potential leakage of anything previously placed in
4859 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004860 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004861
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004865}
4866
4867/* Finish authenticating and decrypting a message in a multipart AEAD
4868 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004869psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4870 uint8_t *plaintext,
4871 size_t plaintext_size,
4872 size_t *plaintext_length,
4873 const uint8_t *tag,
4874 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004875{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004876 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4877
Paul Elliott302ff6b2021-04-20 18:10:30 +01004878 *plaintext_length = 0;
4879
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 status = psa_aead_final_checks(operation);
4881 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004882 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004884
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004886 status = PSA_ERROR_BAD_STATE;
4887 goto exit;
4888 }
4889
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4891 plaintext_size,
4892 plaintext_length,
4893 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004894
4895exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004897
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004899}
4900
4901/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004902psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004903{
4904 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4905
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004907 /* The object has (apparently) been initialized but it is not (yet)
4908 * in use. It's ok to call abort on such an object, and there's
4909 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004911 }
4912
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004914
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004918}
4919
Gilles Peskinee59236f2018-01-27 23:32:46 +01004920/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004921/* Generators */
4922/****************************************************************/
4923
Przemek Stekiel69c46792022-06-10 12:59:51 +02004924#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004925 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004926 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05304927 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304928 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08004929#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004930#endif /* At least one builtin KDF */
4931
Przemek Stekiel69c46792022-06-10 12:59:51 +02004932#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004933 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4934 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4935static psa_status_t psa_key_derivation_start_hmac(
4936 psa_mac_operation_t *operation,
4937 psa_algorithm_t hash_alg,
4938 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02004940{
4941 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4942 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4944 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4945 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02004946
Steven Cooreman72f736a2021-05-07 14:14:37 +02004947 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02004949
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 status = psa_driver_wrapper_mac_sign_setup(operation,
4951 &attributes,
4952 hmac_key, hmac_key_length,
4953 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02004954
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 psa_reset_key_attributes(&attributes);
4956 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02004957}
4958#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004959
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004960#define HKDF_STATE_INIT 0 /* no input yet */
4961#define HKDF_STATE_STARTED 1 /* got salt */
4962#define HKDF_STATE_KEYED 2 /* got key */
4963#define HKDF_STATE_OUTPUT 3 /* output started */
4964
Gilles Peskinecbe66502019-05-16 16:59:18 +02004965static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004967{
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4969 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4970 } else {
4971 return operation->alg;
4972 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004973}
4974
Gilles Peskine449bd832023-01-11 14:50:10 +01004975psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004976{
4977 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4979 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004980 /* The object has (apparently) been initialized but it is not
4981 * in use. It's ok to call abort on such an object, and there's
4982 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004984#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
4986 mbedtls_free(operation->ctx.hkdf.info);
4987 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4988 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004989#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004990#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4991 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004992 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4993 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4994 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4995 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004996 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02004998 }
4999
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005001 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005003 }
5004
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005006 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005008 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005009#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005011 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005013 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005014#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005015 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005016
5017 /* We leave the fields Ai and output_block to be erased safely by the
5018 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005020#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5021 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005022#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5024 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5025 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5026 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005027#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305028#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305029 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305030 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005031 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305032 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305033 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305034
5035 status = PSA_SUCCESS;
5036 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305037#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005038 {
5039 status = PSA_ERROR_BAD_STATE;
5040 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 mbedtls_platform_zeroize(operation, sizeof(*operation));
5042 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005043}
5044
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005045psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005047{
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005049 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005051 }
5052
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005053 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005055}
5056
Gilles Peskine449bd832023-01-11 14:50:10 +01005057psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5058 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005059{
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 if (operation->alg == 0) {
5061 return PSA_ERROR_BAD_STATE;
5062 }
5063 if (capacity > operation->capacity) {
5064 return PSA_ERROR_INVALID_ARGUMENT;
5065 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005066 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005068}
5069
Przemek Stekiel69c46792022-06-10 12:59:51 +02005070#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005071/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005072static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5073 psa_algorithm_t kdf_alg,
5074 uint8_t *output,
5075 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005076{
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5078 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005079 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005080 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005081#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005083#else
5084 const uint8_t last_block = 0xff;
5085#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005086
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 if (hkdf->state < HKDF_STATE_KEYED ||
5088 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005089#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005091#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 )) {
5093 return PSA_ERROR_BAD_STATE;
5094 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005095 hkdf->state = HKDF_STATE_OUTPUT;
5096
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005098 /* Copy what remains of the current block */
5099 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005101 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 }
5103 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005104 output += n;
5105 output_length -= n;
5106 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005108 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005110 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005111 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005112 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005113 * object was corrupted or if this function is called directly
5114 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 if (hkdf->block_number == last_block) {
5116 return PSA_ERROR_BAD_STATE;
5117 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005118
5119 /* We need a new block */
5120 ++hkdf->block_number;
5121 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005122
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5124 hash_alg,
5125 hkdf->prk,
5126 hash_length);
5127 if (status != PSA_SUCCESS) {
5128 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005129 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005130
5131 if (hkdf->block_number != 1) {
5132 status = psa_mac_update(&hkdf->hmac,
5133 hkdf->output_block,
5134 hash_length);
5135 if (status != PSA_SUCCESS) {
5136 return status;
5137 }
5138 }
5139 status = psa_mac_update(&hkdf->hmac,
5140 hkdf->info,
5141 hkdf->info_length);
5142 if (status != PSA_SUCCESS) {
5143 return status;
5144 }
5145 status = psa_mac_update(&hkdf->hmac,
5146 &hkdf->block_number, 1);
5147 if (status != PSA_SUCCESS) {
5148 return status;
5149 }
5150 status = psa_mac_sign_finish(&hkdf->hmac,
5151 hkdf->output_block,
5152 sizeof(hkdf->output_block),
5153 &hmac_output_length);
5154 if (status != PSA_SUCCESS) {
5155 return status;
5156 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005157 }
5158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005160}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005161#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005162
John Durkop07cc04a2020-11-16 22:08:34 -08005163#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5164 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005165static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5166 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005168{
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5170 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005171 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5172 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005173 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005174
Janos Follath7742fee2019-06-17 12:58:10 +01005175 /* We can't be wanting more output after block 0xff, otherwise
5176 * the capacity check in psa_key_derivation_output_bytes() would have
5177 * prevented this call. It could happen only if the operation
5178 * object was corrupted or if this function is called directly
5179 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 if (tls12_prf->block_number == 0xff) {
5181 return PSA_ERROR_CORRUPTION_DETECTED;
5182 }
Janos Follath7742fee2019-06-17 12:58:10 +01005183
5184 /* We need a new block */
5185 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005186 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005187
5188 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5189 *
5190 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5191 *
5192 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5193 * HMAC_hash(secret, A(2) + seed) +
5194 * HMAC_hash(secret, A(3) + seed) + ...
5195 *
5196 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005197 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005198 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005199 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005200 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005201 * is currently extracted as `output_block` and where i is
5202 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005203 */
5204
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 status = psa_key_derivation_start_hmac(&hmac,
5206 hash_alg,
5207 tls12_prf->secret,
5208 tls12_prf->secret_length);
5209 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005210 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005212
5213 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005215 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5216 * the variable seed and in this instance means it in the context of the
5217 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 status = psa_mac_update(&hmac,
5219 tls12_prf->label,
5220 tls12_prf->label_length);
5221 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005222 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 }
5224 status = psa_mac_update(&hmac,
5225 tls12_prf->seed,
5226 tls12_prf->seed_length);
5227 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005228 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 }
5230 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005231 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5233 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005234 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005236 }
5237
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 status = psa_mac_sign_finish(&hmac,
5239 tls12_prf->Ai, hash_length,
5240 &hmac_output_length);
5241 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005242 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 }
5244 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005245 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005247
5248 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 status = psa_key_derivation_start_hmac(&hmac,
5250 hash_alg,
5251 tls12_prf->secret,
5252 tls12_prf->secret_length);
5253 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005254 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 }
5256 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5257 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005258 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 }
5260 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5261 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005262 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 }
5264 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5265 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005266 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 }
5268 status = psa_mac_sign_finish(&hmac,
5269 tls12_prf->output_block, hash_length,
5270 &hmac_output_length);
5271 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005272 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005274
Janos Follath7742fee2019-06-17 12:58:10 +01005275
5276cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 cleanup_status = psa_mac_abort(&hmac);
5278 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005279 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005281
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005283}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005284
Janos Follath844eb0e2019-06-19 12:10:49 +01005285static psa_status_t psa_key_derivation_tls12_prf_read(
5286 psa_tls12_prf_key_derivation_t *tls12_prf,
5287 psa_algorithm_t alg,
5288 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005290{
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5292 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005293 psa_status_t status;
5294 uint8_t offset, length;
5295
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005297 case PSA_TLS12_PRF_STATE_LABEL_SET:
5298 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5299 break;
5300 case PSA_TLS12_PRF_STATE_OUTPUT:
5301 break;
5302 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005304 }
5305
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005307 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 if (tls12_prf->left_in_block == 0) {
5309 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5310 alg);
5311 if (status != PSA_SUCCESS) {
5312 return status;
5313 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005314
5315 continue;
5316 }
5317
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005319 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005321 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005323
5324 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005326 output += length;
5327 output_length -= length;
5328 tls12_prf->left_in_block -= length;
5329 }
5330
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005332}
John Durkop07cc04a2020-11-16 22:08:34 -08005333#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5334 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005335
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005336#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5337static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5338 psa_tls12_ecjpake_to_pms_t *ecjpake,
5339 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005341{
5342 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005343 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005344
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 if (output_length != 32) {
5346 return PSA_ERROR_INVALID_ARGUMENT;
5347 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005348
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5350 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5351 &output_size);
5352 if (status != PSA_SUCCESS) {
5353 return status;
5354 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005355
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 if (output_size != output_length) {
5357 return PSA_ERROR_GENERIC_ERROR;
5358 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005359
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005361}
5362#endif
5363
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305364#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305365static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5366 psa_pbkdf2_key_derivation_t *pbkdf2,
5367 psa_algorithm_t prf_alg,
5368 uint8_t prf_output_length,
5369 psa_key_attributes_t *attributes)
5370{
5371 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305372 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305373 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305374 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305375 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305376 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305377 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305378
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305379 mac_operation.is_sign = 1;
5380 mac_operation.mac_size = prf_output_length;
5381 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305382
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305383 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5384 attributes,
5385 pbkdf2->password,
5386 pbkdf2->password_length,
5387 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305388 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305389 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305390 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305391 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5392 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305393 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305394 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305395 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305396 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305397 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305398 }
5399 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5400 &mac_output_length);
5401 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305402 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305403 }
5404
5405 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305406 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305407 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305408 }
5409
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305410 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305411
5412 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305413 /* We are passing prf_output_length as mac_size because the driver
5414 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305415 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305416 status = psa_driver_wrapper_mac_compute(attributes,
5417 pbkdf2->password,
5418 pbkdf2->password_length,
5419 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305420 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305421 &mac_output_length);
5422 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305423 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305424 }
5425
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305426 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305427 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305428
5429cleanup:
5430 /* Zeroise buffers to clear sensitive data from memory. */
5431 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5432 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305433}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305434
5435static psa_status_t psa_key_derivation_pbkdf2_read(
5436 psa_pbkdf2_key_derivation_t *pbkdf2,
5437 psa_algorithm_t kdf_alg,
5438 uint8_t *output,
5439 size_t output_length)
5440{
5441 psa_status_t status;
5442 psa_algorithm_t prf_alg;
5443 uint8_t prf_output_length;
5444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5445 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5446 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5447
5448 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5449 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5450 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5451 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305452 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5453 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305454 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305455 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305456 } else {
5457 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305458 }
5459
5460 switch (pbkdf2->state) {
5461 case PSA_PBKDF2_STATE_PASSWORD_SET:
5462 /* Initially we need a new block so bytes_used is equal to block size*/
5463 pbkdf2->bytes_used = prf_output_length;
5464 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5465 break;
5466 case PSA_PBKDF2_STATE_OUTPUT:
5467 break;
5468 default:
5469 return PSA_ERROR_BAD_STATE;
5470 }
5471
5472 while (output_length != 0) {
5473 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5474 if (n > output_length) {
5475 n = (uint8_t) output_length;
5476 }
5477 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5478 output += n;
5479 output_length -= n;
5480 pbkdf2->bytes_used += n;
5481
5482 if (output_length == 0) {
5483 break;
5484 }
5485
5486 /* We need a new block */
5487 pbkdf2->bytes_used = 0;
5488 pbkdf2->block_number++;
5489
5490 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5491 prf_output_length,
5492 &attributes);
5493 if (status != PSA_SUCCESS) {
5494 return status;
5495 }
5496 }
5497
5498 return PSA_SUCCESS;
5499}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305500#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305501
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005502psa_status_t psa_key_derivation_output_bytes(
5503 psa_key_derivation_operation_t *operation,
5504 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005506{
5507 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005509
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005511 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005513 }
5514
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005516 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005517 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005518 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005519 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005520 goto exit;
5521 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005524 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005525 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5526 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005527 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005528 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005530 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005531 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005532
Przemek Stekiel69c46792022-06-10 12:59:51 +02005533#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5535 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5536 output, output_length);
5537 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005538#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005539#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5540 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5542 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5543 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5544 kdf_alg, output,
5545 output_length);
5546 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005547#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5548 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005549#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005551 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5553 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005554#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305555#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305556 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305557 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5558 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305559 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305560#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005561
Gilles Peskineeab56e42018-07-12 17:12:33 +02005562 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005563 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005565 }
5566
5567exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005569 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005570 * This allows us to differentiate between exhausted operations and
5571 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5572 * operations. */
5573 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005575 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005577 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005579}
5580
David Brown7807bf72021-02-09 16:28:23 -07005581#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005582static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005583{
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 if (data_size >= 8) {
5585 mbedtls_des_key_set_parity(data);
5586 }
5587 if (data_size >= 16) {
5588 mbedtls_des_key_set_parity(data + 8);
5589 }
5590 if (data_size >= 24) {
5591 mbedtls_des_key_set_parity(data + 16);
5592 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005593}
David Brown7807bf72021-02-09 16:28:23 -07005594#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005595
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005596/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 * ECC keys on a Weierstrass elliptic curve require the generation
5598 * of a private key which is an integer
5599 * in the range [1, N - 1], where N is the boundary of the private key domain:
5600 * N is the prime p for Diffie-Hellman, or the order of the
5601 * curve’s base point for ECC.
5602 *
5603 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5604 * This function generates the private key using the following process:
5605 *
5606 * 1. Draw a byte string of length ceiling(m/8) bytes.
5607 * 2. If m is not a multiple of 8, set the most significant
5608 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5609 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5610 * 4. If k > N - 2, discard the result and return to step 1.
5611 * 5. Output k + 1 as the private key.
5612 *
5613 * This method allows compliance to NIST standards, specifically the methods titled
5614 * Key-Pair Generation by Testing Candidates in the following publications:
5615 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5616 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5617 * Diffie-Hellman keys.
5618 *
5619 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5620 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5621 *
5622 * Note: Function allocates memory for *data buffer, so given *data should be
5623 * always NULL.
5624 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005625#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5626#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005627static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005628 psa_key_slot_t *slot,
5629 size_t bits,
5630 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005631 uint8_t **data
5632 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005633{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005634 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005635 mbedtls_mpi k;
5636 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005637 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005638 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005639 size_t m;
5640 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 mbedtls_mpi_init(&k);
5643 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005644
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005645 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005647 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01005648 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005649
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005651 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005652 goto cleanup;
5653 }
5654
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005655 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005657
Gilles Peskine449bd832023-01-11 14:50:10 +01005658 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005659
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005660 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005661 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005662 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005663
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005664 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005665
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005666 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005668
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005669 /* Note: This function is always called with *data == NULL and it
5670 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 *data = mbedtls_calloc(1, m_bytes);
5672 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005673 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005674 goto cleanup;
5675 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005676
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005678 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005680 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005682
5683 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005685 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005686 * (8 * ceiling(m/8) - m) bits of the first byte in
5687 * the string to zero.
5688 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005690 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005691 }
5692
5693 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005694 * big-endian byte string.
5695 */
5696 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005697
5698 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 * Result of comparison is returned. When it indicates error
5700 * then this function is called again.
5701 */
5702 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005703 }
5704
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005705 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5707 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005708cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 if (ret != 0) {
5710 status = mbedtls_to_psa_error(ret);
5711 }
5712 if (status != PSA_SUCCESS) {
5713 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005714 *data = NULL;
5715 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 mbedtls_mpi_free(&k);
5717 mbedtls_mpi_free(&diff_N_2);
5718 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005719}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005720
5721/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5722 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5723 *
5724 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5725 * draw a 32-byte string and process it as specified in
5726 * Elliptic Curves for Security [RFC7748] §5.
5727 *
5728 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5729 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5730 *
5731 * Note: Function allocates memory for *data buffer, so given *data should be
5732 * always NULL.
5733 */
5734
5735static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5736 size_t bits,
5737 psa_key_derivation_operation_t *operation,
5738 uint8_t **data
5739 )
5740{
5741 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005742 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005743
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005745 case 255:
5746 output_length = 32;
5747 break;
5748 case 448:
5749 output_length = 56;
5750 break;
5751 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005753 break;
5754 }
5755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 if (*data == NULL) {
5759 return PSA_ERROR_INSUFFICIENT_MEMORY;
5760 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005761
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005765 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005769 case 255:
5770 (*data)[0] &= 248;
5771 (*data)[31] &= 127;
5772 (*data)[31] |= 64;
5773 break;
5774 case 448:
5775 (*data)[0] &= 252;
5776 (*data)[55] |= 128;
5777 break;
5778 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005780 break;
5781 }
5782
5783 return status;
5784}
Valerio Setti86587ab2023-06-27 13:09:30 +02005785#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5786static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5787 psa_key_slot_t *slot, size_t bits,
5788 psa_key_derivation_operation_t *operation, uint8_t **data)
5789{
5790 (void) slot;
5791 (void) bits;
5792 (void) operation;
5793 (void) data;
5794 return PSA_ERROR_NOT_SUPPORTED;
5795}
5796
5797static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5798 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5799{
5800 (void) bits;
5801 (void) operation;
5802 (void) data;
5803 return PSA_ERROR_NOT_SUPPORTED;
5804}
5805#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5806#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005807
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005808static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005809 psa_key_slot_t *slot,
5810 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005812{
5813 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305815 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005816 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5819 return PSA_ERROR_INVALID_ARGUMENT;
5820 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005821
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005822#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005823 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5825 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5826 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005827 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5829 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005830 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 }
5832 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005833 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5835 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005836 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005838 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005839 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005840#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005841 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 if (key_type_is_raw_bytes(slot->attr.type)) {
5843 if (bits % 8 != 0) {
5844 return PSA_ERROR_INVALID_ARGUMENT;
5845 }
5846 data = mbedtls_calloc(1, bytes);
5847 if (data == NULL) {
5848 return PSA_ERROR_INSUFFICIENT_MEMORY;
5849 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005850
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 status = psa_key_derivation_output_bytes(operation, data, bytes);
5852 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005853 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 }
David Brown12ca5032021-02-11 11:02:00 -07005855#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5857 psa_des_set_key_parity(data, bytes);
5858 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005859#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 } else {
5861 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005862 }
Ronald Crondd04d422020-11-28 15:14:42 +01005863
Ronald Cronbf33c932020-11-28 18:06:53 +01005864 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005865
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005866 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005867 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 &storage_size);
5869 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305870 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 }
Archanad8a83dc2021-06-14 10:04:16 +05305872 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 status = psa_allocate_buffer_to_slot(slot, storage_size);
5874 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305875 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 }
Archanad8a83dc2021-06-14 10:04:16 +05305877
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005878 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 data, bytes,
5880 slot->key.data,
5881 slot->key.bytes,
5882 &slot->key.bytes, &bits);
5883 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005884 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005886
5887exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 mbedtls_free(data);
5889 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005890}
5891
Gilles Peskine092ce512024-02-20 12:31:24 +01005892static const psa_key_production_parameters_t default_production_parameters =
5893 PSA_KEY_PRODUCTION_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005894
Gilles Peskine092ce512024-02-20 12:31:24 +01005895int psa_key_production_parameters_are_default(
5896 const psa_key_production_parameters_t *params,
5897 size_t params_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005898{
Gilles Peskine092ce512024-02-20 12:31:24 +01005899 if (params->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005900 return 0;
5901 }
Gilles Peskine092ce512024-02-20 12:31:24 +01005902 if (params_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005903 return 0;
5904 }
5905 return 1;
5906}
5907
5908psa_status_t psa_key_derivation_output_key_ext(
5909 const psa_key_attributes_t *attributes,
5910 psa_key_derivation_operation_t *operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005911 const psa_key_production_parameters_t *params,
5912 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005913 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005914{
5915 psa_status_t status;
5916 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005917 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005918
Ronald Cron81709fc2020-11-14 12:10:32 +01005919 *key = MBEDTLS_SVC_KEY_ID_INIT;
5920
Gilles Peskine0f84d622019-09-12 19:03:13 +02005921 /* Reject any attempt to create a zero-length key so that we don't
5922 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 if (psa_get_key_bits(attributes) == 0) {
5924 return PSA_ERROR_INVALID_ARGUMENT;
5925 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005926
Gilles Peskine092ce512024-02-20 12:31:24 +01005927 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005928 return PSA_ERROR_INVALID_ARGUMENT;
5929 }
5930
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 if (operation->alg == PSA_ALG_NONE) {
5932 return PSA_ERROR_BAD_STATE;
5933 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005934
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 if (!operation->can_output_key) {
5936 return PSA_ERROR_NOT_PERMITTED;
5937 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005938
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5940 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005941#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005943 /* Deriving a key in a secure element is not implemented yet. */
5944 status = PSA_ERROR_NOT_SUPPORTED;
5945 }
5946#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 if (status == PSA_SUCCESS) {
5948 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005949 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005951 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 if (status == PSA_SUCCESS) {
5953 status = psa_finish_key_creation(slot, driver, key);
5954 }
5955 if (status != PSA_SUCCESS) {
5956 psa_fail_key_creation(slot, driver);
5957 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005958
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005960}
5961
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005962psa_status_t psa_key_derivation_output_key(
5963 const psa_key_attributes_t *attributes,
5964 psa_key_derivation_operation_t *operation,
5965 mbedtls_svc_key_id_t *key)
5966{
Gilles Peskinec81393b2024-02-14 20:51:28 +01005967 return psa_key_derivation_output_key_ext(attributes, operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005968 &default_production_parameters, 0,
Gilles Peskinec81393b2024-02-14 20:51:28 +01005969 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005970}
Gilles Peskineeab56e42018-07-12 17:12:33 +02005971
5972
5973/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005974/* Key derivation */
5975/****************************************************************/
5976
Steven Cooreman932ffb72021-02-15 12:14:32 +01005977#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005978static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005979{
5980#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5982 return 1;
5983 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005984#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005985#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5987 return 1;
5988 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005989#endif
5990#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5992 return 1;
5993 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005994#endif
5995#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5997 return 1;
5998 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005999#endif
6000#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6002 return 1;
6003 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006004#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006005#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6007 return 1;
6008 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006009#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306010#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6011 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6012 return 1;
6013 }
6014#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306015#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6016 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6017 return 1;
6018 }
6019#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006021}
6022
Gilles Peskine449bd832023-01-11 14:50:10 +01006023static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006024{
6025 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 psa_status_t status = psa_hash_setup(&operation, alg);
6027 psa_hash_abort(&operation);
6028 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006029}
6030
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306031static psa_status_t psa_key_derivation_set_maximum_capacity(
6032 psa_key_derivation_operation_t *operation,
6033 psa_algorithm_t kdf_alg)
6034{
6035#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6036 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6037 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6038 return PSA_SUCCESS;
6039 }
6040#endif
6041#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
6042 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6043#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05306044 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306045 PSA_KEY_TYPE_AES,
6046 128U,
6047 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306048#else
6049 operation->capacity = SIZE_MAX;
6050#endif
6051 return PSA_SUCCESS;
6052 }
6053#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6054
6055 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6056 * invalid or meaningless but it does not affect this function */
6057 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6058 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306059 if (hash_size == 0) {
6060 return PSA_ERROR_NOT_SUPPORTED;
6061 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306062
6063 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6064 * we might fail later, which is somewhat unfriendly and potentially
6065 * risk-prone. */
6066 psa_status_t status = psa_hash_try_support(hash_alg);
6067 if (status != PSA_SUCCESS) {
6068 return status;
6069 }
6070
6071#if defined(PSA_WANT_ALG_HKDF)
6072 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6073 operation->capacity = 255 * hash_size;
6074 } else
6075#endif
6076#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6077 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6078 operation->capacity = hash_size;
6079 } else
6080#endif
6081#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6082 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6083 operation->capacity = 255 * hash_size;
6084 } else
6085#endif
6086#if defined(PSA_WANT_ALG_TLS12_PRF)
6087 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6088 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6089 operation->capacity = SIZE_MAX;
6090 } else
6091#endif
6092#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6093 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6094 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6095 /* Master Secret is always 48 bytes
6096 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6097 operation->capacity = 48U;
6098 } else
6099#endif
6100#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6101 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6102#if (SIZE_MAX > UINT32_MAX)
6103 operation->capacity = UINT32_MAX * hash_size;
6104#else
6105 operation->capacity = SIZE_MAX;
6106#endif
6107 } else
6108#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6109 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306110 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306111 status = PSA_ERROR_NOT_SUPPORTED;
6112 }
6113 return status;
6114}
6115
Gilles Peskine969c5d62019-01-16 15:53:06 +01006116static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006117 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006119{
Janos Follath5fe19732019-06-20 15:09:30 +01006120 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6121 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006123
Gilles Peskine969c5d62019-01-16 15:53:06 +01006124 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 if (!is_kdf_alg_supported(kdf_alg)) {
6126 return PSA_ERROR_NOT_SUPPORTED;
6127 }
John Durkop07cc04a2020-11-16 22:08:34 -08006128
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306129 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6130 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306131 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006132}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006135{
6136#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 if (alg == PSA_ALG_ECDH) {
6138 return PSA_SUCCESS;
6139 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006140#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006141#if defined(PSA_WANT_ALG_FFDH)
6142 if (alg == PSA_ALG_FFDH) {
6143 return PSA_SUCCESS;
6144 }
6145#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006146 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006148}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006149
6150static int psa_key_derivation_allows_free_form_secret_input(
6151 psa_algorithm_t kdf_alg)
6152{
6153#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6154 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6155 return 0;
6156 }
6157#endif
6158 (void) kdf_alg;
6159 return 1;
6160}
John Durkop07cc04a2020-11-16 22:08:34 -08006161#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006162
Gilles Peskine449bd832023-01-11 14:50:10 +01006163psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6164 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006165{
6166 psa_status_t status;
6167
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 if (operation->alg != 0) {
6169 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006170 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6173 return PSA_ERROR_INVALID_ARGUMENT;
6174 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6175#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6176 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6177 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6178 status = psa_key_agreement_try_support(ka_alg);
6179 if (status != PSA_SUCCESS) {
6180 return status;
6181 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006182 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6183 return PSA_ERROR_INVALID_ARGUMENT;
6184 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6186#else
6187 return PSA_ERROR_NOT_SUPPORTED;
6188#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6189 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6190#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6191 status = psa_key_derivation_setup_kdf(operation, alg);
6192#else
6193 return PSA_ERROR_NOT_SUPPORTED;
6194#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6195 } else {
6196 return PSA_ERROR_INVALID_ARGUMENT;
6197 }
6198
6199 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006200 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 }
6202 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006203}
6204
Przemek Stekiel69c46792022-06-10 12:59:51 +02006205#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006206static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6207 psa_algorithm_t kdf_alg,
6208 psa_key_derivation_step_t step,
6209 const uint8_t *data,
6210 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006211{
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006213 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006215 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006216#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6218 return PSA_ERROR_INVALID_ARGUMENT;
6219 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006220#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 if (hkdf->state != HKDF_STATE_INIT) {
6222 return PSA_ERROR_BAD_STATE;
6223 } else {
6224 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6225 hash_alg,
6226 data, data_length);
6227 if (status != PSA_SUCCESS) {
6228 return status;
6229 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006230 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006232 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006233 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006234#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006236 /* We shouldn't be in different state as HKDF_EXPAND only allows
6237 * two inputs: SECRET (this case) and INFO which does not modify
6238 * the state. It could happen only if the hkdf
6239 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 if (hkdf->state != HKDF_STATE_INIT) {
6241 return PSA_ERROR_BAD_STATE;
6242 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006243
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006244 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6246 return PSA_ERROR_INVALID_ARGUMENT;
6247 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006248
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 memcpy(hkdf->prk, data, data_length);
6250 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006251#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006252 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006253 /* HKDF: If no salt was provided, use an empty salt.
6254 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006256#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6258 return PSA_ERROR_BAD_STATE;
6259 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006260#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6262 hash_alg,
6263 NULL, 0);
6264 if (status != PSA_SUCCESS) {
6265 return status;
6266 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006267 hkdf->state = HKDF_STATE_STARTED;
6268 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 if (hkdf->state != HKDF_STATE_STARTED) {
6270 return PSA_ERROR_BAD_STATE;
6271 }
6272 status = psa_mac_update(&hkdf->hmac,
6273 data, data_length);
6274 if (status != PSA_SUCCESS) {
6275 return status;
6276 }
6277 status = psa_mac_sign_finish(&hkdf->hmac,
6278 hkdf->prk,
6279 sizeof(hkdf->prk),
6280 &data_length);
6281 if (status != PSA_SUCCESS) {
6282 return status;
6283 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006284 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006285
Gilles Peskine2b522db2019-04-12 15:11:49 +02006286 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006287 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006288#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006290 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006292 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006294#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006295 {
6296 /* Block 0 is empty, and the next block will be
6297 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006299 }
6300
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006302 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006303#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6305 return PSA_ERROR_INVALID_ARGUMENT;
6306 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006307#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006308#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6310 hkdf->state == HKDF_STATE_INIT) {
6311 return PSA_ERROR_BAD_STATE;
6312 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006313#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 if (hkdf->state == HKDF_STATE_OUTPUT) {
6315 return PSA_ERROR_BAD_STATE;
6316 }
6317 if (hkdf->info_set) {
6318 return PSA_ERROR_BAD_STATE;
6319 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006320 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 if (data_length != 0) {
6322 hkdf->info = mbedtls_calloc(1, data_length);
6323 if (hkdf->info == NULL) {
6324 return PSA_ERROR_INSUFFICIENT_MEMORY;
6325 }
6326 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006327 }
6328 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006330 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006332 }
6333}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006334#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006335
John Durkop07cc04a2020-11-16 22:08:34 -08006336#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6337 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006338static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6339 const uint8_t *data,
6340 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006341{
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6343 return PSA_ERROR_BAD_STATE;
6344 }
Janos Follathf08e2652019-06-13 09:05:41 +01006345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 if (data_length != 0) {
6347 prf->seed = mbedtls_calloc(1, data_length);
6348 if (prf->seed == NULL) {
6349 return PSA_ERROR_INSUFFICIENT_MEMORY;
6350 }
Janos Follathf08e2652019-06-13 09:05:41 +01006351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006353 prf->seed_length = data_length;
6354 }
Janos Follathf08e2652019-06-13 09:05:41 +01006355
Gilles Peskine43f958b2020-12-13 14:55:14 +01006356 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006357
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006359}
6360
Gilles Peskine449bd832023-01-11 14:50:10 +01006361static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6362 const uint8_t *data,
6363 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006364{
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6366 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6367 return PSA_ERROR_BAD_STATE;
6368 }
Janos Follath81550542019-06-13 14:26:34 +01006369
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 if (data_length != 0) {
6371 prf->secret = mbedtls_calloc(1, data_length);
6372 if (prf->secret == NULL) {
6373 return PSA_ERROR_INSUFFICIENT_MEMORY;
6374 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006377 prf->secret_length = data_length;
6378 }
Janos Follath81550542019-06-13 14:26:34 +01006379
Gilles Peskine43f958b2020-12-13 14:55:14 +01006380 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006381
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006383}
6384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6386 const uint8_t *data,
6387 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006388{
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6390 return PSA_ERROR_BAD_STATE;
6391 }
Janos Follath63028dd2019-06-13 09:15:47 +01006392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 if (data_length != 0) {
6394 prf->label = mbedtls_calloc(1, data_length);
6395 if (prf->label == NULL) {
6396 return PSA_ERROR_INSUFFICIENT_MEMORY;
6397 }
Janos Follath63028dd2019-06-13 09:15:47 +01006398
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006400 prf->label_length = data_length;
6401 }
Janos Follath63028dd2019-06-13 09:15:47 +01006402
Gilles Peskine43f958b2020-12-13 14:55:14 +01006403 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006406}
6407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6409 psa_key_derivation_step_t step,
6410 const uint8_t *data,
6411 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006412{
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006414 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006416 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006418 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006420 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006422 }
6423}
John Durkop07cc04a2020-11-16 22:08:34 -08006424#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6425 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6426
6427#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6428static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6429 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006430 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006432{
6433 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6435 4 + data_length + prf->other_secret_length :
6436 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006437
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6439 return PSA_ERROR_INVALID_ARGUMENT;
6440 }
John Durkop07cc04a2020-11-16 22:08:34 -08006441
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 uint8_t *pms = mbedtls_calloc(1, pms_len);
6443 if (pms == NULL) {
6444 return PSA_ERROR_INSUFFICIENT_MEMORY;
6445 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006446 uint8_t *cur = pms;
6447
6448 /* pure-PSK:
6449 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006450 *
6451 * The premaster secret is formed as follows: if the PSK is N octets
6452 * long, concatenate a uint16 with the value N, N zero octets, a second
6453 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006454 *
6455 * mixed-PSK:
6456 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6457 * follows: concatenate a uint16 with the length of the other secret,
6458 * the other secret itself, uint16 with the length of PSK, and the
6459 * PSK itself.
6460 * For details please check:
6461 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6462 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6463 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006464 */
6465
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6467 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6468 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6469 if (prf->other_secret_length != 0) {
6470 memcpy(cur, prf->other_secret, prf->other_secret_length);
6471 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006472 cur += prf->other_secret_length;
6473 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 } else {
6475 *cur++ = MBEDTLS_BYTE_1(data_length);
6476 *cur++ = MBEDTLS_BYTE_0(data_length);
6477 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006478 cur += data_length;
6479 }
6480
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 *cur++ = MBEDTLS_BYTE_1(data_length);
6482 *cur++ = MBEDTLS_BYTE_0(data_length);
6483 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006484 cur += data_length;
6485
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006486 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006487
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006488 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006490}
Janos Follath6660f0e2019-06-17 08:44:03 +01006491
Przemek Stekiele47201b2022-04-19 13:53:28 +02006492static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6493 psa_tls12_prf_key_derivation_t *prf,
6494 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006496{
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6498 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006499 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006500
6501 if (data_length != 0) {
6502 prf->other_secret = mbedtls_calloc(1, data_length);
6503 if (prf->other_secret == NULL) {
6504 return PSA_ERROR_INSUFFICIENT_MEMORY;
6505 }
6506
6507 memcpy(prf->other_secret, data, data_length);
6508 prf->other_secret_length = data_length;
6509 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006510 prf->other_secret_length = 0;
6511 }
6512
6513 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6514
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006516}
6517
Janos Follath6660f0e2019-06-17 08:44:03 +01006518static psa_status_t psa_tls12_prf_psk_to_ms_input(
6519 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006520 psa_key_derivation_step_t step,
6521 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006523{
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006525 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 return psa_tls12_prf_psk_to_ms_set_key(prf,
6527 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006528 break;
6529 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6531 data,
6532 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006533 break;
6534 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006536 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006537
Przemek Stekiele47201b2022-04-19 13:53:28 +02006538 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006539}
John Durkop07cc04a2020-11-16 22:08:34 -08006540#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006541
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006542#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6543static psa_status_t psa_tls12_ecjpake_to_pms_input(
6544 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006545 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006546 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006548{
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6550 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6551 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006552 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006553
6554 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 if (data[0] != 0x04) {
6556 return PSA_ERROR_INVALID_ARGUMENT;
6557 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006558
6559 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006560 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006561
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006563}
6564#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306565
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306566#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306567static psa_status_t psa_pbkdf2_set_input_cost(
6568 psa_pbkdf2_key_derivation_t *pbkdf2,
6569 psa_key_derivation_step_t step,
6570 uint64_t data)
6571{
6572 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6573 return PSA_ERROR_INVALID_ARGUMENT;
6574 }
6575
6576 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6577 return PSA_ERROR_BAD_STATE;
6578 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306579
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306580 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306581 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306582 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306583
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306584 if (data == 0) {
6585 return PSA_ERROR_INVALID_ARGUMENT;
6586 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306587
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306588 pbkdf2->input_cost = data;
6589 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6590
6591 return PSA_SUCCESS;
6592}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306593
6594static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6595 const uint8_t *data,
6596 size_t data_length)
6597{
Gilles Peskineead17662023-08-20 22:02:51 +02006598 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6599 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6600 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6601 /* Appending to existing salt. No state change. */
6602 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306603 return PSA_ERROR_BAD_STATE;
6604 }
6605
Gilles Peskineca57d782023-07-20 19:08:06 +02006606 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006607 /* Appending an empty string, nothing to do. */
6608 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306609 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306610
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306611 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6612 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306613 return PSA_ERROR_INSUFFICIENT_MEMORY;
6614 }
6615
Gilles Peskineead17662023-08-20 22:02:51 +02006616 if (pbkdf2->salt_length != 0) {
6617 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6618 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306619 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306620 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306621 mbedtls_free(pbkdf2->salt);
6622 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306623 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306624 return PSA_SUCCESS;
6625}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306626
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306627#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306628static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306629 const uint8_t *input,
6630 size_t input_len,
6631 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306632 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306633{
6634 psa_status_t status = PSA_SUCCESS;
6635 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6636 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306637 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306638 } else {
6639 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306640 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306641 }
6642 return status;
6643}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306644#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306645
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306646#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306647static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6648 size_t input_len,
6649 uint8_t *output,
6650 size_t *output_len)
6651{
6652 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306653 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306654 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306655 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306656 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6657 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6658 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306659 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306660 * mac_size as the driver function sets mac_output_length = mac_size
6661 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306662 status = psa_driver_wrapper_mac_compute(&attributes,
6663 zeros, sizeof(zeros),
6664 PSA_ALG_CMAC, input, input_len,
6665 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306666 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6667 128U,
6668 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306669 output_len);
6670 } else {
6671 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306672 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306673 }
6674 return status;
6675}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306676#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306677
6678static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306679 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306680 const uint8_t *data,
6681 size_t data_length)
6682{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306683 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306684 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6685 return PSA_ERROR_BAD_STATE;
6686 }
6687
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306688#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306689 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6690 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6691 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6692 pbkdf2->password,
6693 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306694 } else
6695#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6696#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6697 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306698 status = psa_pbkdf2_cmac_set_password(data, data_length,
6699 pbkdf2->password,
6700 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306701 } else
6702#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6703 {
6704 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306705 }
6706
6707 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6708
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306709 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306710}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306711
6712static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306713 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306714 psa_key_derivation_step_t step,
6715 const uint8_t *data,
6716 size_t data_length)
6717{
6718 switch (step) {
6719 case PSA_KEY_DERIVATION_INPUT_SALT:
6720 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6721 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306722 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306723 default:
6724 return PSA_ERROR_INVALID_ARGUMENT;
6725 }
6726}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306727#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306728
Gilles Peskineb8965192019-09-24 16:21:10 +02006729/** Check whether the given key type is acceptable for the given
6730 * input step of a key derivation.
6731 *
6732 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6733 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6734 * Both secret and non-secret inputs can alternatively have the type
6735 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6736 * that the input was passed as a buffer rather than via a key object.
6737 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006738static int psa_key_derivation_check_input_type(
6739 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006740 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006741{
Gilles Peskine449bd832023-01-11 14:50:10 +01006742 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006743 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006744 if (key_type == PSA_KEY_TYPE_DERIVE) {
6745 return PSA_SUCCESS;
6746 }
6747 if (key_type == PSA_KEY_TYPE_NONE) {
6748 return PSA_SUCCESS;
6749 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006750 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006751 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 if (key_type == PSA_KEY_TYPE_DERIVE) {
6753 return PSA_SUCCESS;
6754 }
6755 if (key_type == PSA_KEY_TYPE_NONE) {
6756 return PSA_SUCCESS;
6757 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006758 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006759 case PSA_KEY_DERIVATION_INPUT_LABEL:
6760 case PSA_KEY_DERIVATION_INPUT_SALT:
6761 case PSA_KEY_DERIVATION_INPUT_INFO:
6762 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6764 return PSA_SUCCESS;
6765 }
6766 if (key_type == PSA_KEY_TYPE_NONE) {
6767 return PSA_SUCCESS;
6768 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006769 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306770 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6771 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6772 return PSA_SUCCESS;
6773 }
6774 if (key_type == PSA_KEY_TYPE_DERIVE) {
6775 return PSA_SUCCESS;
6776 }
6777 if (key_type == PSA_KEY_TYPE_NONE) {
6778 return PSA_SUCCESS;
6779 }
6780 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006781 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006782 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006783}
6784
Janos Follathb80a94e2019-06-12 15:54:46 +01006785static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006786 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006787 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006788 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006789 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006791{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006792 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006793 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006794
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 status = psa_key_derivation_check_input_type(step, key_type);
6796 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006797 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006799
Przemek Stekiel69c46792022-06-10 12:59:51 +02006800#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006801 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6802 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6803 step, data, data_length);
6804 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006805#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006806#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6808 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6809 step, data, data_length);
6810 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006811#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6812#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6814 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6815 step, data, data_length);
6816 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006817#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006818#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006820 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6822 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006823#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306824#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306825 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306826 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6827 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306828 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306829#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006830 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006831 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006832 (void) data;
6833 (void) data_length;
6834 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006835 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006836 }
6837
Gilles Peskine224b0d62019-09-23 18:13:17 +02006838exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 if (status != PSA_SUCCESS) {
6840 psa_key_derivation_abort(operation);
6841 }
6842 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006843}
6844
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306845static psa_status_t psa_key_derivation_input_integer_internal(
6846 psa_key_derivation_operation_t *operation,
6847 psa_key_derivation_step_t step,
6848 uint64_t value)
6849{
6850 psa_status_t status;
6851 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6852
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306853#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306854 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306855 status = psa_pbkdf2_set_input_cost(
6856 &operation->ctx.pbkdf2, step, value);
6857 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306858#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306859 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306860 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306861 (void) value;
6862 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306863 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306864 }
6865
6866 if (status != PSA_SUCCESS) {
6867 psa_key_derivation_abort(operation);
6868 }
6869 return status;
6870}
6871
Janos Follath51f4a0f2019-06-14 11:35:55 +01006872psa_status_t psa_key_derivation_input_bytes(
6873 psa_key_derivation_operation_t *operation,
6874 psa_key_derivation_step_t step,
6875 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006877{
Gilles Peskine449bd832023-01-11 14:50:10 +01006878 return psa_key_derivation_input_internal(operation, step,
6879 PSA_KEY_TYPE_NONE,
6880 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006881}
6882
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306883psa_status_t psa_key_derivation_input_integer(
6884 psa_key_derivation_operation_t *operation,
6885 psa_key_derivation_step_t step,
6886 uint64_t value)
6887{
6888 return psa_key_derivation_input_integer_internal(operation, step, value);
6889}
6890
Janos Follath51f4a0f2019-06-14 11:35:55 +01006891psa_status_t psa_key_derivation_input_key(
6892 psa_key_derivation_operation_t *operation,
6893 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006895{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006896 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006897 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006898 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006899
Ronald Cron5c522922020-11-14 16:35:34 +01006900 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006901 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6902 if (status != PSA_SUCCESS) {
6903 psa_key_derivation_abort(operation);
6904 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006905 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006906
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306907 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6908 * permission to output to a key object. */
6909 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6910 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006911 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006912 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006913
Gilles Peskine449bd832023-01-11 14:50:10 +01006914 status = psa_key_derivation_input_internal(operation,
6915 step, slot->attr.type,
6916 slot->key.data,
6917 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006918
Ryan Everett5ac6fa72024-02-14 17:11:36 +00006919 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006920
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006922}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006923
6924
6925
6926/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006927/* Key agreement */
6928/****************************************************************/
6929
Gilles Peskine449bd832023-01-11 14:50:10 +01006930psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6931 const uint8_t *key_buffer,
6932 size_t key_buffer_size,
6933 psa_algorithm_t alg,
6934 const uint8_t *peer_key,
6935 size_t peer_key_length,
6936 uint8_t *shared_secret,
6937 size_t shared_secret_size,
6938 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006939{
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006941#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006942 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6944 key_buffer_size, alg,
6945 peer_key, peer_key_length,
6946 shared_secret,
6947 shared_secret_size,
6948 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006949#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006950
6951#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6952 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006953 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006954 peer_key,
6955 peer_key_length,
6956 key_buffer,
6957 key_buffer_size,
6958 shared_secret,
6959 shared_secret_size,
6960 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006961#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6962
Gilles Peskine01d718c2018-09-18 12:01:02 +02006963 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006964 (void) attributes;
6965 (void) key_buffer;
6966 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006967 (void) peer_key;
6968 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006969 (void) shared_secret;
6970 (void) shared_secret_size;
6971 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006972 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006973 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006974}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006975
Aditya Deshpande17845b82022-10-13 17:21:01 +01006976/** Internal function for raw key agreement
6977 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006978 * to the driver's implementation if a driver is present.
6979 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006980 * (psa_key_agreement_raw_builtin).
6981 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006982static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6983 psa_key_slot_t *private_key,
6984 const uint8_t *peer_key,
6985 size_t peer_key_length,
6986 uint8_t *shared_secret,
6987 size_t shared_secret_size,
6988 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006989{
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6991 return PSA_ERROR_NOT_SUPPORTED;
6992 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006993
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006994 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006995 private_key->key.data,
6996 private_key->key.bytes, alg,
6997 peer_key, peer_key_length,
6998 shared_secret,
6999 shared_secret_size,
7000 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007001}
7002
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007003/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007004 * to potentially free embedded data structures and wipe confidential data.
7005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007006static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7007 psa_key_derivation_step_t step,
7008 psa_key_slot_t *private_key,
7009 const uint8_t *peer_key,
7010 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007011{
7012 psa_status_t status;
Moritz Fischer967f8cd2024-03-02 00:55:06 +00007013 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
Gilles Peskine01d718c2018-09-18 12:01:02 +02007014 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007015 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007016
7017 /* Step 1: run the secret agreement algorithm to generate the shared
7018 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 status = psa_key_agreement_raw_internal(ka_alg,
7020 private_key,
7021 peer_key, peer_key_length,
7022 shared_secret,
7023 sizeof(shared_secret),
7024 &shared_secret_length);
7025 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007026 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007028
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007029 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007030 * the shared secret. A shared secret is permitted wherever a key
7031 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007032 status = psa_key_derivation_input_internal(operation, step,
7033 PSA_KEY_TYPE_DERIVE,
7034 shared_secret,
7035 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007036exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7038 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007039}
7040
Gilles Peskine449bd832023-01-11 14:50:10 +01007041psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7042 psa_key_derivation_step_t step,
7043 mbedtls_svc_key_id_t private_key,
7044 const uint8_t *peer_key,
7045 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007046{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007047 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007048 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007049 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007050
Gilles Peskine449bd832023-01-11 14:50:10 +01007051 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7052 return PSA_ERROR_INVALID_ARGUMENT;
7053 }
Ronald Cron5c522922020-11-14 16:35:34 +01007054 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007055 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7056 if (status != PSA_SUCCESS) {
7057 return status;
7058 }
7059 status = psa_key_agreement_internal(operation, step,
7060 slot,
7061 peer_key, peer_key_length);
7062 if (status != PSA_SUCCESS) {
7063 psa_key_derivation_abort(operation);
7064 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007065 /* If a private key has been added as SECRET, we allow the derived
7066 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007068 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007070 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007071
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007072 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007073
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007075}
7076
Gilles Peskine449bd832023-01-11 14:50:10 +01007077psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7078 mbedtls_svc_key_id_t private_key,
7079 const uint8_t *peer_key,
7080 size_t peer_key_length,
7081 uint8_t *output,
7082 size_t output_size,
7083 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007084{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007085 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007086 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007087 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007088 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007089
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007091 status = PSA_ERROR_INVALID_ARGUMENT;
7092 goto exit;
7093 }
Ronald Cron5c522922020-11-14 16:35:34 +01007094 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7096 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007097 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007099
Gilles Peskine3e561302022-04-14 00:17:15 +02007100 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7101 * for the output size. The PSA specification only guarantees that this
7102 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7103 * but it might be nice to allow smaller buffers if the output fits.
7104 * At the time of writing this comment, with only ECDH implemented,
7105 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7106 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7107 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007108 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7110 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007111 status = PSA_ERROR_BUFFER_TOO_SMALL;
7112 goto exit;
7113 }
7114
Gilles Peskine449bd832023-01-11 14:50:10 +01007115 status = psa_key_agreement_raw_internal(alg, slot,
7116 peer_key, peer_key_length,
7117 output, output_size,
7118 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007119
7120exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007122 /* If an error happens and is not handled properly, the output
7123 * may be used as a key to protect sensitive data. Arrange for such
7124 * a key to be random, which is likely to result in decryption or
7125 * verification errors. This is better than filling the buffer with
7126 * some constant data such as zeros, which would result in the data
7127 * being protected with a reproducible, easily knowable key.
7128 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007129 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007130 *output_length = output_size;
7131 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007132
Ryan Everetta103ec92024-01-31 13:59:57 +00007133 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007134
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007136}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007137
7138
Gilles Peskine30524eb2020-11-13 17:02:26 +01007139
Gilles Peskine86a440b2018-11-12 18:39:40 +01007140/****************************************************************/
7141/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007142/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007143
Gilles Peskine672a7712023-04-28 21:00:28 +02007144#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7145#include "entropy_poll.h"
7146#endif
7147
Gilles Peskine30524eb2020-11-13 17:02:26 +01007148/** Initialize the PSA random generator.
Paul Elliottd35dce62024-03-15 13:57:16 +00007149 *
7150 * Note: the mbedtls_threading_psa_rngdata_mutex should be held when calling
7151 * this function if mutexes are enabled.
Gilles Peskine30524eb2020-11-13 17:02:26 +01007152 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007153static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007154{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007155#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007156 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007157#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7158
Gilles Peskine30524eb2020-11-13 17:02:26 +01007159 /* Set default configuration if
7160 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007161 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007162 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007163 }
7164 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007165 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007167
Gilles Peskine449bd832023-01-11 14:50:10 +01007168 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007169#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7170 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7171 /* The PSA entropy injection feature depends on using NV seed as an entropy
7172 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 mbedtls_entropy_add_source(&rng->entropy,
7174 mbedtls_nv_seed_poll, NULL,
7175 MBEDTLS_ENTROPY_BLOCK_SIZE,
7176 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007177#endif
7178
Valerio Setti061d4e42024-02-26 12:52:44 +01007179 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007180#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007181}
7182
7183/** Deinitialize the PSA random generator.
Paul Elliottd35dce62024-03-15 13:57:16 +00007184 *
7185 * Note: the mbedtls_threading_psa_rngdata_mutex should be held when calling
7186 * this function if mutexes are enabled.
Gilles Peskine30524eb2020-11-13 17:02:26 +01007187 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007188static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007189{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007190#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007191 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007192#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01007193 mbedtls_psa_drbg_free(&rng->drbg);
Valerio Setti83e0de82023-11-24 12:13:05 +01007194 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007195#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007196}
7197
7198/** Seed the PSA random generator.
7199 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007200static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007201{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007202#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7203 /* Do nothing: the external RNG seeds itself. */
7204 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007205 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007206#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007207 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01007208 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01007209 drbg_seed, sizeof(drbg_seed) - 1);
7210 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007211#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007212}
7213
Gilles Peskine449bd832023-01-11 14:50:10 +01007214psa_status_t psa_generate_random(uint8_t *output,
7215 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007216{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007217 GUARD_MODULE_INITIALIZED;
7218
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007219#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7220
7221 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007222 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7223 output, output_size,
7224 &output_length);
7225 if (status != PSA_SUCCESS) {
7226 return status;
7227 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007228 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007229 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 if (output_length != output_size) {
7231 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7232 }
7233 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007234
7235#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7236
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 while (output_size > 0) {
Valerio Setti718180c2024-02-27 11:58:39 +01007238 int ret = MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
Gilles Peskine71ddab92021-01-04 21:01:07 +01007239 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007240 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7241 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7242 output_size);
Valerio Setti718180c2024-02-27 11:58:39 +01007243#if defined(MBEDTLS_CTR_DRBG_C)
7244 ret = mbedtls_ctr_drbg_random(&global_data.rng.drbg, output, request_size);
7245#elif defined(MBEDTLS_HMAC_DRBG_C)
7246 ret = mbedtls_hmac_drbg_random(&global_data.rng.drbg, output, request_size);
7247#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01007248 if (ret != 0) {
7249 return mbedtls_to_psa_error(ret);
7250 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007251 output_size -= request_size;
7252 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007253 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007255#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007256}
7257
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007258#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007259psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7260 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007261{
Paul Elliott600472b2024-02-23 17:26:58 +00007262 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 return PSA_ERROR_NOT_PERMITTED;
7264 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007265
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7267 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7268 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7269 return PSA_ERROR_INVALID_ARGUMENT;
7270 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007271
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007273}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007274#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007275
Ronald Cron3772afe2021-02-08 16:10:05 +01007276/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007277 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007278 * \param type The key type
7279 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007280 *
7281 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007282 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007283 * \retval #PSA_ERROR_INVALID_ARGUMENT
7284 * The size in bits of the key is not valid.
7285 * \retval #PSA_ERROR_NOT_SUPPORTED
7286 * The type and/or the size in bits of the key or the combination of
7287 * the two is not supported.
7288 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007289static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007290 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007291{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007293
Gilles Peskine449bd832023-01-11 14:50:10 +01007294 if (key_type_is_raw_bytes(type)) {
7295 status = psa_validate_unstructured_key_bit_size(type, bits);
7296 if (status != PSA_SUCCESS) {
7297 return status;
7298 }
7299 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007300#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007301 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7302 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7303 return PSA_ERROR_NOT_SUPPORTED;
7304 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007305 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007306 return PSA_ERROR_NOT_SUPPORTED;
7307 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007308
Ronald Cron01b2aba2020-10-05 09:42:02 +02007309 /* Accept only byte-aligned keys, for the same reasons as
7310 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007311 if (bits % 8 != 0) {
7312 return PSA_ERROR_NOT_SUPPORTED;
7313 }
7314 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007315#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007316
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007317#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007318 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007319 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 return PSA_SUCCESS;
7321 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007322#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007323
Valerio Settia55f0422023-07-10 15:34:41 +02007324#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007325 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007326 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007327 return PSA_ERROR_NOT_SUPPORTED;
7328 }
7329 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007330#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007331 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007333 }
7334
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007336}
7337
Ronald Cron55ed0592020-10-05 10:30:40 +02007338psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007339 const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007340 const psa_key_production_parameters_t *params, size_t params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007341 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007342{
7343 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007344 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007345
Gilles Peskine7a18f962024-02-12 16:48:11 +01007346 /* Only used for RSA */
Gilles Peskine092ce512024-02-20 12:31:24 +01007347 (void) params;
7348 (void) params_data_length;
Gilles Peskine7a18f962024-02-12 16:48:11 +01007349
Gilles Peskine449bd832023-01-11 14:50:10 +01007350 if (key_type_is_raw_bytes(type)) {
7351 status = psa_generate_random(key_buffer, key_buffer_size);
7352 if (status != PSA_SUCCESS) {
7353 return status;
7354 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007355
David Brown12ca5032021-02-11 11:02:00 -07007356#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007357 if (type == PSA_KEY_TYPE_DES) {
7358 psa_des_set_key_parity(key_buffer, key_buffer_size);
7359 }
David Brown8107e312021-02-12 08:08:46 -07007360#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007361 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007362
Valerio Setti76df8c12023-07-11 14:11:28 +02007363#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007364 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskinee22f6a92024-02-26 15:45:33 +01007365 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskine4c32b692024-02-16 00:11:09 +01007366 params, params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007367 key_buffer,
7368 key_buffer_size,
7369 key_buffer_length);
7370 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007371#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007372
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007373#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007374 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7375 return mbedtls_psa_ecp_generate_key(attributes,
7376 key_buffer,
7377 key_buffer_size,
7378 key_buffer_length);
7379 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007380#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007381
Valerio Settia55f0422023-07-10 15:34:41 +02007382#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007383 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7384 return mbedtls_psa_ffdh_generate_key(attributes,
7385 key_buffer,
7386 key_buffer_size,
7387 key_buffer_length);
7388 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007389#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007390 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 (void) key_buffer_length;
7392 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007393 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007394
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007396}
Darryl Green0c6575a2018-11-07 16:05:30 +00007397
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007398psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007399 const psa_key_production_parameters_t *params,
7400 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007401 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007402{
7403 psa_status_t status;
7404 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007405 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007406 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007407
Ronald Cron81709fc2020-11-14 12:10:32 +01007408 *key = MBEDTLS_SVC_KEY_ID_INIT;
7409
Gilles Peskine0f84d622019-09-12 19:03:13 +02007410 /* Reject any attempt to create a zero-length key so that we don't
7411 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007412 if (psa_get_key_bits(attributes) == 0) {
7413 return PSA_ERROR_INVALID_ARGUMENT;
7414 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007415
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007416 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007417 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007418 return PSA_ERROR_INVALID_ARGUMENT;
7419 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007420
Gilles Peskine7a18f962024-02-12 16:48:11 +01007421#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007422 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +01007423 if (params->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01007424 return PSA_ERROR_INVALID_ARGUMENT;
7425 }
7426 } else
7427#endif
Gilles Peskine092ce512024-02-20 12:31:24 +01007428 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007429 return PSA_ERROR_INVALID_ARGUMENT;
7430 }
7431
Gilles Peskine449bd832023-01-11 14:50:10 +01007432 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7433 &slot, &driver);
7434 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007435 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007436 }
Gilles Peskine11792082019-08-06 18:36:36 +02007437
Ronald Cron977c2472020-10-13 08:32:21 +02007438 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307439 * storage ( thus not in the case of generating a key in a secure element
7440 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7441 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007442 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007443 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007445 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007446 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007448 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007450
7451 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007452 attributes->type,
7453 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007455 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007456 attributes, &key_buffer_size);
7457 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007458 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007459 }
Ronald Cron977c2472020-10-13 08:32:21 +02007460 }
Ronald Cron977c2472020-10-13 08:32:21 +02007461
Gilles Peskine449bd832023-01-11 14:50:10 +01007462 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7463 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007464 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 }
Ronald Cron977c2472020-10-13 08:32:21 +02007466 }
7467
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007469 params, params_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01007470 slot->key.data, slot->key.bytes,
7471 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 if (status != PSA_SUCCESS) {
7473 psa_remove_key_data_from_memory(slot);
7474 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007475
Gilles Peskine11792082019-08-06 18:36:36 +02007476exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007477 if (status == PSA_SUCCESS) {
7478 status = psa_finish_key_creation(slot, driver, key);
7479 }
7480 if (status != PSA_SUCCESS) {
7481 psa_fail_key_creation(slot, driver);
7482 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007483
Gilles Peskine449bd832023-01-11 14:50:10 +01007484 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007485}
7486
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007487psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7488 mbedtls_svc_key_id_t *key)
7489{
7490 return psa_generate_key_ext(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007491 &default_production_parameters, 0,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007492 key);
7493}
7494
Gilles Peskinee59236f2018-01-27 23:32:46 +01007495/****************************************************************/
7496/* Module setup */
7497/****************************************************************/
7498
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007499#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007500psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 void (* entropy_init)(mbedtls_entropy_context *ctx),
7502 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007503{
Paul Elliott8e151532024-02-23 17:35:29 +00007504 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7505
7506#if defined(MBEDTLS_THREADING_C)
7507 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
7508#endif /* defined(MBEDTLS_THREADING_C) */
7509
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
Paul Elliott8e151532024-02-23 17:35:29 +00007511 status = PSA_ERROR_BAD_STATE;
7512 } else {
7513 global_data.rng.entropy_init = entropy_init;
7514 global_data.rng.entropy_free = entropy_free;
7515 status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01007516 }
Paul Elliott8e151532024-02-23 17:35:29 +00007517
7518#if defined(MBEDTLS_THREADING_C)
7519 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
7520#endif /* defined(MBEDTLS_THREADING_C) */
7521
7522 return status;
Gilles Peskine5e769522018-11-20 21:59:56 +01007523}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007524#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007525
Gilles Peskine449bd832023-01-11 14:50:10 +01007526void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007527{
Paul Elliott47cee8e2024-03-08 21:38:02 +00007528
Paul Elliott600472b2024-02-23 17:26:58 +00007529#if defined(MBEDTLS_THREADING_C)
7530 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
7531#endif /* defined(MBEDTLS_THREADING_C) */
7532
Paul Elliott47cee8e2024-03-08 21:38:02 +00007533 /* Nothing to do to free transaction. */
7534 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED) {
7535 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
Valerio Setti83e0de82023-11-24 12:13:05 +01007536 }
Paul Elliott600472b2024-02-23 17:26:58 +00007537
Paul Elliott47cee8e2024-03-08 21:38:02 +00007538 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED) {
7539 psa_wipe_all_key_slots();
7540 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
7541 }
Ronald Cron9ba76912021-04-10 16:57:30 +02007542
Paul Elliott600472b2024-02-23 17:26:58 +00007543#if defined(MBEDTLS_THREADING_C)
7544 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
7545#endif /* defined(MBEDTLS_THREADING_C) */
7546
Paul Elliott47cee8e2024-03-08 21:38:02 +00007547#if defined(MBEDTLS_THREADING_C)
7548 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
7549#endif /* defined(MBEDTLS_THREADING_C) */
7550
7551 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7552 mbedtls_psa_random_free(&global_data.rng);
7553 }
7554 global_data.rng_state = RNG_NOT_INITIALIZED;
7555 mbedtls_platform_zeroize(&global_data.rng, sizeof(global_data.rng));
7556
7557#if defined(MBEDTLS_THREADING_C)
7558 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
7559#endif /* defined(MBEDTLS_THREADING_C) */
7560
7561#if defined(MBEDTLS_THREADING_C)
7562 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
7563#endif /* defined(MBEDTLS_THREADING_C) */
7564
Ronald Cron9ba76912021-04-10 16:57:30 +02007565 /* Terminate drivers */
Paul Elliott47cee8e2024-03-08 21:38:02 +00007566 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED) {
7567 psa_driver_wrapper_free();
7568 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
7569 }
7570
7571#if defined(MBEDTLS_THREADING_C)
7572 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
7573#endif /* defined(MBEDTLS_THREADING_C) */
7574
Gilles Peskinee59236f2018-01-27 23:32:46 +01007575}
7576
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007577#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7578/** Recover a transaction that was interrupted by a power failure.
7579 *
7580 * This function is called during initialization, before psa_crypto_init()
7581 * returns. If this function returns a failure status, the initialization
7582 * fails.
7583 */
7584static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007585 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007586{
Gilles Peskine449bd832023-01-11 14:50:10 +01007587 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007588 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7589 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 /* TODO - fall through to the failure case until this
7591 * is implemented.
7592 * https://github.com/ARMmbed/mbed-crypto/issues/218
7593 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007594 default:
7595 /* We found an unsupported transaction in the storage.
7596 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007598 }
7599}
7600#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7601
Paul Elliott47cee8e2024-03-08 21:38:02 +00007602static psa_status_t mbedtls_psa_crypto_init_subsystem(mbedtls_psa_crypto_subsystem subsystem)
7603{
7604 psa_status_t status = PSA_SUCCESS;
7605 uint8_t driver_wrappers_initialized = 0;
7606
7607 switch (subsystem) {
7608 case PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS:
7609
7610#if defined(MBEDTLS_THREADING_C)
7611 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
7612#endif /* defined(MBEDTLS_THREADING_C) */
7613
7614 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED)) {
7615 /* Init drivers */
7616 status = psa_driver_wrapper_init();
7617
7618 /* Drivers need shutdown regardless of startup errors. */
7619 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
7620
7621
7622 }
7623#if defined(MBEDTLS_THREADING_C)
7624 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7625 &mbedtls_threading_psa_globaldata_mutex));
7626#endif /* defined(MBEDTLS_THREADING_C) */
7627
7628 break;
7629
7630 case PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS:
7631
7632#if defined(MBEDTLS_THREADING_C)
7633 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
7634#endif /* defined(MBEDTLS_THREADING_C) */
7635
7636 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED)) {
7637 status = psa_initialize_key_slots();
7638
7639 /* Need to wipe keys even if initialization fails. */
7640 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
7641
7642 }
7643#if defined(MBEDTLS_THREADING_C)
7644 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7645 &mbedtls_threading_psa_globaldata_mutex));
7646#endif /* defined(MBEDTLS_THREADING_C) */
7647
7648 break;
7649
7650 case PSA_CRYPTO_SUBSYSTEM_RNG:
7651
7652#if defined(MBEDTLS_THREADING_C)
7653 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
7654#endif /* defined(MBEDTLS_THREADING_C) */
7655
7656 driver_wrappers_initialized =
7657 (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED);
7658
7659#if defined(MBEDTLS_THREADING_C)
7660 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7661 &mbedtls_threading_psa_globaldata_mutex));
7662#endif /* defined(MBEDTLS_THREADING_C) */
7663
7664 /* Need to use separate mutex here, as initialisation can require
7665 * testing of init flags, which requires locking the global data
7666 * mutex. */
7667#if defined(MBEDTLS_THREADING_C)
7668 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex));
7669#endif /* defined(MBEDTLS_THREADING_C) */
7670
7671 /* Initialize and seed the random generator. */
7672 if (global_data.rng_state == RNG_NOT_INITIALIZED && driver_wrappers_initialized) {
7673 mbedtls_psa_random_init(&global_data.rng);
7674 global_data.rng_state = RNG_INITIALIZED;
7675
7676 status = mbedtls_psa_random_seed(&global_data.rng);
7677 if (status == PSA_SUCCESS) {
7678 global_data.rng_state = RNG_SEEDED;
7679 }
7680 }
7681
7682#if defined(MBEDTLS_THREADING_C)
7683 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7684 &mbedtls_threading_psa_rngdata_mutex));
7685#endif /* defined(MBEDTLS_THREADING_C) */
7686
Paul Elliott47cee8e2024-03-08 21:38:02 +00007687 break;
7688
7689 case PSA_CRYPTO_SUBSYSTEM_TRANSACTION:
7690
7691#if defined(MBEDTLS_THREADING_C)
7692 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
7693#endif /* defined(MBEDTLS_THREADING_C) */
7694
7695 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED)) {
7696#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7697 status = psa_crypto_load_transaction();
7698 if (status == PSA_SUCCESS) {
7699 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7700 if (status == PSA_SUCCESS) {
7701 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
7702 }
7703 status = psa_crypto_stop_transaction();
7704 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
7705 /* There's no transaction to complete. It's all good. */
7706 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
7707 status = PSA_SUCCESS;
7708 }
7709#else /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
7710 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
7711 status = PSA_SUCCESS;
7712#endif /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
7713 }
7714
7715#if defined(MBEDTLS_THREADING_C)
7716 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7717 &mbedtls_threading_psa_globaldata_mutex));
7718#endif /* defined(MBEDTLS_THREADING_C) */
7719
7720 break;
7721
7722 default:
7723 status = PSA_ERROR_CORRUPTION_DETECTED;
7724 }
7725
7726 /* Exit label only required when using threading macros. */
7727#if defined(MBEDTLS_THREADING_C)
7728exit:
7729#endif /* defined(MBEDTLS_THREADING_C) */
7730
7731 return status;
7732}
7733
Gilles Peskine449bd832023-01-11 14:50:10 +01007734psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007735{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007736 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007737
Paul Elliott47cee8e2024-03-08 21:38:02 +00007738 /* Double initialization is explicitly allowed. Early out if everything is
7739 * done. */
7740 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007741 return PSA_SUCCESS;
7742 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007743
Paul Elliott47cee8e2024-03-08 21:38:02 +00007744 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS);
Valerio Setti402cfba2023-11-13 10:24:32 +01007745 if (status != PSA_SUCCESS) {
7746 goto exit;
7747 }
7748
Paul Elliott47cee8e2024-03-08 21:38:02 +00007749 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS);
Gilles Peskine449bd832023-01-11 14:50:10 +01007750 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007751 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007752 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007753
Paul Elliott47cee8e2024-03-08 21:38:02 +00007754 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_RNG);
7755 if (status != PSA_SUCCESS) {
7756 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02007757 }
Gilles Peskinefc762652019-07-22 19:30:34 +02007758
Paul Elliott47cee8e2024-03-08 21:38:02 +00007759 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_TRANSACTION);
Gilles Peskinee59236f2018-01-27 23:32:46 +01007760
7761exit:
Paul Elliott600472b2024-02-23 17:26:58 +00007762
Gilles Peskine449bd832023-01-11 14:50:10 +01007763 if (status != PSA_SUCCESS) {
7764 mbedtls_psa_crypto_free();
7765 }
Paul Elliott47cee8e2024-03-08 21:38:02 +00007766
Gilles Peskine449bd832023-01-11 14:50:10 +01007767 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007768}
7769
Yanray Wangffc3c482023-07-11 12:01:04 +08007770#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007771psa_status_t psa_crypto_driver_pake_get_password_len(
7772 const psa_crypto_driver_pake_inputs_t *inputs,
7773 size_t *password_len)
7774{
7775 if (inputs->password_len == 0) {
7776 return PSA_ERROR_BAD_STATE;
7777 }
7778
7779 *password_len = inputs->password_len;
7780
7781 return PSA_SUCCESS;
7782}
7783
7784psa_status_t psa_crypto_driver_pake_get_password(
7785 const psa_crypto_driver_pake_inputs_t *inputs,
7786 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7787{
7788 if (inputs->password_len == 0) {
7789 return PSA_ERROR_BAD_STATE;
7790 }
7791
7792 if (buffer_size < inputs->password_len) {
7793 return PSA_ERROR_BUFFER_TOO_SMALL;
7794 }
7795
7796 memcpy(buffer, inputs->password, inputs->password_len);
7797 *buffer_length = inputs->password_len;
7798
7799 return PSA_SUCCESS;
7800}
7801
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007802psa_status_t psa_crypto_driver_pake_get_user_len(
7803 const psa_crypto_driver_pake_inputs_t *inputs,
7804 size_t *user_len)
7805{
7806 if (inputs->user_len == 0) {
7807 return PSA_ERROR_BAD_STATE;
7808 }
7809
7810 *user_len = inputs->user_len;
7811
7812 return PSA_SUCCESS;
7813}
7814
7815psa_status_t psa_crypto_driver_pake_get_user(
7816 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007817 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007818{
7819 if (inputs->user_len == 0) {
7820 return PSA_ERROR_BAD_STATE;
7821 }
7822
Przemek Stekielc0e62502023-03-14 11:49:36 +01007823 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007824 return PSA_ERROR_BUFFER_TOO_SMALL;
7825 }
7826
Przemek Stekielc0e62502023-03-14 11:49:36 +01007827 memcpy(user_id, inputs->user, inputs->user_len);
7828 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007829
7830 return PSA_SUCCESS;
7831}
7832
7833psa_status_t psa_crypto_driver_pake_get_peer_len(
7834 const psa_crypto_driver_pake_inputs_t *inputs,
7835 size_t *peer_len)
7836{
7837 if (inputs->peer_len == 0) {
7838 return PSA_ERROR_BAD_STATE;
7839 }
7840
7841 *peer_len = inputs->peer_len;
7842
7843 return PSA_SUCCESS;
7844}
7845
7846psa_status_t psa_crypto_driver_pake_get_peer(
7847 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007848 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007849{
7850 if (inputs->peer_len == 0) {
7851 return PSA_ERROR_BAD_STATE;
7852 }
7853
Przemek Stekielc0e62502023-03-14 11:49:36 +01007854 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007855 return PSA_ERROR_BUFFER_TOO_SMALL;
7856 }
7857
Przemek Stekielc0e62502023-03-14 11:49:36 +01007858 memcpy(peer_id, inputs->peer, inputs->peer_len);
7859 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007860
7861 return PSA_SUCCESS;
7862}
7863
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007864psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7865 const psa_crypto_driver_pake_inputs_t *inputs,
7866 psa_pake_cipher_suite_t *cipher_suite)
7867{
7868 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7869 return PSA_ERROR_BAD_STATE;
7870 }
7871
7872 *cipher_suite = inputs->cipher_suite;
7873
7874 return PSA_SUCCESS;
7875}
7876
Neil Armstronga7d08c32022-06-01 18:21:20 +02007877psa_status_t psa_pake_setup(
7878 psa_pake_operation_t *operation,
7879 const psa_pake_cipher_suite_t *cipher_suite)
7880{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007881 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007882
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007883 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007884 status = PSA_ERROR_BAD_STATE;
7885 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007886 }
7887
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007888 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007889 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007890 status = PSA_ERROR_INVALID_ARGUMENT;
7891 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007892 }
7893
Przemek Stekiel51eac532022-12-07 11:04:51 +01007894 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7895
Przemek Stekiele12ed362022-12-21 12:54:46 +01007896 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007897 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7898 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007899 operation->data.inputs.cipher_suite = *cipher_suite;
7900
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007901#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007902 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007903 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007904 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007905
David Horstmann16f01512023-06-14 17:21:07 +01007906 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007907 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007908 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007909#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007910 {
7911 status = PSA_ERROR_NOT_SUPPORTED;
7912 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007913 }
7914
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007915 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7916
Przemek Stekiel51eac532022-12-07 11:04:51 +01007917 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007918exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007919 psa_pake_abort(operation);
7920 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007921}
7922
7923psa_status_t psa_pake_set_password_key(
7924 psa_pake_operation_t *operation,
7925 mbedtls_svc_key_id_t password)
7926{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007927 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007928 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7929 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007930 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007931
Przemek Stekiel51eac532022-12-07 11:04:51 +01007932 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007933 status = PSA_ERROR_BAD_STATE;
7934 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007935 }
7936
Przemek Stekiel6c764412022-11-22 14:05:12 +01007937 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7938 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007939 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007940 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007941 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007942 }
7943
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007944 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007945
Przemek Stekiel51eac532022-12-07 11:04:51 +01007946 if (type != PSA_KEY_TYPE_PASSWORD &&
7947 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7948 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007949 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007950 }
7951
Przemek Stekiel51eac532022-12-07 11:04:51 +01007952 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7953 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007954 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007955 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007956 }
7957
7958 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7959 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01007960 operation->data.inputs.attributes = slot->attr;
7961
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007962exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007963 if (status != PSA_SUCCESS) {
7964 psa_pake_abort(operation);
7965 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00007966 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007967 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007968}
7969
7970psa_status_t psa_pake_set_user(
7971 psa_pake_operation_t *operation,
7972 const uint8_t *user_id,
7973 size_t user_id_len)
7974{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007975 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007976
7977 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007978 status = PSA_ERROR_BAD_STATE;
7979 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007980 }
7981
Przemek Stekiel51eac532022-12-07 11:04:51 +01007982 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007983 status = PSA_ERROR_INVALID_ARGUMENT;
7984 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007985 }
7986
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007987 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007988 status = PSA_ERROR_BAD_STATE;
7989 goto exit;
7990 }
7991
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007992 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7993 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007994 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7995 goto exit;
7996 }
7997
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007998 memcpy(operation->data.inputs.user, user_id, user_id_len);
7999 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008000
8001 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008002exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008003 psa_pake_abort(operation);
8004 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008005}
8006
8007psa_status_t psa_pake_set_peer(
8008 psa_pake_operation_t *operation,
8009 const uint8_t *peer_id,
8010 size_t peer_id_len)
8011{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008012 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008013
8014 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008015 status = PSA_ERROR_BAD_STATE;
8016 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008017 }
8018
Przemek Stekiel51eac532022-12-07 11:04:51 +01008019 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008020 status = PSA_ERROR_INVALID_ARGUMENT;
8021 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008022 }
8023
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008024 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008025 status = PSA_ERROR_BAD_STATE;
8026 goto exit;
8027 }
8028
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008029 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8030 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008031 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8032 goto exit;
8033 }
8034
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008035 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8036 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008037
8038 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008039exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008040 psa_pake_abort(operation);
8041 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008042}
8043
8044psa_status_t psa_pake_set_role(
8045 psa_pake_operation_t *operation,
8046 psa_pake_role_t role)
8047{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008048 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008049
Przemek Stekiel51eac532022-12-07 11:04:51 +01008050 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008051 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008052 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008053 }
8054
Przemek Stekiel09104b82023-03-06 14:11:51 +01008055 switch (operation->alg) {
8056#if defined(PSA_WANT_ALG_JPAKE)
8057 case PSA_ALG_JPAKE:
8058 if (role == PSA_PAKE_ROLE_NONE) {
8059 return PSA_SUCCESS;
8060 }
8061 status = PSA_ERROR_INVALID_ARGUMENT;
8062 break;
8063#endif
8064 default:
8065 (void) role;
8066 status = PSA_ERROR_NOT_SUPPORTED;
8067 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008068 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008069exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008070 psa_pake_abort(operation);
8071 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008072}
8073
David Horstmanne7f21e62023-05-12 18:17:21 +01008074/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008075#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008076static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008077 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008078{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008079 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008080 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008081 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008082
David Horstmann024e5c52023-06-14 15:48:21 +01008083 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008084 is_x1 = (stage->outputs < 1);
8085 } else {
8086 is_x1 = (stage->inputs < 1);
8087 }
8088
David Horstmann74a3d8c2023-06-14 18:28:19 +01008089 key_share_step = is_x1 ?
8090 PSA_JPAKE_X1_STEP_KEY_SHARE :
8091 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008092 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008093 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8094 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8095 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8096 } else {
8097 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008098 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008099 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008100}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008101#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008102
Przemek Stekiel2797d372022-12-22 11:19:22 +01008103static psa_status_t psa_pake_complete_inputs(
8104 psa_pake_operation_t *operation)
8105{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008106 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008107 /* Create copy of the inputs on stack as inputs share memory
8108 with the driver context which will be setup by the driver. */
8109 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008110
Przemek Stekielfde11282023-03-13 16:06:09 +01008111 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008112 return PSA_ERROR_BAD_STATE;
8113 }
8114
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008115 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008116 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8117 return PSA_ERROR_BAD_STATE;
8118 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008119 }
8120
Przemek Stekiel18620a32023-01-17 16:34:52 +01008121 /* Clear driver context */
8122 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8123
8124 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008125
8126 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008127 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008128
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008129 /* User and peer are translated to role. */
8130 mbedtls_free(inputs.user);
8131 mbedtls_free(inputs.peer);
8132
Przemek Stekiel2797d372022-12-22 11:19:22 +01008133 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008134#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008135 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008136 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008137 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008138#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008139 {
8140 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008141 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008142 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008143 return status;
8144}
8145
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008146#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008147static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008148 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008149 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008150 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008151{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008152 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8153 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8154 step != PSA_PAKE_STEP_ZK_PROOF) {
8155 return PSA_ERROR_INVALID_ARGUMENT;
8156 }
8157
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008158 psa_jpake_computation_stage_t *computation_stage =
8159 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008160
David Horstmann5da95602023-06-08 15:37:12 +01008161 if (computation_stage->round != PSA_JPAKE_FIRST &&
8162 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008163 return PSA_ERROR_BAD_STATE;
8164 }
8165
David Horstmanne7f21e62023-05-12 18:17:21 +01008166 /* Check that the step we are given is the one we were expecting */
8167 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008168 return PSA_ERROR_BAD_STATE;
8169 }
8170
David Horstmanne7f21e62023-05-12 18:17:21 +01008171 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8172 computation_stage->inputs == 0 &&
8173 computation_stage->outputs == 0) {
8174 /* Start of the round, so function decides whether we are inputting
8175 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008176 computation_stage->io_mode = io_mode;
8177 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008178 /* Middle of the round so the mode we are in must match the function
8179 * called by the user */
8180 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008181 }
8182
8183 return PSA_SUCCESS;
8184}
8185
David Horstmanne7f21e62023-05-12 18:17:21 +01008186static psa_status_t psa_jpake_epilogue(
8187 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008188 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008189{
David Horstmanne7f21e62023-05-12 18:17:21 +01008190 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008191 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008192
David Horstmanne7f21e62023-05-12 18:17:21 +01008193 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8194 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008195 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008196 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008197 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008198 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008199 }
8200 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008201 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008202 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008203 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008204 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008205 }
8206 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008207 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8208 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008209 /* End of a round, move to the next round */
8210 stage->inputs = 0;
8211 stage->outputs = 0;
8212 stage->round++;
8213 }
8214 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008215 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008216 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008217 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008218 return PSA_SUCCESS;
8219}
David Horstmanne7f21e62023-05-12 18:17:21 +01008220
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008221#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008222
Neil Armstronga7d08c32022-06-01 18:21:20 +02008223psa_status_t psa_pake_output(
8224 psa_pake_operation_t *operation,
8225 psa_pake_step_t step,
8226 uint8_t *output,
8227 size_t output_size,
8228 size_t *output_length)
8229{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008231 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008232 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008233
8234 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008235 status = psa_pake_complete_inputs(operation);
8236 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008237 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008238 }
8239 }
8240
8241 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008242 status = PSA_ERROR_BAD_STATE;
8243 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008244 }
8245
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008246 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008247 status = PSA_ERROR_INVALID_ARGUMENT;
8248 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008249 }
8250
Przemek Stekiele12ed362022-12-21 12:54:46 +01008251 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008252#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008253 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008254 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008255 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008256 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008257 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008258 driver_step = convert_jpake_computation_stage_to_driver_step(
8259 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008260 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008261#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008262 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008263 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008264 status = PSA_ERROR_NOT_SUPPORTED;
8265 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008266 }
8267
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008268 status = psa_driver_wrapper_pake_output(operation, driver_step,
8269 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008270
8271 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008272 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008273 }
8274
8275 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008276#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008277 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008278 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008279 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008280 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008281 }
8282 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008283#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008284 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008285 status = PSA_ERROR_NOT_SUPPORTED;
8286 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008287 }
8288
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008289 return PSA_SUCCESS;
8290exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008291 psa_pake_abort(operation);
8292 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008293}
8294
8295psa_status_t psa_pake_input(
8296 psa_pake_operation_t *operation,
8297 psa_pake_step_t step,
8298 const uint8_t *input,
8299 size_t input_length)
8300{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008301 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008302 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008303 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8304 operation->primitive,
8305 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008306
8307 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008308 status = psa_pake_complete_inputs(operation);
8309 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008310 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008311 }
8312 }
8313
8314 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008315 status = PSA_ERROR_BAD_STATE;
8316 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008317 }
8318
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008319 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008320 status = PSA_ERROR_INVALID_ARGUMENT;
8321 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008322 }
8323
Przemek Stekiele12ed362022-12-21 12:54:46 +01008324 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008325#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008326 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008327 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008328 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008329 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008330 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008331 driver_step = convert_jpake_computation_stage_to_driver_step(
8332 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008333 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008334#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008335 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008336 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008337 status = PSA_ERROR_NOT_SUPPORTED;
8338 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008339 }
8340
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008341 status = psa_driver_wrapper_pake_input(operation, driver_step,
8342 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008343
8344 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008345 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008346 }
8347
8348 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008349#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008350 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008351 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008352 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008353 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008354 }
8355 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008356#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008357 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008358 status = PSA_ERROR_NOT_SUPPORTED;
8359 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008360 }
8361
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008362 return PSA_SUCCESS;
8363exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008364 psa_pake_abort(operation);
8365 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008366}
8367
8368psa_status_t psa_pake_get_implicit_key(
8369 psa_pake_operation_t *operation,
8370 psa_key_derivation_operation_t *output)
8371{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008372 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008373 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008374 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008375 size_t shared_key_len = 0;
8376
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008377 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008378 status = PSA_ERROR_BAD_STATE;
8379 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008380 }
8381
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008382#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008383 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008384 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008385 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008386 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008387 status = PSA_ERROR_BAD_STATE;
8388 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008389 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008390 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008391#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008392 {
8393 status = PSA_ERROR_NOT_SUPPORTED;
8394 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008395 }
8396
Przemek Stekiel0c781802022-11-29 14:53:13 +01008397 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8398 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008399 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008400 &shared_key_len);
8401
8402 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008403 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008404 }
8405
8406 status = psa_key_derivation_input_bytes(output,
8407 PSA_KEY_DERIVATION_INPUT_SECRET,
8408 shared_key,
8409 shared_key_len);
8410
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008411 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008412exit:
8413 abort_status = psa_pake_abort(operation);
8414 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008415}
8416
8417psa_status_t psa_pake_abort(
8418 psa_pake_operation_t *operation)
8419{
Przemek Stekield69dca92023-01-31 19:59:20 +01008420 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008421
Przemek Stekield69dca92023-01-31 19:59:20 +01008422 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008423 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008424 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008425
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008426 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8427 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008428 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008429 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008430 }
8431 if (operation->data.inputs.user != NULL) {
8432 mbedtls_free(operation->data.inputs.user);
8433 }
8434 if (operation->data.inputs.peer != NULL) {
8435 mbedtls_free(operation->data.inputs.peer);
8436 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008437 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008438 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008439
Przemek Stekield69dca92023-01-31 19:59:20 +01008440 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008441}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008442#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008443
Gilles Peskinee59236f2018-01-27 23:32:46 +01008444#endif /* MBEDTLS_PSA_CRYPTO_C */