blob: 88842678f52330a4ae3b0bc6c85d97c30d35f5f2 [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 Elliott47cee8e2024-03-08 21:38:02 +000096typedef enum {
97 PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS = 0,
98 PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS,
99 PSA_CRYPTO_SUBSYSTEM_RNG,
100 PSA_CRYPTO_SUBSYSTEM_TRANSACTION,
101} mbedtls_psa_crypto_subsystem;
102
103#define PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED 0x01
104#define PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED 0x02
105#define PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED 0x04
106
107#define PSA_CRYPTO_SUBSYSTEM_ALL_INITIALISED ( \
108 PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED | \
109 PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED | \
110 PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED)
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +0100113 uint8_t initialized;
114 uint8_t rng_state;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100115 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100116} psa_global_data_t;
117
118static psa_global_data_t global_data;
119
Paul Elliott600472b2024-02-23 17:26:58 +0000120static uint8_t psa_get_initialized(void)
121{
122 uint8_t initialized;
123
124#if defined(MBEDTLS_THREADING_C)
Paul Elliott47cee8e2024-03-08 21:38:02 +0000125 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
126#endif /* defined(MBEDTLS_THREADING_C) */
127
128 initialized = global_data.rng_state == RNG_SEEDED;
129
130#if defined(MBEDTLS_THREADING_C)
131 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
132#endif /* defined(MBEDTLS_THREADING_C) */
133
134#if defined(MBEDTLS_THREADING_C)
Paul Elliott600472b2024-02-23 17:26:58 +0000135 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
136#endif /* defined(MBEDTLS_THREADING_C) */
137
Paul Elliott47cee8e2024-03-08 21:38:02 +0000138 initialized =
139 (initialized && (global_data.initialized == PSA_CRYPTO_SUBSYSTEM_ALL_INITIALISED));
Paul Elliott600472b2024-02-23 17:26:58 +0000140
141#if defined(MBEDTLS_THREADING_C)
142 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
143#endif /* defined(MBEDTLS_THREADING_C) */
144
145 return initialized;
146}
147
Paul Elliott35816522024-02-23 17:47:42 +0000148static uint8_t psa_get_drivers_initialized(void)
149{
150 uint8_t initialized;
151
152#if defined(MBEDTLS_THREADING_C)
153 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
154#endif /* defined(MBEDTLS_THREADING_C) */
155
Paul Elliott47cee8e2024-03-08 21:38:02 +0000156 initialized = (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED) != 0;
Paul Elliott35816522024-02-23 17:47:42 +0000157
158#if defined(MBEDTLS_THREADING_C)
159 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
160#endif /* defined(MBEDTLS_THREADING_C) */
161
162 return initialized;
163}
164
itayzafrir0adf0fc2018-09-06 16:24:41 +0300165#define GUARD_MODULE_INITIALIZED \
Paul Elliott600472b2024-02-23 17:26:58 +0000166 if (psa_get_initialized() == 0) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300168
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100169int psa_can_do_hash(psa_algorithm_t hash_alg)
170{
171 (void) hash_alg;
Paul Elliott35816522024-02-23 17:47:42 +0000172 return psa_get_drivers_initialized();
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100173}
Valerio Settic6f004f2023-12-12 11:27:36 +0100174
Valerio Setti1fff4f22023-12-28 14:19:34 +0100175int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
Valerio Settic6f004f2023-12-12 11:27:36 +0100176{
Valerio Setti1fff4f22023-12-28 14:19:34 +0100177 (void) key_type;
Valerio Settic6f004f2023-12-12 11:27:36 +0100178 (void) cipher_alg;
Paul Elliott35816522024-02-23 17:47:42 +0000179 return psa_get_drivers_initialized();
Valerio Settic6f004f2023-12-12 11:27:36 +0100180}
181
182
Valerio Settia55f0422023-07-10 15:34:41 +0200183#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200184 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200185 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200186static int psa_is_dh_key_size_valid(size_t bits)
187{
Valerio Setti504a1022024-01-17 15:19:30 +0100188 switch (bits) {
189#if defined(PSA_WANT_DH_RFC7919_2048)
190 case 2048:
191 return 1;
192#endif /* PSA_WANT_DH_RFC7919_2048 */
193#if defined(PSA_WANT_DH_RFC7919_3072)
194 case 3072:
195 return 1;
196#endif /* PSA_WANT_DH_RFC7919_3072 */
197#if defined(PSA_WANT_DH_RFC7919_4096)
198 case 4096:
199 return 1;
200#endif /* PSA_WANT_DH_RFC7919_4096 */
201#if defined(PSA_WANT_DH_RFC7919_6144)
202 case 6144:
203 return 1;
204#endif /* PSA_WANT_DH_RFC7919_6144 */
205#if defined(PSA_WANT_DH_RFC7919_8192)
206 case 8192:
207 return 1;
208#endif /* PSA_WANT_DH_RFC7919_8192 */
209 default:
210 return 0;
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200211 }
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200212}
Valerio Settia55f0422023-07-10 15:34:41 +0200213#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200214 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200215 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100218{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100219 /* Mbed TLS error codes can combine a high-level error code and a
220 * low-level error code. The low-level error usually reflects the
221 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 int low_level_ret = -(-ret & 0x007f);
223 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100224 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100226
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100227#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100228 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
229 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100231 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
232 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100233#endif
234
235#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200236 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
237 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
238 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
239 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
240 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200242 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200244 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100246#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200247
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100248#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000249 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100250 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100252#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100253
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100254#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100255 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100257 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100259#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100260
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100261#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200262 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100264#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200265
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100266#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200267 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200269 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100271#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200272
Dave Rodgman509b5672023-08-16 19:26:23 +0100273#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100274 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100276 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100278 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100282 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100286 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100288#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
291 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100292 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
293 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
297 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100299 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100301#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100302
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100303#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100306#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100307
Gilles Peskinee59236f2018-01-27 23:32:46 +0100308 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
309 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
310 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100312
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100313#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100314 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200316 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100318 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100320#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100321
Gilles Peskine82e57d12020-11-13 21:31:17 +0100322#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100324 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
325 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100326 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100328 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
329 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100331 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100333#endif
334
Dave Rodgmandea266f2023-08-31 11:52:43 +0100335#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100336 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100338 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100340 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100342#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100343 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100345#endif
346#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100347
Dave Rodgman509b5672023-08-16 19:26:23 +0100348#if defined(MBEDTLS_BIGNUM_C)
349#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100350 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100352#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100353 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100355 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100357 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100359 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100361 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100363 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100365 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100367#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100368
Dave Rodgman509b5672023-08-16 19:26:23 +0100369#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100370 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100372 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
373 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100375#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
376 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100377 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100379#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100380 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
381 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100383 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100385 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
386 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100388 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100390 case MBEDTLS_ERR_PK_INVALID_ALG:
391 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
392 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100394 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200396 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100398#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100399
Gilles Peskineff2d2002019-05-06 15:26:23 +0200400 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200402 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200404
Dave Rodgman509b5672023-08-16 19:26:23 +0100405#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100406 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100408 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100410 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100412 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100414 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
415 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100417 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100419 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100421 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100423#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200424
Dave Rodgman1dab4452023-09-01 09:59:51 +0100425#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300426 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300427 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300429 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300431 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300433 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
434 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300436 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100438 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100440
Dave Rodgman509b5672023-08-16 19:26:23 +0100441#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000442 case MBEDTLS_ERR_ECP_IN_PROGRESS:
443 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100444#endif
445#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000446
Janos Follatha13b9052019-11-22 12:48:59 +0000447 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300449
Gilles Peskinee59236f2018-01-27 23:32:46 +0100450 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100452 }
453}
454
Paul Elliottdc42ca82023-02-24 18:11:59 +0000455/**
456 * \brief For output buffers which contain "tags"
457 * (outputs that may be checked for validity like
458 * hashes, MACs and signatures), fill the unused
459 * part of the output buffer (the whole buffer on
460 * error, the trailing part on success) with
461 * something that isn't a valid tag (barring an
462 * attack on the tag and deliberately-crafted
463 * input), in case the caller doesn't check the
464 * return status properly.
465 *
466 * \param output_buffer Pointer to buffer to wipe. May not be NULL
467 * unless \p output_buffer_size is zero.
468 * \param status Status of function called to generate
469 * output_buffer originally
470 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
471 * could be NULL.
472 * \param output_buffer_length Length of data written to output_buffer, must be
473 * less than \p output_buffer_size
474 */
475static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000476 size_t output_buffer_size, size_t output_buffer_length)
477{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000478 size_t offset = 0;
479
480 if (output_buffer_size == 0) {
481 /* If output_buffer_size is 0 then we have nothing to do. We must not
482 call memset because output_buffer may be NULL in this case */
483 return;
484 }
485
486 if (status == PSA_SUCCESS) {
487 offset = output_buffer_length;
488 }
489
490 memset(output_buffer + offset, '!', output_buffer_size - offset);
491}
492
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
495 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496{
497 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200501 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200502 case PSA_KEY_TYPE_PASSWORD:
503 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200504 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100505#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200506 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 if (bits != 128 && bits != 192 && bits != 256) {
508 return PSA_ERROR_INVALID_ARGUMENT;
509 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200510 break;
511#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200512#if defined(PSA_WANT_KEY_TYPE_ARIA)
513 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (bits != 128 && bits != 192 && bits != 256) {
515 return PSA_ERROR_INVALID_ARGUMENT;
516 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200517 break;
518#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100519#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200520 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (bits != 128 && bits != 192 && bits != 256) {
522 return PSA_ERROR_INVALID_ARGUMENT;
523 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200524 break;
525#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100526#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200527 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (bits != 64 && bits != 128 && bits != 192) {
529 return PSA_ERROR_INVALID_ARGUMENT;
530 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200531 break;
532#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100533#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200534 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if (bits != 256) {
536 return PSA_ERROR_INVALID_ARGUMENT;
537 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200538 break;
539#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200540 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200542 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (bits % 8 != 0) {
544 return PSA_ERROR_INVALID_ARGUMENT;
545 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200548}
549
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100550/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100551 *
Steven Cooremancd640932021-03-08 12:00:27 +0100552 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
553 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100554 *
555 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100556 * \param[in] key_type The key type of the key to be used with the
557 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100558 *
559 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100560 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100561 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100562 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100563 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100564MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100565 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100567{
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if (PSA_ALG_IS_HMAC(algorithm)) {
569 if (key_type == PSA_KEY_TYPE_HMAC) {
570 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100571 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100572 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
575 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
576 * key. */
577 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
578 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
579 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
580 * the block length (larger than 1) for block ciphers. */
581 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
582 return PSA_SUCCESS;
583 }
584 }
585 }
586
587 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100588}
589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
591 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200592{
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (slot->key.data != NULL) {
594 return PSA_ERROR_ALREADY_EXISTS;
595 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 slot->key.data = mbedtls_calloc(1, buffer_length);
598 if (slot->key.data == NULL) {
599 return PSA_ERROR_INSUFFICIENT_MEMORY;
600 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200601
Ronald Cronea0f8a62020-11-25 17:52:23 +0100602 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200604}
605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
607 const uint8_t *data,
608 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200609{
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 psa_status_t status = psa_allocate_buffer_to_slot(slot,
611 data_length);
612 if (status != PSA_SUCCESS) {
613 return status;
614 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 memcpy(slot->key.data, data, data_length);
617 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200618}
619
Ronald Crona0fe59f2020-11-29 11:14:53 +0100620psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100621 const psa_key_attributes_t *attributes,
622 const uint8_t *data, size_t data_length,
623 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100625{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100626 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100627 psa_key_type_t type = attributes->type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100628
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200629 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (data_length == 0) {
631 return PSA_ERROR_NOT_SUPPORTED;
632 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (key_type_is_raw_bytes(type)) {
635 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200636
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100637 status = psa_validate_unstructured_key_bit_size(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 *bits);
639 if (status != PSA_SUCCESS) {
640 return status;
641 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200642
Ronald Crondd04d422020-11-28 15:14:42 +0100643 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100645 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 return PSA_SUCCESS;
649 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200650#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200651 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100652 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200653 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Valerio Setti504a1022024-01-17 15:19:30 +0100654 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100655 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200656 return mbedtls_psa_ffdh_import_key(attributes,
657 data, data_length,
658 key_buffer, key_buffer_size,
659 key_buffer_length,
660 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100661 }
Valerio Settia55f0422023-07-10 15:34:41 +0200662#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200663 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200664#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
666 if (PSA_KEY_TYPE_IS_ECC(type)) {
667 return mbedtls_psa_ecp_import_key(attributes,
668 data, data_length,
669 key_buffer, key_buffer_size,
670 key_buffer_length,
671 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100672 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200673#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800674 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200675#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
676 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
678 if (PSA_KEY_TYPE_IS_RSA(type)) {
679 return mbedtls_psa_rsa_import_key(attributes,
680 data, data_length,
681 key_buffer, key_buffer_size,
682 key_buffer_length,
683 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200684 }
Valerio Settife478902023-07-25 12:27:19 +0200685#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
686 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800687 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100688 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100691}
692
Gilles Peskinef603c712019-01-19 13:40:11 +0100693/** Calculate the intersection of two algorithm usage policies.
694 *
695 * Return 0 (which allows no operation) on incompatibility.
696 */
697static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100698 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100699 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100701{
Gilles Peskine549ea862019-05-22 11:45:59 +0200702 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (alg1 == alg2) {
704 return alg1;
705 }
Steven Cooreman03488022021-02-18 12:07:20 +0100706 /* If the policies are from the same hash-and-sign family, check
707 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
709 PSA_ALG_IS_SIGN_HASH(alg2) &&
710 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
711 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
712 return alg2;
713 }
714 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
715 return alg1;
716 }
Steven Cooreman03488022021-02-18 12:07:20 +0100717 }
718 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100719 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100720 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
722 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
723 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
724 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
725 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100726 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100727
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100728 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
730 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
731 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
732 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100733 }
734 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
736 (alg1_len <= alg2_len)) {
737 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100738 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
740 (alg2_len <= alg1_len)) {
741 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100742 }
743 }
744 /* If the policies are from the same MAC family, check whether one
745 * of them is a minimum-MAC-length policy. Calculate the most
746 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
748 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
749 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100750 /* Validate the combination of key type and algorithm. Since the base
751 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
753 return 0;
754 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100755
Steven Cooreman31a876d2021-03-03 20:47:40 +0100756 /* Get the (exact or at-least) output lengths for both sides of the
757 * requested intersection. None of the currently supported algorithms
758 * have an output length dependent on the actual key size, so setting it
759 * to a bogus value of 0 is currently OK.
760 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100761 * Note that for at-least-this-length wildcard algorithms, the output
762 * length is set to the shortest allowed length, which allows us to
763 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
765 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100766 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100767
Steven Cooremana1d83222021-02-25 10:20:29 +0100768 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
770 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
771 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100772 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100773
774 /* If only one is an at-least-this-length policy, the intersection would
775 * be the other (fixed-length) policy as long as said fixed length is
776 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
778 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100779 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
781 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100782 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100783
Steven Cooremancd640932021-03-08 12:00:27 +0100784 /* If none of them are wildcards, check whether they define the same tag
785 * length. This is still possible here when one is default-length and
786 * the other specific-length. Ensure to always return the
787 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (alg1_len == alg2_len) {
789 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
790 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100791 }
792 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100794}
795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796static int psa_key_algorithm_permits(psa_key_type_t key_type,
797 psa_algorithm_t policy_alg,
798 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200799{
Gilles Peskine549ea862019-05-22 11:45:59 +0200800 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if (requested_alg == policy_alg) {
802 return 1;
803 }
Steven Cooreman03488022021-02-18 12:07:20 +0100804 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
805 * and requested_alg is the same hash-and-sign family with any hash,
806 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
808 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
809 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
810 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100811 }
812 /* If policy_alg is a wildcard AEAD algorithm of the same base as
813 * the requested algorithm, check the requested tag length to be
814 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (PSA_ALG_IS_AEAD(policy_alg) &&
816 PSA_ALG_IS_AEAD(requested_alg) &&
817 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
818 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
819 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
820 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
821 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100822 }
Steven Cooremancd640932021-03-08 12:00:27 +0100823 /* If policy_alg is a MAC algorithm of the same base as the requested
824 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (PSA_ALG_IS_MAC(policy_alg) &&
826 PSA_ALG_IS_MAC(requested_alg) &&
827 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
828 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100829 /* Validate the combination of key type and algorithm. Since the policy
830 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
832 return 0;
833 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100834
Steven Cooreman31a876d2021-03-03 20:47:40 +0100835 /* Get both the requested output length for the algorithm which is to be
836 * verified, and the default output length for the base algorithm.
837 * Note that none of the currently supported algorithms have an output
838 * length dependent on actual key size, so setting it to a bogus value
839 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100840 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100842 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 key_type, 0,
844 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100845
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100846 /* If the policy is default-length, only allow an algorithm with
847 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
849 return requested_output_length == default_output_length;
850 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100851
852 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100853 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
855 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
856 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100857 }
858
Steven Cooremancd640932021-03-08 12:00:27 +0100859 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
860 * check for the requested MAC length to be equal to or longer than the
861 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
863 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
864 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100865 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200866 }
Steven Cooremance48e852020-10-05 16:02:45 +0200867 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200868 * a key derivation with that key agreement should also be allowed. This
869 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
871 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
872 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
873 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200874 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100875 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200877}
878
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100879/** Test whether a policy permits an algorithm.
880 *
881 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100882 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100883 * \note This function requires providing the key type for which the policy is
884 * being validated, since some algorithm policy definitions (e.g. MAC)
885 * have different properties depending on what kind of cipher it is
886 * combined with.
887 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100888 * \retval PSA_SUCCESS When \p alg is a specific algorithm
889 * allowed by the \p policy.
890 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
891 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
892 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100893 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
895 psa_key_type_t key_type,
896 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100897{
Steven Cooremand788fab2021-02-25 11:29:17 +0100898 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if (alg == 0) {
900 return PSA_ERROR_INVALID_ARGUMENT;
901 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100902
Steven Cooreman7e39f052021-02-23 14:18:32 +0100903 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if (PSA_ALG_IS_WILDCARD(alg)) {
905 return PSA_ERROR_INVALID_ARGUMENT;
906 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
909 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
910 return PSA_SUCCESS;
911 } else {
912 return PSA_ERROR_NOT_PERMITTED;
913 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100914}
915
Gilles Peskinef603c712019-01-19 13:40:11 +0100916/** Restrict a key policy based on a constraint.
917 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100918 * \note This function requires providing the key type for which the policy is
919 * being restricted, since some algorithm policy definitions (e.g. MAC)
920 * have different properties depending on what kind of cipher it is
921 * combined with.
922 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100923 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100924 * \param[in,out] policy The policy to restrict.
925 * \param[in] constraint The policy constraint to apply.
926 *
927 * \retval #PSA_SUCCESS
928 * \c *policy contains the intersection of the original value of
929 * \c *policy and \c *constraint.
930 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100931 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100932 * \c *policy is unchanged.
933 */
934static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100935 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100936 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100938{
939 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 psa_key_policy_algorithm_intersection(key_type, policy->alg,
941 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200942 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
944 constraint->alg2);
945 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
946 return PSA_ERROR_INVALID_ARGUMENT;
947 }
948 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
949 return PSA_ERROR_INVALID_ARGUMENT;
950 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100951 policy->usage &= constraint->usage;
952 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200953 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100955}
956
Przemek Stekiel061f6942022-11-30 10:51:35 +0100957/** Get the description of a key given its identifier and policy constraints
958 * and lock it.
959 *
960 * The key must have allow all the usage flags set in \p usage. If \p alg is
961 * nonzero, the key must allow operations with this algorithm. If \p alg is
962 * zero, the algorithm is not checked.
963 *
964 * In case of a persistent key, the function loads the description of the key
965 * into a key slot if not already done.
966 *
Ryan Everett098c6652024-01-03 13:03:36 +0000967 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +0000968 * It is the responsibility of the caller to then unregister
969 * once they have finished reading the contents of the slot.
970 * The caller unregisters by calling psa_unregister_read() or
971 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
972 * if and only if the caller already holds the global key slot mutex
973 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
974 * the unregister with mutex lock and unlock operations.
Przemek Stekiel061f6942022-11-30 10:51:35 +0100975 */
976static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100977 mbedtls_svc_key_id_t key,
978 psa_key_slot_t **p_slot,
979 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100981{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200982 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100983 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 status = psa_get_and_lock_key_slot(key, p_slot);
986 if (status != PSA_SUCCESS) {
987 return status;
988 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200989 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100990
991 /* Enforce that usage policy for the key slot contains all the flags
992 * required by the usage parameter. There is one exception: public
993 * keys can always be exported, so we treat public key objects as
994 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100996 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001000 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001001 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001002 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001003
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001004 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 if (alg != 0) {
1006 status = psa_key_policy_permits(&slot->attr.policy,
1007 slot->attr.type,
1008 alg);
1009 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001010 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001012 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001015
1016error:
1017 *p_slot = NULL;
Ryan Everettfb792ca2024-01-31 13:40:05 +00001018 psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001021}
Darryl Green940d72c2018-07-13 13:18:51 +01001022
Ronald Cron5c522922020-11-14 16:35:34 +01001023/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001024 *
1025 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001026 * available, as opposed to a key in a secure element and/or to be used
1027 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001028 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001029 * This is a temporary function that may be used instead of
1030 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1031 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001032 *
Ryan Everett098c6652024-01-03 13:03:36 +00001033 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +00001034 * It is the responsibility of the caller to then unregister
1035 * once they have finished reading the contents of the slot.
1036 * The caller unregisters by calling psa_unregister_read() or
1037 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
1038 * if and only if the caller already holds the global key slot mutex
1039 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
1040 * psa_unregister_read() with mutex lock and unlock operations.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001041 */
Ronald Cron5c522922020-11-14 16:35:34 +01001042static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1043 mbedtls_svc_key_id_t key,
1044 psa_key_slot_t **p_slot,
1045 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001047{
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1049 usage, alg);
1050 if (status != PSA_SUCCESS) {
1051 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001052 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everettfb792ca2024-01-31 13:40:05 +00001055 psa_unregister_read_under_mutex(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 *p_slot = NULL;
1057 return PSA_ERROR_NOT_SUPPORTED;
1058 }
1059
1060 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001061}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001064{
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001066 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001068
Ronald Cronea0f8a62020-11-25 17:52:23 +01001069 slot->key.data = NULL;
1070 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001073}
1074
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001075/** Completely wipe a slot in memory, including its policy.
1076 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001077psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001078{
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 /*
1082 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +00001083 * do our best to report an unexpected amount of registered readers or
1084 * an unexpected state.
1085 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1086 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1088 * function is called as part of the execution of a test suite, the
1089 * execution of the test suite is stopped in error if the assertion fails.
1090 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001091 switch (slot->state) {
1092 case PSA_SLOT_FULL:
1093 /* In this state psa_wipe_key_slot() must only be called if the
1094 * caller is the last reader. */
1095 case PSA_SLOT_PENDING_DELETION:
1096 /* In this state psa_wipe_key_slot() must only be called if the
1097 * caller is the last reader. */
1098 if (slot->registered_readers != 1) {
1099 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1);
1100 status = PSA_ERROR_CORRUPTION_DETECTED;
1101 }
1102 break;
1103 case PSA_SLOT_FILLING:
1104 /* In this state registered_readers must be 0. */
1105 if (slot->registered_readers != 0) {
1106 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0);
1107 status = PSA_ERROR_CORRUPTION_DETECTED;
1108 }
1109 break;
1110 case PSA_SLOT_EMPTY:
1111 /* The slot is already empty, it cannot be wiped. */
1112 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1113 status = PSA_ERROR_CORRUPTION_DETECTED;
1114 break;
1115 default:
1116 /* The slot's state is invalid. */
1117 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001118 }
1119
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001120 /* Multipart operations may still be using the key. This is safe
1121 * because all multipart operation objects are independent from
1122 * the key slot: if they need to access the key after the setup
1123 * phase, they have a copy of the key. Note that this means that
1124 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001125 /* At this point, key material and other type-specific content has
1126 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001127 * zeroize because the metadata is not particularly sensitive.
1128 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 memset(slot, 0, sizeof(*slot));
1130 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001131}
1132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001134{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001135 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001136 psa_status_t status; /* status of the last operation */
1137 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001138#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1139 psa_se_drv_table_entry_t *driver;
1140#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if (mbedtls_svc_key_id_is_null(key)) {
1143 return PSA_SUCCESS;
1144 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001145
Ronald Cronf2911112020-10-29 17:51:10 +01001146 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001147 * Get the description of the key in a key slot, and register to read it.
1148 * In the case of a persistent key, this will load the key description
1149 * from persistent memory if not done yet.
1150 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001151 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001152 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 status = psa_get_and_lock_key_slot(key, &slot);
1154 if (status != PSA_SUCCESS) {
1155 return status;
1156 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001157
Ryan Everettc053d962024-01-25 17:56:32 +00001158#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001159 /* We cannot unlock between setting the state to PENDING_DELETION
1160 * and destroying the key in storage, as otherwise another thread
1161 * could load the key into a new slot and the key will not be
1162 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001163 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1164 &mbedtls_threading_key_slot_mutex));
Ryan Everettd868b742024-03-08 18:35:09 +00001165
1166 if (slot->state == PSA_SLOT_PENDING_DELETION) {
1167 /* Another thread has destroyed the key between us locking the slot
1168 * and us gaining the mutex. Unregister from the slot,
1169 * and report that the key does not exist. */
1170 status = psa_unregister_read(slot);
1171
1172 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1173 &mbedtls_threading_key_slot_mutex));
1174 return (status == PSA_SUCCESS) ? PSA_ERROR_INVALID_HANDLE : status;
1175 }
Ryan Everettc053d962024-01-25 17:56:32 +00001176#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001177 /* Set the key slot containing the key description's state to
1178 * PENDING_DELETION. This stops new operations from registering
1179 * to read the slot. Current readers can safely continue to access
1180 * the key within the slot; the last registered reader will
1181 * automatically wipe the slot when they call psa_unregister_read().
1182 * If the key is persistent, we can now delete the copy of the key
1183 * from memory. If the key is opaque, we require the driver to
1184 * deal with the deletion. */
Ryan Everettd868b742024-03-08 18:35:09 +00001185 overall_status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1186 PSA_SLOT_PENDING_DELETION);
Ryan Everettc053d962024-01-25 17:56:32 +00001187
Ryan Everettd868b742024-03-08 18:35:09 +00001188 if (overall_status != PSA_SUCCESS) {
Ryan Everettc053d962024-01-25 17:56:32 +00001189 goto exit;
1190 }
Ronald Cronf2911112020-10-29 17:51:10 +01001191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001193 /* Refuse the destruction of a read-only key (which may or may not work
1194 * if we attempt it, depending on whether the key is merely read-only
1195 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001196 * Just do the best we can, which is to wipe the copy in memory
1197 * (done in this function's cleanup code). */
1198 overall_status = PSA_ERROR_NOT_PERMITTED;
1199 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001200 }
1201
Gilles Peskine354f7672019-07-12 23:46:38 +02001202#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1204 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001205 /* For a key in a secure element, we need to do three things:
1206 * remove the key file in internal storage, destroy the
1207 * key inside the secure element, and update the driver's
1208 * persistent data. Start a transaction that will encompass these
1209 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001211 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001213 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 status = psa_crypto_save_transaction();
1215 if (status != PSA_SUCCESS) {
1216 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001217 /* We should still try to destroy the key in the secure
1218 * element and the key metadata in storage. This is especially
1219 * important if the error is that the storage is full.
1220 * But how to do it exactly without risking an inconsistent
1221 * state after a reset?
1222 * https://github.com/ARMmbed/mbed-crypto/issues/215
1223 */
1224 overall_status = status;
1225 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001226 }
1227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 status = psa_destroy_se_key(driver,
1229 psa_key_slot_get_slot_number(slot));
1230 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001231 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001233 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001234#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1235
Darryl Greend49a4992018-06-18 17:27:26 +01001236#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001238 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001239 * The slot will still hold a copy of the key until the last reader
1240 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 status = psa_destroy_persistent_key(slot->attr.id);
1242 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001243 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 }
Darryl Greend49a4992018-06-18 17:27:26 +01001245 }
1246#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001247
Gilles Peskinefc762652019-07-22 19:30:34 +02001248#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 if (driver != NULL) {
1250 status = psa_save_se_persistent_data(driver);
1251 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001252 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 }
1254 status = psa_crypto_stop_transaction();
1255 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001256 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001258 }
1259#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1260
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001261exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001262 /* Unregister from reading the slot. If we are the last active reader
1263 * then this will wipe the slot. */
1264 status = psa_unregister_read(slot);
1265 /* Prioritize CORRUPTION_DETECTED from unregistering over
1266 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001268 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 }
Ryan Everettc053d962024-01-25 17:56:32 +00001270
1271#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001272 /* Don't overwrite existing errors if the unlock fails. */
1273 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001274 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1275 &mbedtls_threading_key_slot_mutex));
1276#endif
1277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001279}
1280
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001281/** Retrieve all the publicly-accessible attributes of a key.
1282 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1284 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001286 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001287 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001288
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1292 if (status != PSA_SUCCESS) {
1293 return status;
1294 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001295
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001296 *attributes = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001297
1298#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1300 psa_set_key_slot_number(attributes,
1301 psa_key_slot_get_slot_number(slot));
1302 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001303#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001304
Gilles Peskine97c0b2f2024-02-16 00:49:46 +01001305 return psa_unregister_read_under_mutex(slot);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001306}
1307
Gilles Peskinec8000c02019-08-02 20:15:51 +02001308#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1309psa_status_t psa_get_key_slot_number(
1310 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001312{
Gilles Peskine972539c2024-02-28 01:49:45 +01001313 if (attributes->has_slot_number) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001314 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 return PSA_SUCCESS;
1316 } else {
1317 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001318 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001319}
1320#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1323 size_t key_buffer_size,
1324 uint8_t *data,
1325 size_t data_size,
1326 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001327{
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (key_buffer_size > data_size) {
1329 return PSA_ERROR_BUFFER_TOO_SMALL;
1330 }
1331 memcpy(data, key_buffer, key_buffer_size);
1332 memset(data + key_buffer_size, 0,
1333 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001334 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001336}
1337
Ronald Cron7285cda2020-11-26 14:46:37 +01001338psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001339 const psa_key_attributes_t *attributes,
1340 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001342{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001343 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 if (key_type_is_raw_bytes(type) ||
1346 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001347 PSA_KEY_TYPE_IS_ECC(type) ||
1348 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 return psa_export_key_buffer_internal(
1350 key_buffer, key_buffer_size,
1351 data, data_size, data_length);
1352 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001353 /* This shouldn't happen in the reference implementation, but
1354 it is valid for a special-purpose implementation to omit
1355 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001357 }
1358}
1359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1361 uint8_t *data,
1362 size_t data_size,
1363 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001364{
1365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1366 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1367 psa_key_slot_t *slot;
1368
Ronald Crone907e552021-01-18 13:22:38 +01001369 /* Reject a zero-length output buffer now, since this can never be a
1370 * valid key representation. This way we know that data must be a valid
1371 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 if (data_size == 0) {
1373 return PSA_ERROR_BUFFER_TOO_SMALL;
1374 }
Ronald Crone907e552021-01-18 13:22:38 +01001375
Ronald Cron9486f9d2020-11-26 13:36:23 +01001376 /* Set the key to empty now, so that even when there are errors, we always
1377 * set data_length to a value between 0 and data_size. On error, setting
1378 * the key to empty is a good choice because an empty key representation is
1379 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001380 *data_length = 0;
1381
Ronald Cron9486f9d2020-11-26 13:36:23 +01001382 /* Export requires the EXPORT flag. There is an exception for public keys,
1383 * which don't require any flag, but
1384 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1385 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1387 PSA_KEY_USAGE_EXPORT, 0);
1388 if (status != PSA_SUCCESS) {
1389 return status;
1390 }
mohammad160306e79202018-03-28 13:17:44 +03001391
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001392 status = psa_driver_wrapper_export_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 slot->key.data, slot->key.bytes,
1394 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001395
Ryan Everetta103ec92024-01-31 13:59:57 +00001396 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001397
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001399}
1400
Ronald Cron7285cda2020-11-26 14:46:37 +01001401psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001402 const psa_key_attributes_t *attributes,
1403 const uint8_t *key_buffer,
1404 size_t key_buffer_size,
1405 uint8_t *data,
1406 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001408{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001409 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001410
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001411 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001412 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1413 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001414 /* Exporting public -> public */
1415 return psa_export_key_buffer_internal(
1416 key_buffer, key_buffer_size,
1417 data, data_size, data_length);
1418 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001419#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001420 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1421 return mbedtls_psa_rsa_export_public_key(attributes,
1422 key_buffer,
1423 key_buffer_size,
1424 data,
1425 data_size,
1426 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001427#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001428 /* We don't know how to convert a private RSA key to public. */
1429 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001430#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001431 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001432 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001433#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001434 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1435 return mbedtls_psa_ecp_export_public_key(attributes,
1436 key_buffer,
1437 key_buffer_size,
1438 data,
1439 data_size,
1440 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001441#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001442 /* We don't know how to convert a private ECC key to public */
1443 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001444#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001445 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001446 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001447#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001448 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001449 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001450 key_buffer,
1451 key_buffer_size,
1452 data, data_size,
1453 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001454#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001455 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001456#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001457 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001459 (void) key_buffer;
1460 (void) key_buffer_size;
1461 (void) data;
1462 (void) data_size;
1463 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001465 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001466}
Ronald Crond18b5f82020-11-25 16:46:05 +01001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1469 uint8_t *data,
1470 size_t data_size,
1471 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001472{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001474 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001475 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001476
Ronald Crone907e552021-01-18 13:22:38 +01001477 /* Reject a zero-length output buffer now, since this can never be a
1478 * valid key representation. This way we know that data must be a valid
1479 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 if (data_size == 0) {
1481 return PSA_ERROR_BUFFER_TOO_SMALL;
1482 }
Ronald Crone907e552021-01-18 13:22:38 +01001483
Darryl Greendd8fb772018-11-07 16:00:44 +00001484 /* Set the key to empty now, so that even when there are errors, we always
1485 * set data_length to a value between 0 and data_size. On error, setting
1486 * the key to empty is a good choice because an empty key representation is
1487 * unlikely to be accepted anywhere. */
1488 *data_length = 0;
1489
1490 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1492 if (status != PSA_SUCCESS) {
1493 return status;
1494 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1497 status = PSA_ERROR_INVALID_ARGUMENT;
1498 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001499 }
1500
Ronald Cron67227982020-11-26 15:16:05 +01001501 status = psa_driver_wrapper_export_public_key(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001502 &slot->attr, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001504
1505exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001506 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001509}
1510
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001511/** Validate that a key policy is internally well-formed.
1512 *
1513 * This function only rejects invalid policies. It does not validate the
1514 * consistency of the policy with respect to other attributes of the key
1515 * such as the key type.
1516 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001517static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001518{
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1520 PSA_KEY_USAGE_COPY |
1521 PSA_KEY_USAGE_ENCRYPT |
1522 PSA_KEY_USAGE_DECRYPT |
1523 PSA_KEY_USAGE_SIGN_MESSAGE |
1524 PSA_KEY_USAGE_VERIFY_MESSAGE |
1525 PSA_KEY_USAGE_SIGN_HASH |
1526 PSA_KEY_USAGE_VERIFY_HASH |
1527 PSA_KEY_USAGE_VERIFY_DERIVATION |
1528 PSA_KEY_USAGE_DERIVE)) != 0) {
1529 return PSA_ERROR_INVALID_ARGUMENT;
1530 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001533}
1534
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001535/** Validate the internal consistency of key attributes.
1536 *
1537 * This function only rejects invalid attribute values. If does not
1538 * validate the consistency of the attributes with any key data that may
1539 * be involved in the creation of the key.
1540 *
1541 * Call this function early in the key creation process.
1542 *
1543 * \param[in] attributes Key attributes for the new key.
1544 * \param[out] p_drv On any return, the driver for the key, if any.
1545 * NULL for a transparent key.
1546 *
1547 */
1548static psa_status_t psa_validate_key_attributes(
1549 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001551{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001552 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1554 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 status = psa_validate_key_location(lifetime, p_drv);
1557 if (status != PSA_SUCCESS) {
1558 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001559 }
1560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 status = psa_validate_key_persistence(lifetime);
1562 if (status != PSA_SUCCESS) {
1563 return status;
1564 }
1565
1566 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1567 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1568 return PSA_ERROR_INVALID_ARGUMENT;
1569 }
1570 } else {
1571 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1572 return PSA_ERROR_INVALID_ARGUMENT;
1573 }
1574 }
1575
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001576 status = psa_validate_key_policy(&attributes->policy);
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 if (status != PSA_SUCCESS) {
1578 return status;
1579 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001580
1581 /* Refuse to create overly large keys.
1582 * Note that this doesn't trigger on import if the attributes don't
1583 * explicitly specify a size (so psa_get_key_bits returns 0), so
1584 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1586 return PSA_ERROR_NOT_SUPPORTED;
1587 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001590}
1591
Gilles Peskine4747d192019-04-17 15:05:45 +02001592/** Prepare a key slot to receive key material.
1593 *
1594 * This function allocates a key slot and sets its metadata.
1595 *
1596 * If this function fails, call psa_fail_key_creation().
1597 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001598 * This function is intended to be used as follows:
1599 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001600 * it with the specified attributes, and in case of a volatile key assign it
1601 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001602 * -# Populate the slot with the key material.
1603 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1604 * In case of failure at any step, stop the sequence and call
1605 * psa_fail_key_creation().
1606 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001607 * On success, the key slot's state is PSA_SLOT_FILLING.
1608 * It is the responsibility of the caller to change the slot's state to
1609 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001610 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001611 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001612 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001613 * \param[out] p_slot On success, a pointer to the prepared slot.
1614 * \param[out] p_drv On any return, the driver for the key, if any.
1615 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001616 *
1617 * \retval #PSA_SUCCESS
1618 * The key slot is ready to receive key material.
1619 * \return If this function fails, the key slot is an invalid state.
1620 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001621 */
1622static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001623 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001625 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001627{
1628 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001629 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001630 psa_key_slot_t *slot;
1631
Gilles Peskinedf179142019-07-15 22:02:14 +02001632 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001633 *p_drv = NULL;
1634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 status = psa_validate_key_attributes(attributes, p_drv);
1636 if (status != PSA_SUCCESS) {
1637 return status;
1638 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001639
Ryan Everett3d8118d2024-01-30 16:58:47 +00001640#if defined(MBEDTLS_THREADING_C)
1641 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1642 &mbedtls_threading_key_slot_mutex));
1643#endif
Ryan Everettb69118e2024-01-02 15:54:32 +00001644 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001645#if defined(MBEDTLS_THREADING_C)
1646 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1647 &mbedtls_threading_key_slot_mutex));
1648#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 if (status != PSA_SUCCESS) {
1650 return status;
1651 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 slot = *p_slot;
1653
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001654 /* We're storing the declared bit-size of the key. It's up to each
1655 * creation mechanism to verify that this information is correct.
1656 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001657 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001658 * is optional (import, copy). In case of a volatile key, assign it the
1659 * volatile key identifier associated to the slot returned to contain its
1660 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001661
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001662 slot->attr = *attributes;
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001664#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1665 slot->attr.id = volatile_key_id;
1666#else
1667 slot->attr.id.key_id = volatile_key_id;
1668#endif
1669 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001670
Gilles Peskinecbaff462019-07-12 23:46:04 +02001671#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001672 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001673 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001674 * create the key file in internal storage, create the
1675 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001676 * persistent data. This is done by starting a transaction that will
1677 * encompass these three actions.
1678 * For registering a volatile key, we just need to find an appropriate
1679 * slot number inside the SE. Since the key is designated volatile, creating
1680 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001681 /* The first thing to do is to find a slot number for the new key.
1682 * We save the slot number in persistent storage as part of the
1683 * transaction data. It will be needed to recover if the power
1684 * fails during the key creation process, to clean up on the secure
1685 * element side after restarting. Obtaining a slot number from the
1686 * secure element driver updates its persistent state, but we do not yet
1687 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001688 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001690 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1692 &slot_number);
1693 if (status != PSA_SUCCESS) {
1694 return status;
1695 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001696
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001697 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001699 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001700 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001701 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 status = psa_crypto_save_transaction();
1703 if (status != PSA_SUCCESS) {
1704 (void) psa_crypto_stop_transaction();
1705 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001706 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001707 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001708
1709 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001711 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001712
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001714 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001716 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001717#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001720}
Gilles Peskine4747d192019-04-17 15:05:45 +02001721
1722/** Finalize the creation of a key once its key material has been set.
1723 *
1724 * This entails writing the key to persistent storage.
1725 *
1726 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001727 * See the documentation of psa_start_key_creation() for the intended use
1728 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001729 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001730 * If the finalization succeeds, the function sets the key slot's state to
1731 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1732 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001733 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001734 * \param[in,out] slot Pointer to the slot with key material.
1735 * \param[in] driver The secure element driver for the key,
1736 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001737 * \param[out] key On success, identifier of the key. Note that the
1738 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001739 *
1740 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001741 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001742 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1743 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1744 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1745 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1746 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1747 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001748 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001749 * \return If this function fails, the key slot is an invalid state.
1750 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001751 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001752static psa_status_t psa_finish_key_creation(
1753 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001754 psa_se_drv_table_entry_t *driver,
1755 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001756{
1757 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001758 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001759 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001760
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001761#if defined(MBEDTLS_THREADING_C)
1762 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1763 &mbedtls_threading_key_slot_mutex));
1764#endif
1765
Gilles Peskine4747d192019-04-17 15:05:45 +02001766#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001768#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001770 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001771 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001773
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001774 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1775 sizeof(data.slot_number),
1776 "Slot number size does not match psa_se_key_data_storage_t");
1777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1779 status = psa_save_persistent_key(&slot->attr,
1780 (uint8_t *) &data,
1781 sizeof(data));
1782 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001783#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1784 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001785 /* Key material is saved in export representation in the slot, so
1786 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 status = psa_save_persistent_key(&slot->attr,
1788 slot->key.data,
1789 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001790 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001791 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001792#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1793
Gilles Peskinecbaff462019-07-12 23:46:04 +02001794#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001795 /* Finish the transaction for a key creation. This does not
1796 * happen when registering an existing key. Detect this case
1797 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001798 * creation of a persistent key in a secure element requires a transaction,
1799 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 if (driver != NULL &&
1801 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1802 status = psa_save_se_persistent_data(driver);
1803 if (status != PSA_SUCCESS) {
1804 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001805
1806#if defined(MBEDTLS_THREADING_C)
1807 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1808 &mbedtls_threading_key_slot_mutex));
1809#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001811 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001813 }
1814#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1815
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001817 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001818 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1819 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001821 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001823 }
Ronald Cron50972942020-11-14 11:28:25 +01001824
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001825#if defined(MBEDTLS_THREADING_C)
1826 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1827 &mbedtls_threading_key_slot_mutex));
1828#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001830}
1831
1832/** Abort the creation of a key.
1833 *
1834 * You may call this function after calling psa_start_key_creation(),
1835 * or after psa_finish_key_creation() fails. In other circumstances, this
1836 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001837 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001838 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001839 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001840 * \param[in,out] slot Pointer to the slot with key material.
1841 * \param[in] driver The secure element driver for the key,
1842 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001843 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001844static void psa_fail_key_creation(psa_key_slot_t *slot,
1845 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001846{
Gilles Peskine011e4282019-06-26 18:34:38 +02001847 (void) driver;
1848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001850 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001852
Ryan Everettb7101442024-01-23 20:09:49 +00001853#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00001854 /* If the lock operation fails we still wipe the slot.
1855 * Operations will no longer work after a failed lock,
1856 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00001857 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
1858#endif
1859
Gilles Peskine011e4282019-06-26 18:34:38 +02001860#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001861 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001862 * element, and the failure happened later (when saving metadata
1863 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001864 * element.
1865 * https://github.com/ARMmbed/mbed-crypto/issues/217
1866 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001867
Gilles Peskined7729582019-08-05 15:55:54 +02001868 /* Abort the ongoing transaction if any (there may not be one if
1869 * the creation process failed before starting one, or if the
1870 * key creation is a registration of a key in a secure element).
1871 * Earlier functions must already have done what it takes to undo any
1872 * partial creation. All that's left is to update the transaction data
1873 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001875#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00001878
1879#if defined(MBEDTLS_THREADING_C)
1880 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
1881#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02001882}
1883
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001884/** Validate optional attributes during key creation.
1885 *
1886 * Some key attributes are optional during key creation. If they are
1887 * specified in the attributes structure, check that they are consistent
1888 * with the data in the slot.
1889 *
1890 * This function should be called near the end of key creation, after
1891 * the slot in memory is fully populated but before saving persistent data.
1892 */
1893static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001894 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001897 if (attributes->type != 0) {
1898 if (attributes->type != slot->attr.type) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 return PSA_ERROR_INVALID_ARGUMENT;
1900 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001901 }
1902
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001903 if (attributes->bits != 0) {
1904 if (attributes->bits != slot->attr.bits) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 return PSA_ERROR_INVALID_ARGUMENT;
1906 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001907 }
1908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001910}
1911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1913 const uint8_t *data,
1914 size_t data_length,
1915 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001916{
1917 psa_status_t status;
1918 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001919 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001920 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301921 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001922
Ronald Cron81709fc2020-11-14 12:10:32 +01001923 *key = MBEDTLS_SVC_KEY_ID_INIT;
1924
Gilles Peskine0f84d622019-09-12 19:03:13 +02001925 /* Reject zero-length symmetric keys (including raw data key objects).
1926 * This also rejects any key which might be encoded as an empty string,
1927 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 if (data_length == 0) {
1929 return PSA_ERROR_INVALID_ARGUMENT;
1930 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001931
Archana449608b2021-09-08 15:36:05 +05301932 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 if (data_length > SIZE_MAX / 8) {
1934 return PSA_ERROR_NOT_SUPPORTED;
1935 }
Archana6ed4bda2021-08-04 10:47:15 +05301936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1938 &slot, &driver);
1939 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001940 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001942
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001943 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301944 * storage ( thus not in the case of importing a key in a secure element
1945 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1946 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001948 if (psa_key_lifetime_is_external(attributes->lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301949 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 attributes, data, data_length, &storage_size);
1951 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301952 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 }
Archanad8a83dc2021-06-14 10:04:16 +05301954 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 status = psa_allocate_buffer_to_slot(slot, storage_size);
1956 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001957 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001959 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001960
1961 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 status = psa_driver_wrapper_import_key(attributes,
1963 data, data_length,
1964 slot->key.data,
1965 slot->key.bytes,
1966 &slot->key.bytes, &bits);
1967 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001968 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001970
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001972 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001974 status = PSA_ERROR_INVALID_ARGUMENT;
1975 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001976 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001977
Archana6ed4bda2021-08-04 10:47:15 +05301978 /* Enforce a size limit, and in particular ensure that the bit
1979 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301981 status = PSA_ERROR_NOT_SUPPORTED;
1982 goto exit;
1983 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 status = psa_validate_optional_attributes(slot, attributes);
1985 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001986 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001990exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 if (status != PSA_SUCCESS) {
1992 psa_fail_key_creation(slot, driver);
1993 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001996}
1997
Gilles Peskined7729582019-08-05 15:55:54 +02001998#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1999psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002001{
2002 psa_status_t status;
2003 psa_key_slot_t *slot = NULL;
2004 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002005 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002006
2007 /* Leaving attributes unspecified is not currently supported.
2008 * It could make sense to query the key type and size from the
2009 * secure element, but not all secure elements support this
2010 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2012 return PSA_ERROR_NOT_SUPPORTED;
2013 }
2014 if (psa_get_key_bits(attributes) == 0) {
2015 return PSA_ERROR_NOT_SUPPORTED;
2016 }
Gilles Peskined7729582019-08-05 15:55:54 +02002017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2019 &slot, &driver);
2020 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002021 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 }
Gilles Peskined7729582019-08-05 15:55:54 +02002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002025
2026exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (status != PSA_SUCCESS) {
2028 psa_fail_key_creation(slot, driver);
2029 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002030
Gilles Peskined7729582019-08-05 15:55:54 +02002031 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 psa_close_key(key);
2033 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002034}
2035#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2038 const psa_key_attributes_t *specified_attributes,
2039 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002040{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002041 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002042 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002043 psa_key_slot_t *source_slot = NULL;
2044 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002045 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002046 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302047 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002048
Ronald Cron81709fc2020-11-14 12:10:32 +01002049 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2050
Archana8a180362021-07-05 02:18:48 +05302051 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2053 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002054 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 status = psa_validate_optional_attributes(source_slot,
2058 specified_attributes);
2059 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002060 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002062
Archana9d17bf42021-09-10 06:22:44 +05302063 /* The target key type and number of bits have been validated by
2064 * psa_validate_optional_attributes() to be either equal to zero or
2065 * equal to the ones of the source key. So it is safe to inherit
2066 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302067 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002068 actual_attributes.bits = source_slot->attr.bits;
2069 actual_attributes.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302070
2071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 status = psa_restrict_key_policy(source_slot->attr.type,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002073 &actual_attributes.policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 &source_slot->attr.policy);
2075 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2080 &target_slot, &driver);
2081 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002082 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 }
2084 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2085 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302086 /*
Archana9d17bf42021-09-10 06:22:44 +05302087 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302088 * the source key would need to be exported as plaintext and re-imported
2089 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302090 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302091 * appropriate API invocations from the application, if needed.
2092 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002093 status = PSA_ERROR_NOT_SUPPORTED;
2094 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002095 }
Archana8a180362021-07-05 02:18:48 +05302096 /*
2097 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302098 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302099 * - For opaque keys this translates to an invocation of the drivers'
2100 * copy_key entry point through the dispatch layer.
2101 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002102 if (psa_key_lifetime_is_external(actual_attributes.lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2104 &storage_size);
2105 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302106 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 }
Archana374fe5b2021-09-08 15:50:28 +05302108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2110 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302111 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 }
Archana374fe5b2021-09-08 15:50:28 +05302113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 status = psa_driver_wrapper_copy_key(&actual_attributes,
2115 source_slot->key.data,
2116 source_slot->key.bytes,
2117 target_slot->key.data,
2118 target_slot->key.bytes,
2119 &target_slot->key.bytes);
2120 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 }
2123 } else {
2124 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302125 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 source_slot->key.bytes);
2127 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302128 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 }
Archana8a180362021-07-05 02:18:48 +05302130 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002132exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (status != PSA_SUCCESS) {
2134 psa_fail_key_creation(target_slot, driver);
2135 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002136
Ryan Everetta103ec92024-01-31 13:59:57 +00002137 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002140}
2141
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002142
2143
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002144/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002145/* Message digests */
2146/****************************************************************/
2147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002149{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002150 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (operation->id == 0) {
2152 return PSA_SUCCESS;
2153 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002156 operation->id = 0;
2157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002159}
2160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2162 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002163{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002164 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002165
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002166 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002168 status = PSA_ERROR_BAD_STATE;
2169 goto exit;
2170 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002173 status = PSA_ERROR_INVALID_ARGUMENT;
2174 goto exit;
2175 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002176
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002177 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2178 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002182
2183exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (status != PSA_SUCCESS) {
2185 psa_hash_abort(operation);
2186 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002187
2188 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002189}
2190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2192 const uint8_t *input,
2193 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002195 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002198 status = PSA_ERROR_BAD_STATE;
2199 goto exit;
2200 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002201
Steven Cooremanc8288352021-03-04 14:02:19 +01002202 /* Don't require hash implementations to behave correctly on a
2203 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if (input_length == 0) {
2205 return PSA_SUCCESS;
2206 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002209
2210exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 if (status != PSA_SUCCESS) {
2212 psa_hash_abort(operation);
2213 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002216}
2217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2219 uint8_t *hash,
2220 size_t hash_size,
2221 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002222{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002223 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (operation->id == 0) {
2225 return PSA_ERROR_BAD_STATE;
2226 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002227
Steven Cooreman1e582352021-02-18 17:24:37 +01002228 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 operation, hash, hash_size, hash_length);
2230 psa_hash_abort(operation);
2231 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002232}
2233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2235 const uint8_t *hash,
2236 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002237{
Ronald Cron69a63422021-10-18 09:47:58 +02002238 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002240 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 operation,
2242 actual_hash, sizeof(actual_hash),
2243 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002246 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002250 status = PSA_ERROR_INVALID_SIGNATURE;
2251 goto exit;
2252 }
2253
Dave Rodgman78701152023-08-29 14:20:18 +01002254 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002255 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002257
2258exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2260 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002261 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002265}
2266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267psa_status_t psa_hash_compute(psa_algorithm_t alg,
2268 const uint8_t *input, size_t input_length,
2269 uint8_t *hash, size_t hash_size,
2270 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002271{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002272 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (!PSA_ALG_IS_HASH(alg)) {
2274 return PSA_ERROR_INVALID_ARGUMENT;
2275 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2278 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002279}
2280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281psa_status_t psa_hash_compare(psa_algorithm_t alg,
2282 const uint8_t *input, size_t input_length,
2283 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002284{
Ronald Cron69a63422021-10-18 09:47:58 +02002285 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002286 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 if (!PSA_ALG_IS_HASH(alg)) {
2289 return PSA_ERROR_INVALID_ARGUMENT;
2290 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002291
Steven Cooreman1e582352021-02-18 17:24:37 +01002292 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 alg, input, input_length,
2294 actual_hash, sizeof(actual_hash),
2295 &actual_hash_length);
2296 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002297 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 }
2299 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002300 status = PSA_ERROR_INVALID_SIGNATURE;
2301 goto exit;
2302 }
Dave Rodgman78701152023-08-29 14:20:18 +01002303 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002304 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002306
2307exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2309 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002310}
2311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2313 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002314{
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (source_operation->id == 0 ||
2316 target_operation->id != 0) {
2317 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002318 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2321 target_operation);
2322 if (status != PSA_SUCCESS) {
2323 psa_hash_abort(target_operation);
2324 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002327}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002328
2329
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002331/* MAC */
2332/****************************************************************/
2333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002335{
Steven Cooremane6804192021-03-19 18:28:56 +01002336 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (operation->id == 0) {
2338 return PSA_SUCCESS;
2339 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002342 operation->mac_size = 0;
2343 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002344 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002347}
2348
Ronald Cron2dff3b22021-06-17 16:33:22 +02002349static psa_status_t psa_mac_finalize_alg_and_key_validation(
2350 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002351 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002353{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002354 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 psa_key_type_t key_type = psa_get_key_type(attributes);
2356 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (!PSA_ALG_IS_MAC(alg)) {
2359 return PSA_ERROR_INVALID_ARGUMENT;
2360 }
Steven Cooremane6804192021-03-19 18:28:56 +01002361
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002362 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 status = psa_mac_key_can_do(alg, key_type);
2364 if (status != PSA_SUCCESS) {
2365 return status;
2366 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002367
Steven Cooremand1a68f12021-05-10 11:13:41 +02002368 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002372 /* A very short MAC is too short for security since it can be
2373 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2374 * so we make this our minimum, even though 32 bits is still
2375 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002377 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2380 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002381 /* It's impossible to "truncate" to a larger length than the full length
2382 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002384 }
2385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002387 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2388 * that is disabled in the compile-time configuration. The result can
2389 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2390 * configuration into account. In this case, force a return of
2391 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2392 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2393 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2394 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2395 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002397 }
2398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002400}
2401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2403 mbedtls_svc_key_id_t key,
2404 psa_algorithm_t alg,
2405 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002406{
2407 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2408 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002409 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002410
2411 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002413 status = PSA_ERROR_BAD_STATE;
2414 goto exit;
2415 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002416
2417 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 key,
2419 &slot,
2420 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2421 alg);
2422 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002423 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002425
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002426 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 &operation->mac_size);
2428 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002429 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002431
2432 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002433 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 if (is_sign) {
2435 status = psa_driver_wrapper_mac_sign_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002436 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 slot->key.data,
2438 slot->key.bytes,
2439 alg);
2440 } else {
2441 status = psa_driver_wrapper_mac_verify_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002442 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 slot->key.data,
2444 slot->key.bytes,
2445 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002446 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002447
Gilles Peskinefbfac682018-07-08 20:51:54 +02002448exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 if (status != PSA_SUCCESS) {
2450 psa_mac_abort(operation);
2451 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002452
Ryan Everettfb9857f2024-02-14 12:16:41 +00002453 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002456}
2457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2459 mbedtls_svc_key_id_t key,
2460 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002461{
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002463}
2464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2466 mbedtls_svc_key_id_t key,
2467 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002468{
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002470}
2471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2473 const uint8_t *input,
2474 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002475{
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 if (operation->id == 0) {
2477 return PSA_ERROR_BAD_STATE;
2478 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002479
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002480 /* Don't require hash implementations to behave correctly on a
2481 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 if (input_length == 0) {
2483 return PSA_SUCCESS;
2484 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2487 input, input_length);
2488 if (status != PSA_SUCCESS) {
2489 psa_mac_abort(operation);
2490 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493}
2494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2496 uint8_t *mac,
2497 size_t mac_size,
2498 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002499{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002500 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2501 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002504 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002505 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002506 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002509 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002510 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002511 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002512
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002513 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2514 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002516 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002517 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002518 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002521 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002522 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002523 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 status = psa_driver_wrapper_mac_sign_finish(operation,
2526 mac, operation->mac_size,
2527 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002528
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002529exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002530 /* In case of success, set the potential excess room in the output buffer
2531 * to an invalid value, to avoid potentially leaking a longer MAC.
2532 * In case of error, set the output length and content to a safe default,
2533 * such that in case the caller misses an error check, the output would be
2534 * an unachievable MAC.
2535 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002537 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002538 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002539 }
mohammad16036df908f2018-04-02 08:34:15 -07002540
Paul Elliottdc42ca82023-02-24 18:11:59 +00002541 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002546}
2547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2549 const uint8_t *mac,
2550 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002551{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002552 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2553 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002556 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002557 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002558 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002561 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002562 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002563 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002566 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002567 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002568 }
2569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 status = psa_driver_wrapper_mac_verify_finish(operation,
2571 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002572
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002573exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002577}
2578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2580 psa_algorithm_t alg,
2581 const uint8_t *input,
2582 size_t input_length,
2583 uint8_t *mac,
2584 size_t mac_size,
2585 size_t *mac_length,
2586 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002587{
2588 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002589 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2590 psa_key_slot_t *slot;
2591 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002592
Ronald Cron51131b52021-06-17 17:17:20 +02002593 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 key,
2595 &slot,
2596 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2597 alg);
2598 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002599 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002601
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002602 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 &operation_mac_size);
2604 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002605 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002609 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002610 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002611 }
2612
2613 status = psa_driver_wrapper_mac_compute(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002614 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 slot->key.data, slot->key.bytes,
2616 alg,
2617 input, input_length,
2618 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002619
2620exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002621 /* In case of success, set the potential excess room in the output buffer
2622 * to an invalid value, to avoid potentially leaking a longer MAC.
2623 * In case of error, set the output length and content to a safe default,
2624 * such that in case the caller misses an error check, the output would be
2625 * an unachievable MAC.
2626 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002628 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002629 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002630 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002631
2632 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002633
Ryan Everetta103ec92024-01-31 13:59:57 +00002634 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002637}
2638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002640 psa_algorithm_t alg,
2641 const uint8_t *input,
2642 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 uint8_t *mac,
2644 size_t mac_size,
2645 size_t *mac_length)
2646{
2647 return psa_mac_compute_internal(key, alg,
2648 input, input_length,
2649 mac, mac_size, mac_length, 1);
2650}
2651
2652psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2653 psa_algorithm_t alg,
2654 const uint8_t *input,
2655 size_t input_length,
2656 const uint8_t *mac,
2657 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002658{
2659 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002660 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2661 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 status = psa_mac_compute_internal(key, alg,
2664 input, input_length,
2665 actual_mac, sizeof(actual_mac),
2666 &actual_mac_length, 0);
2667 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002668 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002672 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002673 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002674 }
Dave Rodgman78701152023-08-29 14:20:18 +01002675 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002676 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002677 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002678 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002679
2680exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002684}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002685
Gilles Peskinee59236f2018-01-27 23:32:46 +01002686/****************************************************************/
2687/* Asymmetric cryptography */
2688/****************************************************************/
2689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2691 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002692{
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 if (input_is_message) {
2694 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2695 return PSA_ERROR_INVALID_ARGUMENT;
2696 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2699 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2700 return PSA_ERROR_INVALID_ARGUMENT;
2701 }
2702 }
2703 } else {
2704 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2705 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002706 }
2707 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002708
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002710}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2713 int input_is_message,
2714 psa_algorithm_t alg,
2715 const uint8_t *input,
2716 size_t input_length,
2717 uint8_t *signature,
2718 size_t signature_size,
2719 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002720{
2721 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2722 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2723 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002724
2725 *signature_length = 0;
2726
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 status = psa_sign_verify_check_alg(input_is_message, alg);
2728 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002729 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002731
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002732 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002733 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002734 * buffer can in principle be empty since it doesn't actually have
2735 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 if (signature_size == 0) {
2737 return PSA_ERROR_BUFFER_TOO_SMALL;
2738 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002739
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002740 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 key, &slot,
2742 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2743 PSA_KEY_USAGE_SIGN_HASH,
2744 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002747 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002751 status = PSA_ERROR_INVALID_ARGUMENT;
2752 goto exit;
2753 }
2754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002756 status = psa_driver_wrapper_sign_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002757 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002758 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 signature, signature_size, signature_length);
2760 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002761
2762 status = psa_driver_wrapper_sign_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002763 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002764 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002766 }
2767
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002768
2769exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002770 psa_wipe_tag_output_buffer(signature, status, signature_size,
2771 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002772
Ryan Everetta103ec92024-01-31 13:59:57 +00002773 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002776}
2777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2779 int input_is_message,
2780 psa_algorithm_t alg,
2781 const uint8_t *input,
2782 size_t input_length,
2783 const uint8_t *signature,
2784 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002785{
2786 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2787 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2788 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 status = psa_sign_verify_check_alg(input_is_message, alg);
2791 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002792 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002794
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002795 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 key, &slot,
2797 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2798 PSA_KEY_USAGE_VERIFY_HASH,
2799 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002800
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 if (status != PSA_SUCCESS) {
2802 return status;
2803 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002806 status = psa_driver_wrapper_verify_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002807 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002808 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 signature, signature_length);
2810 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002811 status = psa_driver_wrapper_verify_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002812 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002813 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002815 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002816
Ryan Everetta103ec92024-01-31 13:59:57 +00002817 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002820
2821}
2822
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002823psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002824 const psa_key_attributes_t *attributes,
2825 const uint8_t *key_buffer,
2826 size_t key_buffer_size,
2827 psa_algorithm_t alg,
2828 const uint8_t *input,
2829 size_t input_length,
2830 uint8_t *signature,
2831 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002833{
2834 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002835
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002837 size_t hash_length;
2838 uint8_t hash[PSA_HASH_MAX_SIZE];
2839
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002840 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 PSA_ALG_SIGN_GET_HASH(alg),
2842 input, input_length,
2843 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002846 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002848
2849 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 attributes, key_buffer, key_buffer_size,
2851 alg, hash, hash_length,
2852 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002853 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002854
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002856}
2857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2859 psa_algorithm_t alg,
2860 const uint8_t *input,
2861 size_t input_length,
2862 uint8_t *signature,
2863 size_t signature_size,
2864 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002865{
2866 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002867 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002869}
2870
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002871psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002872 const psa_key_attributes_t *attributes,
2873 const uint8_t *key_buffer,
2874 size_t key_buffer_size,
2875 psa_algorithm_t alg,
2876 const uint8_t *input,
2877 size_t input_length,
2878 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002880{
2881 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002884 size_t hash_length;
2885 uint8_t hash[PSA_HASH_MAX_SIZE];
2886
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002887 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 PSA_ALG_SIGN_GET_HASH(alg),
2889 input, input_length,
2890 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002891
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002893 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002895
2896 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 attributes, key_buffer, key_buffer_size,
2898 alg, hash, hash_length,
2899 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002900 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002901
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002903}
2904
Gilles Peskine449bd832023-01-11 14:50:10 +01002905psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2906 psa_algorithm_t alg,
2907 const uint8_t *input,
2908 size_t input_length,
2909 const uint8_t *signature,
2910 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002911{
2912 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002913 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002915}
2916
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002917psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002918 const psa_key_attributes_t *attributes,
2919 const uint8_t *key_buffer, size_t key_buffer_size,
2920 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002922{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002923 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2925 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002926#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2928 return mbedtls_psa_rsa_sign_hash(
2929 attributes,
2930 key_buffer, key_buffer_size,
2931 alg, hash, hash_length,
2932 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002933#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2934 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 } else {
2936 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002937 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002938 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002940#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2942 return mbedtls_psa_ecdsa_sign_hash(
2943 attributes,
2944 key_buffer, key_buffer_size,
2945 alg, hash, hash_length,
2946 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002947#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2948 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 } else {
2950 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002951 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002952 }
Ronald Cron4501c982021-03-19 20:09:32 +01002953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 (void) key_buffer;
2955 (void) key_buffer_size;
2956 (void) hash;
2957 (void) hash_length;
2958 (void) signature;
2959 (void) signature_size;
2960 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002963}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2966 psa_algorithm_t alg,
2967 const uint8_t *hash,
2968 size_t hash_length,
2969 uint8_t *signature,
2970 size_t signature_size,
2971 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002972{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002973 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002974 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002976}
2977
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002978psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002979 const psa_key_attributes_t *attributes,
2980 const uint8_t *key_buffer, size_t key_buffer_size,
2981 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002983{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002984 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2986 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002987#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2989 return mbedtls_psa_rsa_verify_hash(
2990 attributes,
2991 key_buffer, key_buffer_size,
2992 alg, hash, hash_length,
2993 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002994#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2995 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 } else {
2997 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002998 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002999 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003001#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3003 return mbedtls_psa_ecdsa_verify_hash(
3004 attributes,
3005 key_buffer, key_buffer_size,
3006 alg, hash, hash_length,
3007 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003008#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3009 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 } else {
3011 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003012 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003013 }
Ronald Cron4501c982021-03-19 20:09:32 +01003014
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 (void) key_buffer;
3016 (void) key_buffer_size;
3017 (void) hash;
3018 (void) hash_length;
3019 (void) signature;
3020 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003023}
3024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3026 psa_algorithm_t alg,
3027 const uint8_t *hash,
3028 size_t hash_length,
3029 const uint8_t *signature,
3030 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003031{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003032 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003033 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003035}
3036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3038 psa_algorithm_t alg,
3039 const uint8_t *input,
3040 size_t input_length,
3041 const uint8_t *salt,
3042 size_t salt_length,
3043 uint8_t *output,
3044 size_t output_size,
3045 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003046{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003047 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003048 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003049 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003050
Darryl Green5cc689a2018-07-24 15:34:10 +01003051 (void) input;
3052 (void) input_length;
3053 (void) salt;
3054 (void) output;
3055 (void) output_size;
3056
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003057 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003058
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3060 return PSA_ERROR_INVALID_ARGUMENT;
3061 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003062
Valerio Setti5bb454a2024-01-15 10:43:16 +01003063 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3065 if (status != PSA_SUCCESS) {
3066 return status;
3067 }
3068 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3069 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003070 status = PSA_ERROR_INVALID_ARGUMENT;
3071 goto exit;
3072 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003073
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003074 status = psa_driver_wrapper_asymmetric_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003075 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003076 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003078exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003079 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003082}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3085 psa_algorithm_t alg,
3086 const uint8_t *input,
3087 size_t input_length,
3088 const uint8_t *salt,
3089 size_t salt_length,
3090 uint8_t *output,
3091 size_t output_size,
3092 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003094 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003095 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003096 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003097
Darryl Green5cc689a2018-07-24 15:34:10 +01003098 (void) input;
3099 (void) input_length;
3100 (void) salt;
3101 (void) output;
3102 (void) output_size;
3103
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003104 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003105
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3107 return PSA_ERROR_INVALID_ARGUMENT;
3108 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003109
Valerio Setti5bb454a2024-01-15 10:43:16 +01003110 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3112 if (status != PSA_SUCCESS) {
3113 return status;
3114 }
3115 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003116 status = PSA_ERROR_INVALID_ARGUMENT;
3117 goto exit;
3118 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003119
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003120 status = psa_driver_wrapper_asymmetric_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003121 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003122 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003124
3125exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003126 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003127
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003129}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003130
Paul Elliott9fe12f62022-11-30 19:16:02 +00003131/****************************************************************/
3132/* Asymmetric interruptible cryptography */
3133/****************************************************************/
3134
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003135static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3136
Paul Elliott9fe12f62022-11-30 19:16:02 +00003137void psa_interruptible_set_max_ops(uint32_t max_ops)
3138{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003139 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003140}
3141
3142uint32_t psa_interruptible_get_max_ops(void)
3143{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003144 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003145}
3146
Paul Elliott9fe12f62022-11-30 19:16:02 +00003147uint32_t psa_sign_hash_get_num_ops(
3148 const psa_sign_hash_interruptible_operation_t *operation)
3149{
Paul Elliott296ede92022-12-15 17:00:30 +00003150 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003151}
3152
3153uint32_t psa_verify_hash_get_num_ops(
3154 const psa_verify_hash_interruptible_operation_t *operation)
3155{
Paul Elliott296ede92022-12-15 17:00:30 +00003156 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003157}
3158
Paul Elliott59ad9452022-12-18 15:09:02 +00003159static psa_status_t psa_sign_hash_abort_internal(
3160 psa_sign_hash_interruptible_operation_t *operation)
3161{
3162 if (operation->id == 0) {
3163 /* The object has (apparently) been initialized but it is not (yet)
3164 * in use. It's ok to call abort on such an object, and there's
3165 * nothing to do. */
3166 return PSA_SUCCESS;
3167 }
3168
3169 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3170
3171 status = psa_driver_wrapper_sign_hash_abort(operation);
3172
3173 operation->id = 0;
3174
Paul Elliotte6145dc2023-02-07 12:51:21 +00003175 /* Do not clear either the error_occurred or num_ops elements here as they
3176 * only want to be cleared by the application calling abort, not by abort
3177 * being called at completion of an operation. */
3178
Paul Elliott59ad9452022-12-18 15:09:02 +00003179 return status;
3180}
3181
Paul Elliott9fe12f62022-11-30 19:16:02 +00003182psa_status_t psa_sign_hash_start(
3183 psa_sign_hash_interruptible_operation_t *operation,
3184 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3185 const uint8_t *hash, size_t hash_length)
3186{
3187 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3188 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3189 psa_key_slot_t *slot;
3190
Paul Elliottc9774412023-02-06 15:14:07 +00003191 /* Check that start has not been previously called, or operation has not
3192 * previously errored. */
3193 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003194 return PSA_ERROR_BAD_STATE;
3195 }
3196
Paul Elliott9fe12f62022-11-30 19:16:02 +00003197 status = psa_sign_verify_check_alg(0, alg);
3198 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003199 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003200 return status;
3201 }
3202
3203 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3204 PSA_KEY_USAGE_SIGN_HASH,
3205 alg);
3206
3207 if (status != PSA_SUCCESS) {
3208 goto exit;
3209 }
3210
3211 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3212 status = PSA_ERROR_INVALID_ARGUMENT;
3213 goto exit;
3214 }
3215
Paul Elliott296ede92022-12-15 17:00:30 +00003216 /* Ensure ops count gets reset, in case of operation re-use. */
3217 operation->num_ops = 0;
3218
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003219 status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003220 slot->key.data,
3221 slot->key.bytes, alg,
3222 hash, hash_length);
3223exit:
3224
3225 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003226 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003227 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003228 }
3229
Ryan Everettdcc03d52024-02-14 15:44:13 +00003230 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003231
Paul Elliottc9774412023-02-06 15:14:07 +00003232 if (unlock_status != PSA_SUCCESS) {
3233 operation->error_occurred = 1;
3234 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003235
Paul Elliottc9774412023-02-06 15:14:07 +00003236 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003237}
3238
3239
3240psa_status_t psa_sign_hash_complete(
3241 psa_sign_hash_interruptible_operation_t *operation,
3242 uint8_t *signature, size_t signature_size,
3243 size_t *signature_length)
3244{
3245 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3246
3247 *signature_length = 0;
3248
Paul Elliottc9774412023-02-06 15:14:07 +00003249 /* Check that start has been called first, and that operation has not
3250 * previously errored. */
3251 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003252 status = PSA_ERROR_BAD_STATE;
3253 goto exit;
3254 }
3255
Paul Elliott096abc42023-02-03 18:33:23 +00003256 /* Immediately reject a zero-length signature buffer. This guarantees that
3257 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003258 if (signature_size == 0) {
3259 status = PSA_ERROR_BUFFER_TOO_SMALL;
3260 goto exit;
3261 }
3262
3263 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3264 signature_size,
3265 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003266
Paul Elliott296ede92022-12-15 17:00:30 +00003267 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003268 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003269
Paul Elliottebe225c2023-02-10 14:32:53 +00003270exit:
3271
Paul Elliott59200a22023-02-21 15:07:40 +00003272 psa_wipe_tag_output_buffer(signature, status, signature_size,
3273 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003274
Paul Elliott53bb3122023-02-10 14:22:22 +00003275 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003276 if (status != PSA_SUCCESS) {
3277 operation->error_occurred = 1;
3278 }
3279
Paul Elliott59ad9452022-12-18 15:09:02 +00003280 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003281 }
3282
3283 return status;
3284}
3285
3286psa_status_t psa_sign_hash_abort(
3287 psa_sign_hash_interruptible_operation_t *operation)
3288{
Paul Elliott59ad9452022-12-18 15:09:02 +00003289 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3290
3291 status = psa_sign_hash_abort_internal(operation);
3292
3293 /* We clear the number of ops done here, so that it is not cleared when
3294 * the operation fails or succeeds, only on manual abort. */
3295 operation->num_ops = 0;
3296
Paul Elliottc9774412023-02-06 15:14:07 +00003297 /* Likewise, failure state. */
3298 operation->error_occurred = 0;
3299
Paul Elliott59ad9452022-12-18 15:09:02 +00003300 return status;
3301}
3302
3303static psa_status_t psa_verify_hash_abort_internal(
3304 psa_verify_hash_interruptible_operation_t *operation)
3305{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003306 if (operation->id == 0) {
3307 /* The object has (apparently) been initialized but it is not (yet)
3308 * in use. It's ok to call abort on such an object, and there's
3309 * nothing to do. */
3310 return PSA_SUCCESS;
3311 }
3312
Paul Elliott59ad9452022-12-18 15:09:02 +00003313 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3314
3315 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003316
3317 operation->id = 0;
3318
Paul Elliotte6145dc2023-02-07 12:51:21 +00003319 /* Do not clear either the error_occurred or num_ops elements here as they
3320 * only want to be cleared by the application calling abort, not by abort
3321 * being called at completion of an operation. */
3322
Paul Elliott59ad9452022-12-18 15:09:02 +00003323 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003324}
3325
3326psa_status_t psa_verify_hash_start(
3327 psa_verify_hash_interruptible_operation_t *operation,
3328 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3329 const uint8_t *hash, size_t hash_length,
3330 const uint8_t *signature, size_t signature_length)
3331{
3332 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3333 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3334 psa_key_slot_t *slot;
3335
Paul Elliottc9774412023-02-06 15:14:07 +00003336 /* Check that start has not been previously called, or operation has not
3337 * previously errored. */
3338 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003339 return PSA_ERROR_BAD_STATE;
3340 }
3341
3342 status = psa_sign_verify_check_alg(0, alg);
3343 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003344 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003345 return status;
3346 }
3347
3348 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3349 PSA_KEY_USAGE_VERIFY_HASH,
3350 alg);
3351
3352 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003353 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003354 return status;
3355 }
3356
Paul Elliott296ede92022-12-15 17:00:30 +00003357 /* Ensure ops count gets reset, in case of operation re-use. */
3358 operation->num_ops = 0;
3359
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003360 status = psa_driver_wrapper_verify_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003361 slot->key.data,
3362 slot->key.bytes,
3363 alg, hash, hash_length,
3364 signature, signature_length);
3365
3366 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003367 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003368 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003369 }
3370
Ryan Everett291267f2024-02-14 15:59:15 +00003371 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003372
Paul Elliottc9774412023-02-06 15:14:07 +00003373 if (unlock_status != PSA_SUCCESS) {
3374 operation->error_occurred = 1;
3375 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003376
Paul Elliottc9774412023-02-06 15:14:07 +00003377 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003378}
3379
3380psa_status_t psa_verify_hash_complete(
3381 psa_verify_hash_interruptible_operation_t *operation)
3382{
3383 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3384
Paul Elliottc9774412023-02-06 15:14:07 +00003385 /* Check that start has been called first, and that operation has not
3386 * previously errored. */
3387 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003388 status = PSA_ERROR_BAD_STATE;
3389 goto exit;
3390 }
3391
3392 status = psa_driver_wrapper_verify_hash_complete(operation);
3393
Paul Elliott296ede92022-12-15 17:00:30 +00003394 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003395 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003396 operation);
3397
Paul Elliottebe225c2023-02-10 14:32:53 +00003398exit:
3399
Paul Elliott9fe12f62022-11-30 19:16:02 +00003400 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003401 if (status != PSA_SUCCESS) {
3402 operation->error_occurred = 1;
3403 }
3404
Paul Elliott59ad9452022-12-18 15:09:02 +00003405 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003406 }
3407
3408 return status;
3409}
3410
3411psa_status_t psa_verify_hash_abort(
3412 psa_verify_hash_interruptible_operation_t *operation)
3413{
Paul Elliott59ad9452022-12-18 15:09:02 +00003414 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003415
Paul Elliott59ad9452022-12-18 15:09:02 +00003416 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003417
Paul Elliott59ad9452022-12-18 15:09:02 +00003418 /* We clear the number of ops done here, so that it is not cleared when
3419 * the operation fails or succeeds, only on manual abort. */
3420 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003421
Paul Elliottc9774412023-02-06 15:14:07 +00003422 /* Likewise, failure state. */
3423 operation->error_occurred = 0;
3424
Paul Elliott59ad9452022-12-18 15:09:02 +00003425 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003426}
3427
Paul Elliott588f8ed2022-12-02 18:10:26 +00003428/****************************************************************/
3429/* Asymmetric interruptible cryptography internal */
3430/* implementations */
3431/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003432
Paul Elliott588f8ed2022-12-02 18:10:26 +00003433void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3434{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003435
Paul Elliott588f8ed2022-12-02 18:10:26 +00003436#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3437 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3438 defined(MBEDTLS_ECP_RESTARTABLE)
3439
3440 /* Internal implementation uses zero to indicate infinite number max ops,
3441 * therefore avoid this value, and set to minimum possible. */
3442 if (max_ops == 0) {
3443 max_ops = 1;
3444 }
3445
Paul Elliott588f8ed2022-12-02 18:10:26 +00003446 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003447#else
3448 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003449#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3450 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3451 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3452}
3453
Paul Elliott588f8ed2022-12-02 18:10:26 +00003454uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003455 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003456{
3457#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3458 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3459 defined(MBEDTLS_ECP_RESTARTABLE)
3460
Paul Elliott93d9ca82023-02-15 18:14:21 +00003461 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003462#else
3463 (void) operation;
3464 return 0;
3465#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3466 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3467 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3468}
3469
3470uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003471 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003472{
3473 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3474 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3475 defined(MBEDTLS_ECP_RESTARTABLE)
3476
Paul Elliott93d9ca82023-02-15 18:14:21 +00003477 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003478#else
3479 (void) operation;
3480 return 0;
3481#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3482 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3483 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3484}
3485
3486psa_status_t mbedtls_psa_sign_hash_start(
3487 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3488 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3489 size_t key_buffer_size, psa_algorithm_t alg,
3490 const uint8_t *hash, size_t hash_length)
3491{
3492 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003493 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003494
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003495 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003496 return PSA_ERROR_NOT_SUPPORTED;
3497 }
3498
3499 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003500 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003501 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003502
3503#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003504 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3505 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003506
Paul Elliotta1c94092023-02-15 16:38:04 +00003507 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3508
Paul Elliott93d9ca82023-02-15 18:14:21 +00003509 /* Ensure num_ops is zero'ed in case of context re-use. */
3510 operation->num_ops = 0;
3511
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003512 status = mbedtls_psa_ecp_load_representation(attributes->type,
3513 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003514 key_buffer,
3515 key_buffer_size,
3516 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003517
Paul Elliott068fe072023-01-16 13:59:15 +00003518 if (status != PSA_SUCCESS) {
3519 return status;
3520 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003521
Paul Elliott1bc59df2023-02-05 13:41:57 +00003522 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003523 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003524
Paul Elliott068fe072023-01-16 13:59:15 +00003525 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003526 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003527 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003528
Paul Elliott0290a762023-02-09 14:30:24 +00003529 /* We only need to store the same length of hash as the private key size
3530 * here, it would be truncated by the internal implementation anyway. */
3531 required_hash_length = (hash_length < operation->coordinate_bytes ?
3532 hash_length : operation->coordinate_bytes);
3533
Paul Elliottba70ad42023-02-15 18:23:53 +00003534 if (required_hash_length > sizeof(operation->hash)) {
3535 /* Shouldn't happen, but better safe than sorry. */
3536 return PSA_ERROR_CORRUPTION_DETECTED;
3537 }
3538
Paul Elliott0290a762023-02-09 14:30:24 +00003539 memcpy(operation->hash, hash, required_hash_length);
3540 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003541
3542 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003543
3544#else
Paul Elliott068fe072023-01-16 13:59:15 +00003545 (void) operation;
3546 (void) key_buffer;
3547 (void) key_buffer_size;
3548 (void) alg;
3549 (void) hash;
3550 (void) hash_length;
3551 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003552 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003553
Paul Elliott068fe072023-01-16 13:59:15 +00003554 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003555#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3556 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3557 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003558}
3559
3560psa_status_t mbedtls_psa_sign_hash_complete(
3561 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3562 uint8_t *signature, size_t signature_size,
3563 size_t *signature_length)
3564{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003565#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3566 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3567 defined(MBEDTLS_ECP_RESTARTABLE)
3568
Paul Elliotta1c94092023-02-15 16:38:04 +00003569 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003570 mbedtls_mpi r;
3571 mbedtls_mpi s;
3572
Paul Elliott46845252023-02-03 14:59:11 +00003573 mbedtls_mpi_init(&r);
3574 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003575
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003576 /* Ensure max_ops is set to the current value (or default). */
3577 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3578
Paul Elliotta1c94092023-02-15 16:38:04 +00003579 if (signature_size < 2 * operation->coordinate_bytes) {
3580 status = PSA_ERROR_BUFFER_TOO_SMALL;
3581 goto exit;
3582 }
3583
Paul Elliott588f8ed2022-12-02 18:10:26 +00003584 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003585
Paul Elliott588f8ed2022-12-02 18:10:26 +00003586#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3587 status = mbedtls_to_psa_error(
3588 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003589 &r,
3590 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003591 &operation->ctx->d,
3592 operation->hash,
3593 operation->hash_length,
3594 operation->md_alg,
3595 mbedtls_psa_get_random,
3596 MBEDTLS_PSA_RANDOM_STATE,
3597 &operation->restart_ctx));
3598#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003599 status = PSA_ERROR_NOT_SUPPORTED;
3600 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003601#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3602 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003603 status = mbedtls_to_psa_error(
3604 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003605 &r,
3606 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003607 &operation->ctx->d,
3608 operation->hash,
3609 operation->hash_length,
3610 mbedtls_psa_get_random,
3611 MBEDTLS_PSA_RANDOM_STATE,
3612 mbedtls_psa_get_random,
3613 MBEDTLS_PSA_RANDOM_STATE,
3614 &operation->restart_ctx));
3615 }
3616
Paul Elliottf8e5b562023-02-19 18:43:45 +00003617 /* Hide the fact that the restart context only holds a delta of number of
3618 * ops done during the last operation, not an absolute value. */
3619 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3620
Paul Elliott724bd252023-02-08 12:35:08 +00003621 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003622 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003623 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003624 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003625 operation->coordinate_bytes)
3626 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003627
3628 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003629 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003630 }
3631
3632 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003633 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003634 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003635 operation->coordinate_bytes,
3636 operation->coordinate_bytes)
3637 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638
3639 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003640 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003641 }
3642
Paul Elliott1bc59df2023-02-05 13:41:57 +00003643 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003644
Paul Elliott724bd252023-02-08 12:35:08 +00003645 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646 }
Paul Elliott724bd252023-02-08 12:35:08 +00003647
3648exit:
3649
3650 mbedtls_mpi_free(&r);
3651 mbedtls_mpi_free(&s);
3652 return status;
3653
Paul Elliott588f8ed2022-12-02 18:10:26 +00003654 #else
3655
3656 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003657 (void) signature;
3658 (void) signature_size;
3659 (void) signature_length;
3660
3661 return PSA_ERROR_NOT_SUPPORTED;
3662
3663#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3664 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3665 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3666}
3667
3668psa_status_t mbedtls_psa_sign_hash_abort(
3669 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3670{
3671
3672#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3673 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3674 defined(MBEDTLS_ECP_RESTARTABLE)
3675
3676 if (operation->ctx) {
3677 mbedtls_ecdsa_free(operation->ctx);
3678 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003679 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003680 }
3681
3682 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3683
Paul Elliott93d9ca82023-02-15 18:14:21 +00003684 operation->num_ops = 0;
3685
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686 return PSA_SUCCESS;
3687
3688#else
3689
3690 (void) operation;
3691
3692 return PSA_ERROR_NOT_SUPPORTED;
3693
3694#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3695 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3696 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3697}
3698
3699psa_status_t mbedtls_psa_verify_hash_start(
3700 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3701 const psa_key_attributes_t *attributes,
3702 const uint8_t *key_buffer, size_t key_buffer_size,
3703 psa_algorithm_t alg,
3704 const uint8_t *hash, size_t hash_length,
3705 const uint8_t *signature, size_t signature_length)
3706{
3707 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003708 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003709 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003710
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003711 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003712 return PSA_ERROR_NOT_SUPPORTED;
3713 }
3714
3715 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003716 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003717 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003718
3719#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003720 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3721 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722
Paul Elliotta1c94092023-02-15 16:38:04 +00003723 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3724 mbedtls_mpi_init(&operation->r);
3725 mbedtls_mpi_init(&operation->s);
3726
Paul Elliott93d9ca82023-02-15 18:14:21 +00003727 /* Ensure num_ops is zero'ed in case of context re-use. */
3728 operation->num_ops = 0;
3729
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003730 status = mbedtls_psa_ecp_load_representation(attributes->type,
3731 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003732 key_buffer,
3733 key_buffer_size,
3734 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003735
Paul Elliott068fe072023-01-16 13:59:15 +00003736 if (status != PSA_SUCCESS) {
3737 return status;
3738 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003739
Paul Elliottc569fc22023-02-10 13:02:54 +00003740 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003741
Paul Elliott1bc59df2023-02-05 13:41:57 +00003742 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003743 return PSA_ERROR_INVALID_SIGNATURE;
3744 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003745
Paul Elliott068fe072023-01-16 13:59:15 +00003746 status = mbedtls_to_psa_error(
3747 mbedtls_mpi_read_binary(&operation->r,
3748 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003749 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003750
Paul Elliott068fe072023-01-16 13:59:15 +00003751 if (status != PSA_SUCCESS) {
3752 return status;
3753 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003754
Paul Elliott068fe072023-01-16 13:59:15 +00003755 status = mbedtls_to_psa_error(
3756 mbedtls_mpi_read_binary(&operation->s,
3757 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003758 coordinate_bytes,
3759 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003760
Paul Elliott068fe072023-01-16 13:59:15 +00003761 if (status != PSA_SUCCESS) {
3762 return status;
3763 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003764
Paul Elliott2c9843f2023-02-15 17:32:42 +00003765 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003766
Paul Elliott2c9843f2023-02-15 17:32:42 +00003767 if (status != PSA_SUCCESS) {
3768 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003769 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770
Paul Elliott0290a762023-02-09 14:30:24 +00003771 /* We only need to store the same length of hash as the private key size
3772 * here, it would be truncated by the internal implementation anyway. */
3773 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3774 coordinate_bytes);
3775
Paul Elliottba70ad42023-02-15 18:23:53 +00003776 if (required_hash_length > sizeof(operation->hash)) {
3777 /* Shouldn't happen, but better safe than sorry. */
3778 return PSA_ERROR_CORRUPTION_DETECTED;
3779 }
3780
Paul Elliott0290a762023-02-09 14:30:24 +00003781 memcpy(operation->hash, hash, required_hash_length);
3782 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003783
3784 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003785#else
Paul Elliott068fe072023-01-16 13:59:15 +00003786 (void) operation;
3787 (void) key_buffer;
3788 (void) key_buffer_size;
3789 (void) alg;
3790 (void) hash;
3791 (void) hash_length;
3792 (void) signature;
3793 (void) signature_length;
3794 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003795 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003796 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003797
Paul Elliott068fe072023-01-16 13:59:15 +00003798 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003799#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3800 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3801 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802}
3803
3804psa_status_t mbedtls_psa_verify_hash_complete(
3805 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3806{
3807
3808#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3809 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3810 defined(MBEDTLS_ECP_RESTARTABLE)
3811
Paul Elliottf8e5b562023-02-19 18:43:45 +00003812 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3813
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003814 /* Ensure max_ops is set to the current value (or default). */
3815 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3816
Paul Elliottf8e5b562023-02-19 18:43:45 +00003817 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003818 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3819 operation->hash,
3820 operation->hash_length,
3821 &operation->ctx->Q,
3822 &operation->r,
3823 &operation->s,
3824 &operation->restart_ctx));
3825
Paul Elliottf8e5b562023-02-19 18:43:45 +00003826 /* Hide the fact that the restart context only holds a delta of number of
3827 * ops done during the last operation, not an absolute value. */
3828 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3829
3830 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003831#else
3832 (void) operation;
3833
3834 return PSA_ERROR_NOT_SUPPORTED;
3835
3836#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3837 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3838 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3839}
3840
3841psa_status_t mbedtls_psa_verify_hash_abort(
3842 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3843{
3844
3845#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3846 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3847 defined(MBEDTLS_ECP_RESTARTABLE)
3848
3849 if (operation->ctx) {
3850 mbedtls_ecdsa_free(operation->ctx);
3851 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003852 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853 }
3854
3855 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3856
Paul Elliott93d9ca82023-02-15 18:14:21 +00003857 operation->num_ops = 0;
3858
Paul Elliott588f8ed2022-12-02 18:10:26 +00003859 mbedtls_mpi_free(&operation->r);
3860 mbedtls_mpi_free(&operation->s);
3861
3862 return PSA_SUCCESS;
3863
3864#else
3865 (void) operation;
3866
3867 return PSA_ERROR_NOT_SUPPORTED;
3868
3869#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3870 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3871 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3872}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003873
mohammad1603503973b2018-03-12 15:59:30 +02003874/****************************************************************/
3875/* Symmetric cryptography */
3876/****************************************************************/
3877
Gilles Peskine449bd832023-01-11 14:50:10 +01003878static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3879 mbedtls_svc_key_id_t key,
3880 psa_algorithm_t alg,
3881 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003882{
3883 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3884 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003885 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003886 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3887 PSA_KEY_USAGE_ENCRYPT :
3888 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003889
3890 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003892 status = PSA_ERROR_BAD_STATE;
3893 goto exit;
3894 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003895
Gilles Peskine449bd832023-01-11 14:50:10 +01003896 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003897 status = PSA_ERROR_INVALID_ARGUMENT;
3898 goto exit;
3899 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003900
Gilles Peskine449bd832023-01-11 14:50:10 +01003901 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3902 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003903 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003904 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003905
Ronald Cron49fafa92021-03-10 08:34:23 +01003906 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003907 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003908 * so we only set it (in the driver wrapper) after resources have been
3909 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003910 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003911 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003912 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003913 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003914 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 }
3916 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003917
3918 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003919 if (cipher_operation == MBEDTLS_ENCRYPT) {
3920 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003921 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003922 slot->key.data,
3923 slot->key.bytes,
3924 alg);
3925 } else {
3926 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003927 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003928 slot->key.data,
3929 slot->key.bytes,
3930 alg);
3931 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003932
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003933exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003934 if (status != PSA_SUCCESS) {
3935 psa_cipher_abort(operation);
3936 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003937
Ryan Everettc0053cc2024-02-14 16:27:13 +00003938 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003939
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003941}
3942
Gilles Peskine449bd832023-01-11 14:50:10 +01003943psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3944 mbedtls_svc_key_id_t key,
3945 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003946{
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003948}
3949
Gilles Peskine449bd832023-01-11 14:50:10 +01003950psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3951 mbedtls_svc_key_id_t key,
3952 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003953{
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003955}
3956
Gilles Peskine449bd832023-01-11 14:50:10 +01003957psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3958 uint8_t *iv,
3959 size_t iv_size,
3960 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003961{
Ronald Cron6d051732020-10-01 14:10:20 +02003962 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003963 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07003964 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003965
Gilles Peskine449bd832023-01-11 14:50:10 +01003966 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003967 status = PSA_ERROR_BAD_STATE;
3968 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003969 }
3970
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003972 status = PSA_ERROR_BAD_STATE;
3973 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003974 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003975
Ronald Cron2fb90522021-07-05 12:31:44 +02003976 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003978 status = PSA_ERROR_BUFFER_TOO_SMALL;
3979 goto exit;
3980 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003981
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02003983 status = PSA_ERROR_GENERIC_ERROR;
3984 goto exit;
3985 }
3986
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 status = psa_generate_random(local_iv, default_iv_length);
3988 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003989 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 }
Ronald Cron5618a392021-03-26 09:52:26 +01003991
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 status = psa_driver_wrapper_cipher_set_iv(operation,
3993 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003994
3995exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 if (status == PSA_SUCCESS) {
3997 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02003998 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003999 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004001 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004003 }
Ronald Cron6d051732020-10-01 14:10:20 +02004004
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004006}
4007
Gilles Peskine449bd832023-01-11 14:50:10 +01004008psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4009 const uint8_t *iv,
4010 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004011{
Ronald Cron6d051732020-10-01 14:10:20 +02004012 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004013
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004015 status = PSA_ERROR_BAD_STATE;
4016 goto exit;
4017 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004018
Gilles Peskine449bd832023-01-11 14:50:10 +01004019 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004020 status = PSA_ERROR_BAD_STATE;
4021 goto exit;
4022 }
Ronald Crona0d68172021-03-26 10:15:08 +01004023
Gilles Peskine449bd832023-01-11 14:50:10 +01004024 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004025 status = PSA_ERROR_INVALID_ARGUMENT;
4026 goto exit;
4027 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004028
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 status = psa_driver_wrapper_cipher_set_iv(operation,
4030 iv,
4031 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004032
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004033exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004034 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004035 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 } else {
4037 psa_cipher_abort(operation);
4038 }
4039 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004040}
4041
Gilles Peskine449bd832023-01-11 14:50:10 +01004042psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4043 const uint8_t *input,
4044 size_t input_length,
4045 uint8_t *output,
4046 size_t output_size,
4047 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004048{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004049 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004050
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004052 status = PSA_ERROR_BAD_STATE;
4053 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004054 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004055
Gilles Peskine449bd832023-01-11 14:50:10 +01004056 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004057 status = PSA_ERROR_BAD_STATE;
4058 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004059 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004060
Gilles Peskine449bd832023-01-11 14:50:10 +01004061 status = psa_driver_wrapper_cipher_update(operation,
4062 input,
4063 input_length,
4064 output,
4065 output_size,
4066 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004067
4068exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 if (status != PSA_SUCCESS) {
4070 psa_cipher_abort(operation);
4071 }
Ronald Cron6d051732020-10-01 14:10:20 +02004072
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004074}
4075
Gilles Peskine449bd832023-01-11 14:50:10 +01004076psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4077 uint8_t *output,
4078 size_t output_size,
4079 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004080{
David Saadab4ecc272019-02-14 13:48:10 +02004081 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004082
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004084 status = PSA_ERROR_BAD_STATE;
4085 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004086 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004087
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004089 status = PSA_ERROR_BAD_STATE;
4090 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004091 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004092
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 status = psa_driver_wrapper_cipher_finish(operation,
4094 output,
4095 output_size,
4096 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004097
4098exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 if (status == PSA_SUCCESS) {
4100 return psa_cipher_abort(operation);
4101 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004102 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004104
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004106 }
mohammad1603503973b2018-03-12 15:59:30 +02004107}
4108
Gilles Peskine449bd832023-01-11 14:50:10 +01004109psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004110{
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004112 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004113 * in use. It's ok to call abort on such an object, and there's
4114 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004116 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004117
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004119
Ronald Cron49fafa92021-03-10 08:34:23 +01004120 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004121 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004122 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004123
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004125}
4126
Gilles Peskine449bd832023-01-11 14:50:10 +01004127psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4128 psa_algorithm_t alg,
4129 const uint8_t *input,
4130 size_t input_length,
4131 uint8_t *output,
4132 size_t output_size,
4133 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004134{
4135 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004136 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004137 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004138 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4139 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004142 status = PSA_ERROR_INVALID_ARGUMENT;
4143 goto exit;
4144 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004145
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4147 PSA_KEY_USAGE_ENCRYPT,
4148 alg);
4149 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004150 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4154 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004155 status = PSA_ERROR_GENERIC_ERROR;
4156 goto exit;
4157 }
4158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 if (default_iv_length > 0) {
4160 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004161 status = PSA_ERROR_BUFFER_TOO_SMALL;
4162 goto exit;
4163 }
4164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 status = psa_generate_random(local_iv, default_iv_length);
4166 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004167 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004169 }
4170
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004171 status = psa_driver_wrapper_cipher_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004172 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004173 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004174 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004176
4177exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004178 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004180 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004181 }
Ronald Cron23919522021-07-08 19:00:07 +02004182
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 if (status == PSA_SUCCESS) {
4184 if (default_iv_length > 0) {
4185 memcpy(output, local_iv, default_iv_length);
4186 }
4187 *output_length += default_iv_length;
4188 } else {
4189 *output_length = 0;
4190 }
4191
4192 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004193}
4194
Gilles Peskine449bd832023-01-11 14:50:10 +01004195psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4196 psa_algorithm_t alg,
4197 const uint8_t *input,
4198 size_t input_length,
4199 uint8_t *output,
4200 size_t output_size,
4201 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004202{
4203 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004204 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004205 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004206
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004208 status = PSA_ERROR_INVALID_ARGUMENT;
4209 goto exit;
4210 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4213 PSA_KEY_USAGE_DECRYPT,
4214 alg);
4215 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004216 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004218
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4220 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004221 status = PSA_ERROR_INVALID_ARGUMENT;
4222 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004224 status = PSA_ERROR_INVALID_ARGUMENT;
4225 goto exit;
4226 }
4227
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004228 status = psa_driver_wrapper_cipher_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004229 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004230 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004232
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004233exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004234 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004236 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004238
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004240 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 }
Ronald Cron23919522021-07-08 19:00:07 +02004242
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004244}
4245
4246
mohammad16035955c982018-04-26 00:53:03 +03004247/****************************************************************/
4248/* AEAD */
4249/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004250
Paul Elliottbaff51c2021-09-28 17:44:45 +01004251/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004252static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004253{
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004255}
4256
4257/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004258static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4259 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004260{
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004264#if defined(PSA_WANT_ALG_GCM)
4265 case PSA_ALG_GCM:
4266 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 * arbitrarily large nonces. Please note that we do not generally
4268 * recommend the usage of nonces of greater length than
4269 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4270 * size, which can then lead to collisions if you encrypt a very
4271 * large number of messages.*/
4272 if (nonce_length != 0) {
4273 return PSA_SUCCESS;
4274 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004275 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004276#endif /* PSA_WANT_ALG_GCM */
4277#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004278 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 if (nonce_length >= 7 && nonce_length <= 13) {
4280 return PSA_SUCCESS;
4281 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004282 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004283#endif /* PSA_WANT_ALG_CCM */
4284#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004285 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 if (nonce_length == 12) {
4287 return PSA_SUCCESS;
4288 } else if (nonce_length == 8) {
4289 return PSA_ERROR_NOT_SUPPORTED;
4290 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004291 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004292#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004293 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004294 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004296 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004297
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004299}
4300
Gilles Peskine449bd832023-01-11 14:50:10 +01004301static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004302{
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4304 return PSA_ERROR_INVALID_ARGUMENT;
4305 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004306
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004308}
4309
Gilles Peskine449bd832023-01-11 14:50:10 +01004310psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4311 psa_algorithm_t alg,
4312 const uint8_t *nonce,
4313 size_t nonce_length,
4314 const uint8_t *additional_data,
4315 size_t additional_data_length,
4316 const uint8_t *plaintext,
4317 size_t plaintext_length,
4318 uint8_t *ciphertext,
4319 size_t ciphertext_size,
4320 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004321{
Ronald Cron215633c2021-03-16 17:15:37 +01004322 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4323 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004324
mohammad1603f08a5502018-06-03 15:05:47 +03004325 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004326
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 status = psa_aead_check_algorithm(alg);
4328 if (status != PSA_SUCCESS) {
4329 return status;
4330 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004331
Ronald Cron9a986162021-03-26 12:40:07 +01004332 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4334 if (status != PSA_SUCCESS) {
4335 return status;
4336 }
mohammad16035955c982018-04-26 00:53:03 +03004337
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 status = psa_aead_check_nonce_length(alg, nonce_length);
4339 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004340 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004342
Ronald Cronde822812021-03-17 16:08:20 +01004343 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004344 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004345 alg,
4346 nonce, nonce_length,
4347 additional_data, additional_data_length,
4348 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4352 memset(ciphertext, 0, ciphertext_size);
4353 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004354
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004355exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004356 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004357
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004359}
4360
Gilles Peskine449bd832023-01-11 14:50:10 +01004361psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4362 psa_algorithm_t alg,
4363 const uint8_t *nonce,
4364 size_t nonce_length,
4365 const uint8_t *additional_data,
4366 size_t additional_data_length,
4367 const uint8_t *ciphertext,
4368 size_t ciphertext_length,
4369 uint8_t *plaintext,
4370 size_t plaintext_size,
4371 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004372{
Ronald Cron215633c2021-03-16 17:15:37 +01004373 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4374 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004375
Gilles Peskineee652a32018-06-01 19:23:52 +02004376 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004377
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 status = psa_aead_check_algorithm(alg);
4379 if (status != PSA_SUCCESS) {
4380 return status;
4381 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004382
Ronald Cron9a986162021-03-26 12:40:07 +01004383 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4385 if (status != PSA_SUCCESS) {
4386 return status;
4387 }
mohammad16035955c982018-04-26 00:53:03 +03004388
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 status = psa_aead_check_nonce_length(alg, nonce_length);
4390 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004391 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004393
Ronald Cronde822812021-03-17 16:08:20 +01004394 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004395 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004396 alg,
4397 nonce, nonce_length,
4398 additional_data, additional_data_length,
4399 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 if (status != PSA_SUCCESS && plaintext_size != 0) {
4403 memset(plaintext, 0, plaintext_size);
4404 }
mohammad160360a64d02018-06-03 17:20:42 +03004405
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004406exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004407 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 return status;
mohammad16035955c982018-04-26 00:53:03 +03004410}
4411
Gilles Peskine449bd832023-01-11 14:50:10 +01004412static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4413{
4414 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004415
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004417#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004419 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4421 return PSA_ERROR_INVALID_ARGUMENT;
4422 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004423 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004424#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004425
Przemek Stekiel86679c72022-10-06 17:06:56 +02004426#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004428 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4430 return PSA_ERROR_INVALID_ARGUMENT;
4431 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004432 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004433#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004434
Przemek Stekiel86679c72022-10-06 17:06:56 +02004435#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004437 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 if (tag_len != 16) {
4439 return PSA_ERROR_INVALID_ARGUMENT;
4440 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004441 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004442#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004443
4444 default:
4445 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004447 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004449}
4450
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004451/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004452static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4453 int is_encrypt,
4454 mbedtls_svc_key_id_t key,
4455 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004456{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004457 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4458 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4459 psa_key_slot_t *slot = NULL;
4460 psa_key_usage_t key_usage = 0;
4461
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 status = psa_aead_check_algorithm(alg);
4463 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004464 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004466
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004468 status = PSA_ERROR_BAD_STATE;
4469 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004470 }
4471
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 if (operation->nonce_set || operation->lengths_set ||
4473 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004474 status = PSA_ERROR_BAD_STATE;
4475 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004476 }
4477
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004479 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004481 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004483
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4485 alg);
4486 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004487 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004489
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004491 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004493
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 if (is_encrypt) {
4495 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004496 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 slot->key.data,
4498 slot->key.bytes,
4499 alg);
4500 } else {
4501 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004502 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 slot->key.data,
4504 slot->key.bytes,
4505 alg);
4506 }
4507 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004508 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004510
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004511 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004512
4513exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00004514 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004517 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004519 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 } else {
4521 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004522 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004525}
4526
Paul Elliott302ff6b2021-04-20 18:10:30 +01004527/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004528psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4529 mbedtls_svc_key_id_t key,
4530 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004531{
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004533}
4534
4535/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004536psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4537 mbedtls_svc_key_id_t key,
4538 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004539{
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004541}
4542
4543/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004544psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4545 uint8_t *nonce,
4546 size_t nonce_size,
4547 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004548{
4549 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004550 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004551 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004552
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004553 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004556 status = PSA_ERROR_BAD_STATE;
4557 goto exit;
4558 }
4559
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004561 status = PSA_ERROR_BAD_STATE;
4562 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004563 }
4564
Paul Elliotte193ea82021-10-01 13:00:16 +01004565 /* For CCM, this size may not be correct according to the PSA
4566 * specification. The PSA Crypto 1.0.1 specification states:
4567 *
4568 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4569 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4570 *
4571 * However this restriction that L has to be the smallest integer is not
4572 * applied in practice, and it is not implementable here since the
4573 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4575 operation->alg);
4576 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004577 status = PSA_ERROR_BUFFER_TOO_SMALL;
4578 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004579 }
4580
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 status = psa_generate_random(local_nonce, required_nonce_size);
4582 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004583 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004587
Paul Elliott39dc6b82021-05-11 19:16:09 +01004588exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 if (status == PSA_SUCCESS) {
4590 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004591 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 } else {
4593 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004594 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004595
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004597}
4598
4599/* Set the nonce for a multipart authenticated encryption or decryption
4600 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004601psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4602 const uint8_t *nonce,
4603 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004604{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004605 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004608 status = PSA_ERROR_BAD_STATE;
4609 goto exit;
4610 }
4611
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004613 status = PSA_ERROR_BAD_STATE;
4614 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004615 }
4616
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4618 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004619 status = PSA_ERROR_INVALID_ARGUMENT;
4620 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004621 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4624 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004625
Paul Elliott39dc6b82021-05-11 19:16:09 +01004626exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004628 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 } else {
4630 psa_aead_abort(operation);
4631 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004634}
4635
4636/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004637psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4638 size_t ad_length,
4639 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004640{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004641 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4642
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004644 status = PSA_ERROR_BAD_STATE;
4645 goto exit;
4646 }
4647
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 if (operation->lengths_set || operation->ad_started ||
4649 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004650 status = PSA_ERROR_BAD_STATE;
4651 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004652 }
4653
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004655#if defined(PSA_WANT_ALG_GCM)
4656 case PSA_ALG_GCM:
4657 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 * bits. Without the guard this code will generate warnings on 32bit
4659 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004660#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 if (((uint64_t) ad_length) >> 61 != 0 ||
4662 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004663 status = PSA_ERROR_INVALID_ARGUMENT;
4664 goto exit;
4665 }
Paul Elliott325d3742021-09-27 17:56:28 +01004666#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004667 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004668#endif /* PSA_WANT_ALG_GCM */
4669#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004670 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004672 status = PSA_ERROR_INVALID_ARGUMENT;
4673 goto exit;
4674 }
4675 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004676#endif /* PSA_WANT_ALG_CCM */
4677#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004678 case PSA_ALG_CHACHA20_POLY1305:
4679 /* No length restrictions for ChaChaPoly. */
4680 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004681#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004682 default:
4683 break;
4684 }
Paul Elliott325d3742021-09-27 17:56:28 +01004685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4687 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004688
Paul Elliott39dc6b82021-05-11 19:16:09 +01004689exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004691 operation->ad_remaining = ad_length;
4692 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004693 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 } else {
4695 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004696 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004699}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004700
4701/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004702psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4703 const uint8_t *input,
4704 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004705{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004706 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004709 status = PSA_ERROR_BAD_STATE;
4710 goto exit;
4711 }
4712
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004714 status = PSA_ERROR_BAD_STATE;
4715 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004716 }
4717
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 if (operation->lengths_set) {
4719 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004720 status = PSA_ERROR_INVALID_ARGUMENT;
4721 goto exit;
4722 }
4723
4724 operation->ad_remaining -= input_length;
4725 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004726#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004728 status = PSA_ERROR_BAD_STATE;
4729 goto exit;
4730 }
4731#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004732
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 status = psa_driver_wrapper_aead_update_ad(operation, input,
4734 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004735
Paul Elliott39dc6b82021-05-11 19:16:09 +01004736exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004738 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 } else {
4740 psa_aead_abort(operation);
4741 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004742
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004744}
4745
4746/* Encrypt or decrypt a message fragment in an active multipart AEAD
4747 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004748psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4749 const uint8_t *input,
4750 size_t input_length,
4751 uint8_t *output,
4752 size_t output_size,
4753 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004754{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004755 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004756
4757 *output_length = 0;
4758
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004760 status = PSA_ERROR_BAD_STATE;
4761 goto exit;
4762 }
4763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004765 status = PSA_ERROR_BAD_STATE;
4766 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004767 }
4768
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004770 /* Additional data length was supplied, but not all the additional
4771 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004773 status = PSA_ERROR_INVALID_ARGUMENT;
4774 goto exit;
4775 }
4776
4777 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004779 status = PSA_ERROR_INVALID_ARGUMENT;
4780 goto exit;
4781 }
4782
4783 operation->body_remaining -= input_length;
4784 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004785#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004787 status = PSA_ERROR_BAD_STATE;
4788 goto exit;
4789 }
4790#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004791
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4793 output, output_size,
4794 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004795
Paul Elliott39dc6b82021-05-11 19:16:09 +01004796exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004798 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 } else {
4800 psa_aead_abort(operation);
4801 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004802
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004804}
4805
Gilles Peskine449bd832023-01-11 14:50:10 +01004806static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004807{
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 if (operation->id == 0 || !operation->nonce_set) {
4809 return PSA_ERROR_BAD_STATE;
4810 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4813 operation->body_remaining != 0)) {
4814 return PSA_ERROR_INVALID_ARGUMENT;
4815 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004818}
4819
Paul Elliott302ff6b2021-04-20 18:10:30 +01004820/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004821psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4822 uint8_t *ciphertext,
4823 size_t ciphertext_size,
4824 size_t *ciphertext_length,
4825 uint8_t *tag,
4826 size_t tag_size,
4827 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004828{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004829 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4830
Paul Elliott302ff6b2021-04-20 18:10:30 +01004831 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004832 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 status = psa_aead_final_checks(operation);
4835 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004836 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004838
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004840 status = PSA_ERROR_BAD_STATE;
4841 goto exit;
4842 }
4843
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4845 ciphertext_size,
4846 ciphertext_length,
4847 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004848
Paul Elliott39dc6b82021-05-11 19:16:09 +01004849exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004850
4851
Paul Elliottf47b0952021-05-21 18:02:33 +01004852 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004853 * the zero tag size, make sure the tag is set to something implausible.
4854 * Even if the operation succeeds, make sure we clear the rest of the
4855 * buffer to prevent potential leakage of anything previously placed in
4856 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004857 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004858
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004860
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004862}
4863
4864/* Finish authenticating and decrypting a message in a multipart AEAD
4865 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004866psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4867 uint8_t *plaintext,
4868 size_t plaintext_size,
4869 size_t *plaintext_length,
4870 const uint8_t *tag,
4871 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004872{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004873 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4874
Paul Elliott302ff6b2021-04-20 18:10:30 +01004875 *plaintext_length = 0;
4876
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 status = psa_aead_final_checks(operation);
4878 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004879 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004883 status = PSA_ERROR_BAD_STATE;
4884 goto exit;
4885 }
4886
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4888 plaintext_size,
4889 plaintext_length,
4890 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004891
4892exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004894
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004896}
4897
4898/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004899psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004900{
4901 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4902
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004904 /* The object has (apparently) been initialized but it is not (yet)
4905 * in use. It's ok to call abort on such an object, and there's
4906 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004908 }
4909
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004911
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004913
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004915}
4916
Gilles Peskinee59236f2018-01-27 23:32:46 +01004917/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004918/* Generators */
4919/****************************************************************/
4920
Przemek Stekiel69c46792022-06-10 12:59:51 +02004921#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004922 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004923 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05304924 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304925 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08004926#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004927#endif /* At least one builtin KDF */
4928
Przemek Stekiel69c46792022-06-10 12:59:51 +02004929#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004930 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4931 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4932static psa_status_t psa_key_derivation_start_hmac(
4933 psa_mac_operation_t *operation,
4934 psa_algorithm_t hash_alg,
4935 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02004937{
4938 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4941 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4942 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02004943
Steven Cooreman72f736a2021-05-07 14:14:37 +02004944 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02004946
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 status = psa_driver_wrapper_mac_sign_setup(operation,
4948 &attributes,
4949 hmac_key, hmac_key_length,
4950 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02004951
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 psa_reset_key_attributes(&attributes);
4953 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02004954}
4955#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004956
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004957#define HKDF_STATE_INIT 0 /* no input yet */
4958#define HKDF_STATE_STARTED 1 /* got salt */
4959#define HKDF_STATE_KEYED 2 /* got key */
4960#define HKDF_STATE_OUTPUT 3 /* output started */
4961
Gilles Peskinecbe66502019-05-16 16:59:18 +02004962static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004964{
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4966 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4967 } else {
4968 return operation->alg;
4969 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004970}
4971
Gilles Peskine449bd832023-01-11 14:50:10 +01004972psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004973{
4974 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4976 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004977 /* The object has (apparently) been initialized but it is not
4978 * in use. It's ok to call abort on such an object, and there's
4979 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004981#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
4983 mbedtls_free(operation->ctx.hkdf.info);
4984 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4985 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004986#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004987#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4988 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4990 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4991 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4992 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004993 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02004995 }
4996
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004998 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005000 }
5001
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005003 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005005 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005006#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005008 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005010 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005011#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005012 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005013
5014 /* We leave the fields Ai and output_block to be erased safely by the
5015 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005017#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5018 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005019#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5021 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5022 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5023 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005024#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305025#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305026 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305027 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005028 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305029 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305030 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305031
5032 status = PSA_SUCCESS;
5033 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305034#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005035 {
5036 status = PSA_ERROR_BAD_STATE;
5037 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 mbedtls_platform_zeroize(operation, sizeof(*operation));
5039 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005040}
5041
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005042psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005044{
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005046 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005048 }
5049
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005050 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005052}
5053
Gilles Peskine449bd832023-01-11 14:50:10 +01005054psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5055 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005056{
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 if (operation->alg == 0) {
5058 return PSA_ERROR_BAD_STATE;
5059 }
5060 if (capacity > operation->capacity) {
5061 return PSA_ERROR_INVALID_ARGUMENT;
5062 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005065}
5066
Przemek Stekiel69c46792022-06-10 12:59:51 +02005067#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005068/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005069static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5070 psa_algorithm_t kdf_alg,
5071 uint8_t *output,
5072 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005073{
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5075 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005076 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005077 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005078#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005080#else
5081 const uint8_t last_block = 0xff;
5082#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005083
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 if (hkdf->state < HKDF_STATE_KEYED ||
5085 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005086#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005088#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 )) {
5090 return PSA_ERROR_BAD_STATE;
5091 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005092 hkdf->state = HKDF_STATE_OUTPUT;
5093
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005095 /* Copy what remains of the current block */
5096 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005098 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 }
5100 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005101 output += n;
5102 output_length -= n;
5103 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005105 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005107 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005108 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005109 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005110 * object was corrupted or if this function is called directly
5111 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 if (hkdf->block_number == last_block) {
5113 return PSA_ERROR_BAD_STATE;
5114 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005115
5116 /* We need a new block */
5117 ++hkdf->block_number;
5118 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005119
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5121 hash_alg,
5122 hkdf->prk,
5123 hash_length);
5124 if (status != PSA_SUCCESS) {
5125 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005126 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005127
5128 if (hkdf->block_number != 1) {
5129 status = psa_mac_update(&hkdf->hmac,
5130 hkdf->output_block,
5131 hash_length);
5132 if (status != PSA_SUCCESS) {
5133 return status;
5134 }
5135 }
5136 status = psa_mac_update(&hkdf->hmac,
5137 hkdf->info,
5138 hkdf->info_length);
5139 if (status != PSA_SUCCESS) {
5140 return status;
5141 }
5142 status = psa_mac_update(&hkdf->hmac,
5143 &hkdf->block_number, 1);
5144 if (status != PSA_SUCCESS) {
5145 return status;
5146 }
5147 status = psa_mac_sign_finish(&hkdf->hmac,
5148 hkdf->output_block,
5149 sizeof(hkdf->output_block),
5150 &hmac_output_length);
5151 if (status != PSA_SUCCESS) {
5152 return status;
5153 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005154 }
5155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005157}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005158#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005159
John Durkop07cc04a2020-11-16 22:08:34 -08005160#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5161 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005162static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5163 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005165{
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5167 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005168 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5169 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005170 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005171
Janos Follath7742fee2019-06-17 12:58:10 +01005172 /* We can't be wanting more output after block 0xff, otherwise
5173 * the capacity check in psa_key_derivation_output_bytes() would have
5174 * prevented this call. It could happen only if the operation
5175 * object was corrupted or if this function is called directly
5176 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 if (tls12_prf->block_number == 0xff) {
5178 return PSA_ERROR_CORRUPTION_DETECTED;
5179 }
Janos Follath7742fee2019-06-17 12:58:10 +01005180
5181 /* We need a new block */
5182 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005183 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005184
5185 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5186 *
5187 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5188 *
5189 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5190 * HMAC_hash(secret, A(2) + seed) +
5191 * HMAC_hash(secret, A(3) + seed) + ...
5192 *
5193 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005194 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005195 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005196 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005197 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005198 * is currently extracted as `output_block` and where i is
5199 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005200 */
5201
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 status = psa_key_derivation_start_hmac(&hmac,
5203 hash_alg,
5204 tls12_prf->secret,
5205 tls12_prf->secret_length);
5206 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005207 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005209
5210 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005212 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5213 * the variable seed and in this instance means it in the context of the
5214 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 status = psa_mac_update(&hmac,
5216 tls12_prf->label,
5217 tls12_prf->label_length);
5218 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005219 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 }
5221 status = psa_mac_update(&hmac,
5222 tls12_prf->seed,
5223 tls12_prf->seed_length);
5224 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005225 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 }
5227 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005228 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5230 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005231 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005233 }
5234
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 status = psa_mac_sign_finish(&hmac,
5236 tls12_prf->Ai, hash_length,
5237 &hmac_output_length);
5238 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005239 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 }
5241 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005242 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005244
5245 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 status = psa_key_derivation_start_hmac(&hmac,
5247 hash_alg,
5248 tls12_prf->secret,
5249 tls12_prf->secret_length);
5250 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005251 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 }
5253 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5254 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005255 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 }
5257 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5258 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005259 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 }
5261 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5262 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005263 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 }
5265 status = psa_mac_sign_finish(&hmac,
5266 tls12_prf->output_block, hash_length,
5267 &hmac_output_length);
5268 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005269 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005271
Janos Follath7742fee2019-06-17 12:58:10 +01005272
5273cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 cleanup_status = psa_mac_abort(&hmac);
5275 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005276 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005278
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005280}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005281
Janos Follath844eb0e2019-06-19 12:10:49 +01005282static psa_status_t psa_key_derivation_tls12_prf_read(
5283 psa_tls12_prf_key_derivation_t *tls12_prf,
5284 psa_algorithm_t alg,
5285 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005287{
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5289 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005290 psa_status_t status;
5291 uint8_t offset, length;
5292
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005294 case PSA_TLS12_PRF_STATE_LABEL_SET:
5295 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5296 break;
5297 case PSA_TLS12_PRF_STATE_OUTPUT:
5298 break;
5299 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005301 }
5302
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005304 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 if (tls12_prf->left_in_block == 0) {
5306 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5307 alg);
5308 if (status != PSA_SUCCESS) {
5309 return status;
5310 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005311
5312 continue;
5313 }
5314
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005316 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005318 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005320
5321 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005323 output += length;
5324 output_length -= length;
5325 tls12_prf->left_in_block -= length;
5326 }
5327
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005329}
John Durkop07cc04a2020-11-16 22:08:34 -08005330#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5331 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005332
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005333#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5334static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5335 psa_tls12_ecjpake_to_pms_t *ecjpake,
5336 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005338{
5339 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005340 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005341
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 if (output_length != 32) {
5343 return PSA_ERROR_INVALID_ARGUMENT;
5344 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005345
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5347 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5348 &output_size);
5349 if (status != PSA_SUCCESS) {
5350 return status;
5351 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005352
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 if (output_size != output_length) {
5354 return PSA_ERROR_GENERIC_ERROR;
5355 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005356
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005358}
5359#endif
5360
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305361#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305362static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5363 psa_pbkdf2_key_derivation_t *pbkdf2,
5364 psa_algorithm_t prf_alg,
5365 uint8_t prf_output_length,
5366 psa_key_attributes_t *attributes)
5367{
5368 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305369 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305370 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305371 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305372 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305373 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305374 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305375
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305376 mac_operation.is_sign = 1;
5377 mac_operation.mac_size = prf_output_length;
5378 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305379
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305380 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5381 attributes,
5382 pbkdf2->password,
5383 pbkdf2->password_length,
5384 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305385 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305386 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305387 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305388 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5389 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305390 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305391 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305392 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305393 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305394 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305395 }
5396 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5397 &mac_output_length);
5398 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305399 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305400 }
5401
5402 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305403 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305404 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305405 }
5406
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305407 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305408
5409 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305410 /* We are passing prf_output_length as mac_size because the driver
5411 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305412 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305413 status = psa_driver_wrapper_mac_compute(attributes,
5414 pbkdf2->password,
5415 pbkdf2->password_length,
5416 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305417 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305418 &mac_output_length);
5419 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305420 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305421 }
5422
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305423 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305424 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305425
5426cleanup:
5427 /* Zeroise buffers to clear sensitive data from memory. */
5428 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5429 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305430}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305431
5432static psa_status_t psa_key_derivation_pbkdf2_read(
5433 psa_pbkdf2_key_derivation_t *pbkdf2,
5434 psa_algorithm_t kdf_alg,
5435 uint8_t *output,
5436 size_t output_length)
5437{
5438 psa_status_t status;
5439 psa_algorithm_t prf_alg;
5440 uint8_t prf_output_length;
5441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5442 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5443 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5444
5445 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5446 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5447 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5448 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305449 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5450 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305451 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305452 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305453 } else {
5454 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305455 }
5456
5457 switch (pbkdf2->state) {
5458 case PSA_PBKDF2_STATE_PASSWORD_SET:
5459 /* Initially we need a new block so bytes_used is equal to block size*/
5460 pbkdf2->bytes_used = prf_output_length;
5461 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5462 break;
5463 case PSA_PBKDF2_STATE_OUTPUT:
5464 break;
5465 default:
5466 return PSA_ERROR_BAD_STATE;
5467 }
5468
5469 while (output_length != 0) {
5470 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5471 if (n > output_length) {
5472 n = (uint8_t) output_length;
5473 }
5474 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5475 output += n;
5476 output_length -= n;
5477 pbkdf2->bytes_used += n;
5478
5479 if (output_length == 0) {
5480 break;
5481 }
5482
5483 /* We need a new block */
5484 pbkdf2->bytes_used = 0;
5485 pbkdf2->block_number++;
5486
5487 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5488 prf_output_length,
5489 &attributes);
5490 if (status != PSA_SUCCESS) {
5491 return status;
5492 }
5493 }
5494
5495 return PSA_SUCCESS;
5496}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305497#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305498
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005499psa_status_t psa_key_derivation_output_bytes(
5500 psa_key_derivation_operation_t *operation,
5501 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005503{
5504 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005508 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005510 }
5511
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005513 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005514 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005515 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005516 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005517 goto exit;
5518 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005520 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005521 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005522 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5523 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005524 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005525 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005527 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005528 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005529
Przemek Stekiel69c46792022-06-10 12:59:51 +02005530#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5532 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5533 output, output_length);
5534 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005535#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005536#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5537 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5539 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5540 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5541 kdf_alg, output,
5542 output_length);
5543 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005544#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5545 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005546#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005548 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5550 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005551#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305552#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305553 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305554 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5555 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305556 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305557#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005558
Gilles Peskineeab56e42018-07-12 17:12:33 +02005559 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005560 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005562 }
5563
5564exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005566 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005567 * This allows us to differentiate between exhausted operations and
5568 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5569 * operations. */
5570 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005572 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005574 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005576}
5577
David Brown7807bf72021-02-09 16:28:23 -07005578#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005579static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005580{
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 if (data_size >= 8) {
5582 mbedtls_des_key_set_parity(data);
5583 }
5584 if (data_size >= 16) {
5585 mbedtls_des_key_set_parity(data + 8);
5586 }
5587 if (data_size >= 24) {
5588 mbedtls_des_key_set_parity(data + 16);
5589 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005590}
David Brown7807bf72021-02-09 16:28:23 -07005591#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005592
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005593/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 * ECC keys on a Weierstrass elliptic curve require the generation
5595 * of a private key which is an integer
5596 * in the range [1, N - 1], where N is the boundary of the private key domain:
5597 * N is the prime p for Diffie-Hellman, or the order of the
5598 * curve’s base point for ECC.
5599 *
5600 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5601 * This function generates the private key using the following process:
5602 *
5603 * 1. Draw a byte string of length ceiling(m/8) bytes.
5604 * 2. If m is not a multiple of 8, set the most significant
5605 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5606 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5607 * 4. If k > N - 2, discard the result and return to step 1.
5608 * 5. Output k + 1 as the private key.
5609 *
5610 * This method allows compliance to NIST standards, specifically the methods titled
5611 * Key-Pair Generation by Testing Candidates in the following publications:
5612 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5613 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5614 * Diffie-Hellman keys.
5615 *
5616 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5617 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5618 *
5619 * Note: Function allocates memory for *data buffer, so given *data should be
5620 * always NULL.
5621 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005622#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5623#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005624static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005625 psa_key_slot_t *slot,
5626 size_t bits,
5627 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005628 uint8_t **data
5629 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005630{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005631 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005632 mbedtls_mpi k;
5633 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005634 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005635 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005636 size_t m;
5637 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005638
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 mbedtls_mpi_init(&k);
5640 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005641
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005642 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005644 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01005645 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005646
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005648 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005649 goto cleanup;
5650 }
5651
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005652 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005654
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005656
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005657 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005658 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005659 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005660
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005661 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005662
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005663 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005665
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005666 /* Note: This function is always called with *data == NULL and it
5667 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 *data = mbedtls_calloc(1, m_bytes);
5669 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005670 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005671 goto cleanup;
5672 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005675 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005677 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005679
5680 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005682 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005683 * (8 * ceiling(m/8) - m) bits of the first byte in
5684 * the string to zero.
5685 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005687 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005688 }
5689
5690 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005691 * big-endian byte string.
5692 */
5693 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005694
5695 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 * Result of comparison is returned. When it indicates error
5697 * then this function is called again.
5698 */
5699 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005700 }
5701
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005702 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5704 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005705cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 if (ret != 0) {
5707 status = mbedtls_to_psa_error(ret);
5708 }
5709 if (status != PSA_SUCCESS) {
5710 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005711 *data = NULL;
5712 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 mbedtls_mpi_free(&k);
5714 mbedtls_mpi_free(&diff_N_2);
5715 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005716}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005717
5718/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5719 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5720 *
5721 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5722 * draw a 32-byte string and process it as specified in
5723 * Elliptic Curves for Security [RFC7748] §5.
5724 *
5725 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5726 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5727 *
5728 * Note: Function allocates memory for *data buffer, so given *data should be
5729 * always NULL.
5730 */
5731
5732static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5733 size_t bits,
5734 psa_key_derivation_operation_t *operation,
5735 uint8_t **data
5736 )
5737{
5738 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005739 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005740
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005742 case 255:
5743 output_length = 32;
5744 break;
5745 case 448:
5746 output_length = 56;
5747 break;
5748 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005750 break;
5751 }
5752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005754
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 if (*data == NULL) {
5756 return PSA_ERROR_INSUFFICIENT_MEMORY;
5757 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005758
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005760
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005762 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005764
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005766 case 255:
5767 (*data)[0] &= 248;
5768 (*data)[31] &= 127;
5769 (*data)[31] |= 64;
5770 break;
5771 case 448:
5772 (*data)[0] &= 252;
5773 (*data)[55] |= 128;
5774 break;
5775 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005777 break;
5778 }
5779
5780 return status;
5781}
Valerio Setti86587ab2023-06-27 13:09:30 +02005782#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5783static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5784 psa_key_slot_t *slot, size_t bits,
5785 psa_key_derivation_operation_t *operation, uint8_t **data)
5786{
5787 (void) slot;
5788 (void) bits;
5789 (void) operation;
5790 (void) data;
5791 return PSA_ERROR_NOT_SUPPORTED;
5792}
5793
5794static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5795 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5796{
5797 (void) bits;
5798 (void) operation;
5799 (void) data;
5800 return PSA_ERROR_NOT_SUPPORTED;
5801}
5802#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5803#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005804
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005805static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005806 psa_key_slot_t *slot,
5807 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005809{
5810 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305812 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005813 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5816 return PSA_ERROR_INVALID_ARGUMENT;
5817 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005818
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005819#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005820 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5822 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5823 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005824 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5826 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005827 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 }
5829 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005830 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5832 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005833 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005835 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005836 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005837#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005838 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 if (key_type_is_raw_bytes(slot->attr.type)) {
5840 if (bits % 8 != 0) {
5841 return PSA_ERROR_INVALID_ARGUMENT;
5842 }
5843 data = mbedtls_calloc(1, bytes);
5844 if (data == NULL) {
5845 return PSA_ERROR_INSUFFICIENT_MEMORY;
5846 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005847
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 status = psa_key_derivation_output_bytes(operation, data, bytes);
5849 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005850 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 }
David Brown12ca5032021-02-11 11:02:00 -07005852#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5854 psa_des_set_key_parity(data, bytes);
5855 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005856#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 } else {
5858 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005859 }
Ronald Crondd04d422020-11-28 15:14:42 +01005860
Ronald Cronbf33c932020-11-28 18:06:53 +01005861 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005862
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005863 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005864 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 &storage_size);
5866 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305867 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 }
Archanad8a83dc2021-06-14 10:04:16 +05305869 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 status = psa_allocate_buffer_to_slot(slot, storage_size);
5871 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305872 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 }
Archanad8a83dc2021-06-14 10:04:16 +05305874
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005875 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 data, bytes,
5877 slot->key.data,
5878 slot->key.bytes,
5879 &slot->key.bytes, &bits);
5880 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005881 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005883
5884exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 mbedtls_free(data);
5886 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005887}
5888
Gilles Peskine092ce512024-02-20 12:31:24 +01005889static const psa_key_production_parameters_t default_production_parameters =
5890 PSA_KEY_PRODUCTION_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005891
Gilles Peskine092ce512024-02-20 12:31:24 +01005892int psa_key_production_parameters_are_default(
5893 const psa_key_production_parameters_t *params,
5894 size_t params_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005895{
Gilles Peskine092ce512024-02-20 12:31:24 +01005896 if (params->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005897 return 0;
5898 }
Gilles Peskine092ce512024-02-20 12:31:24 +01005899 if (params_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005900 return 0;
5901 }
5902 return 1;
5903}
5904
5905psa_status_t psa_key_derivation_output_key_ext(
5906 const psa_key_attributes_t *attributes,
5907 psa_key_derivation_operation_t *operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005908 const psa_key_production_parameters_t *params,
5909 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005910 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005911{
5912 psa_status_t status;
5913 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005914 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005915
Ronald Cron81709fc2020-11-14 12:10:32 +01005916 *key = MBEDTLS_SVC_KEY_ID_INIT;
5917
Gilles Peskine0f84d622019-09-12 19:03:13 +02005918 /* Reject any attempt to create a zero-length key so that we don't
5919 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 if (psa_get_key_bits(attributes) == 0) {
5921 return PSA_ERROR_INVALID_ARGUMENT;
5922 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005923
Gilles Peskine092ce512024-02-20 12:31:24 +01005924 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005925 return PSA_ERROR_INVALID_ARGUMENT;
5926 }
5927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 if (operation->alg == PSA_ALG_NONE) {
5929 return PSA_ERROR_BAD_STATE;
5930 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005931
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 if (!operation->can_output_key) {
5933 return PSA_ERROR_NOT_PERMITTED;
5934 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005935
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5937 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005938#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005940 /* Deriving a key in a secure element is not implemented yet. */
5941 status = PSA_ERROR_NOT_SUPPORTED;
5942 }
5943#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 if (status == PSA_SUCCESS) {
5945 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005946 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005948 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 if (status == PSA_SUCCESS) {
5950 status = psa_finish_key_creation(slot, driver, key);
5951 }
5952 if (status != PSA_SUCCESS) {
5953 psa_fail_key_creation(slot, driver);
5954 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005955
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005957}
5958
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005959psa_status_t psa_key_derivation_output_key(
5960 const psa_key_attributes_t *attributes,
5961 psa_key_derivation_operation_t *operation,
5962 mbedtls_svc_key_id_t *key)
5963{
Gilles Peskinec81393b2024-02-14 20:51:28 +01005964 return psa_key_derivation_output_key_ext(attributes, operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005965 &default_production_parameters, 0,
Gilles Peskinec81393b2024-02-14 20:51:28 +01005966 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005967}
Gilles Peskineeab56e42018-07-12 17:12:33 +02005968
5969
5970/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005971/* Key derivation */
5972/****************************************************************/
5973
Steven Cooreman932ffb72021-02-15 12:14:32 +01005974#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005975static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005976{
5977#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5979 return 1;
5980 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005981#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005982#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5984 return 1;
5985 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005986#endif
5987#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5989 return 1;
5990 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005991#endif
5992#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5994 return 1;
5995 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005996#endif
5997#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5999 return 1;
6000 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006001#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006002#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6004 return 1;
6005 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006006#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306007#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6008 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6009 return 1;
6010 }
6011#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306012#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6013 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6014 return 1;
6015 }
6016#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006018}
6019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006021{
6022 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 psa_status_t status = psa_hash_setup(&operation, alg);
6024 psa_hash_abort(&operation);
6025 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006026}
6027
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306028static psa_status_t psa_key_derivation_set_maximum_capacity(
6029 psa_key_derivation_operation_t *operation,
6030 psa_algorithm_t kdf_alg)
6031{
6032#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6033 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6034 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6035 return PSA_SUCCESS;
6036 }
6037#endif
6038#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
6039 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6040#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05306041 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306042 PSA_KEY_TYPE_AES,
6043 128U,
6044 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306045#else
6046 operation->capacity = SIZE_MAX;
6047#endif
6048 return PSA_SUCCESS;
6049 }
6050#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6051
6052 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6053 * invalid or meaningless but it does not affect this function */
6054 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6055 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306056 if (hash_size == 0) {
6057 return PSA_ERROR_NOT_SUPPORTED;
6058 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306059
6060 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6061 * we might fail later, which is somewhat unfriendly and potentially
6062 * risk-prone. */
6063 psa_status_t status = psa_hash_try_support(hash_alg);
6064 if (status != PSA_SUCCESS) {
6065 return status;
6066 }
6067
6068#if defined(PSA_WANT_ALG_HKDF)
6069 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6070 operation->capacity = 255 * hash_size;
6071 } else
6072#endif
6073#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6074 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6075 operation->capacity = hash_size;
6076 } else
6077#endif
6078#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6079 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6080 operation->capacity = 255 * hash_size;
6081 } else
6082#endif
6083#if defined(PSA_WANT_ALG_TLS12_PRF)
6084 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6085 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6086 operation->capacity = SIZE_MAX;
6087 } else
6088#endif
6089#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6090 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6091 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6092 /* Master Secret is always 48 bytes
6093 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6094 operation->capacity = 48U;
6095 } else
6096#endif
6097#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6098 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6099#if (SIZE_MAX > UINT32_MAX)
6100 operation->capacity = UINT32_MAX * hash_size;
6101#else
6102 operation->capacity = SIZE_MAX;
6103#endif
6104 } else
6105#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6106 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306107 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306108 status = PSA_ERROR_NOT_SUPPORTED;
6109 }
6110 return status;
6111}
6112
Gilles Peskine969c5d62019-01-16 15:53:06 +01006113static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006114 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006116{
Janos Follath5fe19732019-06-20 15:09:30 +01006117 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6118 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006120
Gilles Peskine969c5d62019-01-16 15:53:06 +01006121 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 if (!is_kdf_alg_supported(kdf_alg)) {
6123 return PSA_ERROR_NOT_SUPPORTED;
6124 }
John Durkop07cc04a2020-11-16 22:08:34 -08006125
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306126 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6127 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306128 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006129}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006130
Gilles Peskine449bd832023-01-11 14:50:10 +01006131static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006132{
6133#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 if (alg == PSA_ALG_ECDH) {
6135 return PSA_SUCCESS;
6136 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006137#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006138#if defined(PSA_WANT_ALG_FFDH)
6139 if (alg == PSA_ALG_FFDH) {
6140 return PSA_SUCCESS;
6141 }
6142#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006143 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006145}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006146
6147static int psa_key_derivation_allows_free_form_secret_input(
6148 psa_algorithm_t kdf_alg)
6149{
6150#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6151 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6152 return 0;
6153 }
6154#endif
6155 (void) kdf_alg;
6156 return 1;
6157}
John Durkop07cc04a2020-11-16 22:08:34 -08006158#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006159
Gilles Peskine449bd832023-01-11 14:50:10 +01006160psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6161 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006162{
6163 psa_status_t status;
6164
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 if (operation->alg != 0) {
6166 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006167 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006168
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6170 return PSA_ERROR_INVALID_ARGUMENT;
6171 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6172#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6173 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6174 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6175 status = psa_key_agreement_try_support(ka_alg);
6176 if (status != PSA_SUCCESS) {
6177 return status;
6178 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006179 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6180 return PSA_ERROR_INVALID_ARGUMENT;
6181 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6183#else
6184 return PSA_ERROR_NOT_SUPPORTED;
6185#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6186 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6187#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6188 status = psa_key_derivation_setup_kdf(operation, alg);
6189#else
6190 return PSA_ERROR_NOT_SUPPORTED;
6191#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6192 } else {
6193 return PSA_ERROR_INVALID_ARGUMENT;
6194 }
6195
6196 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006197 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 }
6199 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006200}
6201
Przemek Stekiel69c46792022-06-10 12:59:51 +02006202#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006203static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6204 psa_algorithm_t kdf_alg,
6205 psa_key_derivation_step_t step,
6206 const uint8_t *data,
6207 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006208{
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006210 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006212 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006213#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6215 return PSA_ERROR_INVALID_ARGUMENT;
6216 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006217#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 if (hkdf->state != HKDF_STATE_INIT) {
6219 return PSA_ERROR_BAD_STATE;
6220 } else {
6221 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6222 hash_alg,
6223 data, data_length);
6224 if (status != PSA_SUCCESS) {
6225 return status;
6226 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006227 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006229 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006230 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006231#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006233 /* We shouldn't be in different state as HKDF_EXPAND only allows
6234 * two inputs: SECRET (this case) and INFO which does not modify
6235 * the state. It could happen only if the hkdf
6236 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 if (hkdf->state != HKDF_STATE_INIT) {
6238 return PSA_ERROR_BAD_STATE;
6239 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006240
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006241 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6243 return PSA_ERROR_INVALID_ARGUMENT;
6244 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006245
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 memcpy(hkdf->prk, data, data_length);
6247 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006248#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006249 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006250 /* HKDF: If no salt was provided, use an empty salt.
6251 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006253#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6255 return PSA_ERROR_BAD_STATE;
6256 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006257#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6259 hash_alg,
6260 NULL, 0);
6261 if (status != PSA_SUCCESS) {
6262 return status;
6263 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006264 hkdf->state = HKDF_STATE_STARTED;
6265 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 if (hkdf->state != HKDF_STATE_STARTED) {
6267 return PSA_ERROR_BAD_STATE;
6268 }
6269 status = psa_mac_update(&hkdf->hmac,
6270 data, data_length);
6271 if (status != PSA_SUCCESS) {
6272 return status;
6273 }
6274 status = psa_mac_sign_finish(&hkdf->hmac,
6275 hkdf->prk,
6276 sizeof(hkdf->prk),
6277 &data_length);
6278 if (status != PSA_SUCCESS) {
6279 return status;
6280 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006281 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006282
Gilles Peskine2b522db2019-04-12 15:11:49 +02006283 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006284 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006285#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006287 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006289 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006291#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006292 {
6293 /* Block 0 is empty, and the next block will be
6294 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006296 }
6297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006299 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006300#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6302 return PSA_ERROR_INVALID_ARGUMENT;
6303 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006304#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006305#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6307 hkdf->state == HKDF_STATE_INIT) {
6308 return PSA_ERROR_BAD_STATE;
6309 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006310#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 if (hkdf->state == HKDF_STATE_OUTPUT) {
6312 return PSA_ERROR_BAD_STATE;
6313 }
6314 if (hkdf->info_set) {
6315 return PSA_ERROR_BAD_STATE;
6316 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006317 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 if (data_length != 0) {
6319 hkdf->info = mbedtls_calloc(1, data_length);
6320 if (hkdf->info == NULL) {
6321 return PSA_ERROR_INSUFFICIENT_MEMORY;
6322 }
6323 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006324 }
6325 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006327 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006329 }
6330}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006331#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006332
John Durkop07cc04a2020-11-16 22:08:34 -08006333#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6334 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006335static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6336 const uint8_t *data,
6337 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006338{
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6340 return PSA_ERROR_BAD_STATE;
6341 }
Janos Follathf08e2652019-06-13 09:05:41 +01006342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 if (data_length != 0) {
6344 prf->seed = mbedtls_calloc(1, data_length);
6345 if (prf->seed == NULL) {
6346 return PSA_ERROR_INSUFFICIENT_MEMORY;
6347 }
Janos Follathf08e2652019-06-13 09:05:41 +01006348
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006350 prf->seed_length = data_length;
6351 }
Janos Follathf08e2652019-06-13 09:05:41 +01006352
Gilles Peskine43f958b2020-12-13 14:55:14 +01006353 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006354
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006356}
6357
Gilles Peskine449bd832023-01-11 14:50:10 +01006358static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6359 const uint8_t *data,
6360 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006361{
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6363 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6364 return PSA_ERROR_BAD_STATE;
6365 }
Janos Follath81550542019-06-13 14:26:34 +01006366
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 if (data_length != 0) {
6368 prf->secret = mbedtls_calloc(1, data_length);
6369 if (prf->secret == NULL) {
6370 return PSA_ERROR_INSUFFICIENT_MEMORY;
6371 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006372
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006374 prf->secret_length = data_length;
6375 }
Janos Follath81550542019-06-13 14:26:34 +01006376
Gilles Peskine43f958b2020-12-13 14:55:14 +01006377 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006380}
6381
Gilles Peskine449bd832023-01-11 14:50:10 +01006382static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6383 const uint8_t *data,
6384 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006385{
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6387 return PSA_ERROR_BAD_STATE;
6388 }
Janos Follath63028dd2019-06-13 09:15:47 +01006389
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 if (data_length != 0) {
6391 prf->label = mbedtls_calloc(1, data_length);
6392 if (prf->label == NULL) {
6393 return PSA_ERROR_INSUFFICIENT_MEMORY;
6394 }
Janos Follath63028dd2019-06-13 09:15:47 +01006395
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006397 prf->label_length = data_length;
6398 }
Janos Follath63028dd2019-06-13 09:15:47 +01006399
Gilles Peskine43f958b2020-12-13 14:55:14 +01006400 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006401
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006403}
6404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6406 psa_key_derivation_step_t step,
6407 const uint8_t *data,
6408 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006409{
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006411 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006413 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006415 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006417 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006419 }
6420}
John Durkop07cc04a2020-11-16 22:08:34 -08006421#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6422 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6423
6424#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6425static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6426 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006427 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006429{
6430 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6432 4 + data_length + prf->other_secret_length :
6433 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006434
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6436 return PSA_ERROR_INVALID_ARGUMENT;
6437 }
John Durkop07cc04a2020-11-16 22:08:34 -08006438
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 uint8_t *pms = mbedtls_calloc(1, pms_len);
6440 if (pms == NULL) {
6441 return PSA_ERROR_INSUFFICIENT_MEMORY;
6442 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006443 uint8_t *cur = pms;
6444
6445 /* pure-PSK:
6446 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006447 *
6448 * The premaster secret is formed as follows: if the PSK is N octets
6449 * long, concatenate a uint16 with the value N, N zero octets, a second
6450 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006451 *
6452 * mixed-PSK:
6453 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6454 * follows: concatenate a uint16 with the length of the other secret,
6455 * the other secret itself, uint16 with the length of PSK, and the
6456 * PSK itself.
6457 * For details please check:
6458 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6459 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6460 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006461 */
6462
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6464 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6465 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6466 if (prf->other_secret_length != 0) {
6467 memcpy(cur, prf->other_secret, prf->other_secret_length);
6468 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006469 cur += prf->other_secret_length;
6470 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 } else {
6472 *cur++ = MBEDTLS_BYTE_1(data_length);
6473 *cur++ = MBEDTLS_BYTE_0(data_length);
6474 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006475 cur += data_length;
6476 }
6477
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 *cur++ = MBEDTLS_BYTE_1(data_length);
6479 *cur++ = MBEDTLS_BYTE_0(data_length);
6480 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006481 cur += data_length;
6482
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006483 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006484
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006485 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006487}
Janos Follath6660f0e2019-06-17 08:44:03 +01006488
Przemek Stekiele47201b2022-04-19 13:53:28 +02006489static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6490 psa_tls12_prf_key_derivation_t *prf,
6491 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006493{
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6495 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006496 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006497
6498 if (data_length != 0) {
6499 prf->other_secret = mbedtls_calloc(1, data_length);
6500 if (prf->other_secret == NULL) {
6501 return PSA_ERROR_INSUFFICIENT_MEMORY;
6502 }
6503
6504 memcpy(prf->other_secret, data, data_length);
6505 prf->other_secret_length = data_length;
6506 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006507 prf->other_secret_length = 0;
6508 }
6509
6510 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6511
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006513}
6514
Janos Follath6660f0e2019-06-17 08:44:03 +01006515static psa_status_t psa_tls12_prf_psk_to_ms_input(
6516 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006517 psa_key_derivation_step_t step,
6518 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006520{
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006522 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 return psa_tls12_prf_psk_to_ms_set_key(prf,
6524 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006525 break;
6526 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6528 data,
6529 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006530 break;
6531 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006533 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006534
Przemek Stekiele47201b2022-04-19 13:53:28 +02006535 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006536}
John Durkop07cc04a2020-11-16 22:08:34 -08006537#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006538
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006539#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6540static psa_status_t psa_tls12_ecjpake_to_pms_input(
6541 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006542 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006543 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006544 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006545{
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6547 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6548 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006549 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006550
6551 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006552 if (data[0] != 0x04) {
6553 return PSA_ERROR_INVALID_ARGUMENT;
6554 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006555
6556 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006560}
6561#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306562
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306563#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306564static psa_status_t psa_pbkdf2_set_input_cost(
6565 psa_pbkdf2_key_derivation_t *pbkdf2,
6566 psa_key_derivation_step_t step,
6567 uint64_t data)
6568{
6569 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6570 return PSA_ERROR_INVALID_ARGUMENT;
6571 }
6572
6573 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6574 return PSA_ERROR_BAD_STATE;
6575 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306576
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306577 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306578 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306579 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306580
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306581 if (data == 0) {
6582 return PSA_ERROR_INVALID_ARGUMENT;
6583 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306584
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306585 pbkdf2->input_cost = data;
6586 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6587
6588 return PSA_SUCCESS;
6589}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306590
6591static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6592 const uint8_t *data,
6593 size_t data_length)
6594{
Gilles Peskineead17662023-08-20 22:02:51 +02006595 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6596 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6597 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6598 /* Appending to existing salt. No state change. */
6599 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306600 return PSA_ERROR_BAD_STATE;
6601 }
6602
Gilles Peskineca57d782023-07-20 19:08:06 +02006603 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006604 /* Appending an empty string, nothing to do. */
6605 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306606 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306607
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306608 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6609 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306610 return PSA_ERROR_INSUFFICIENT_MEMORY;
6611 }
6612
Gilles Peskineead17662023-08-20 22:02:51 +02006613 if (pbkdf2->salt_length != 0) {
6614 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6615 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306616 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306617 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306618 mbedtls_free(pbkdf2->salt);
6619 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306620 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306621 return PSA_SUCCESS;
6622}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306623
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306624#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306625static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306626 const uint8_t *input,
6627 size_t input_len,
6628 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306629 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306630{
6631 psa_status_t status = PSA_SUCCESS;
6632 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6633 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306634 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306635 } else {
6636 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306637 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306638 }
6639 return status;
6640}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306641#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306642
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306643#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306644static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6645 size_t input_len,
6646 uint8_t *output,
6647 size_t *output_len)
6648{
6649 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306650 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306651 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306652 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306653 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6654 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6655 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306656 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306657 * mac_size as the driver function sets mac_output_length = mac_size
6658 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306659 status = psa_driver_wrapper_mac_compute(&attributes,
6660 zeros, sizeof(zeros),
6661 PSA_ALG_CMAC, input, input_len,
6662 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306663 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6664 128U,
6665 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306666 output_len);
6667 } else {
6668 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306669 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306670 }
6671 return status;
6672}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306673#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306674
6675static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306676 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306677 const uint8_t *data,
6678 size_t data_length)
6679{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306680 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306681 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6682 return PSA_ERROR_BAD_STATE;
6683 }
6684
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306685#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306686 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6687 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6688 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6689 pbkdf2->password,
6690 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306691 } else
6692#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6693#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6694 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306695 status = psa_pbkdf2_cmac_set_password(data, data_length,
6696 pbkdf2->password,
6697 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306698 } else
6699#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6700 {
6701 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306702 }
6703
6704 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6705
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306706 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306707}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306708
6709static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306710 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306711 psa_key_derivation_step_t step,
6712 const uint8_t *data,
6713 size_t data_length)
6714{
6715 switch (step) {
6716 case PSA_KEY_DERIVATION_INPUT_SALT:
6717 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6718 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306719 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306720 default:
6721 return PSA_ERROR_INVALID_ARGUMENT;
6722 }
6723}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306724#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306725
Gilles Peskineb8965192019-09-24 16:21:10 +02006726/** Check whether the given key type is acceptable for the given
6727 * input step of a key derivation.
6728 *
6729 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6730 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6731 * Both secret and non-secret inputs can alternatively have the type
6732 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6733 * that the input was passed as a buffer rather than via a key object.
6734 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006735static int psa_key_derivation_check_input_type(
6736 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006737 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006738{
Gilles Peskine449bd832023-01-11 14:50:10 +01006739 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006740 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 if (key_type == PSA_KEY_TYPE_DERIVE) {
6742 return PSA_SUCCESS;
6743 }
6744 if (key_type == PSA_KEY_TYPE_NONE) {
6745 return PSA_SUCCESS;
6746 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006747 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006748 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 if (key_type == PSA_KEY_TYPE_DERIVE) {
6750 return PSA_SUCCESS;
6751 }
6752 if (key_type == PSA_KEY_TYPE_NONE) {
6753 return PSA_SUCCESS;
6754 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006755 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006756 case PSA_KEY_DERIVATION_INPUT_LABEL:
6757 case PSA_KEY_DERIVATION_INPUT_SALT:
6758 case PSA_KEY_DERIVATION_INPUT_INFO:
6759 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006760 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6761 return PSA_SUCCESS;
6762 }
6763 if (key_type == PSA_KEY_TYPE_NONE) {
6764 return PSA_SUCCESS;
6765 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006766 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306767 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6768 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6769 return PSA_SUCCESS;
6770 }
6771 if (key_type == PSA_KEY_TYPE_DERIVE) {
6772 return PSA_SUCCESS;
6773 }
6774 if (key_type == PSA_KEY_TYPE_NONE) {
6775 return PSA_SUCCESS;
6776 }
6777 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006778 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006780}
6781
Janos Follathb80a94e2019-06-12 15:54:46 +01006782static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006783 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006784 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006785 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006786 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006787 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006788{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006789 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006791
Gilles Peskine449bd832023-01-11 14:50:10 +01006792 status = psa_key_derivation_check_input_type(step, key_type);
6793 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006794 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006796
Przemek Stekiel69c46792022-06-10 12:59:51 +02006797#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6799 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6800 step, data, data_length);
6801 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006802#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006803#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006804 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6805 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6806 step, data, data_length);
6807 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006808#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6809#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006810 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6811 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6812 step, data, data_length);
6813 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006814#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006815#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006816 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006817 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006818 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6819 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006820#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306821#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306822 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306823 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6824 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306825 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306826#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006827 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006828 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006829 (void) data;
6830 (void) data_length;
6831 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006833 }
6834
Gilles Peskine224b0d62019-09-23 18:13:17 +02006835exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 if (status != PSA_SUCCESS) {
6837 psa_key_derivation_abort(operation);
6838 }
6839 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006840}
6841
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306842static psa_status_t psa_key_derivation_input_integer_internal(
6843 psa_key_derivation_operation_t *operation,
6844 psa_key_derivation_step_t step,
6845 uint64_t value)
6846{
6847 psa_status_t status;
6848 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6849
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306850#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306851 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306852 status = psa_pbkdf2_set_input_cost(
6853 &operation->ctx.pbkdf2, step, value);
6854 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306855#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306856 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306857 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306858 (void) value;
6859 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306860 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306861 }
6862
6863 if (status != PSA_SUCCESS) {
6864 psa_key_derivation_abort(operation);
6865 }
6866 return status;
6867}
6868
Janos Follath51f4a0f2019-06-14 11:35:55 +01006869psa_status_t psa_key_derivation_input_bytes(
6870 psa_key_derivation_operation_t *operation,
6871 psa_key_derivation_step_t step,
6872 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006874{
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 return psa_key_derivation_input_internal(operation, step,
6876 PSA_KEY_TYPE_NONE,
6877 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006878}
6879
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306880psa_status_t psa_key_derivation_input_integer(
6881 psa_key_derivation_operation_t *operation,
6882 psa_key_derivation_step_t step,
6883 uint64_t value)
6884{
6885 return psa_key_derivation_input_integer_internal(operation, step, value);
6886}
6887
Janos Follath51f4a0f2019-06-14 11:35:55 +01006888psa_status_t psa_key_derivation_input_key(
6889 psa_key_derivation_operation_t *operation,
6890 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006891 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006892{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006893 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006894 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006895 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006896
Ronald Cron5c522922020-11-14 16:35:34 +01006897 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6899 if (status != PSA_SUCCESS) {
6900 psa_key_derivation_abort(operation);
6901 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006902 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006903
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306904 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6905 * permission to output to a key object. */
6906 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6907 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006908 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006910
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 status = psa_key_derivation_input_internal(operation,
6912 step, slot->attr.type,
6913 slot->key.data,
6914 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006915
Ryan Everett5ac6fa72024-02-14 17:11:36 +00006916 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006917
Gilles Peskine449bd832023-01-11 14:50:10 +01006918 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006919}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006920
6921
6922
6923/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006924/* Key agreement */
6925/****************************************************************/
6926
Gilles Peskine449bd832023-01-11 14:50:10 +01006927psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6928 const uint8_t *key_buffer,
6929 size_t key_buffer_size,
6930 psa_algorithm_t alg,
6931 const uint8_t *peer_key,
6932 size_t peer_key_length,
6933 uint8_t *shared_secret,
6934 size_t shared_secret_size,
6935 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006936{
Gilles Peskine449bd832023-01-11 14:50:10 +01006937 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006938#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006939 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6941 key_buffer_size, alg,
6942 peer_key, peer_key_length,
6943 shared_secret,
6944 shared_secret_size,
6945 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006946#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006947
6948#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6949 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006950 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006951 peer_key,
6952 peer_key_length,
6953 key_buffer,
6954 key_buffer_size,
6955 shared_secret,
6956 shared_secret_size,
6957 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006958#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6959
Gilles Peskine01d718c2018-09-18 12:01:02 +02006960 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006961 (void) attributes;
6962 (void) key_buffer;
6963 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006964 (void) peer_key;
6965 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006966 (void) shared_secret;
6967 (void) shared_secret_size;
6968 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006970 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006971}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006972
Aditya Deshpande17845b82022-10-13 17:21:01 +01006973/** Internal function for raw key agreement
6974 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006975 * to the driver's implementation if a driver is present.
6976 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006977 * (psa_key_agreement_raw_builtin).
6978 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006979static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6980 psa_key_slot_t *private_key,
6981 const uint8_t *peer_key,
6982 size_t peer_key_length,
6983 uint8_t *shared_secret,
6984 size_t shared_secret_size,
6985 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006986{
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6988 return PSA_ERROR_NOT_SUPPORTED;
6989 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006990
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006991 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006992 private_key->key.data,
6993 private_key->key.bytes, alg,
6994 peer_key, peer_key_length,
6995 shared_secret,
6996 shared_secret_size,
6997 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006998}
6999
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007000/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007001 * to potentially free embedded data structures and wipe confidential data.
7002 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007003static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7004 psa_key_derivation_step_t step,
7005 psa_key_slot_t *private_key,
7006 const uint8_t *peer_key,
7007 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007008{
7009 psa_status_t status;
Moritz Fischer967f8cd2024-03-02 00:55:06 +00007010 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
Gilles Peskine01d718c2018-09-18 12:01:02 +02007011 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007012 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007013
7014 /* Step 1: run the secret agreement algorithm to generate the shared
7015 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007016 status = psa_key_agreement_raw_internal(ka_alg,
7017 private_key,
7018 peer_key, peer_key_length,
7019 shared_secret,
7020 sizeof(shared_secret),
7021 &shared_secret_length);
7022 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007023 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007025
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007026 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007027 * the shared secret. A shared secret is permitted wherever a key
7028 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007029 status = psa_key_derivation_input_internal(operation, step,
7030 PSA_KEY_TYPE_DERIVE,
7031 shared_secret,
7032 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007033exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7035 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007036}
7037
Gilles Peskine449bd832023-01-11 14:50:10 +01007038psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7039 psa_key_derivation_step_t step,
7040 mbedtls_svc_key_id_t private_key,
7041 const uint8_t *peer_key,
7042 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007043{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007044 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007045 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007046 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007047
Gilles Peskine449bd832023-01-11 14:50:10 +01007048 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7049 return PSA_ERROR_INVALID_ARGUMENT;
7050 }
Ronald Cron5c522922020-11-14 16:35:34 +01007051 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7053 if (status != PSA_SUCCESS) {
7054 return status;
7055 }
7056 status = psa_key_agreement_internal(operation, step,
7057 slot,
7058 peer_key, peer_key_length);
7059 if (status != PSA_SUCCESS) {
7060 psa_key_derivation_abort(operation);
7061 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007062 /* If a private key has been added as SECRET, we allow the derived
7063 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007065 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007067 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007068
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007069 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007070
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007072}
7073
Gilles Peskine449bd832023-01-11 14:50:10 +01007074psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7075 mbedtls_svc_key_id_t private_key,
7076 const uint8_t *peer_key,
7077 size_t peer_key_length,
7078 uint8_t *output,
7079 size_t output_size,
7080 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007081{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007082 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007083 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007084 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007085 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007086
Gilles Peskine449bd832023-01-11 14:50:10 +01007087 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007088 status = PSA_ERROR_INVALID_ARGUMENT;
7089 goto exit;
7090 }
Ronald Cron5c522922020-11-14 16:35:34 +01007091 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7093 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007094 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007096
Gilles Peskine3e561302022-04-14 00:17:15 +02007097 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7098 * for the output size. The PSA specification only guarantees that this
7099 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7100 * but it might be nice to allow smaller buffers if the output fits.
7101 * At the time of writing this comment, with only ECDH implemented,
7102 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7103 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7104 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007105 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7107 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007108 status = PSA_ERROR_BUFFER_TOO_SMALL;
7109 goto exit;
7110 }
7111
Gilles Peskine449bd832023-01-11 14:50:10 +01007112 status = psa_key_agreement_raw_internal(alg, slot,
7113 peer_key, peer_key_length,
7114 output, output_size,
7115 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007116
7117exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007118 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007119 /* If an error happens and is not handled properly, the output
7120 * may be used as a key to protect sensitive data. Arrange for such
7121 * a key to be random, which is likely to result in decryption or
7122 * verification errors. This is better than filling the buffer with
7123 * some constant data such as zeros, which would result in the data
7124 * being protected with a reproducible, easily knowable key.
7125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007126 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007127 *output_length = output_size;
7128 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007129
Ryan Everetta103ec92024-01-31 13:59:57 +00007130 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007131
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007133}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007134
7135
Gilles Peskine30524eb2020-11-13 17:02:26 +01007136
Gilles Peskine86a440b2018-11-12 18:39:40 +01007137/****************************************************************/
7138/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007139/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007140
Gilles Peskine672a7712023-04-28 21:00:28 +02007141#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7142#include "entropy_poll.h"
7143#endif
7144
Gilles Peskine30524eb2020-11-13 17:02:26 +01007145/** Initialize the PSA random generator.
7146 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007147static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007148{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007149#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007151#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7152
Gilles Peskine30524eb2020-11-13 17:02:26 +01007153 /* Set default configuration if
7154 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007155 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007156 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 }
7158 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007159 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007160 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007161
Gilles Peskine449bd832023-01-11 14:50:10 +01007162 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007163#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7164 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7165 /* The PSA entropy injection feature depends on using NV seed as an entropy
7166 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007167 mbedtls_entropy_add_source(&rng->entropy,
7168 mbedtls_nv_seed_poll, NULL,
7169 MBEDTLS_ENTROPY_BLOCK_SIZE,
7170 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007171#endif
7172
Valerio Setti061d4e42024-02-26 12:52:44 +01007173 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007174#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007175}
7176
7177/** Deinitialize the PSA random generator.
7178 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007179static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007180{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007181#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007182 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007183#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01007184 mbedtls_psa_drbg_free(&rng->drbg);
Valerio Setti83e0de82023-11-24 12:13:05 +01007185 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007186#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007187}
7188
7189/** Seed the PSA random generator.
7190 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007191static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007192{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007193#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7194 /* Do nothing: the external RNG seeds itself. */
7195 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007197#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007198 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01007199 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01007200 drbg_seed, sizeof(drbg_seed) - 1);
7201 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007202#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007203}
7204
Gilles Peskine449bd832023-01-11 14:50:10 +01007205psa_status_t psa_generate_random(uint8_t *output,
7206 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007207{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007208 GUARD_MODULE_INITIALIZED;
7209
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007210#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7211
7212 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7214 output, output_size,
7215 &output_length);
7216 if (status != PSA_SUCCESS) {
7217 return status;
7218 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007219 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007220 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 if (output_length != output_size) {
7222 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7223 }
7224 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007225
7226#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7227
Gilles Peskine449bd832023-01-11 14:50:10 +01007228 while (output_size > 0) {
Valerio Setti718180c2024-02-27 11:58:39 +01007229 int ret = MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
Gilles Peskine71ddab92021-01-04 21:01:07 +01007230 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7232 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7233 output_size);
Valerio Setti718180c2024-02-27 11:58:39 +01007234#if defined(MBEDTLS_CTR_DRBG_C)
7235 ret = mbedtls_ctr_drbg_random(&global_data.rng.drbg, output, request_size);
7236#elif defined(MBEDTLS_HMAC_DRBG_C)
7237 ret = mbedtls_hmac_drbg_random(&global_data.rng.drbg, output, request_size);
7238#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 if (ret != 0) {
7240 return mbedtls_to_psa_error(ret);
7241 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007242 output_size -= request_size;
7243 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007244 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007245 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007246#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007247}
7248
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007249#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007250psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7251 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007252{
Paul Elliott600472b2024-02-23 17:26:58 +00007253 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 return PSA_ERROR_NOT_PERMITTED;
7255 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007256
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7258 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7259 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7260 return PSA_ERROR_INVALID_ARGUMENT;
7261 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007262
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007264}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007265#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007266
Ronald Cron3772afe2021-02-08 16:10:05 +01007267/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007268 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007269 * \param type The key type
7270 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007271 *
7272 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007273 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007274 * \retval #PSA_ERROR_INVALID_ARGUMENT
7275 * The size in bits of the key is not valid.
7276 * \retval #PSA_ERROR_NOT_SUPPORTED
7277 * The type and/or the size in bits of the key or the combination of
7278 * the two is not supported.
7279 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007280static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007282{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007283 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007284
Gilles Peskine449bd832023-01-11 14:50:10 +01007285 if (key_type_is_raw_bytes(type)) {
7286 status = psa_validate_unstructured_key_bit_size(type, bits);
7287 if (status != PSA_SUCCESS) {
7288 return status;
7289 }
7290 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007291#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007292 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7293 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7294 return PSA_ERROR_NOT_SUPPORTED;
7295 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007296 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007297 return PSA_ERROR_NOT_SUPPORTED;
7298 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007299
Ronald Cron01b2aba2020-10-05 09:42:02 +02007300 /* Accept only byte-aligned keys, for the same reasons as
7301 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007302 if (bits % 8 != 0) {
7303 return PSA_ERROR_NOT_SUPPORTED;
7304 }
7305 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007306#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007307
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007308#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007310 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007311 return PSA_SUCCESS;
7312 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007313#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007314
Valerio Settia55f0422023-07-10 15:34:41 +02007315#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007316 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007317 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007318 return PSA_ERROR_NOT_SUPPORTED;
7319 }
7320 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007321#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007322 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007323 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007324 }
7325
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007327}
7328
Ronald Cron55ed0592020-10-05 10:30:40 +02007329psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007330 const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007331 const psa_key_production_parameters_t *params, size_t params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007333{
7334 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007335 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007336
Gilles Peskine7a18f962024-02-12 16:48:11 +01007337 /* Only used for RSA */
Gilles Peskine092ce512024-02-20 12:31:24 +01007338 (void) params;
7339 (void) params_data_length;
Gilles Peskine7a18f962024-02-12 16:48:11 +01007340
Gilles Peskine449bd832023-01-11 14:50:10 +01007341 if (key_type_is_raw_bytes(type)) {
7342 status = psa_generate_random(key_buffer, key_buffer_size);
7343 if (status != PSA_SUCCESS) {
7344 return status;
7345 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007346
David Brown12ca5032021-02-11 11:02:00 -07007347#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007348 if (type == PSA_KEY_TYPE_DES) {
7349 psa_des_set_key_parity(key_buffer, key_buffer_size);
7350 }
David Brown8107e312021-02-12 08:08:46 -07007351#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007352 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007353
Valerio Setti76df8c12023-07-11 14:11:28 +02007354#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007355 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskinee22f6a92024-02-26 15:45:33 +01007356 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskine4c32b692024-02-16 00:11:09 +01007357 params, params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007358 key_buffer,
7359 key_buffer_size,
7360 key_buffer_length);
7361 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007362#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007363
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007364#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7366 return mbedtls_psa_ecp_generate_key(attributes,
7367 key_buffer,
7368 key_buffer_size,
7369 key_buffer_length);
7370 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007371#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007372
Valerio Settia55f0422023-07-10 15:34:41 +02007373#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007374 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7375 return mbedtls_psa_ffdh_generate_key(attributes,
7376 key_buffer,
7377 key_buffer_size,
7378 key_buffer_length);
7379 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007380#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007381 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 (void) key_buffer_length;
7383 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007384 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007385
Gilles Peskine449bd832023-01-11 14:50:10 +01007386 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007387}
Darryl Green0c6575a2018-11-07 16:05:30 +00007388
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007389psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007390 const psa_key_production_parameters_t *params,
7391 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007392 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007393{
7394 psa_status_t status;
7395 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007396 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007397 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007398
Ronald Cron81709fc2020-11-14 12:10:32 +01007399 *key = MBEDTLS_SVC_KEY_ID_INIT;
7400
Gilles Peskine0f84d622019-09-12 19:03:13 +02007401 /* Reject any attempt to create a zero-length key so that we don't
7402 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007403 if (psa_get_key_bits(attributes) == 0) {
7404 return PSA_ERROR_INVALID_ARGUMENT;
7405 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007406
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007407 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007408 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007409 return PSA_ERROR_INVALID_ARGUMENT;
7410 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007411
Gilles Peskine7a18f962024-02-12 16:48:11 +01007412#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007413 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +01007414 if (params->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01007415 return PSA_ERROR_INVALID_ARGUMENT;
7416 }
7417 } else
7418#endif
Gilles Peskine092ce512024-02-20 12:31:24 +01007419 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007420 return PSA_ERROR_INVALID_ARGUMENT;
7421 }
7422
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7424 &slot, &driver);
7425 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007426 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 }
Gilles Peskine11792082019-08-06 18:36:36 +02007428
Ronald Cron977c2472020-10-13 08:32:21 +02007429 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307430 * storage ( thus not in the case of generating a key in a secure element
7431 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7432 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007434 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007436 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007437 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007439 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007440 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007441
7442 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007443 attributes->type,
7444 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007446 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 attributes, &key_buffer_size);
7448 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007449 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 }
Ronald Cron977c2472020-10-13 08:32:21 +02007451 }
Ronald Cron977c2472020-10-13 08:32:21 +02007452
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7454 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007455 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007456 }
Ronald Cron977c2472020-10-13 08:32:21 +02007457 }
7458
Gilles Peskine449bd832023-01-11 14:50:10 +01007459 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007460 params, params_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01007461 slot->key.data, slot->key.bytes,
7462 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01007463 if (status != PSA_SUCCESS) {
7464 psa_remove_key_data_from_memory(slot);
7465 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007466
Gilles Peskine11792082019-08-06 18:36:36 +02007467exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 if (status == PSA_SUCCESS) {
7469 status = psa_finish_key_creation(slot, driver, key);
7470 }
7471 if (status != PSA_SUCCESS) {
7472 psa_fail_key_creation(slot, driver);
7473 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007474
Gilles Peskine449bd832023-01-11 14:50:10 +01007475 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007476}
7477
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007478psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7479 mbedtls_svc_key_id_t *key)
7480{
7481 return psa_generate_key_ext(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007482 &default_production_parameters, 0,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007483 key);
7484}
7485
Gilles Peskinee59236f2018-01-27 23:32:46 +01007486/****************************************************************/
7487/* Module setup */
7488/****************************************************************/
7489
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007490#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007491psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007492 void (* entropy_init)(mbedtls_entropy_context *ctx),
7493 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007494{
Paul Elliott8e151532024-02-23 17:35:29 +00007495 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7496
7497#if defined(MBEDTLS_THREADING_C)
7498 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
7499#endif /* defined(MBEDTLS_THREADING_C) */
7500
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
Paul Elliott8e151532024-02-23 17:35:29 +00007502 status = PSA_ERROR_BAD_STATE;
7503 } else {
7504 global_data.rng.entropy_init = entropy_init;
7505 global_data.rng.entropy_free = entropy_free;
7506 status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01007507 }
Paul Elliott8e151532024-02-23 17:35:29 +00007508
7509#if defined(MBEDTLS_THREADING_C)
7510 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
7511#endif /* defined(MBEDTLS_THREADING_C) */
7512
7513 return status;
Gilles Peskine5e769522018-11-20 21:59:56 +01007514}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007515#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007516
Gilles Peskine449bd832023-01-11 14:50:10 +01007517void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007518{
Paul Elliott47cee8e2024-03-08 21:38:02 +00007519
Paul Elliott600472b2024-02-23 17:26:58 +00007520#if defined(MBEDTLS_THREADING_C)
7521 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
7522#endif /* defined(MBEDTLS_THREADING_C) */
7523
Paul Elliott47cee8e2024-03-08 21:38:02 +00007524 /* Nothing to do to free transaction. */
7525 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED) {
7526 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
Valerio Setti83e0de82023-11-24 12:13:05 +01007527 }
Paul Elliott600472b2024-02-23 17:26:58 +00007528
Paul Elliott47cee8e2024-03-08 21:38:02 +00007529 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED) {
7530 psa_wipe_all_key_slots();
7531 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
7532 }
Ronald Cron9ba76912021-04-10 16:57:30 +02007533
Paul Elliott600472b2024-02-23 17:26:58 +00007534#if defined(MBEDTLS_THREADING_C)
7535 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
7536#endif /* defined(MBEDTLS_THREADING_C) */
7537
Paul Elliott47cee8e2024-03-08 21:38:02 +00007538#if defined(MBEDTLS_THREADING_C)
7539 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
7540#endif /* defined(MBEDTLS_THREADING_C) */
7541
7542 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7543 mbedtls_psa_random_free(&global_data.rng);
7544 }
7545 global_data.rng_state = RNG_NOT_INITIALIZED;
7546 mbedtls_platform_zeroize(&global_data.rng, sizeof(global_data.rng));
7547
7548#if defined(MBEDTLS_THREADING_C)
7549 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
7550#endif /* defined(MBEDTLS_THREADING_C) */
7551
7552#if defined(MBEDTLS_THREADING_C)
7553 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
7554#endif /* defined(MBEDTLS_THREADING_C) */
7555
Ronald Cron9ba76912021-04-10 16:57:30 +02007556 /* Terminate drivers */
Paul Elliott47cee8e2024-03-08 21:38:02 +00007557 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED) {
7558 psa_driver_wrapper_free();
7559 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
7560 }
7561
7562#if defined(MBEDTLS_THREADING_C)
7563 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
7564#endif /* defined(MBEDTLS_THREADING_C) */
7565
Gilles Peskinee59236f2018-01-27 23:32:46 +01007566}
7567
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007568#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7569/** Recover a transaction that was interrupted by a power failure.
7570 *
7571 * This function is called during initialization, before psa_crypto_init()
7572 * returns. If this function returns a failure status, the initialization
7573 * fails.
7574 */
7575static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007576 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007577{
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007579 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7580 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 /* TODO - fall through to the failure case until this
7582 * is implemented.
7583 * https://github.com/ARMmbed/mbed-crypto/issues/218
7584 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007585 default:
7586 /* We found an unsupported transaction in the storage.
7587 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007588 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007589 }
7590}
7591#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7592
Paul Elliott47cee8e2024-03-08 21:38:02 +00007593static psa_status_t mbedtls_psa_crypto_init_subsystem(mbedtls_psa_crypto_subsystem subsystem)
7594{
7595 psa_status_t status = PSA_SUCCESS;
7596 uint8_t driver_wrappers_initialized = 0;
7597
7598 switch (subsystem) {
7599 case PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS:
7600
7601#if defined(MBEDTLS_THREADING_C)
7602 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
7603#endif /* defined(MBEDTLS_THREADING_C) */
7604
7605 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED)) {
7606 /* Init drivers */
7607 status = psa_driver_wrapper_init();
7608
7609 /* Drivers need shutdown regardless of startup errors. */
7610 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
7611
7612
7613 }
7614#if defined(MBEDTLS_THREADING_C)
7615 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7616 &mbedtls_threading_psa_globaldata_mutex));
7617#endif /* defined(MBEDTLS_THREADING_C) */
7618
7619 break;
7620
7621 case PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS:
7622
7623#if defined(MBEDTLS_THREADING_C)
7624 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
7625#endif /* defined(MBEDTLS_THREADING_C) */
7626
7627 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED)) {
7628 status = psa_initialize_key_slots();
7629
7630 /* Need to wipe keys even if initialization fails. */
7631 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
7632
7633 }
7634#if defined(MBEDTLS_THREADING_C)
7635 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7636 &mbedtls_threading_psa_globaldata_mutex));
7637#endif /* defined(MBEDTLS_THREADING_C) */
7638
7639 break;
7640
7641 case PSA_CRYPTO_SUBSYSTEM_RNG:
7642
7643#if defined(MBEDTLS_THREADING_C)
7644 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
7645#endif /* defined(MBEDTLS_THREADING_C) */
7646
7647 driver_wrappers_initialized =
7648 (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED);
7649
7650#if defined(MBEDTLS_THREADING_C)
7651 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7652 &mbedtls_threading_psa_globaldata_mutex));
7653#endif /* defined(MBEDTLS_THREADING_C) */
7654
7655 /* Need to use separate mutex here, as initialisation can require
7656 * testing of init flags, which requires locking the global data
7657 * mutex. */
7658#if defined(MBEDTLS_THREADING_C)
7659 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex));
7660#endif /* defined(MBEDTLS_THREADING_C) */
7661
7662 /* Initialize and seed the random generator. */
7663 if (global_data.rng_state == RNG_NOT_INITIALIZED && driver_wrappers_initialized) {
7664 mbedtls_psa_random_init(&global_data.rng);
7665 global_data.rng_state = RNG_INITIALIZED;
7666
7667 status = mbedtls_psa_random_seed(&global_data.rng);
7668 if (status == PSA_SUCCESS) {
7669 global_data.rng_state = RNG_SEEDED;
7670 }
7671 }
7672
7673#if defined(MBEDTLS_THREADING_C)
7674 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7675 &mbedtls_threading_psa_rngdata_mutex));
7676#endif /* defined(MBEDTLS_THREADING_C) */
7677
7678
7679 break;
7680
7681 case PSA_CRYPTO_SUBSYSTEM_TRANSACTION:
7682
7683#if defined(MBEDTLS_THREADING_C)
7684 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
7685#endif /* defined(MBEDTLS_THREADING_C) */
7686
7687 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED)) {
7688#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7689 status = psa_crypto_load_transaction();
7690 if (status == PSA_SUCCESS) {
7691 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7692 if (status == PSA_SUCCESS) {
7693 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
7694 }
7695 status = psa_crypto_stop_transaction();
7696 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
7697 /* There's no transaction to complete. It's all good. */
7698 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
7699 status = PSA_SUCCESS;
7700 }
7701#else /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
7702 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
7703 status = PSA_SUCCESS;
7704#endif /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
7705 }
7706
7707#if defined(MBEDTLS_THREADING_C)
7708 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7709 &mbedtls_threading_psa_globaldata_mutex));
7710#endif /* defined(MBEDTLS_THREADING_C) */
7711
7712 break;
7713
7714 default:
7715 status = PSA_ERROR_CORRUPTION_DETECTED;
7716 }
7717
7718 /* Exit label only required when using threading macros. */
7719#if defined(MBEDTLS_THREADING_C)
7720exit:
7721#endif /* defined(MBEDTLS_THREADING_C) */
7722
7723 return status;
7724}
7725
Gilles Peskine449bd832023-01-11 14:50:10 +01007726psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007727{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007728 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007729
Paul Elliott47cee8e2024-03-08 21:38:02 +00007730 /* Double initialization is explicitly allowed. Early out if everything is
7731 * done. */
7732 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007733 return PSA_SUCCESS;
7734 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007735
Paul Elliott47cee8e2024-03-08 21:38:02 +00007736 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS);
Valerio Setti402cfba2023-11-13 10:24:32 +01007737 if (status != PSA_SUCCESS) {
7738 goto exit;
7739 }
7740
Paul Elliott47cee8e2024-03-08 21:38:02 +00007741 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS);
Gilles Peskine449bd832023-01-11 14:50:10 +01007742 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007743 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007744 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007745
Paul Elliott47cee8e2024-03-08 21:38:02 +00007746 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_RNG);
7747 if (status != PSA_SUCCESS) {
7748 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02007749 }
Gilles Peskinefc762652019-07-22 19:30:34 +02007750
Paul Elliott47cee8e2024-03-08 21:38:02 +00007751 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_TRANSACTION);
Gilles Peskinee59236f2018-01-27 23:32:46 +01007752
7753exit:
Paul Elliott600472b2024-02-23 17:26:58 +00007754
Gilles Peskine449bd832023-01-11 14:50:10 +01007755 if (status != PSA_SUCCESS) {
7756 mbedtls_psa_crypto_free();
7757 }
Paul Elliott47cee8e2024-03-08 21:38:02 +00007758
Gilles Peskine449bd832023-01-11 14:50:10 +01007759 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007760}
7761
Yanray Wangffc3c482023-07-11 12:01:04 +08007762#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007763psa_status_t psa_crypto_driver_pake_get_password_len(
7764 const psa_crypto_driver_pake_inputs_t *inputs,
7765 size_t *password_len)
7766{
7767 if (inputs->password_len == 0) {
7768 return PSA_ERROR_BAD_STATE;
7769 }
7770
7771 *password_len = inputs->password_len;
7772
7773 return PSA_SUCCESS;
7774}
7775
7776psa_status_t psa_crypto_driver_pake_get_password(
7777 const psa_crypto_driver_pake_inputs_t *inputs,
7778 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7779{
7780 if (inputs->password_len == 0) {
7781 return PSA_ERROR_BAD_STATE;
7782 }
7783
7784 if (buffer_size < inputs->password_len) {
7785 return PSA_ERROR_BUFFER_TOO_SMALL;
7786 }
7787
7788 memcpy(buffer, inputs->password, inputs->password_len);
7789 *buffer_length = inputs->password_len;
7790
7791 return PSA_SUCCESS;
7792}
7793
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007794psa_status_t psa_crypto_driver_pake_get_user_len(
7795 const psa_crypto_driver_pake_inputs_t *inputs,
7796 size_t *user_len)
7797{
7798 if (inputs->user_len == 0) {
7799 return PSA_ERROR_BAD_STATE;
7800 }
7801
7802 *user_len = inputs->user_len;
7803
7804 return PSA_SUCCESS;
7805}
7806
7807psa_status_t psa_crypto_driver_pake_get_user(
7808 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007809 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007810{
7811 if (inputs->user_len == 0) {
7812 return PSA_ERROR_BAD_STATE;
7813 }
7814
Przemek Stekielc0e62502023-03-14 11:49:36 +01007815 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007816 return PSA_ERROR_BUFFER_TOO_SMALL;
7817 }
7818
Przemek Stekielc0e62502023-03-14 11:49:36 +01007819 memcpy(user_id, inputs->user, inputs->user_len);
7820 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007821
7822 return PSA_SUCCESS;
7823}
7824
7825psa_status_t psa_crypto_driver_pake_get_peer_len(
7826 const psa_crypto_driver_pake_inputs_t *inputs,
7827 size_t *peer_len)
7828{
7829 if (inputs->peer_len == 0) {
7830 return PSA_ERROR_BAD_STATE;
7831 }
7832
7833 *peer_len = inputs->peer_len;
7834
7835 return PSA_SUCCESS;
7836}
7837
7838psa_status_t psa_crypto_driver_pake_get_peer(
7839 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007840 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007841{
7842 if (inputs->peer_len == 0) {
7843 return PSA_ERROR_BAD_STATE;
7844 }
7845
Przemek Stekielc0e62502023-03-14 11:49:36 +01007846 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007847 return PSA_ERROR_BUFFER_TOO_SMALL;
7848 }
7849
Przemek Stekielc0e62502023-03-14 11:49:36 +01007850 memcpy(peer_id, inputs->peer, inputs->peer_len);
7851 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007852
7853 return PSA_SUCCESS;
7854}
7855
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007856psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7857 const psa_crypto_driver_pake_inputs_t *inputs,
7858 psa_pake_cipher_suite_t *cipher_suite)
7859{
7860 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7861 return PSA_ERROR_BAD_STATE;
7862 }
7863
7864 *cipher_suite = inputs->cipher_suite;
7865
7866 return PSA_SUCCESS;
7867}
7868
Neil Armstronga7d08c32022-06-01 18:21:20 +02007869psa_status_t psa_pake_setup(
7870 psa_pake_operation_t *operation,
7871 const psa_pake_cipher_suite_t *cipher_suite)
7872{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007873 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007874
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007875 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007876 status = PSA_ERROR_BAD_STATE;
7877 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007878 }
7879
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007880 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007881 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007882 status = PSA_ERROR_INVALID_ARGUMENT;
7883 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007884 }
7885
Przemek Stekiel51eac532022-12-07 11:04:51 +01007886 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7887
Przemek Stekiele12ed362022-12-21 12:54:46 +01007888 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007889 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7890 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007891 operation->data.inputs.cipher_suite = *cipher_suite;
7892
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007893#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007894 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007895 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007896 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007897
David Horstmann16f01512023-06-14 17:21:07 +01007898 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007899 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007900 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007901#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007902 {
7903 status = PSA_ERROR_NOT_SUPPORTED;
7904 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007905 }
7906
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007907 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7908
Przemek Stekiel51eac532022-12-07 11:04:51 +01007909 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007910exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007911 psa_pake_abort(operation);
7912 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007913}
7914
7915psa_status_t psa_pake_set_password_key(
7916 psa_pake_operation_t *operation,
7917 mbedtls_svc_key_id_t password)
7918{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007919 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007920 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7921 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007922 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007923
Przemek Stekiel51eac532022-12-07 11:04:51 +01007924 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007925 status = PSA_ERROR_BAD_STATE;
7926 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007927 }
7928
Przemek Stekiel6c764412022-11-22 14:05:12 +01007929 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7930 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007931 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007932 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007933 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007934 }
7935
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007936 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007937
Przemek Stekiel51eac532022-12-07 11:04:51 +01007938 if (type != PSA_KEY_TYPE_PASSWORD &&
7939 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7940 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007941 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007942 }
7943
Przemek Stekiel51eac532022-12-07 11:04:51 +01007944 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7945 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007946 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007947 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007948 }
7949
7950 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7951 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01007952 operation->data.inputs.attributes = slot->attr;
7953
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007954exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007955 if (status != PSA_SUCCESS) {
7956 psa_pake_abort(operation);
7957 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00007958 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007959 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007960}
7961
7962psa_status_t psa_pake_set_user(
7963 psa_pake_operation_t *operation,
7964 const uint8_t *user_id,
7965 size_t user_id_len)
7966{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007967 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007968
7969 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007970 status = PSA_ERROR_BAD_STATE;
7971 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007972 }
7973
Przemek Stekiel51eac532022-12-07 11:04:51 +01007974 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007975 status = PSA_ERROR_INVALID_ARGUMENT;
7976 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007977 }
7978
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007979 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007980 status = PSA_ERROR_BAD_STATE;
7981 goto exit;
7982 }
7983
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007984 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7985 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007986 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7987 goto exit;
7988 }
7989
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007990 memcpy(operation->data.inputs.user, user_id, user_id_len);
7991 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007992
7993 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007994exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007995 psa_pake_abort(operation);
7996 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007997}
7998
7999psa_status_t psa_pake_set_peer(
8000 psa_pake_operation_t *operation,
8001 const uint8_t *peer_id,
8002 size_t peer_id_len)
8003{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008004 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008005
8006 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008007 status = PSA_ERROR_BAD_STATE;
8008 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008009 }
8010
Przemek Stekiel51eac532022-12-07 11:04:51 +01008011 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008012 status = PSA_ERROR_INVALID_ARGUMENT;
8013 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008014 }
8015
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008016 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008017 status = PSA_ERROR_BAD_STATE;
8018 goto exit;
8019 }
8020
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008021 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8022 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008023 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8024 goto exit;
8025 }
8026
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008027 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8028 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008029
8030 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008031exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008032 psa_pake_abort(operation);
8033 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008034}
8035
8036psa_status_t psa_pake_set_role(
8037 psa_pake_operation_t *operation,
8038 psa_pake_role_t role)
8039{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008040 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008041
Przemek Stekiel51eac532022-12-07 11:04:51 +01008042 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008043 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008044 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008045 }
8046
Przemek Stekiel09104b82023-03-06 14:11:51 +01008047 switch (operation->alg) {
8048#if defined(PSA_WANT_ALG_JPAKE)
8049 case PSA_ALG_JPAKE:
8050 if (role == PSA_PAKE_ROLE_NONE) {
8051 return PSA_SUCCESS;
8052 }
8053 status = PSA_ERROR_INVALID_ARGUMENT;
8054 break;
8055#endif
8056 default:
8057 (void) role;
8058 status = PSA_ERROR_NOT_SUPPORTED;
8059 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008060 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008061exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008062 psa_pake_abort(operation);
8063 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008064}
8065
David Horstmanne7f21e62023-05-12 18:17:21 +01008066/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008067#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008068static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008069 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008070{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008071 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008072 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008073 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008074
David Horstmann024e5c52023-06-14 15:48:21 +01008075 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008076 is_x1 = (stage->outputs < 1);
8077 } else {
8078 is_x1 = (stage->inputs < 1);
8079 }
8080
David Horstmann74a3d8c2023-06-14 18:28:19 +01008081 key_share_step = is_x1 ?
8082 PSA_JPAKE_X1_STEP_KEY_SHARE :
8083 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008084 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008085 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8086 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8087 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8088 } else {
8089 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008090 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008091 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008092}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008093#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008094
Przemek Stekiel2797d372022-12-22 11:19:22 +01008095static psa_status_t psa_pake_complete_inputs(
8096 psa_pake_operation_t *operation)
8097{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008098 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008099 /* Create copy of the inputs on stack as inputs share memory
8100 with the driver context which will be setup by the driver. */
8101 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008102
Przemek Stekielfde11282023-03-13 16:06:09 +01008103 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008104 return PSA_ERROR_BAD_STATE;
8105 }
8106
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008107 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008108 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8109 return PSA_ERROR_BAD_STATE;
8110 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008111 }
8112
Przemek Stekiel18620a32023-01-17 16:34:52 +01008113 /* Clear driver context */
8114 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8115
8116 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008117
8118 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008119 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008120
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008121 /* User and peer are translated to role. */
8122 mbedtls_free(inputs.user);
8123 mbedtls_free(inputs.peer);
8124
Przemek Stekiel2797d372022-12-22 11:19:22 +01008125 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008126#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008127 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008128 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008129 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008130#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008131 {
8132 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008133 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008134 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008135 return status;
8136}
8137
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008138#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008139static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008140 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008141 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008142 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008143{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008144 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8145 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8146 step != PSA_PAKE_STEP_ZK_PROOF) {
8147 return PSA_ERROR_INVALID_ARGUMENT;
8148 }
8149
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008150 psa_jpake_computation_stage_t *computation_stage =
8151 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008152
David Horstmann5da95602023-06-08 15:37:12 +01008153 if (computation_stage->round != PSA_JPAKE_FIRST &&
8154 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008155 return PSA_ERROR_BAD_STATE;
8156 }
8157
David Horstmanne7f21e62023-05-12 18:17:21 +01008158 /* Check that the step we are given is the one we were expecting */
8159 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008160 return PSA_ERROR_BAD_STATE;
8161 }
8162
David Horstmanne7f21e62023-05-12 18:17:21 +01008163 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8164 computation_stage->inputs == 0 &&
8165 computation_stage->outputs == 0) {
8166 /* Start of the round, so function decides whether we are inputting
8167 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008168 computation_stage->io_mode = io_mode;
8169 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008170 /* Middle of the round so the mode we are in must match the function
8171 * called by the user */
8172 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008173 }
8174
8175 return PSA_SUCCESS;
8176}
8177
David Horstmanne7f21e62023-05-12 18:17:21 +01008178static psa_status_t psa_jpake_epilogue(
8179 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008180 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008181{
David Horstmanne7f21e62023-05-12 18:17:21 +01008182 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008183 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008184
David Horstmanne7f21e62023-05-12 18:17:21 +01008185 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8186 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008187 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008188 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008189 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008190 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008191 }
8192 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008193 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008194 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008195 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008196 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008197 }
8198 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008199 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8200 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008201 /* End of a round, move to the next round */
8202 stage->inputs = 0;
8203 stage->outputs = 0;
8204 stage->round++;
8205 }
8206 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008207 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008208 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008209 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008210 return PSA_SUCCESS;
8211}
David Horstmanne7f21e62023-05-12 18:17:21 +01008212
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008213#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008214
Neil Armstronga7d08c32022-06-01 18:21:20 +02008215psa_status_t psa_pake_output(
8216 psa_pake_operation_t *operation,
8217 psa_pake_step_t step,
8218 uint8_t *output,
8219 size_t output_size,
8220 size_t *output_length)
8221{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008222 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008223 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008224 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008225
8226 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008227 status = psa_pake_complete_inputs(operation);
8228 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008229 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008230 }
8231 }
8232
8233 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008234 status = PSA_ERROR_BAD_STATE;
8235 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008236 }
8237
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008238 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008239 status = PSA_ERROR_INVALID_ARGUMENT;
8240 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008241 }
8242
Przemek Stekiele12ed362022-12-21 12:54:46 +01008243 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008244#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008245 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008246 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008247 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008248 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008249 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008250 driver_step = convert_jpake_computation_stage_to_driver_step(
8251 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008252 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008253#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008254 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008255 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008256 status = PSA_ERROR_NOT_SUPPORTED;
8257 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008258 }
8259
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008260 status = psa_driver_wrapper_pake_output(operation, driver_step,
8261 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008262
8263 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008264 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008265 }
8266
8267 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008268#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008269 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008270 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008271 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008272 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008273 }
8274 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008275#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008276 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008277 status = PSA_ERROR_NOT_SUPPORTED;
8278 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008279 }
8280
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008281 return PSA_SUCCESS;
8282exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008283 psa_pake_abort(operation);
8284 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008285}
8286
8287psa_status_t psa_pake_input(
8288 psa_pake_operation_t *operation,
8289 psa_pake_step_t step,
8290 const uint8_t *input,
8291 size_t input_length)
8292{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008293 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008294 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008295 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8296 operation->primitive,
8297 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008298
8299 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008300 status = psa_pake_complete_inputs(operation);
8301 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008302 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008303 }
8304 }
8305
8306 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008307 status = PSA_ERROR_BAD_STATE;
8308 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008309 }
8310
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008311 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008312 status = PSA_ERROR_INVALID_ARGUMENT;
8313 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008314 }
8315
Przemek Stekiele12ed362022-12-21 12:54:46 +01008316 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008317#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008318 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008319 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008320 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008321 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008322 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008323 driver_step = convert_jpake_computation_stage_to_driver_step(
8324 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008325 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008326#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008327 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008328 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008329 status = PSA_ERROR_NOT_SUPPORTED;
8330 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008331 }
8332
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008333 status = psa_driver_wrapper_pake_input(operation, driver_step,
8334 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008335
8336 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008337 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008338 }
8339
8340 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008341#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008342 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008343 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008344 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008345 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008346 }
8347 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008348#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008349 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008350 status = PSA_ERROR_NOT_SUPPORTED;
8351 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008352 }
8353
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008354 return PSA_SUCCESS;
8355exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008356 psa_pake_abort(operation);
8357 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008358}
8359
8360psa_status_t psa_pake_get_implicit_key(
8361 psa_pake_operation_t *operation,
8362 psa_key_derivation_operation_t *output)
8363{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008365 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008366 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008367 size_t shared_key_len = 0;
8368
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008369 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008370 status = PSA_ERROR_BAD_STATE;
8371 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008372 }
8373
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008374#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008375 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008376 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008377 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008378 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008379 status = PSA_ERROR_BAD_STATE;
8380 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008381 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008382 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008383#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008384 {
8385 status = PSA_ERROR_NOT_SUPPORTED;
8386 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008387 }
8388
Przemek Stekiel0c781802022-11-29 14:53:13 +01008389 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8390 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008391 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008392 &shared_key_len);
8393
8394 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008395 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008396 }
8397
8398 status = psa_key_derivation_input_bytes(output,
8399 PSA_KEY_DERIVATION_INPUT_SECRET,
8400 shared_key,
8401 shared_key_len);
8402
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008403 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008404exit:
8405 abort_status = psa_pake_abort(operation);
8406 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008407}
8408
8409psa_status_t psa_pake_abort(
8410 psa_pake_operation_t *operation)
8411{
Przemek Stekield69dca92023-01-31 19:59:20 +01008412 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008413
Przemek Stekield69dca92023-01-31 19:59:20 +01008414 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008415 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008416 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008417
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008418 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8419 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008420 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008421 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008422 }
8423 if (operation->data.inputs.user != NULL) {
8424 mbedtls_free(operation->data.inputs.user);
8425 }
8426 if (operation->data.inputs.peer != NULL) {
8427 mbedtls_free(operation->data.inputs.peer);
8428 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008429 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008430 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008431
Przemek Stekield69dca92023-01-31 19:59:20 +01008432 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008433}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008434#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008435
Gilles Peskinee59236f2018-01-27 23:32:46 +01008436#endif /* MBEDTLS_PSA_CRYPTO_C */