blob: 373918a885c1b2d33b3eac3d3d79528cb5de041a [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinee59236f2018-01-27 23:32:46 +01007 */
8
Gilles Peskinedb09ef62020-06-03 01:43:33 +02009#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010010#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010011
12#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030013
John Durkop07cc04a2020-11-16 22:08:34 -080014#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
15#include "check_crypto_config.h"
16#endif
17
Gilles Peskinee59236f2018-01-27 23:32:46 +010018#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010019#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010020
Ronald Crond6d28882020-12-14 14:56:02 +010021#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010022#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010023#include "psa_crypto_invasive.h"
Xiaokang Qiane9c39c42023-08-09 08:50:19 +000024#include "psa_crypto_driver_wrappers.h"
Xiaokang Qianfe9666b2023-09-11 10:36:20 +000025#include "psa_crypto_driver_wrappers_no_static.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010027#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010028#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010029#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010031#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020033#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020034#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskineb2b64d32020-12-14 16:43:58 +010040#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010041
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include <stdlib.h>
43#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010046#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020047#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000048#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020049#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010050#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020051#include "mbedtls/chacha20.h"
52#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/cipher.h"
54#include "mbedtls/ccm.h"
55#include "mbedtls/cmac.h"
Dave Rodgman78701152023-08-29 14:20:18 +010056#include "mbedtls/constant_time.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020058#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010059#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010060#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/error.h"
62#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/md5.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010064#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000065#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010066#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000067#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/sha1.h"
71#include "mbedtls/sha256.h"
72#include "mbedtls/sha512.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010073#include "mbedtls/psa_util.h"
Paul Elliott600472b2024-02-23 17:26:58 +000074#include "mbedtls/threading.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020076#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
77 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
78 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020079#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020080#endif
81
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010082/****************************************************************/
83/* Global data, support functions and library management */
84/****************************************************************/
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020087{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020089}
90
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010091/* Values for psa_global_data_t::rng_state */
92#define RNG_NOT_INITIALIZED 0
93#define RNG_INITIALIZED 1
94#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010095
Paul Elliott0db6a902024-03-15 13:48:20 +000096/* IDs for PSA crypto subsystems. Starts at 1 to catch potential uninitialized
97 * variables as arguments. */
Paul Elliott47cee8e2024-03-08 21:38:02 +000098typedef enum {
Paul Elliott0db6a902024-03-15 13:48:20 +000099 PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS = 1,
Paul Elliott47cee8e2024-03-08 21:38:02 +0000100 PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS,
101 PSA_CRYPTO_SUBSYSTEM_RNG,
102 PSA_CRYPTO_SUBSYSTEM_TRANSACTION,
103} mbedtls_psa_crypto_subsystem;
104
105#define PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED 0x01
106#define PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED 0x02
107#define PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED 0x04
108
109#define PSA_CRYPTO_SUBSYSTEM_ALL_INITIALISED ( \
110 PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED | \
111 PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED | \
112 PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED)
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +0100115 uint8_t initialized;
116 uint8_t rng_state;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100117 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118} psa_global_data_t;
119
120static psa_global_data_t global_data;
121
Paul Elliott600472b2024-02-23 17:26:58 +0000122static uint8_t psa_get_initialized(void)
123{
124 uint8_t initialized;
125
126#if defined(MBEDTLS_THREADING_C)
Paul Elliott47cee8e2024-03-08 21:38:02 +0000127 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
128#endif /* defined(MBEDTLS_THREADING_C) */
129
130 initialized = global_data.rng_state == RNG_SEEDED;
131
132#if defined(MBEDTLS_THREADING_C)
133 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
134#endif /* defined(MBEDTLS_THREADING_C) */
135
136#if defined(MBEDTLS_THREADING_C)
Paul Elliott600472b2024-02-23 17:26:58 +0000137 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
138#endif /* defined(MBEDTLS_THREADING_C) */
139
Paul Elliott47cee8e2024-03-08 21:38:02 +0000140 initialized =
141 (initialized && (global_data.initialized == PSA_CRYPTO_SUBSYSTEM_ALL_INITIALISED));
Paul Elliott600472b2024-02-23 17:26:58 +0000142
143#if defined(MBEDTLS_THREADING_C)
144 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
145#endif /* defined(MBEDTLS_THREADING_C) */
146
147 return initialized;
148}
149
Paul Elliott35816522024-02-23 17:47:42 +0000150static uint8_t psa_get_drivers_initialized(void)
151{
152 uint8_t initialized;
153
154#if defined(MBEDTLS_THREADING_C)
155 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
156#endif /* defined(MBEDTLS_THREADING_C) */
157
Paul Elliott47cee8e2024-03-08 21:38:02 +0000158 initialized = (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED) != 0;
Paul Elliott35816522024-02-23 17:47:42 +0000159
160#if defined(MBEDTLS_THREADING_C)
161 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
162#endif /* defined(MBEDTLS_THREADING_C) */
163
164 return initialized;
165}
166
itayzafrir0adf0fc2018-09-06 16:24:41 +0300167#define GUARD_MODULE_INITIALIZED \
Paul Elliott600472b2024-02-23 17:26:58 +0000168 if (psa_get_initialized() == 0) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300170
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100171int psa_can_do_hash(psa_algorithm_t hash_alg)
172{
173 (void) hash_alg;
Paul Elliott35816522024-02-23 17:47:42 +0000174 return psa_get_drivers_initialized();
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100175}
Valerio Settic6f004f2023-12-12 11:27:36 +0100176
Valerio Setti1fff4f22023-12-28 14:19:34 +0100177int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
Valerio Settic6f004f2023-12-12 11:27:36 +0100178{
Valerio Setti1fff4f22023-12-28 14:19:34 +0100179 (void) key_type;
Valerio Settic6f004f2023-12-12 11:27:36 +0100180 (void) cipher_alg;
Paul Elliott35816522024-02-23 17:47:42 +0000181 return psa_get_drivers_initialized();
Valerio Settic6f004f2023-12-12 11:27:36 +0100182}
183
184
Valerio Settia55f0422023-07-10 15:34:41 +0200185#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200186 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200187 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200188static int psa_is_dh_key_size_valid(size_t bits)
189{
Valerio Setti504a1022024-01-17 15:19:30 +0100190 switch (bits) {
191#if defined(PSA_WANT_DH_RFC7919_2048)
192 case 2048:
193 return 1;
194#endif /* PSA_WANT_DH_RFC7919_2048 */
195#if defined(PSA_WANT_DH_RFC7919_3072)
196 case 3072:
197 return 1;
198#endif /* PSA_WANT_DH_RFC7919_3072 */
199#if defined(PSA_WANT_DH_RFC7919_4096)
200 case 4096:
201 return 1;
202#endif /* PSA_WANT_DH_RFC7919_4096 */
203#if defined(PSA_WANT_DH_RFC7919_6144)
204 case 6144:
205 return 1;
206#endif /* PSA_WANT_DH_RFC7919_6144 */
207#if defined(PSA_WANT_DH_RFC7919_8192)
208 case 8192:
209 return 1;
210#endif /* PSA_WANT_DH_RFC7919_8192 */
211 default:
212 return 0;
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200213 }
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200214}
Valerio Settia55f0422023-07-10 15:34:41 +0200215#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200216 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200217 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100220{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100221 /* Mbed TLS error codes can combine a high-level error code and a
222 * low-level error code. The low-level error usually reflects the
223 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 int low_level_ret = -(-ret & 0x007f);
225 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100226 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100228
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100229#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
231 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100233 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
234 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100235#endif
236
237#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200238 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
239 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
240 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
241 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
242 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200244 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200246 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100248#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200249
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100250#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000251 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100252 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100254#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100255
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100256#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100257 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100259 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100261#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100262
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100263#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200264 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100266#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200267
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100268#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200269 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200271 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100273#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200274
Dave Rodgman509b5672023-08-16 19:26:23 +0100275#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100276 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100278 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100282 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100286 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100290#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
293 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100294 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
295 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
299 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100301 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100303#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100304
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100305#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100308#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100309
Gilles Peskinee59236f2018-01-27 23:32:46 +0100310 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
311 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
312 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100314
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100315#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100316 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200318 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100322#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100323
Gilles Peskine82e57d12020-11-13 21:31:17 +0100324#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100326 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
327 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100328 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100330 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
331 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100333 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100335#endif
336
Dave Rodgmandea266f2023-08-31 11:52:43 +0100337#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100338 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100340 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100342 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100344#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100345 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100347#endif
348#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100349
Dave Rodgman509b5672023-08-16 19:26:23 +0100350#if defined(MBEDTLS_BIGNUM_C)
351#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100352 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100354#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100355 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
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_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100359 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100361 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
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_DIVISION_BY_ZERO:
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_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100367 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100369#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100370
Dave Rodgman509b5672023-08-16 19:26:23 +0100371#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100372 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100374 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
375 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100377#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
378 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100379 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100381#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100382 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
383 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100385 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100387 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
388 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100390 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100392 case MBEDTLS_ERR_PK_INVALID_ALG:
393 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
394 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100396 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200398 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100400#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100401
Gilles Peskineff2d2002019-05-06 15:26:23 +0200402 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200404 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200406
Dave Rodgman509b5672023-08-16 19:26:23 +0100407#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100408 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100410 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100412 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100414 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100416 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
417 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100419 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100421 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100423 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100425#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200426
Dave Rodgman1dab4452023-09-01 09:59:51 +0100427#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300428 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300429 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300431 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300433 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300435 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
436 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300438 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100440 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100442
Dave Rodgman509b5672023-08-16 19:26:23 +0100443#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000444 case MBEDTLS_ERR_ECP_IN_PROGRESS:
445 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100446#endif
447#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000448
Janos Follatha13b9052019-11-22 12:48:59 +0000449 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300451
Gilles Peskinee59236f2018-01-27 23:32:46 +0100452 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100454 }
455}
456
Paul Elliottdc42ca82023-02-24 18:11:59 +0000457/**
458 * \brief For output buffers which contain "tags"
459 * (outputs that may be checked for validity like
460 * hashes, MACs and signatures), fill the unused
461 * part of the output buffer (the whole buffer on
462 * error, the trailing part on success) with
463 * something that isn't a valid tag (barring an
464 * attack on the tag and deliberately-crafted
465 * input), in case the caller doesn't check the
466 * return status properly.
467 *
468 * \param output_buffer Pointer to buffer to wipe. May not be NULL
469 * unless \p output_buffer_size is zero.
470 * \param status Status of function called to generate
471 * output_buffer originally
472 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
473 * could be NULL.
474 * \param output_buffer_length Length of data written to output_buffer, must be
475 * less than \p output_buffer_size
476 */
477static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000478 size_t output_buffer_size, size_t output_buffer_length)
479{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000480 size_t offset = 0;
481
482 if (output_buffer_size == 0) {
483 /* If output_buffer_size is 0 then we have nothing to do. We must not
484 call memset because output_buffer may be NULL in this case */
485 return;
486 }
487
488 if (status == PSA_SUCCESS) {
489 offset = output_buffer_length;
490 }
491
492 memset(output_buffer + offset, '!', output_buffer_size - offset);
493}
494
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
497 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200498{
499 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200502 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200503 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200504 case PSA_KEY_TYPE_PASSWORD:
505 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200506 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100507#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (bits != 128 && bits != 192 && bits != 256) {
510 return PSA_ERROR_INVALID_ARGUMENT;
511 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200512 break;
513#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200514#if defined(PSA_WANT_KEY_TYPE_ARIA)
515 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 if (bits != 128 && bits != 192 && bits != 256) {
517 return PSA_ERROR_INVALID_ARGUMENT;
518 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200519 break;
520#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100521#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (bits != 128 && bits != 192 && bits != 256) {
524 return PSA_ERROR_INVALID_ARGUMENT;
525 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200526 break;
527#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100528#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200529 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 if (bits != 64 && bits != 128 && bits != 192) {
531 return PSA_ERROR_INVALID_ARGUMENT;
532 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533 break;
534#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100535#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200536 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (bits != 256) {
538 return PSA_ERROR_INVALID_ARGUMENT;
539 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200540 break;
541#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200542 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200544 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (bits % 8 != 0) {
546 return PSA_ERROR_INVALID_ARGUMENT;
547 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200550}
551
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100552/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100553 *
Steven Cooremancd640932021-03-08 12:00:27 +0100554 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
555 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100556 *
557 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100558 * \param[in] key_type The key type of the key to be used with the
559 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100560 *
561 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100562 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100563 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100564 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100565 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100566MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100567 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100569{
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (PSA_ALG_IS_HMAC(algorithm)) {
571 if (key_type == PSA_KEY_TYPE_HMAC) {
572 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100573 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100574 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
577 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
578 * key. */
579 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
580 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
581 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
582 * the block length (larger than 1) for block ciphers. */
583 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
584 return PSA_SUCCESS;
585 }
586 }
587 }
588
589 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100590}
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
593 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200594{
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if (slot->key.data != NULL) {
596 return PSA_ERROR_ALREADY_EXISTS;
597 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 slot->key.data = mbedtls_calloc(1, buffer_length);
600 if (slot->key.data == NULL) {
601 return PSA_ERROR_INSUFFICIENT_MEMORY;
602 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200603
Ronald Cronea0f8a62020-11-25 17:52:23 +0100604 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200606}
607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
609 const uint8_t *data,
610 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200611{
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 psa_status_t status = psa_allocate_buffer_to_slot(slot,
613 data_length);
614 if (status != PSA_SUCCESS) {
615 return status;
616 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 memcpy(slot->key.data, data, data_length);
619 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200620}
621
Ronald Crona0fe59f2020-11-29 11:14:53 +0100622psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100623 const psa_key_attributes_t *attributes,
624 const uint8_t *data, size_t data_length,
625 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100627{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100628 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100629 psa_key_type_t type = attributes->type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100630
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200631 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (data_length == 0) {
633 return PSA_ERROR_NOT_SUPPORTED;
634 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 if (key_type_is_raw_bytes(type)) {
637 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200638
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100639 status = psa_validate_unstructured_key_bit_size(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 *bits);
641 if (status != PSA_SUCCESS) {
642 return status;
643 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200644
Ronald Crondd04d422020-11-28 15:14:42 +0100645 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100647 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 return PSA_SUCCESS;
651 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200652#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200653 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100654 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200655 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Valerio Setti504a1022024-01-17 15:19:30 +0100656 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100657 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200658 return mbedtls_psa_ffdh_import_key(attributes,
659 data, data_length,
660 key_buffer, key_buffer_size,
661 key_buffer_length,
662 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100663 }
Valerio Settia55f0422023-07-10 15:34:41 +0200664#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200665 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200666#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
668 if (PSA_KEY_TYPE_IS_ECC(type)) {
669 return mbedtls_psa_ecp_import_key(attributes,
670 data, data_length,
671 key_buffer, key_buffer_size,
672 key_buffer_length,
673 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100674 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200675#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800676 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200677#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
678 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
680 if (PSA_KEY_TYPE_IS_RSA(type)) {
681 return mbedtls_psa_rsa_import_key(attributes,
682 data, data_length,
683 key_buffer, key_buffer_size,
684 key_buffer_length,
685 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200686 }
Valerio Settife478902023-07-25 12:27:19 +0200687#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
688 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800689 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100690 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100693}
694
Gilles Peskinef603c712019-01-19 13:40:11 +0100695/** Calculate the intersection of two algorithm usage policies.
696 *
697 * Return 0 (which allows no operation) on incompatibility.
698 */
699static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100700 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100701 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100703{
Gilles Peskine549ea862019-05-22 11:45:59 +0200704 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (alg1 == alg2) {
706 return alg1;
707 }
Steven Cooreman03488022021-02-18 12:07:20 +0100708 /* If the policies are from the same hash-and-sign family, check
709 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
711 PSA_ALG_IS_SIGN_HASH(alg2) &&
712 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
713 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
714 return alg2;
715 }
716 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
717 return alg1;
718 }
Steven Cooreman03488022021-02-18 12:07:20 +0100719 }
720 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100721 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100722 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
724 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
725 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
726 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
727 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100728 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100729
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100730 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
732 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
733 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
734 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100735 }
736 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
738 (alg1_len <= alg2_len)) {
739 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100740 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
742 (alg2_len <= alg1_len)) {
743 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100744 }
745 }
746 /* If the policies are from the same MAC family, check whether one
747 * of them is a minimum-MAC-length policy. Calculate the most
748 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
750 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
751 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100752 /* Validate the combination of key type and algorithm. Since the base
753 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
755 return 0;
756 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100757
Steven Cooreman31a876d2021-03-03 20:47:40 +0100758 /* Get the (exact or at-least) output lengths for both sides of the
759 * requested intersection. None of the currently supported algorithms
760 * have an output length dependent on the actual key size, so setting it
761 * to a bogus value of 0 is currently OK.
762 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100763 * Note that for at-least-this-length wildcard algorithms, the output
764 * length is set to the shortest allowed length, which allows us to
765 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
767 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100768 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100769
Steven Cooremana1d83222021-02-25 10:20:29 +0100770 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
772 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
773 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100774 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100775
776 /* If only one is an at-least-this-length policy, the intersection would
777 * be the other (fixed-length) policy as long as said fixed length is
778 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
780 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100781 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
783 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100784 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100785
Steven Cooremancd640932021-03-08 12:00:27 +0100786 /* If none of them are wildcards, check whether they define the same tag
787 * length. This is still possible here when one is default-length and
788 * the other specific-length. Ensure to always return the
789 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (alg1_len == alg2_len) {
791 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
792 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100793 }
794 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100796}
797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798static int psa_key_algorithm_permits(psa_key_type_t key_type,
799 psa_algorithm_t policy_alg,
800 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200801{
Gilles Peskine549ea862019-05-22 11:45:59 +0200802 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (requested_alg == policy_alg) {
804 return 1;
805 }
Steven Cooreman03488022021-02-18 12:07:20 +0100806 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
807 * and requested_alg is the same hash-and-sign family with any hash,
808 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
810 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
811 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
812 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100813 }
814 /* If policy_alg is a wildcard AEAD algorithm of the same base as
815 * the requested algorithm, check the requested tag length to be
816 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (PSA_ALG_IS_AEAD(policy_alg) &&
818 PSA_ALG_IS_AEAD(requested_alg) &&
819 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
820 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
821 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
822 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
823 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100824 }
Steven Cooremancd640932021-03-08 12:00:27 +0100825 /* If policy_alg is a MAC algorithm of the same base as the requested
826 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (PSA_ALG_IS_MAC(policy_alg) &&
828 PSA_ALG_IS_MAC(requested_alg) &&
829 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
830 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100831 /* Validate the combination of key type and algorithm. Since the policy
832 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
834 return 0;
835 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100836
Steven Cooreman31a876d2021-03-03 20:47:40 +0100837 /* Get both the requested output length for the algorithm which is to be
838 * verified, and the default output length for the base algorithm.
839 * Note that none of the currently supported algorithms have an output
840 * length dependent on actual key size, so setting it to a bogus value
841 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100842 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100844 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 key_type, 0,
846 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100847
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100848 /* If the policy is default-length, only allow an algorithm with
849 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
851 return requested_output_length == default_output_length;
852 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100853
854 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100855 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
857 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
858 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100859 }
860
Steven Cooremancd640932021-03-08 12:00:27 +0100861 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
862 * check for the requested MAC length to be equal to or longer than the
863 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
865 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
866 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100867 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200868 }
Steven Cooremance48e852020-10-05 16:02:45 +0200869 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200870 * a key derivation with that key agreement should also be allowed. This
871 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
873 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
874 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
875 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200876 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100877 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200879}
880
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100881/** Test whether a policy permits an algorithm.
882 *
883 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100884 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100885 * \note This function requires providing the key type for which the policy is
886 * being validated, since some algorithm policy definitions (e.g. MAC)
887 * have different properties depending on what kind of cipher it is
888 * combined with.
889 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100890 * \retval PSA_SUCCESS When \p alg is a specific algorithm
891 * allowed by the \p policy.
892 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
893 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
894 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100895 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
897 psa_key_type_t key_type,
898 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100899{
Steven Cooremand788fab2021-02-25 11:29:17 +0100900 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (alg == 0) {
902 return PSA_ERROR_INVALID_ARGUMENT;
903 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100904
Steven Cooreman7e39f052021-02-23 14:18:32 +0100905 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if (PSA_ALG_IS_WILDCARD(alg)) {
907 return PSA_ERROR_INVALID_ARGUMENT;
908 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
911 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
912 return PSA_SUCCESS;
913 } else {
914 return PSA_ERROR_NOT_PERMITTED;
915 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100916}
917
Gilles Peskinef603c712019-01-19 13:40:11 +0100918/** Restrict a key policy based on a constraint.
919 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100920 * \note This function requires providing the key type for which the policy is
921 * being restricted, since some algorithm policy definitions (e.g. MAC)
922 * have different properties depending on what kind of cipher it is
923 * combined with.
924 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100925 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100926 * \param[in,out] policy The policy to restrict.
927 * \param[in] constraint The policy constraint to apply.
928 *
929 * \retval #PSA_SUCCESS
930 * \c *policy contains the intersection of the original value of
931 * \c *policy and \c *constraint.
932 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100933 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100934 * \c *policy is unchanged.
935 */
936static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100937 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100938 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100940{
941 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 psa_key_policy_algorithm_intersection(key_type, policy->alg,
943 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200944 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
946 constraint->alg2);
947 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
948 return PSA_ERROR_INVALID_ARGUMENT;
949 }
950 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
951 return PSA_ERROR_INVALID_ARGUMENT;
952 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100953 policy->usage &= constraint->usage;
954 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200955 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100957}
958
Przemek Stekiel061f6942022-11-30 10:51:35 +0100959/** Get the description of a key given its identifier and policy constraints
960 * and lock it.
961 *
962 * The key must have allow all the usage flags set in \p usage. If \p alg is
963 * nonzero, the key must allow operations with this algorithm. If \p alg is
964 * zero, the algorithm is not checked.
965 *
966 * In case of a persistent key, the function loads the description of the key
967 * into a key slot if not already done.
968 *
Ryan Everett098c6652024-01-03 13:03:36 +0000969 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +0000970 * It is the responsibility of the caller to then unregister
971 * once they have finished reading the contents of the slot.
972 * The caller unregisters by calling psa_unregister_read() or
973 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
974 * if and only if the caller already holds the global key slot mutex
975 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
976 * the unregister with mutex lock and unlock operations.
Przemek Stekiel061f6942022-11-30 10:51:35 +0100977 */
978static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100979 mbedtls_svc_key_id_t key,
980 psa_key_slot_t **p_slot,
981 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100983{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200984 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100985 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 status = psa_get_and_lock_key_slot(key, p_slot);
988 if (status != PSA_SUCCESS) {
989 return status;
990 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200991 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100992
993 /* Enforce that usage policy for the key slot contains all the flags
994 * required by the usage parameter. There is one exception: public
995 * keys can always be exported, so we treat public key objects as
996 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100998 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001002 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001003 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001004 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001005
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001006 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 if (alg != 0) {
1008 status = psa_key_policy_permits(&slot->attr.policy,
1009 slot->attr.type,
1010 alg);
1011 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001012 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001014 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001017
1018error:
1019 *p_slot = NULL;
Ryan Everettfb792ca2024-01-31 13:40:05 +00001020 psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001023}
Darryl Green940d72c2018-07-13 13:18:51 +01001024
Ronald Cron5c522922020-11-14 16:35:34 +01001025/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001026 *
1027 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001028 * available, as opposed to a key in a secure element and/or to be used
1029 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001030 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001031 * This is a temporary function that may be used instead of
1032 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1033 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001034 *
Ryan Everett098c6652024-01-03 13:03:36 +00001035 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +00001036 * It is the responsibility of the caller to then unregister
1037 * once they have finished reading the contents of the slot.
1038 * The caller unregisters by calling psa_unregister_read() or
1039 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
1040 * if and only if the caller already holds the global key slot mutex
1041 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
1042 * psa_unregister_read() with mutex lock and unlock operations.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001043 */
Ronald Cron5c522922020-11-14 16:35:34 +01001044static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1045 mbedtls_svc_key_id_t key,
1046 psa_key_slot_t **p_slot,
1047 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001049{
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1051 usage, alg);
1052 if (status != PSA_SUCCESS) {
1053 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001054 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everettfb792ca2024-01-31 13:40:05 +00001057 psa_unregister_read_under_mutex(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 *p_slot = NULL;
1059 return PSA_ERROR_NOT_SUPPORTED;
1060 }
1061
1062 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001063}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001066{
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001068 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001070
Ronald Cronea0f8a62020-11-25 17:52:23 +01001071 slot->key.data = NULL;
1072 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001075}
1076
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001077/** Completely wipe a slot in memory, including its policy.
1078 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001080{
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 /*
1084 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +00001085 * do our best to report an unexpected amount of registered readers or
1086 * an unexpected state.
1087 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1088 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1090 * function is called as part of the execution of a test suite, the
1091 * execution of the test suite is stopped in error if the assertion fails.
1092 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001093 switch (slot->state) {
1094 case PSA_SLOT_FULL:
1095 /* In this state psa_wipe_key_slot() must only be called if the
1096 * caller is the last reader. */
1097 case PSA_SLOT_PENDING_DELETION:
1098 /* In this state psa_wipe_key_slot() must only be called if the
1099 * caller is the last reader. */
1100 if (slot->registered_readers != 1) {
1101 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1);
1102 status = PSA_ERROR_CORRUPTION_DETECTED;
1103 }
1104 break;
1105 case PSA_SLOT_FILLING:
1106 /* In this state registered_readers must be 0. */
1107 if (slot->registered_readers != 0) {
1108 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0);
1109 status = PSA_ERROR_CORRUPTION_DETECTED;
1110 }
1111 break;
1112 case PSA_SLOT_EMPTY:
1113 /* The slot is already empty, it cannot be wiped. */
1114 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1115 status = PSA_ERROR_CORRUPTION_DETECTED;
1116 break;
1117 default:
1118 /* The slot's state is invalid. */
1119 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001120 }
1121
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001122 /* Multipart operations may still be using the key. This is safe
1123 * because all multipart operation objects are independent from
1124 * the key slot: if they need to access the key after the setup
1125 * phase, they have a copy of the key. Note that this means that
1126 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001127 /* At this point, key material and other type-specific content has
1128 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001129 * zeroize because the metadata is not particularly sensitive.
1130 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 memset(slot, 0, sizeof(*slot));
1132 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001133}
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001136{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001137 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001138 psa_status_t status; /* status of the last operation */
1139 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001140#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1141 psa_se_drv_table_entry_t *driver;
1142#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if (mbedtls_svc_key_id_is_null(key)) {
1145 return PSA_SUCCESS;
1146 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001147
Ronald Cronf2911112020-10-29 17:51:10 +01001148 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001149 * Get the description of the key in a key slot, and register to read it.
1150 * In the case of a persistent key, this will load the key description
1151 * from persistent memory if not done yet.
1152 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001153 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001154 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 status = psa_get_and_lock_key_slot(key, &slot);
1156 if (status != PSA_SUCCESS) {
1157 return status;
1158 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001159
Ryan Everettc053d962024-01-25 17:56:32 +00001160#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001161 /* We cannot unlock between setting the state to PENDING_DELETION
1162 * and destroying the key in storage, as otherwise another thread
1163 * could load the key into a new slot and the key will not be
1164 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001165 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1166 &mbedtls_threading_key_slot_mutex));
Ryan Everettd868b742024-03-08 18:35:09 +00001167
1168 if (slot->state == PSA_SLOT_PENDING_DELETION) {
1169 /* Another thread has destroyed the key between us locking the slot
1170 * and us gaining the mutex. Unregister from the slot,
1171 * and report that the key does not exist. */
1172 status = psa_unregister_read(slot);
1173
1174 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1175 &mbedtls_threading_key_slot_mutex));
1176 return (status == PSA_SUCCESS) ? PSA_ERROR_INVALID_HANDLE : status;
1177 }
Ryan Everettc053d962024-01-25 17:56:32 +00001178#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001179 /* Set the key slot containing the key description's state to
1180 * PENDING_DELETION. This stops new operations from registering
1181 * to read the slot. Current readers can safely continue to access
1182 * the key within the slot; the last registered reader will
1183 * automatically wipe the slot when they call psa_unregister_read().
1184 * If the key is persistent, we can now delete the copy of the key
1185 * from memory. If the key is opaque, we require the driver to
1186 * deal with the deletion. */
Ryan Everettd868b742024-03-08 18:35:09 +00001187 overall_status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1188 PSA_SLOT_PENDING_DELETION);
Ryan Everettc053d962024-01-25 17:56:32 +00001189
Ryan Everettd868b742024-03-08 18:35:09 +00001190 if (overall_status != PSA_SUCCESS) {
Ryan Everettc053d962024-01-25 17:56:32 +00001191 goto exit;
1192 }
Ronald Cronf2911112020-10-29 17:51:10 +01001193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001195 /* Refuse the destruction of a read-only key (which may or may not work
1196 * if we attempt it, depending on whether the key is merely read-only
1197 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001198 * Just do the best we can, which is to wipe the copy in memory
1199 * (done in this function's cleanup code). */
1200 overall_status = PSA_ERROR_NOT_PERMITTED;
1201 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001202 }
1203
Gilles Peskine354f7672019-07-12 23:46:38 +02001204#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1206 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001207 /* For a key in a secure element, we need to do three things:
1208 * remove the key file in internal storage, destroy the
1209 * key inside the secure element, and update the driver's
1210 * persistent data. Start a transaction that will encompass these
1211 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001213 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001215 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 status = psa_crypto_save_transaction();
1217 if (status != PSA_SUCCESS) {
1218 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001219 /* We should still try to destroy the key in the secure
1220 * element and the key metadata in storage. This is especially
1221 * important if the error is that the storage is full.
1222 * But how to do it exactly without risking an inconsistent
1223 * state after a reset?
1224 * https://github.com/ARMmbed/mbed-crypto/issues/215
1225 */
1226 overall_status = status;
1227 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001228 }
1229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 status = psa_destroy_se_key(driver,
1231 psa_key_slot_get_slot_number(slot));
1232 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001233 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001235 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001236#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1237
Darryl Greend49a4992018-06-18 17:27:26 +01001238#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001240 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001241 * The slot will still hold a copy of the key until the last reader
1242 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 status = psa_destroy_persistent_key(slot->attr.id);
1244 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001245 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 }
Darryl Greend49a4992018-06-18 17:27:26 +01001247 }
1248#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001249
Gilles Peskinefc762652019-07-22 19:30:34 +02001250#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (driver != NULL) {
1252 status = psa_save_se_persistent_data(driver);
1253 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001254 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 }
1256 status = psa_crypto_stop_transaction();
1257 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001258 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001260 }
1261#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1262
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001263exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001264 /* Unregister from reading the slot. If we are the last active reader
1265 * then this will wipe the slot. */
1266 status = psa_unregister_read(slot);
1267 /* Prioritize CORRUPTION_DETECTED from unregistering over
1268 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001270 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 }
Ryan Everettc053d962024-01-25 17:56:32 +00001272
1273#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001274 /* Don't overwrite existing errors if the unlock fails. */
1275 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001276 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1277 &mbedtls_threading_key_slot_mutex));
1278#endif
1279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001281}
1282
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001283/** Retrieve all the publicly-accessible attributes of a key.
1284 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001285psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1286 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001287{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001288 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001289 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1294 if (status != PSA_SUCCESS) {
1295 return status;
1296 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001297
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001298 *attributes = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001299
1300#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1302 psa_set_key_slot_number(attributes,
1303 psa_key_slot_get_slot_number(slot));
1304 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001305#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001306
Gilles Peskine97c0b2f2024-02-16 00:49:46 +01001307 return psa_unregister_read_under_mutex(slot);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001308}
1309
Gilles Peskinec8000c02019-08-02 20:15:51 +02001310#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1311psa_status_t psa_get_key_slot_number(
1312 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001314{
Gilles Peskine972539c2024-02-28 01:49:45 +01001315 if (attributes->has_slot_number) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001316 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 return PSA_SUCCESS;
1318 } else {
1319 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001320 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001321}
1322#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1325 size_t key_buffer_size,
1326 uint8_t *data,
1327 size_t data_size,
1328 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001329{
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if (key_buffer_size > data_size) {
1331 return PSA_ERROR_BUFFER_TOO_SMALL;
1332 }
1333 memcpy(data, key_buffer, key_buffer_size);
1334 memset(data + key_buffer_size, 0,
1335 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001336 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001338}
1339
Ronald Cron7285cda2020-11-26 14:46:37 +01001340psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001341 const psa_key_attributes_t *attributes,
1342 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001344{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001345 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if (key_type_is_raw_bytes(type) ||
1348 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001349 PSA_KEY_TYPE_IS_ECC(type) ||
1350 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 return psa_export_key_buffer_internal(
1352 key_buffer, key_buffer_size,
1353 data, data_size, data_length);
1354 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001355 /* This shouldn't happen in the reference implementation, but
1356 it is valid for a special-purpose implementation to omit
1357 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001359 }
1360}
1361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1363 uint8_t *data,
1364 size_t data_size,
1365 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001366{
1367 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1368 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1369 psa_key_slot_t *slot;
1370
Ronald Crone907e552021-01-18 13:22:38 +01001371 /* Reject a zero-length output buffer now, since this can never be a
1372 * valid key representation. This way we know that data must be a valid
1373 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 if (data_size == 0) {
1375 return PSA_ERROR_BUFFER_TOO_SMALL;
1376 }
Ronald Crone907e552021-01-18 13:22:38 +01001377
Ronald Cron9486f9d2020-11-26 13:36:23 +01001378 /* Set the key to empty now, so that even when there are errors, we always
1379 * set data_length to a value between 0 and data_size. On error, setting
1380 * the key to empty is a good choice because an empty key representation is
1381 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001382 *data_length = 0;
1383
Ronald Cron9486f9d2020-11-26 13:36:23 +01001384 /* Export requires the EXPORT flag. There is an exception for public keys,
1385 * which don't require any flag, but
1386 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1387 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1389 PSA_KEY_USAGE_EXPORT, 0);
1390 if (status != PSA_SUCCESS) {
1391 return status;
1392 }
mohammad160306e79202018-03-28 13:17:44 +03001393
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001394 status = psa_driver_wrapper_export_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 slot->key.data, slot->key.bytes,
1396 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001397
Ryan Everetta103ec92024-01-31 13:59:57 +00001398 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001401}
1402
Ronald Cron7285cda2020-11-26 14:46:37 +01001403psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001404 const psa_key_attributes_t *attributes,
1405 const uint8_t *key_buffer,
1406 size_t key_buffer_size,
1407 uint8_t *data,
1408 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001410{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001411 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001412
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001413 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001414 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1415 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001416 /* Exporting public -> public */
1417 return psa_export_key_buffer_internal(
1418 key_buffer, key_buffer_size,
1419 data, data_size, data_length);
1420 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001421#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001422 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1423 return mbedtls_psa_rsa_export_public_key(attributes,
1424 key_buffer,
1425 key_buffer_size,
1426 data,
1427 data_size,
1428 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001429#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001430 /* We don't know how to convert a private RSA key to public. */
1431 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001432#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001433 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001434 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001435#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001436 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1437 return mbedtls_psa_ecp_export_public_key(attributes,
1438 key_buffer,
1439 key_buffer_size,
1440 data,
1441 data_size,
1442 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001443#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001444 /* We don't know how to convert a private ECC key to public */
1445 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001446#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001447 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001448 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001449#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001450 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001451 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001452 key_buffer,
1453 key_buffer_size,
1454 data, data_size,
1455 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001456#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001457 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001458#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001459 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001461 (void) key_buffer;
1462 (void) key_buffer_size;
1463 (void) data;
1464 (void) data_size;
1465 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001467 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001468}
Ronald Crond18b5f82020-11-25 16:46:05 +01001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1471 uint8_t *data,
1472 size_t data_size,
1473 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001474{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001475 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001476 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001477 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001478
Ronald Crone907e552021-01-18 13:22:38 +01001479 /* Reject a zero-length output buffer now, since this can never be a
1480 * valid key representation. This way we know that data must be a valid
1481 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 if (data_size == 0) {
1483 return PSA_ERROR_BUFFER_TOO_SMALL;
1484 }
Ronald Crone907e552021-01-18 13:22:38 +01001485
Darryl Greendd8fb772018-11-07 16:00:44 +00001486 /* Set the key to empty now, so that even when there are errors, we always
1487 * set data_length to a value between 0 and data_size. On error, setting
1488 * the key to empty is a good choice because an empty key representation is
1489 * unlikely to be accepted anywhere. */
1490 *data_length = 0;
1491
1492 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1494 if (status != PSA_SUCCESS) {
1495 return status;
1496 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1499 status = PSA_ERROR_INVALID_ARGUMENT;
1500 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001501 }
1502
Ronald Cron67227982020-11-26 15:16:05 +01001503 status = psa_driver_wrapper_export_public_key(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001504 &slot->attr, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001506
1507exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001508 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001511}
1512
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001513/** Validate that a key policy is internally well-formed.
1514 *
1515 * This function only rejects invalid policies. It does not validate the
1516 * consistency of the policy with respect to other attributes of the key
1517 * such as the key type.
1518 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001519static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001520{
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1522 PSA_KEY_USAGE_COPY |
1523 PSA_KEY_USAGE_ENCRYPT |
1524 PSA_KEY_USAGE_DECRYPT |
1525 PSA_KEY_USAGE_SIGN_MESSAGE |
1526 PSA_KEY_USAGE_VERIFY_MESSAGE |
1527 PSA_KEY_USAGE_SIGN_HASH |
1528 PSA_KEY_USAGE_VERIFY_HASH |
1529 PSA_KEY_USAGE_VERIFY_DERIVATION |
1530 PSA_KEY_USAGE_DERIVE)) != 0) {
1531 return PSA_ERROR_INVALID_ARGUMENT;
1532 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001535}
1536
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001537/** Validate the internal consistency of key attributes.
1538 *
1539 * This function only rejects invalid attribute values. If does not
1540 * validate the consistency of the attributes with any key data that may
1541 * be involved in the creation of the key.
1542 *
1543 * Call this function early in the key creation process.
1544 *
1545 * \param[in] attributes Key attributes for the new key.
1546 * \param[out] p_drv On any return, the driver for the key, if any.
1547 * NULL for a transparent key.
1548 *
1549 */
1550static psa_status_t psa_validate_key_attributes(
1551 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001553{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001554 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1556 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 status = psa_validate_key_location(lifetime, p_drv);
1559 if (status != PSA_SUCCESS) {
1560 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001561 }
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 status = psa_validate_key_persistence(lifetime);
1564 if (status != PSA_SUCCESS) {
1565 return status;
1566 }
1567
1568 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1569 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1570 return PSA_ERROR_INVALID_ARGUMENT;
1571 }
1572 } else {
1573 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1574 return PSA_ERROR_INVALID_ARGUMENT;
1575 }
1576 }
1577
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001578 status = psa_validate_key_policy(&attributes->policy);
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if (status != PSA_SUCCESS) {
1580 return status;
1581 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001582
1583 /* Refuse to create overly large keys.
1584 * Note that this doesn't trigger on import if the attributes don't
1585 * explicitly specify a size (so psa_get_key_bits returns 0), so
1586 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1588 return PSA_ERROR_NOT_SUPPORTED;
1589 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001592}
1593
Gilles Peskine4747d192019-04-17 15:05:45 +02001594/** Prepare a key slot to receive key material.
1595 *
1596 * This function allocates a key slot and sets its metadata.
1597 *
1598 * If this function fails, call psa_fail_key_creation().
1599 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001600 * This function is intended to be used as follows:
1601 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001602 * it with the specified attributes, and in case of a volatile key assign it
1603 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001604 * -# Populate the slot with the key material.
1605 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1606 * In case of failure at any step, stop the sequence and call
1607 * psa_fail_key_creation().
1608 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001609 * On success, the key slot's state is PSA_SLOT_FILLING.
1610 * It is the responsibility of the caller to change the slot's state to
1611 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001612 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001613 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001614 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001615 * \param[out] p_slot On success, a pointer to the prepared slot.
1616 * \param[out] p_drv On any return, the driver for the key, if any.
1617 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001618 *
1619 * \retval #PSA_SUCCESS
1620 * The key slot is ready to receive key material.
1621 * \return If this function fails, the key slot is an invalid state.
1622 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001623 */
1624static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001625 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001626 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001627 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001629{
1630 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001631 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001632 psa_key_slot_t *slot;
1633
Gilles Peskinedf179142019-07-15 22:02:14 +02001634 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001635 *p_drv = NULL;
1636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 status = psa_validate_key_attributes(attributes, p_drv);
1638 if (status != PSA_SUCCESS) {
1639 return status;
1640 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001641
Ryan Everett3d8118d2024-01-30 16:58:47 +00001642#if defined(MBEDTLS_THREADING_C)
1643 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1644 &mbedtls_threading_key_slot_mutex));
1645#endif
Ryan Everettb69118e2024-01-02 15:54:32 +00001646 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001647#if defined(MBEDTLS_THREADING_C)
1648 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1649 &mbedtls_threading_key_slot_mutex));
1650#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if (status != PSA_SUCCESS) {
1652 return status;
1653 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001654 slot = *p_slot;
1655
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001656 /* We're storing the declared bit-size of the key. It's up to each
1657 * creation mechanism to verify that this information is correct.
1658 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001659 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001660 * is optional (import, copy). In case of a volatile key, assign it the
1661 * volatile key identifier associated to the slot returned to contain its
1662 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001663
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001664 slot->attr = *attributes;
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001666#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1667 slot->attr.id = volatile_key_id;
1668#else
1669 slot->attr.id.key_id = volatile_key_id;
1670#endif
1671 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001672
Gilles Peskinecbaff462019-07-12 23:46:04 +02001673#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001674 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001675 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001676 * create the key file in internal storage, create the
1677 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001678 * persistent data. This is done by starting a transaction that will
1679 * encompass these three actions.
1680 * For registering a volatile key, we just need to find an appropriate
1681 * slot number inside the SE. Since the key is designated volatile, creating
1682 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001683 /* The first thing to do is to find a slot number for the new key.
1684 * We save the slot number in persistent storage as part of the
1685 * transaction data. It will be needed to recover if the power
1686 * fails during the key creation process, to clean up on the secure
1687 * element side after restarting. Obtaining a slot number from the
1688 * secure element driver updates its persistent state, but we do not yet
1689 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001690 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001692 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1694 &slot_number);
1695 if (status != PSA_SUCCESS) {
1696 return status;
1697 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001698
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001699 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001701 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001702 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001703 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 status = psa_crypto_save_transaction();
1705 if (status != PSA_SUCCESS) {
1706 (void) psa_crypto_stop_transaction();
1707 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001708 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001709 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001710
1711 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001713 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001716 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001718 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001719#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1720
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001722}
Gilles Peskine4747d192019-04-17 15:05:45 +02001723
1724/** Finalize the creation of a key once its key material has been set.
1725 *
1726 * This entails writing the key to persistent storage.
1727 *
1728 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001729 * See the documentation of psa_start_key_creation() for the intended use
1730 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001731 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001732 * If the finalization succeeds, the function sets the key slot's state to
1733 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1734 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001735 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001736 * \param[in,out] slot Pointer to the slot with key material.
1737 * \param[in] driver The secure element driver for the key,
1738 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001739 * \param[out] key On success, identifier of the key. Note that the
1740 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001741 *
1742 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001743 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001744 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1745 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1746 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1747 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1748 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1749 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001750 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001751 * \return If this function fails, the key slot is an invalid state.
1752 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001753 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001754static psa_status_t psa_finish_key_creation(
1755 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001756 psa_se_drv_table_entry_t *driver,
1757 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001758{
1759 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001760 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001761 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001762
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001763#if defined(MBEDTLS_THREADING_C)
1764 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1765 &mbedtls_threading_key_slot_mutex));
1766#endif
1767
Gilles Peskine4747d192019-04-17 15:05:45 +02001768#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001770#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001772 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001773 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001775
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001776 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1777 sizeof(data.slot_number),
1778 "Slot number size does not match psa_se_key_data_storage_t");
1779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1781 status = psa_save_persistent_key(&slot->attr,
1782 (uint8_t *) &data,
1783 sizeof(data));
1784 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001785#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1786 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001787 /* Key material is saved in export representation in the slot, so
1788 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 status = psa_save_persistent_key(&slot->attr,
1790 slot->key.data,
1791 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001792 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001793 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001794#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1795
Gilles Peskinecbaff462019-07-12 23:46:04 +02001796#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001797 /* Finish the transaction for a key creation. This does not
1798 * happen when registering an existing key. Detect this case
1799 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001800 * creation of a persistent key in a secure element requires a transaction,
1801 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if (driver != NULL &&
1803 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1804 status = psa_save_se_persistent_data(driver);
1805 if (status != PSA_SUCCESS) {
1806 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001807
1808#if defined(MBEDTLS_THREADING_C)
1809 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1810 &mbedtls_threading_key_slot_mutex));
1811#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001813 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001815 }
1816#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1817
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001819 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001820 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1821 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001823 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001825 }
Ronald Cron50972942020-11-14 11:28:25 +01001826
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001827#if defined(MBEDTLS_THREADING_C)
1828 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1829 &mbedtls_threading_key_slot_mutex));
1830#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001832}
1833
1834/** Abort the creation of a key.
1835 *
1836 * You may call this function after calling psa_start_key_creation(),
1837 * or after psa_finish_key_creation() fails. In other circumstances, this
1838 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001839 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001840 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001841 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001842 * \param[in,out] slot Pointer to the slot with key material.
1843 * \param[in] driver The secure element driver for the key,
1844 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001845 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001846static void psa_fail_key_creation(psa_key_slot_t *slot,
1847 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001848{
Gilles Peskine011e4282019-06-26 18:34:38 +02001849 (void) driver;
1850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001852 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001854
Ryan Everettb7101442024-01-23 20:09:49 +00001855#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00001856 /* If the lock operation fails we still wipe the slot.
1857 * Operations will no longer work after a failed lock,
1858 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00001859 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
1860#endif
1861
Gilles Peskine011e4282019-06-26 18:34:38 +02001862#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001863 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001864 * element, and the failure happened later (when saving metadata
1865 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001866 * element.
1867 * https://github.com/ARMmbed/mbed-crypto/issues/217
1868 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001869
Gilles Peskined7729582019-08-05 15:55:54 +02001870 /* Abort the ongoing transaction if any (there may not be one if
1871 * the creation process failed before starting one, or if the
1872 * key creation is a registration of a key in a secure element).
1873 * Earlier functions must already have done what it takes to undo any
1874 * partial creation. All that's left is to update the transaction data
1875 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001877#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00001880
1881#if defined(MBEDTLS_THREADING_C)
1882 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
1883#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02001884}
1885
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001886/** Validate optional attributes during key creation.
1887 *
1888 * Some key attributes are optional during key creation. If they are
1889 * specified in the attributes structure, check that they are consistent
1890 * with the data in the slot.
1891 *
1892 * This function should be called near the end of key creation, after
1893 * the slot in memory is fully populated but before saving persistent data.
1894 */
1895static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001899 if (attributes->type != 0) {
1900 if (attributes->type != slot->attr.type) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 return PSA_ERROR_INVALID_ARGUMENT;
1902 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001903 }
1904
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001905 if (attributes->bits != 0) {
1906 if (attributes->bits != slot->attr.bits) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 return PSA_ERROR_INVALID_ARGUMENT;
1908 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001909 }
1910
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001912}
1913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1915 const uint8_t *data,
1916 size_t data_length,
1917 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001918{
1919 psa_status_t status;
1920 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001921 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001922 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301923 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001924
Ronald Cron81709fc2020-11-14 12:10:32 +01001925 *key = MBEDTLS_SVC_KEY_ID_INIT;
1926
Gilles Peskine0f84d622019-09-12 19:03:13 +02001927 /* Reject zero-length symmetric keys (including raw data key objects).
1928 * This also rejects any key which might be encoded as an empty string,
1929 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 if (data_length == 0) {
1931 return PSA_ERROR_INVALID_ARGUMENT;
1932 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001933
Archana449608b2021-09-08 15:36:05 +05301934 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 if (data_length > SIZE_MAX / 8) {
1936 return PSA_ERROR_NOT_SUPPORTED;
1937 }
Archana6ed4bda2021-08-04 10:47:15 +05301938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1940 &slot, &driver);
1941 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001942 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001944
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001945 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301946 * storage ( thus not in the case of importing a key in a secure element
1947 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1948 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001950 if (psa_key_lifetime_is_external(attributes->lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301951 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 attributes, data, data_length, &storage_size);
1953 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301954 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 }
Archanad8a83dc2021-06-14 10:04:16 +05301956 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 status = psa_allocate_buffer_to_slot(slot, storage_size);
1958 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001959 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001961 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001962
1963 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 status = psa_driver_wrapper_import_key(attributes,
1965 data, data_length,
1966 slot->key.data,
1967 slot->key.bytes,
1968 &slot->key.bytes, &bits);
1969 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001970 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001972
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001974 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001976 status = PSA_ERROR_INVALID_ARGUMENT;
1977 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001978 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001979
Archana6ed4bda2021-08-04 10:47:15 +05301980 /* Enforce a size limit, and in particular ensure that the bit
1981 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301983 status = PSA_ERROR_NOT_SUPPORTED;
1984 goto exit;
1985 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 status = psa_validate_optional_attributes(slot, attributes);
1987 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001988 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001992exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 if (status != PSA_SUCCESS) {
1994 psa_fail_key_creation(slot, driver);
1995 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001998}
1999
Gilles Peskined7729582019-08-05 15:55:54 +02002000#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2001psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002003{
2004 psa_status_t status;
2005 psa_key_slot_t *slot = NULL;
2006 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002007 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002008
2009 /* Leaving attributes unspecified is not currently supported.
2010 * It could make sense to query the key type and size from the
2011 * secure element, but not all secure elements support this
2012 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2014 return PSA_ERROR_NOT_SUPPORTED;
2015 }
2016 if (psa_get_key_bits(attributes) == 0) {
2017 return PSA_ERROR_NOT_SUPPORTED;
2018 }
Gilles Peskined7729582019-08-05 15:55:54 +02002019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2021 &slot, &driver);
2022 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002023 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 }
Gilles Peskined7729582019-08-05 15:55:54 +02002025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002027
2028exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (status != PSA_SUCCESS) {
2030 psa_fail_key_creation(slot, driver);
2031 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002032
Gilles Peskined7729582019-08-05 15:55:54 +02002033 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 psa_close_key(key);
2035 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002036}
2037#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2040 const psa_key_attributes_t *specified_attributes,
2041 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002042{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002043 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002044 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002045 psa_key_slot_t *source_slot = NULL;
2046 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002047 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002048 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302049 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002050
Ronald Cron81709fc2020-11-14 12:10:32 +01002051 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2052
Archana8a180362021-07-05 02:18:48 +05302053 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2055 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002056 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 status = psa_validate_optional_attributes(source_slot,
2060 specified_attributes);
2061 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002062 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002064
Archana9d17bf42021-09-10 06:22:44 +05302065 /* The target key type and number of bits have been validated by
2066 * psa_validate_optional_attributes() to be either equal to zero or
2067 * equal to the ones of the source key. So it is safe to inherit
2068 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302069 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002070 actual_attributes.bits = source_slot->attr.bits;
2071 actual_attributes.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302072
2073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 status = psa_restrict_key_policy(source_slot->attr.type,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002075 &actual_attributes.policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 &source_slot->attr.policy);
2077 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002078 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2082 &target_slot, &driver);
2083 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002084 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 }
2086 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2087 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302088 /*
Archana9d17bf42021-09-10 06:22:44 +05302089 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302090 * the source key would need to be exported as plaintext and re-imported
2091 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302092 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302093 * appropriate API invocations from the application, if needed.
2094 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002095 status = PSA_ERROR_NOT_SUPPORTED;
2096 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002097 }
Archana8a180362021-07-05 02:18:48 +05302098 /*
2099 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302100 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302101 * - For opaque keys this translates to an invocation of the drivers'
2102 * copy_key entry point through the dispatch layer.
2103 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002104 if (psa_key_lifetime_is_external(actual_attributes.lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2106 &storage_size);
2107 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302108 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 }
Archana374fe5b2021-09-08 15:50:28 +05302110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2112 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 }
Archana374fe5b2021-09-08 15:50:28 +05302115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 status = psa_driver_wrapper_copy_key(&actual_attributes,
2117 source_slot->key.data,
2118 source_slot->key.bytes,
2119 target_slot->key.data,
2120 target_slot->key.bytes,
2121 &target_slot->key.bytes);
2122 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 }
2125 } else {
2126 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302127 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 source_slot->key.bytes);
2129 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302130 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 }
Archana8a180362021-07-05 02:18:48 +05302132 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002134exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 if (status != PSA_SUCCESS) {
2136 psa_fail_key_creation(target_slot, driver);
2137 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002138
Ryan Everetta103ec92024-01-31 13:59:57 +00002139 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002142}
2143
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002144
2145
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002146/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002147/* Message digests */
2148/****************************************************************/
2149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002151{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002152 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 if (operation->id == 0) {
2154 return PSA_SUCCESS;
2155 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002158 operation->id = 0;
2159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002161}
2162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2164 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002165{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002166 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002167
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002168 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002170 status = PSA_ERROR_BAD_STATE;
2171 goto exit;
2172 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002175 status = PSA_ERROR_INVALID_ARGUMENT;
2176 goto exit;
2177 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002178
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002179 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2180 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002184
2185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (status != PSA_SUCCESS) {
2187 psa_hash_abort(operation);
2188 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002189
2190 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002191}
2192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2194 const uint8_t *input,
2195 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002196{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002197 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002200 status = PSA_ERROR_BAD_STATE;
2201 goto exit;
2202 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002203
Steven Cooremanc8288352021-03-04 14:02:19 +01002204 /* Don't require hash implementations to behave correctly on a
2205 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 if (input_length == 0) {
2207 return PSA_SUCCESS;
2208 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002211
2212exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 if (status != PSA_SUCCESS) {
2214 psa_hash_abort(operation);
2215 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002218}
2219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2221 uint8_t *hash,
2222 size_t hash_size,
2223 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002224{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002225 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 if (operation->id == 0) {
2227 return PSA_ERROR_BAD_STATE;
2228 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002229
Steven Cooreman1e582352021-02-18 17:24:37 +01002230 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 operation, hash, hash_size, hash_length);
2232 psa_hash_abort(operation);
2233 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002234}
2235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2237 const uint8_t *hash,
2238 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239{
Ronald Cron69a63422021-10-18 09:47:58 +02002240 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002241 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002242 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 operation,
2244 actual_hash, sizeof(actual_hash),
2245 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002248 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002252 status = PSA_ERROR_INVALID_SIGNATURE;
2253 goto exit;
2254 }
2255
Dave Rodgman78701152023-08-29 14:20:18 +01002256 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002257 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002259
2260exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2262 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002263 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002267}
2268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269psa_status_t psa_hash_compute(psa_algorithm_t alg,
2270 const uint8_t *input, size_t input_length,
2271 uint8_t *hash, size_t hash_size,
2272 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002273{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002274 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 if (!PSA_ALG_IS_HASH(alg)) {
2276 return PSA_ERROR_INVALID_ARGUMENT;
2277 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002278
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2280 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002281}
2282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283psa_status_t psa_hash_compare(psa_algorithm_t alg,
2284 const uint8_t *input, size_t input_length,
2285 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002286{
Ronald Cron69a63422021-10-18 09:47:58 +02002287 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002288 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 if (!PSA_ALG_IS_HASH(alg)) {
2291 return PSA_ERROR_INVALID_ARGUMENT;
2292 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002293
Steven Cooreman1e582352021-02-18 17:24:37 +01002294 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 alg, input, input_length,
2296 actual_hash, sizeof(actual_hash),
2297 &actual_hash_length);
2298 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002299 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 }
2301 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002302 status = PSA_ERROR_INVALID_SIGNATURE;
2303 goto exit;
2304 }
Dave Rodgman78701152023-08-29 14:20:18 +01002305 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002306 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002308
2309exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2311 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002312}
2313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2315 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002316{
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 if (source_operation->id == 0 ||
2318 target_operation->id != 0) {
2319 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002320 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2323 target_operation);
2324 if (status != PSA_SUCCESS) {
2325 psa_hash_abort(target_operation);
2326 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002329}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330
2331
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002333/* MAC */
2334/****************************************************************/
2335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002337{
Steven Cooremane6804192021-03-19 18:28:56 +01002338 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 if (operation->id == 0) {
2340 return PSA_SUCCESS;
2341 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002344 operation->mac_size = 0;
2345 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002346 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002349}
2350
Ronald Cron2dff3b22021-06-17 16:33:22 +02002351static psa_status_t psa_mac_finalize_alg_and_key_validation(
2352 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002353 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002355{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 psa_key_type_t key_type = psa_get_key_type(attributes);
2358 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 if (!PSA_ALG_IS_MAC(alg)) {
2361 return PSA_ERROR_INVALID_ARGUMENT;
2362 }
Steven Cooremane6804192021-03-19 18:28:56 +01002363
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002364 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 status = psa_mac_key_can_do(alg, key_type);
2366 if (status != PSA_SUCCESS) {
2367 return status;
2368 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002369
Steven Cooremand1a68f12021-05-10 11:13:41 +02002370 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002374 /* A very short MAC is too short for security since it can be
2375 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2376 * so we make this our minimum, even though 32 bits is still
2377 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002379 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2382 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002383 /* It's impossible to "truncate" to a larger length than the full length
2384 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002386 }
2387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002389 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2390 * that is disabled in the compile-time configuration. The result can
2391 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2392 * configuration into account. In this case, force a return of
2393 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2394 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2395 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2396 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2397 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002399 }
2400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002402}
2403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2405 mbedtls_svc_key_id_t key,
2406 psa_algorithm_t alg,
2407 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002408{
2409 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2410 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002411 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002412
2413 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002415 status = PSA_ERROR_BAD_STATE;
2416 goto exit;
2417 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418
2419 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 key,
2421 &slot,
2422 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2423 alg);
2424 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002425 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002427
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002428 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 &operation->mac_size);
2430 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002431 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433
2434 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002435 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 if (is_sign) {
2437 status = psa_driver_wrapper_mac_sign_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002438 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 slot->key.data,
2440 slot->key.bytes,
2441 alg);
2442 } else {
2443 status = psa_driver_wrapper_mac_verify_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002444 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 slot->key.data,
2446 slot->key.bytes,
2447 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002449
Gilles Peskinefbfac682018-07-08 20:51:54 +02002450exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 if (status != PSA_SUCCESS) {
2452 psa_mac_abort(operation);
2453 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002454
Ryan Everettfb9857f2024-02-14 12:16:41 +00002455 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002458}
2459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2461 mbedtls_svc_key_id_t key,
2462 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002463{
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002465}
2466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2468 mbedtls_svc_key_id_t key,
2469 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002470{
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002472}
2473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2475 const uint8_t *input,
2476 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002477{
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 if (operation->id == 0) {
2479 return PSA_ERROR_BAD_STATE;
2480 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002481
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002482 /* Don't require hash implementations to behave correctly on a
2483 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 if (input_length == 0) {
2485 return PSA_SUCCESS;
2486 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002487
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2489 input, input_length);
2490 if (status != PSA_SUCCESS) {
2491 psa_mac_abort(operation);
2492 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495}
2496
Gilles Peskine449bd832023-01-11 14:50:10 +01002497psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2498 uint8_t *mac,
2499 size_t mac_size,
2500 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002501{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002502 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2503 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002506 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002507 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002508 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002511 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002512 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002513 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002514
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002515 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2516 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002518 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002519 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002520 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002523 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002524 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002525 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 status = psa_driver_wrapper_mac_sign_finish(operation,
2528 mac, operation->mac_size,
2529 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002530
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002531exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002532 /* In case of success, set the potential excess room in the output buffer
2533 * to an invalid value, to avoid potentially leaking a longer MAC.
2534 * In case of error, set the output length and content to a safe default,
2535 * such that in case the caller misses an error check, the output would be
2536 * an unachievable MAC.
2537 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002539 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002540 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002541 }
mohammad16036df908f2018-04-02 08:34:15 -07002542
Paul Elliottdc42ca82023-02-24 18:11:59 +00002543 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002548}
2549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2551 const uint8_t *mac,
2552 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002553{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002554 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2555 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002558 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002559 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002560 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002563 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002564 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002565 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002568 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002569 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002570 }
2571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 status = psa_driver_wrapper_mac_verify_finish(operation,
2573 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002574
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002575exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002579}
2580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2582 psa_algorithm_t alg,
2583 const uint8_t *input,
2584 size_t input_length,
2585 uint8_t *mac,
2586 size_t mac_size,
2587 size_t *mac_length,
2588 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002589{
2590 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002591 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2592 psa_key_slot_t *slot;
2593 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594
Ronald Cron51131b52021-06-17 17:17:20 +02002595 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 key,
2597 &slot,
2598 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2599 alg);
2600 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002601 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002603
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002604 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 &operation_mac_size);
2606 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002607 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002611 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002612 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002613 }
2614
2615 status = psa_driver_wrapper_mac_compute(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002616 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 slot->key.data, slot->key.bytes,
2618 alg,
2619 input, input_length,
2620 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002621
2622exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002623 /* In case of success, set the potential excess room in the output buffer
2624 * to an invalid value, to avoid potentially leaking a longer MAC.
2625 * In case of error, set the output length and content to a safe default,
2626 * such that in case the caller misses an error check, the output would be
2627 * an unachievable MAC.
2628 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002630 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002631 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002632 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002633
2634 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002635
Ryan Everetta103ec92024-01-31 13:59:57 +00002636 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002639}
2640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002642 psa_algorithm_t alg,
2643 const uint8_t *input,
2644 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 uint8_t *mac,
2646 size_t mac_size,
2647 size_t *mac_length)
2648{
2649 return psa_mac_compute_internal(key, alg,
2650 input, input_length,
2651 mac, mac_size, mac_length, 1);
2652}
2653
2654psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2655 psa_algorithm_t alg,
2656 const uint8_t *input,
2657 size_t input_length,
2658 const uint8_t *mac,
2659 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002660{
2661 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002662 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2663 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 status = psa_mac_compute_internal(key, alg,
2666 input, input_length,
2667 actual_mac, sizeof(actual_mac),
2668 &actual_mac_length, 0);
2669 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002670 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002674 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002675 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002676 }
Dave Rodgman78701152023-08-29 14:20:18 +01002677 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002678 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002679 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002680 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002681
2682exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002686}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002687
Gilles Peskinee59236f2018-01-27 23:32:46 +01002688/****************************************************************/
2689/* Asymmetric cryptography */
2690/****************************************************************/
2691
Gilles Peskine449bd832023-01-11 14:50:10 +01002692static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2693 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002694{
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 if (input_is_message) {
2696 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2697 return PSA_ERROR_INVALID_ARGUMENT;
2698 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002699
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2701 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2702 return PSA_ERROR_INVALID_ARGUMENT;
2703 }
2704 }
2705 } else {
2706 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2707 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002708 }
2709 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002712}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2715 int input_is_message,
2716 psa_algorithm_t alg,
2717 const uint8_t *input,
2718 size_t input_length,
2719 uint8_t *signature,
2720 size_t signature_size,
2721 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002722{
2723 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2724 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2725 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002726
2727 *signature_length = 0;
2728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 status = psa_sign_verify_check_alg(input_is_message, alg);
2730 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002731 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002733
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002734 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002735 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002736 * buffer can in principle be empty since it doesn't actually have
2737 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 if (signature_size == 0) {
2739 return PSA_ERROR_BUFFER_TOO_SMALL;
2740 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002741
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002742 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 key, &slot,
2744 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2745 PSA_KEY_USAGE_SIGN_HASH,
2746 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002747
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002749 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002753 status = PSA_ERROR_INVALID_ARGUMENT;
2754 goto exit;
2755 }
2756
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002758 status = psa_driver_wrapper_sign_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002759 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002760 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 signature, signature_size, signature_length);
2762 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002763
2764 status = psa_driver_wrapper_sign_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002765 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002766 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002768 }
2769
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002770
2771exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002772 psa_wipe_tag_output_buffer(signature, status, signature_size,
2773 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002774
Ryan Everetta103ec92024-01-31 13:59:57 +00002775 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002778}
2779
Gilles Peskine449bd832023-01-11 14:50:10 +01002780static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2781 int input_is_message,
2782 psa_algorithm_t alg,
2783 const uint8_t *input,
2784 size_t input_length,
2785 const uint8_t *signature,
2786 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002787{
2788 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2789 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2790 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002791
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 status = psa_sign_verify_check_alg(input_is_message, alg);
2793 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002794 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002796
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002797 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 key, &slot,
2799 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2800 PSA_KEY_USAGE_VERIFY_HASH,
2801 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 if (status != PSA_SUCCESS) {
2804 return status;
2805 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002808 status = psa_driver_wrapper_verify_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002809 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002810 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 signature, signature_length);
2812 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002813 status = psa_driver_wrapper_verify_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002814 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002815 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002817 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002818
Ryan Everetta103ec92024-01-31 13:59:57 +00002819 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002820
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002822
2823}
2824
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002825psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002826 const psa_key_attributes_t *attributes,
2827 const uint8_t *key_buffer,
2828 size_t key_buffer_size,
2829 psa_algorithm_t alg,
2830 const uint8_t *input,
2831 size_t input_length,
2832 uint8_t *signature,
2833 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002835{
2836 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002837
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002839 size_t hash_length;
2840 uint8_t hash[PSA_HASH_MAX_SIZE];
2841
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002842 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 PSA_ALG_SIGN_GET_HASH(alg),
2844 input, input_length,
2845 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002846
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002848 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002850
2851 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 attributes, key_buffer, key_buffer_size,
2853 alg, hash, hash_length,
2854 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002855 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002858}
2859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2861 psa_algorithm_t alg,
2862 const uint8_t *input,
2863 size_t input_length,
2864 uint8_t *signature,
2865 size_t signature_size,
2866 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002867{
2868 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002869 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002871}
2872
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002873psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002874 const psa_key_attributes_t *attributes,
2875 const uint8_t *key_buffer,
2876 size_t key_buffer_size,
2877 psa_algorithm_t alg,
2878 const uint8_t *input,
2879 size_t input_length,
2880 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002882{
2883 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002884
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002886 size_t hash_length;
2887 uint8_t hash[PSA_HASH_MAX_SIZE];
2888
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002889 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 PSA_ALG_SIGN_GET_HASH(alg),
2891 input, input_length,
2892 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002893
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002895 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002897
2898 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 attributes, key_buffer, key_buffer_size,
2900 alg, hash, hash_length,
2901 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002902 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002903
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002905}
2906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2908 psa_algorithm_t alg,
2909 const uint8_t *input,
2910 size_t input_length,
2911 const uint8_t *signature,
2912 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002913{
2914 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002915 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002917}
2918
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002919psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002920 const psa_key_attributes_t *attributes,
2921 const uint8_t *key_buffer, size_t key_buffer_size,
2922 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002924{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002925 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2927 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002928#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2930 return mbedtls_psa_rsa_sign_hash(
2931 attributes,
2932 key_buffer, key_buffer_size,
2933 alg, hash, hash_length,
2934 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002935#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2936 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 } else {
2938 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002939 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002940 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002942#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2944 return mbedtls_psa_ecdsa_sign_hash(
2945 attributes,
2946 key_buffer, key_buffer_size,
2947 alg, hash, hash_length,
2948 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002949#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2950 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 } else {
2952 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002953 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002954 }
Ronald Cron4501c982021-03-19 20:09:32 +01002955
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 (void) key_buffer;
2957 (void) key_buffer_size;
2958 (void) hash;
2959 (void) hash_length;
2960 (void) signature;
2961 (void) signature_size;
2962 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002965}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2968 psa_algorithm_t alg,
2969 const uint8_t *hash,
2970 size_t hash_length,
2971 uint8_t *signature,
2972 size_t signature_size,
2973 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002974{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002975 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002976 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002978}
2979
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002980psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002981 const psa_key_attributes_t *attributes,
2982 const uint8_t *key_buffer, size_t key_buffer_size,
2983 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002985{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002986 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2988 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002989#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2991 return mbedtls_psa_rsa_verify_hash(
2992 attributes,
2993 key_buffer, key_buffer_size,
2994 alg, hash, hash_length,
2995 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002996#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2997 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 } else {
2999 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003000 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003001 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003003#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3005 return mbedtls_psa_ecdsa_verify_hash(
3006 attributes,
3007 key_buffer, key_buffer_size,
3008 alg, hash, hash_length,
3009 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003010#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3011 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 } else {
3013 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003014 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003015 }
Ronald Cron4501c982021-03-19 20:09:32 +01003016
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 (void) key_buffer;
3018 (void) key_buffer_size;
3019 (void) hash;
3020 (void) hash_length;
3021 (void) signature;
3022 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003025}
3026
Gilles Peskine449bd832023-01-11 14:50:10 +01003027psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3028 psa_algorithm_t alg,
3029 const uint8_t *hash,
3030 size_t hash_length,
3031 const uint8_t *signature,
3032 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003033{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003034 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003035 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003037}
3038
Gilles Peskine449bd832023-01-11 14:50:10 +01003039psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3040 psa_algorithm_t alg,
3041 const uint8_t *input,
3042 size_t input_length,
3043 const uint8_t *salt,
3044 size_t salt_length,
3045 uint8_t *output,
3046 size_t output_size,
3047 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003048{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003049 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003050 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003051 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003052
Darryl Green5cc689a2018-07-24 15:34:10 +01003053 (void) input;
3054 (void) input_length;
3055 (void) salt;
3056 (void) output;
3057 (void) output_size;
3058
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003059 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003060
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3062 return PSA_ERROR_INVALID_ARGUMENT;
3063 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003064
Valerio Setti5bb454a2024-01-15 10:43:16 +01003065 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3067 if (status != PSA_SUCCESS) {
3068 return status;
3069 }
3070 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3071 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003072 status = PSA_ERROR_INVALID_ARGUMENT;
3073 goto exit;
3074 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003075
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003076 status = psa_driver_wrapper_asymmetric_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003077 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003078 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003080exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003081 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003084}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3087 psa_algorithm_t alg,
3088 const uint8_t *input,
3089 size_t input_length,
3090 const uint8_t *salt,
3091 size_t salt_length,
3092 uint8_t *output,
3093 size_t output_size,
3094 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003095{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003096 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003097 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003098 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003099
Darryl Green5cc689a2018-07-24 15:34:10 +01003100 (void) input;
3101 (void) input_length;
3102 (void) salt;
3103 (void) output;
3104 (void) output_size;
3105
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003106 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003107
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3109 return PSA_ERROR_INVALID_ARGUMENT;
3110 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003111
Valerio Setti5bb454a2024-01-15 10:43:16 +01003112 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3114 if (status != PSA_SUCCESS) {
3115 return status;
3116 }
3117 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003118 status = PSA_ERROR_INVALID_ARGUMENT;
3119 goto exit;
3120 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003121
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003122 status = psa_driver_wrapper_asymmetric_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003123 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003124 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003126
3127exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003128 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003131}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003132
Paul Elliott9fe12f62022-11-30 19:16:02 +00003133/****************************************************************/
3134/* Asymmetric interruptible cryptography */
3135/****************************************************************/
3136
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003137static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3138
Paul Elliott9fe12f62022-11-30 19:16:02 +00003139void psa_interruptible_set_max_ops(uint32_t max_ops)
3140{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003141 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003142}
3143
3144uint32_t psa_interruptible_get_max_ops(void)
3145{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003146 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003147}
3148
Paul Elliott9fe12f62022-11-30 19:16:02 +00003149uint32_t psa_sign_hash_get_num_ops(
3150 const psa_sign_hash_interruptible_operation_t *operation)
3151{
Paul Elliott296ede92022-12-15 17:00:30 +00003152 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003153}
3154
3155uint32_t psa_verify_hash_get_num_ops(
3156 const psa_verify_hash_interruptible_operation_t *operation)
3157{
Paul Elliott296ede92022-12-15 17:00:30 +00003158 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003159}
3160
Paul Elliott59ad9452022-12-18 15:09:02 +00003161static psa_status_t psa_sign_hash_abort_internal(
3162 psa_sign_hash_interruptible_operation_t *operation)
3163{
3164 if (operation->id == 0) {
3165 /* The object has (apparently) been initialized but it is not (yet)
3166 * in use. It's ok to call abort on such an object, and there's
3167 * nothing to do. */
3168 return PSA_SUCCESS;
3169 }
3170
3171 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3172
3173 status = psa_driver_wrapper_sign_hash_abort(operation);
3174
3175 operation->id = 0;
3176
Paul Elliotte6145dc2023-02-07 12:51:21 +00003177 /* Do not clear either the error_occurred or num_ops elements here as they
3178 * only want to be cleared by the application calling abort, not by abort
3179 * being called at completion of an operation. */
3180
Paul Elliott59ad9452022-12-18 15:09:02 +00003181 return status;
3182}
3183
Paul Elliott9fe12f62022-11-30 19:16:02 +00003184psa_status_t psa_sign_hash_start(
3185 psa_sign_hash_interruptible_operation_t *operation,
3186 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3187 const uint8_t *hash, size_t hash_length)
3188{
3189 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3190 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3191 psa_key_slot_t *slot;
3192
Paul Elliottc9774412023-02-06 15:14:07 +00003193 /* Check that start has not been previously called, or operation has not
3194 * previously errored. */
3195 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003196 return PSA_ERROR_BAD_STATE;
3197 }
3198
Paul Elliott9fe12f62022-11-30 19:16:02 +00003199 status = psa_sign_verify_check_alg(0, alg);
3200 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003201 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003202 return status;
3203 }
3204
3205 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3206 PSA_KEY_USAGE_SIGN_HASH,
3207 alg);
3208
3209 if (status != PSA_SUCCESS) {
3210 goto exit;
3211 }
3212
3213 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3214 status = PSA_ERROR_INVALID_ARGUMENT;
3215 goto exit;
3216 }
3217
Paul Elliott296ede92022-12-15 17:00:30 +00003218 /* Ensure ops count gets reset, in case of operation re-use. */
3219 operation->num_ops = 0;
3220
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003221 status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003222 slot->key.data,
3223 slot->key.bytes, alg,
3224 hash, hash_length);
3225exit:
3226
3227 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003228 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003229 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003230 }
3231
Ryan Everettdcc03d52024-02-14 15:44:13 +00003232 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003233
Paul Elliottc9774412023-02-06 15:14:07 +00003234 if (unlock_status != PSA_SUCCESS) {
3235 operation->error_occurred = 1;
3236 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003237
Paul Elliottc9774412023-02-06 15:14:07 +00003238 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003239}
3240
3241
3242psa_status_t psa_sign_hash_complete(
3243 psa_sign_hash_interruptible_operation_t *operation,
3244 uint8_t *signature, size_t signature_size,
3245 size_t *signature_length)
3246{
3247 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3248
3249 *signature_length = 0;
3250
Paul Elliottc9774412023-02-06 15:14:07 +00003251 /* Check that start has been called first, and that operation has not
3252 * previously errored. */
3253 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003254 status = PSA_ERROR_BAD_STATE;
3255 goto exit;
3256 }
3257
Paul Elliott096abc42023-02-03 18:33:23 +00003258 /* Immediately reject a zero-length signature buffer. This guarantees that
3259 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003260 if (signature_size == 0) {
3261 status = PSA_ERROR_BUFFER_TOO_SMALL;
3262 goto exit;
3263 }
3264
3265 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3266 signature_size,
3267 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003268
Paul Elliott296ede92022-12-15 17:00:30 +00003269 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003270 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003271
Paul Elliottebe225c2023-02-10 14:32:53 +00003272exit:
3273
Paul Elliott59200a22023-02-21 15:07:40 +00003274 psa_wipe_tag_output_buffer(signature, status, signature_size,
3275 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003276
Paul Elliott53bb3122023-02-10 14:22:22 +00003277 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003278 if (status != PSA_SUCCESS) {
3279 operation->error_occurred = 1;
3280 }
3281
Paul Elliott59ad9452022-12-18 15:09:02 +00003282 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003283 }
3284
3285 return status;
3286}
3287
3288psa_status_t psa_sign_hash_abort(
3289 psa_sign_hash_interruptible_operation_t *operation)
3290{
Paul Elliott59ad9452022-12-18 15:09:02 +00003291 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3292
3293 status = psa_sign_hash_abort_internal(operation);
3294
3295 /* We clear the number of ops done here, so that it is not cleared when
3296 * the operation fails or succeeds, only on manual abort. */
3297 operation->num_ops = 0;
3298
Paul Elliottc9774412023-02-06 15:14:07 +00003299 /* Likewise, failure state. */
3300 operation->error_occurred = 0;
3301
Paul Elliott59ad9452022-12-18 15:09:02 +00003302 return status;
3303}
3304
3305static psa_status_t psa_verify_hash_abort_internal(
3306 psa_verify_hash_interruptible_operation_t *operation)
3307{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003308 if (operation->id == 0) {
3309 /* The object has (apparently) been initialized but it is not (yet)
3310 * in use. It's ok to call abort on such an object, and there's
3311 * nothing to do. */
3312 return PSA_SUCCESS;
3313 }
3314
Paul Elliott59ad9452022-12-18 15:09:02 +00003315 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3316
3317 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003318
3319 operation->id = 0;
3320
Paul Elliotte6145dc2023-02-07 12:51:21 +00003321 /* Do not clear either the error_occurred or num_ops elements here as they
3322 * only want to be cleared by the application calling abort, not by abort
3323 * being called at completion of an operation. */
3324
Paul Elliott59ad9452022-12-18 15:09:02 +00003325 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003326}
3327
3328psa_status_t psa_verify_hash_start(
3329 psa_verify_hash_interruptible_operation_t *operation,
3330 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3331 const uint8_t *hash, size_t hash_length,
3332 const uint8_t *signature, size_t signature_length)
3333{
3334 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3335 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3336 psa_key_slot_t *slot;
3337
Paul Elliottc9774412023-02-06 15:14:07 +00003338 /* Check that start has not been previously called, or operation has not
3339 * previously errored. */
3340 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003341 return PSA_ERROR_BAD_STATE;
3342 }
3343
3344 status = psa_sign_verify_check_alg(0, alg);
3345 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003346 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003347 return status;
3348 }
3349
3350 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3351 PSA_KEY_USAGE_VERIFY_HASH,
3352 alg);
3353
3354 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003355 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003356 return status;
3357 }
3358
Paul Elliott296ede92022-12-15 17:00:30 +00003359 /* Ensure ops count gets reset, in case of operation re-use. */
3360 operation->num_ops = 0;
3361
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003362 status = psa_driver_wrapper_verify_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003363 slot->key.data,
3364 slot->key.bytes,
3365 alg, hash, hash_length,
3366 signature, signature_length);
3367
3368 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003369 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003370 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003371 }
3372
Ryan Everett291267f2024-02-14 15:59:15 +00003373 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003374
Paul Elliottc9774412023-02-06 15:14:07 +00003375 if (unlock_status != PSA_SUCCESS) {
3376 operation->error_occurred = 1;
3377 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003378
Paul Elliottc9774412023-02-06 15:14:07 +00003379 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003380}
3381
3382psa_status_t psa_verify_hash_complete(
3383 psa_verify_hash_interruptible_operation_t *operation)
3384{
3385 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3386
Paul Elliottc9774412023-02-06 15:14:07 +00003387 /* Check that start has been called first, and that operation has not
3388 * previously errored. */
3389 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003390 status = PSA_ERROR_BAD_STATE;
3391 goto exit;
3392 }
3393
3394 status = psa_driver_wrapper_verify_hash_complete(operation);
3395
Paul Elliott296ede92022-12-15 17:00:30 +00003396 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003397 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003398 operation);
3399
Paul Elliottebe225c2023-02-10 14:32:53 +00003400exit:
3401
Paul Elliott9fe12f62022-11-30 19:16:02 +00003402 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003403 if (status != PSA_SUCCESS) {
3404 operation->error_occurred = 1;
3405 }
3406
Paul Elliott59ad9452022-12-18 15:09:02 +00003407 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003408 }
3409
3410 return status;
3411}
3412
3413psa_status_t psa_verify_hash_abort(
3414 psa_verify_hash_interruptible_operation_t *operation)
3415{
Paul Elliott59ad9452022-12-18 15:09:02 +00003416 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003417
Paul Elliott59ad9452022-12-18 15:09:02 +00003418 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003419
Paul Elliott59ad9452022-12-18 15:09:02 +00003420 /* We clear the number of ops done here, so that it is not cleared when
3421 * the operation fails or succeeds, only on manual abort. */
3422 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003423
Paul Elliottc9774412023-02-06 15:14:07 +00003424 /* Likewise, failure state. */
3425 operation->error_occurred = 0;
3426
Paul Elliott59ad9452022-12-18 15:09:02 +00003427 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003428}
3429
Paul Elliott588f8ed2022-12-02 18:10:26 +00003430/****************************************************************/
3431/* Asymmetric interruptible cryptography internal */
3432/* implementations */
3433/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003434
Paul Elliott588f8ed2022-12-02 18:10:26 +00003435void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3436{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003437
Paul Elliott588f8ed2022-12-02 18:10:26 +00003438#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3439 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3440 defined(MBEDTLS_ECP_RESTARTABLE)
3441
3442 /* Internal implementation uses zero to indicate infinite number max ops,
3443 * therefore avoid this value, and set to minimum possible. */
3444 if (max_ops == 0) {
3445 max_ops = 1;
3446 }
3447
Paul Elliott588f8ed2022-12-02 18:10:26 +00003448 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003449#else
3450 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003451#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3452 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3453 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3454}
3455
Paul Elliott588f8ed2022-12-02 18:10:26 +00003456uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003457 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003458{
3459#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3460 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3461 defined(MBEDTLS_ECP_RESTARTABLE)
3462
Paul Elliott93d9ca82023-02-15 18:14:21 +00003463 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003464#else
3465 (void) operation;
3466 return 0;
3467#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3468 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3469 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3470}
3471
3472uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003473 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003474{
3475 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3476 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3477 defined(MBEDTLS_ECP_RESTARTABLE)
3478
Paul Elliott93d9ca82023-02-15 18:14:21 +00003479 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003480#else
3481 (void) operation;
3482 return 0;
3483#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3484 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3485 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3486}
3487
3488psa_status_t mbedtls_psa_sign_hash_start(
3489 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3490 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3491 size_t key_buffer_size, psa_algorithm_t alg,
3492 const uint8_t *hash, size_t hash_length)
3493{
3494 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003495 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003496
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003497 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003498 return PSA_ERROR_NOT_SUPPORTED;
3499 }
3500
3501 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003502 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003503 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003504
3505#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003506 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3507 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003508
Paul Elliotta1c94092023-02-15 16:38:04 +00003509 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3510
Paul Elliott93d9ca82023-02-15 18:14:21 +00003511 /* Ensure num_ops is zero'ed in case of context re-use. */
3512 operation->num_ops = 0;
3513
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003514 status = mbedtls_psa_ecp_load_representation(attributes->type,
3515 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003516 key_buffer,
3517 key_buffer_size,
3518 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003519
Paul Elliott068fe072023-01-16 13:59:15 +00003520 if (status != PSA_SUCCESS) {
3521 return status;
3522 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003523
Paul Elliott1bc59df2023-02-05 13:41:57 +00003524 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003525 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003526
Paul Elliott068fe072023-01-16 13:59:15 +00003527 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003528 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003529 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003530
Paul Elliott0290a762023-02-09 14:30:24 +00003531 /* We only need to store the same length of hash as the private key size
3532 * here, it would be truncated by the internal implementation anyway. */
3533 required_hash_length = (hash_length < operation->coordinate_bytes ?
3534 hash_length : operation->coordinate_bytes);
3535
Paul Elliottba70ad42023-02-15 18:23:53 +00003536 if (required_hash_length > sizeof(operation->hash)) {
3537 /* Shouldn't happen, but better safe than sorry. */
3538 return PSA_ERROR_CORRUPTION_DETECTED;
3539 }
3540
Paul Elliott0290a762023-02-09 14:30:24 +00003541 memcpy(operation->hash, hash, required_hash_length);
3542 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003543
3544 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003545
3546#else
Paul Elliott068fe072023-01-16 13:59:15 +00003547 (void) operation;
3548 (void) key_buffer;
3549 (void) key_buffer_size;
3550 (void) alg;
3551 (void) hash;
3552 (void) hash_length;
3553 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003554 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003555
Paul Elliott068fe072023-01-16 13:59:15 +00003556 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003557#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3558 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3559 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003560}
3561
3562psa_status_t mbedtls_psa_sign_hash_complete(
3563 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3564 uint8_t *signature, size_t signature_size,
3565 size_t *signature_length)
3566{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003567#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3568 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3569 defined(MBEDTLS_ECP_RESTARTABLE)
3570
Paul Elliotta1c94092023-02-15 16:38:04 +00003571 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003572 mbedtls_mpi r;
3573 mbedtls_mpi s;
3574
Paul Elliott46845252023-02-03 14:59:11 +00003575 mbedtls_mpi_init(&r);
3576 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003577
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003578 /* Ensure max_ops is set to the current value (or default). */
3579 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3580
Paul Elliotta1c94092023-02-15 16:38:04 +00003581 if (signature_size < 2 * operation->coordinate_bytes) {
3582 status = PSA_ERROR_BUFFER_TOO_SMALL;
3583 goto exit;
3584 }
3585
Paul Elliott588f8ed2022-12-02 18:10:26 +00003586 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003587
Paul Elliott588f8ed2022-12-02 18:10:26 +00003588#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3589 status = mbedtls_to_psa_error(
3590 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003591 &r,
3592 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003593 &operation->ctx->d,
3594 operation->hash,
3595 operation->hash_length,
3596 operation->md_alg,
3597 mbedtls_psa_get_random,
3598 MBEDTLS_PSA_RANDOM_STATE,
3599 &operation->restart_ctx));
3600#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003601 status = PSA_ERROR_NOT_SUPPORTED;
3602 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003603#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3604 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003605 status = mbedtls_to_psa_error(
3606 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003607 &r,
3608 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003609 &operation->ctx->d,
3610 operation->hash,
3611 operation->hash_length,
3612 mbedtls_psa_get_random,
3613 MBEDTLS_PSA_RANDOM_STATE,
3614 mbedtls_psa_get_random,
3615 MBEDTLS_PSA_RANDOM_STATE,
3616 &operation->restart_ctx));
3617 }
3618
Paul Elliottf8e5b562023-02-19 18:43:45 +00003619 /* Hide the fact that the restart context only holds a delta of number of
3620 * ops done during the last operation, not an absolute value. */
3621 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3622
Paul Elliott724bd252023-02-08 12:35:08 +00003623 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003624 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003625 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003626 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003627 operation->coordinate_bytes)
3628 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003629
3630 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003631 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003632 }
3633
3634 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003635 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003636 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003637 operation->coordinate_bytes,
3638 operation->coordinate_bytes)
3639 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640
3641 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003642 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003643 }
3644
Paul Elliott1bc59df2023-02-05 13:41:57 +00003645 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646
Paul Elliott724bd252023-02-08 12:35:08 +00003647 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003648 }
Paul Elliott724bd252023-02-08 12:35:08 +00003649
3650exit:
3651
3652 mbedtls_mpi_free(&r);
3653 mbedtls_mpi_free(&s);
3654 return status;
3655
Paul Elliott588f8ed2022-12-02 18:10:26 +00003656 #else
3657
3658 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003659 (void) signature;
3660 (void) signature_size;
3661 (void) signature_length;
3662
3663 return PSA_ERROR_NOT_SUPPORTED;
3664
3665#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3666 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3667 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3668}
3669
3670psa_status_t mbedtls_psa_sign_hash_abort(
3671 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3672{
3673
3674#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3675 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3676 defined(MBEDTLS_ECP_RESTARTABLE)
3677
3678 if (operation->ctx) {
3679 mbedtls_ecdsa_free(operation->ctx);
3680 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003681 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003682 }
3683
3684 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3685
Paul Elliott93d9ca82023-02-15 18:14:21 +00003686 operation->num_ops = 0;
3687
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688 return PSA_SUCCESS;
3689
3690#else
3691
3692 (void) operation;
3693
3694 return PSA_ERROR_NOT_SUPPORTED;
3695
3696#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3697 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3698 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3699}
3700
3701psa_status_t mbedtls_psa_verify_hash_start(
3702 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3703 const psa_key_attributes_t *attributes,
3704 const uint8_t *key_buffer, size_t key_buffer_size,
3705 psa_algorithm_t alg,
3706 const uint8_t *hash, size_t hash_length,
3707 const uint8_t *signature, size_t signature_length)
3708{
3709 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003710 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003711 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003713 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003714 return PSA_ERROR_NOT_SUPPORTED;
3715 }
3716
3717 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003718 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003719 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003720
3721#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003722 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3723 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003724
Paul Elliotta1c94092023-02-15 16:38:04 +00003725 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3726 mbedtls_mpi_init(&operation->r);
3727 mbedtls_mpi_init(&operation->s);
3728
Paul Elliott93d9ca82023-02-15 18:14:21 +00003729 /* Ensure num_ops is zero'ed in case of context re-use. */
3730 operation->num_ops = 0;
3731
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003732 status = mbedtls_psa_ecp_load_representation(attributes->type,
3733 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003734 key_buffer,
3735 key_buffer_size,
3736 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003737
Paul Elliott068fe072023-01-16 13:59:15 +00003738 if (status != PSA_SUCCESS) {
3739 return status;
3740 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003741
Paul Elliottc569fc22023-02-10 13:02:54 +00003742 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003743
Paul Elliott1bc59df2023-02-05 13:41:57 +00003744 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003745 return PSA_ERROR_INVALID_SIGNATURE;
3746 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003747
Paul Elliott068fe072023-01-16 13:59:15 +00003748 status = mbedtls_to_psa_error(
3749 mbedtls_mpi_read_binary(&operation->r,
3750 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003751 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003752
Paul Elliott068fe072023-01-16 13:59:15 +00003753 if (status != PSA_SUCCESS) {
3754 return status;
3755 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003756
Paul Elliott068fe072023-01-16 13:59:15 +00003757 status = mbedtls_to_psa_error(
3758 mbedtls_mpi_read_binary(&operation->s,
3759 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003760 coordinate_bytes,
3761 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003762
Paul Elliott068fe072023-01-16 13:59:15 +00003763 if (status != PSA_SUCCESS) {
3764 return status;
3765 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003766
Paul Elliott2c9843f2023-02-15 17:32:42 +00003767 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003768
Paul Elliott2c9843f2023-02-15 17:32:42 +00003769 if (status != PSA_SUCCESS) {
3770 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003771 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003772
Paul Elliott0290a762023-02-09 14:30:24 +00003773 /* We only need to store the same length of hash as the private key size
3774 * here, it would be truncated by the internal implementation anyway. */
3775 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3776 coordinate_bytes);
3777
Paul Elliottba70ad42023-02-15 18:23:53 +00003778 if (required_hash_length > sizeof(operation->hash)) {
3779 /* Shouldn't happen, but better safe than sorry. */
3780 return PSA_ERROR_CORRUPTION_DETECTED;
3781 }
3782
Paul Elliott0290a762023-02-09 14:30:24 +00003783 memcpy(operation->hash, hash, required_hash_length);
3784 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003785
3786 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003787#else
Paul Elliott068fe072023-01-16 13:59:15 +00003788 (void) operation;
3789 (void) key_buffer;
3790 (void) key_buffer_size;
3791 (void) alg;
3792 (void) hash;
3793 (void) hash_length;
3794 (void) signature;
3795 (void) signature_length;
3796 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003797 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003798 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003799
Paul Elliott068fe072023-01-16 13:59:15 +00003800 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003801#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3802 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3803 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003804}
3805
3806psa_status_t mbedtls_psa_verify_hash_complete(
3807 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3808{
3809
3810#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3811 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3812 defined(MBEDTLS_ECP_RESTARTABLE)
3813
Paul Elliottf8e5b562023-02-19 18:43:45 +00003814 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3815
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003816 /* Ensure max_ops is set to the current value (or default). */
3817 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3818
Paul Elliottf8e5b562023-02-19 18:43:45 +00003819 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003820 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3821 operation->hash,
3822 operation->hash_length,
3823 &operation->ctx->Q,
3824 &operation->r,
3825 &operation->s,
3826 &operation->restart_ctx));
3827
Paul Elliottf8e5b562023-02-19 18:43:45 +00003828 /* Hide the fact that the restart context only holds a delta of number of
3829 * ops done during the last operation, not an absolute value. */
3830 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3831
3832 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003833#else
3834 (void) operation;
3835
3836 return PSA_ERROR_NOT_SUPPORTED;
3837
3838#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3839 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3840 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3841}
3842
3843psa_status_t mbedtls_psa_verify_hash_abort(
3844 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3845{
3846
3847#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3848 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3849 defined(MBEDTLS_ECP_RESTARTABLE)
3850
3851 if (operation->ctx) {
3852 mbedtls_ecdsa_free(operation->ctx);
3853 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003854 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003855 }
3856
3857 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3858
Paul Elliott93d9ca82023-02-15 18:14:21 +00003859 operation->num_ops = 0;
3860
Paul Elliott588f8ed2022-12-02 18:10:26 +00003861 mbedtls_mpi_free(&operation->r);
3862 mbedtls_mpi_free(&operation->s);
3863
3864 return PSA_SUCCESS;
3865
3866#else
3867 (void) operation;
3868
3869 return PSA_ERROR_NOT_SUPPORTED;
3870
3871#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3872 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3873 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3874}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003875
mohammad1603503973b2018-03-12 15:59:30 +02003876/****************************************************************/
3877/* Symmetric cryptography */
3878/****************************************************************/
3879
Gilles Peskine449bd832023-01-11 14:50:10 +01003880static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3881 mbedtls_svc_key_id_t key,
3882 psa_algorithm_t alg,
3883 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003884{
3885 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3886 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003887 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003888 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3889 PSA_KEY_USAGE_ENCRYPT :
3890 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003891
3892 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003893 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003894 status = PSA_ERROR_BAD_STATE;
3895 goto exit;
3896 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003897
Gilles Peskine449bd832023-01-11 14:50:10 +01003898 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003899 status = PSA_ERROR_INVALID_ARGUMENT;
3900 goto exit;
3901 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003902
Gilles Peskine449bd832023-01-11 14:50:10 +01003903 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3904 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003905 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003906 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003907
Ronald Cron49fafa92021-03-10 08:34:23 +01003908 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003909 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003910 * so we only set it (in the driver wrapper) after resources have been
3911 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003912 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003913 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003914 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003916 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003917 }
3918 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003919
3920 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003921 if (cipher_operation == MBEDTLS_ENCRYPT) {
3922 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003923 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 slot->key.data,
3925 slot->key.bytes,
3926 alg);
3927 } else {
3928 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003929 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 slot->key.data,
3931 slot->key.bytes,
3932 alg);
3933 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003934
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003935exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003936 if (status != PSA_SUCCESS) {
3937 psa_cipher_abort(operation);
3938 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003939
Ryan Everettc0053cc2024-02-14 16:27:13 +00003940 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003941
Gilles Peskine449bd832023-01-11 14:50:10 +01003942 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003943}
3944
Gilles Peskine449bd832023-01-11 14:50:10 +01003945psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3946 mbedtls_svc_key_id_t key,
3947 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003948{
Gilles Peskine449bd832023-01-11 14:50:10 +01003949 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003950}
3951
Gilles Peskine449bd832023-01-11 14:50:10 +01003952psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3953 mbedtls_svc_key_id_t key,
3954 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003955{
Gilles Peskine449bd832023-01-11 14:50:10 +01003956 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003957}
3958
Gilles Peskine449bd832023-01-11 14:50:10 +01003959psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3960 uint8_t *iv,
3961 size_t iv_size,
3962 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003963{
Ronald Cron6d051732020-10-01 14:10:20 +02003964 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003965 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07003966 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003967
Gilles Peskine449bd832023-01-11 14:50:10 +01003968 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003969 status = PSA_ERROR_BAD_STATE;
3970 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003971 }
3972
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003974 status = PSA_ERROR_BAD_STATE;
3975 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003976 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003977
Ronald Cron2fb90522021-07-05 12:31:44 +02003978 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003980 status = PSA_ERROR_BUFFER_TOO_SMALL;
3981 goto exit;
3982 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003983
Gilles Peskine449bd832023-01-11 14:50:10 +01003984 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02003985 status = PSA_ERROR_GENERIC_ERROR;
3986 goto exit;
3987 }
3988
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 status = psa_generate_random(local_iv, default_iv_length);
3990 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003991 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 }
Ronald Cron5618a392021-03-26 09:52:26 +01003993
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 status = psa_driver_wrapper_cipher_set_iv(operation,
3995 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003996
3997exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 if (status == PSA_SUCCESS) {
3999 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004000 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004001 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004003 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004004 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004005 }
Ronald Cron6d051732020-10-01 14:10:20 +02004006
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004008}
4009
Gilles Peskine449bd832023-01-11 14:50:10 +01004010psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4011 const uint8_t *iv,
4012 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004013{
Ronald Cron6d051732020-10-01 14:10:20 +02004014 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004015
Gilles Peskine449bd832023-01-11 14:50:10 +01004016 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004017 status = PSA_ERROR_BAD_STATE;
4018 goto exit;
4019 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004020
Gilles Peskine449bd832023-01-11 14:50:10 +01004021 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004022 status = PSA_ERROR_BAD_STATE;
4023 goto exit;
4024 }
Ronald Crona0d68172021-03-26 10:15:08 +01004025
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004027 status = PSA_ERROR_INVALID_ARGUMENT;
4028 goto exit;
4029 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004030
Gilles Peskine449bd832023-01-11 14:50:10 +01004031 status = psa_driver_wrapper_cipher_set_iv(operation,
4032 iv,
4033 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004034
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004035exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004037 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 } else {
4039 psa_cipher_abort(operation);
4040 }
4041 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004042}
4043
Gilles Peskine449bd832023-01-11 14:50:10 +01004044psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4045 const uint8_t *input,
4046 size_t input_length,
4047 uint8_t *output,
4048 size_t output_size,
4049 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004050{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004052
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004054 status = PSA_ERROR_BAD_STATE;
4055 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004056 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004057
Gilles Peskine449bd832023-01-11 14:50:10 +01004058 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004059 status = PSA_ERROR_BAD_STATE;
4060 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004061 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004062
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 status = psa_driver_wrapper_cipher_update(operation,
4064 input,
4065 input_length,
4066 output,
4067 output_size,
4068 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004069
4070exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 if (status != PSA_SUCCESS) {
4072 psa_cipher_abort(operation);
4073 }
Ronald Cron6d051732020-10-01 14:10:20 +02004074
Gilles Peskine449bd832023-01-11 14:50:10 +01004075 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004076}
4077
Gilles Peskine449bd832023-01-11 14:50:10 +01004078psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4079 uint8_t *output,
4080 size_t output_size,
4081 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004082{
David Saadab4ecc272019-02-14 13:48:10 +02004083 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004084
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004086 status = PSA_ERROR_BAD_STATE;
4087 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004088 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004089
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004091 status = PSA_ERROR_BAD_STATE;
4092 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004093 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004094
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 status = psa_driver_wrapper_cipher_finish(operation,
4096 output,
4097 output_size,
4098 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004099
4100exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 if (status == PSA_SUCCESS) {
4102 return psa_cipher_abort(operation);
4103 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004104 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004106
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004108 }
mohammad1603503973b2018-03-12 15:59:30 +02004109}
4110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004112{
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004114 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004115 * in use. It's ok to call abort on such an object, and there's
4116 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004118 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004119
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004121
Ronald Cron49fafa92021-03-10 08:34:23 +01004122 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004123 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004124 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004125
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004127}
4128
Gilles Peskine449bd832023-01-11 14:50:10 +01004129psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4130 psa_algorithm_t alg,
4131 const uint8_t *input,
4132 size_t input_length,
4133 uint8_t *output,
4134 size_t output_size,
4135 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004136{
4137 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004138 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004139 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004140 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4141 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004144 status = PSA_ERROR_INVALID_ARGUMENT;
4145 goto exit;
4146 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004147
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4149 PSA_KEY_USAGE_ENCRYPT,
4150 alg);
4151 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4156 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004157 status = PSA_ERROR_GENERIC_ERROR;
4158 goto exit;
4159 }
4160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 if (default_iv_length > 0) {
4162 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004163 status = PSA_ERROR_BUFFER_TOO_SMALL;
4164 goto exit;
4165 }
4166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 status = psa_generate_random(local_iv, default_iv_length);
4168 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004169 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004171 }
4172
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004173 status = psa_driver_wrapper_cipher_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004174 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004175 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004176 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004178
4179exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004180 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004182 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004183 }
Ronald Cron23919522021-07-08 19:00:07 +02004184
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 if (status == PSA_SUCCESS) {
4186 if (default_iv_length > 0) {
4187 memcpy(output, local_iv, default_iv_length);
4188 }
4189 *output_length += default_iv_length;
4190 } else {
4191 *output_length = 0;
4192 }
4193
4194 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004195}
4196
Gilles Peskine449bd832023-01-11 14:50:10 +01004197psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4198 psa_algorithm_t alg,
4199 const uint8_t *input,
4200 size_t input_length,
4201 uint8_t *output,
4202 size_t output_size,
4203 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004204{
4205 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004206 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004207 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004208
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004210 status = PSA_ERROR_INVALID_ARGUMENT;
4211 goto exit;
4212 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004213
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4215 PSA_KEY_USAGE_DECRYPT,
4216 alg);
4217 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004218 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4222 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004223 status = PSA_ERROR_INVALID_ARGUMENT;
4224 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004226 status = PSA_ERROR_INVALID_ARGUMENT;
4227 goto exit;
4228 }
4229
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004230 status = psa_driver_wrapper_cipher_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004231 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004232 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004234
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004235exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004236 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004238 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004240
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004242 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 }
Ronald Cron23919522021-07-08 19:00:07 +02004244
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004246}
4247
4248
mohammad16035955c982018-04-26 00:53:03 +03004249/****************************************************************/
4250/* AEAD */
4251/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004252
Paul Elliottbaff51c2021-09-28 17:44:45 +01004253/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004254static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004255{
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004257}
4258
4259/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004260static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4261 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004262{
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004266#if defined(PSA_WANT_ALG_GCM)
4267 case PSA_ALG_GCM:
4268 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 * arbitrarily large nonces. Please note that we do not generally
4270 * recommend the usage of nonces of greater length than
4271 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4272 * size, which can then lead to collisions if you encrypt a very
4273 * large number of messages.*/
4274 if (nonce_length != 0) {
4275 return PSA_SUCCESS;
4276 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004277 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004278#endif /* PSA_WANT_ALG_GCM */
4279#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004280 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 if (nonce_length >= 7 && nonce_length <= 13) {
4282 return PSA_SUCCESS;
4283 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004284 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004285#endif /* PSA_WANT_ALG_CCM */
4286#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004287 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004288 if (nonce_length == 12) {
4289 return PSA_SUCCESS;
4290 } else if (nonce_length == 8) {
4291 return PSA_ERROR_NOT_SUPPORTED;
4292 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004293 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004294#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004295 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004296 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004298 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004301}
4302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004304{
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4306 return PSA_ERROR_INVALID_ARGUMENT;
4307 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004308
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004310}
4311
Gilles Peskine449bd832023-01-11 14:50:10 +01004312psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4313 psa_algorithm_t alg,
4314 const uint8_t *nonce,
4315 size_t nonce_length,
4316 const uint8_t *additional_data,
4317 size_t additional_data_length,
4318 const uint8_t *plaintext,
4319 size_t plaintext_length,
4320 uint8_t *ciphertext,
4321 size_t ciphertext_size,
4322 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004323{
Ronald Cron215633c2021-03-16 17:15:37 +01004324 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4325 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004326
mohammad1603f08a5502018-06-03 15:05:47 +03004327 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004328
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 status = psa_aead_check_algorithm(alg);
4330 if (status != PSA_SUCCESS) {
4331 return status;
4332 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004333
Ronald Cron9a986162021-03-26 12:40:07 +01004334 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4336 if (status != PSA_SUCCESS) {
4337 return status;
4338 }
mohammad16035955c982018-04-26 00:53:03 +03004339
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 status = psa_aead_check_nonce_length(alg, nonce_length);
4341 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004342 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004343 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004344
Ronald Cronde822812021-03-17 16:08:20 +01004345 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004346 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004347 alg,
4348 nonce, nonce_length,
4349 additional_data, additional_data_length,
4350 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004352
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4354 memset(ciphertext, 0, ciphertext_size);
4355 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004356
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004357exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004358 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004361}
4362
Gilles Peskine449bd832023-01-11 14:50:10 +01004363psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4364 psa_algorithm_t alg,
4365 const uint8_t *nonce,
4366 size_t nonce_length,
4367 const uint8_t *additional_data,
4368 size_t additional_data_length,
4369 const uint8_t *ciphertext,
4370 size_t ciphertext_length,
4371 uint8_t *plaintext,
4372 size_t plaintext_size,
4373 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004374{
Ronald Cron215633c2021-03-16 17:15:37 +01004375 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4376 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004377
Gilles Peskineee652a32018-06-01 19:23:52 +02004378 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004379
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 status = psa_aead_check_algorithm(alg);
4381 if (status != PSA_SUCCESS) {
4382 return status;
4383 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004384
Ronald Cron9a986162021-03-26 12:40:07 +01004385 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4387 if (status != PSA_SUCCESS) {
4388 return status;
4389 }
mohammad16035955c982018-04-26 00:53:03 +03004390
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 status = psa_aead_check_nonce_length(alg, nonce_length);
4392 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004393 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004395
Ronald Cronde822812021-03-17 16:08:20 +01004396 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004397 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004398 alg,
4399 nonce, nonce_length,
4400 additional_data, additional_data_length,
4401 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004403
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 if (status != PSA_SUCCESS && plaintext_size != 0) {
4405 memset(plaintext, 0, plaintext_size);
4406 }
mohammad160360a64d02018-06-03 17:20:42 +03004407
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004408exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004409 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004410
Gilles Peskine449bd832023-01-11 14:50:10 +01004411 return status;
mohammad16035955c982018-04-26 00:53:03 +03004412}
4413
Gilles Peskine449bd832023-01-11 14:50:10 +01004414static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4415{
4416 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004417
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004419#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004421 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4423 return PSA_ERROR_INVALID_ARGUMENT;
4424 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004425 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004426#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004427
Przemek Stekiel86679c72022-10-06 17:06:56 +02004428#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004430 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4432 return PSA_ERROR_INVALID_ARGUMENT;
4433 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004434 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004435#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004436
Przemek Stekiel86679c72022-10-06 17:06:56 +02004437#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004439 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 if (tag_len != 16) {
4441 return PSA_ERROR_INVALID_ARGUMENT;
4442 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004443 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004444#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004445
4446 default:
4447 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004449 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004451}
4452
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004453/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004454static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4455 int is_encrypt,
4456 mbedtls_svc_key_id_t key,
4457 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004458{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004459 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4460 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4461 psa_key_slot_t *slot = NULL;
4462 psa_key_usage_t key_usage = 0;
4463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 status = psa_aead_check_algorithm(alg);
4465 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004466 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004470 status = PSA_ERROR_BAD_STATE;
4471 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004472 }
4473
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 if (operation->nonce_set || operation->lengths_set ||
4475 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004476 status = PSA_ERROR_BAD_STATE;
4477 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004478 }
4479
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004481 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004483 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004485
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4487 alg);
4488 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004489 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004491
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004493 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004495
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 if (is_encrypt) {
4497 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004498 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 slot->key.data,
4500 slot->key.bytes,
4501 alg);
4502 } else {
4503 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004504 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 slot->key.data,
4506 slot->key.bytes,
4507 alg);
4508 }
4509 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004510 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004512
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004513 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004514
4515exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00004516 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004517
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004519 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004521 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 } else {
4523 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004524 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004527}
4528
Paul Elliott302ff6b2021-04-20 18:10:30 +01004529/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004530psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4531 mbedtls_svc_key_id_t key,
4532 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004533{
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004535}
4536
4537/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004538psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4539 mbedtls_svc_key_id_t key,
4540 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004541{
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004543}
4544
4545/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004546psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4547 uint8_t *nonce,
4548 size_t nonce_size,
4549 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004550{
4551 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004552 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004553 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004554
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004555 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004556
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004558 status = PSA_ERROR_BAD_STATE;
4559 goto exit;
4560 }
4561
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004563 status = PSA_ERROR_BAD_STATE;
4564 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004565 }
4566
Paul Elliotte193ea82021-10-01 13:00:16 +01004567 /* For CCM, this size may not be correct according to the PSA
4568 * specification. The PSA Crypto 1.0.1 specification states:
4569 *
4570 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4571 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4572 *
4573 * However this restriction that L has to be the smallest integer is not
4574 * applied in practice, and it is not implementable here since the
4575 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4577 operation->alg);
4578 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004579 status = PSA_ERROR_BUFFER_TOO_SMALL;
4580 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004581 }
4582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 status = psa_generate_random(local_nonce, required_nonce_size);
4584 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004585 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004587
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004589
Paul Elliott39dc6b82021-05-11 19:16:09 +01004590exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 if (status == PSA_SUCCESS) {
4592 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004593 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 } else {
4595 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004596 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004597
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004599}
4600
4601/* Set the nonce for a multipart authenticated encryption or decryption
4602 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004603psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4604 const uint8_t *nonce,
4605 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004606{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004607 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4608
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004610 status = PSA_ERROR_BAD_STATE;
4611 goto exit;
4612 }
4613
Gilles Peskine449bd832023-01-11 14:50:10 +01004614 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004615 status = PSA_ERROR_BAD_STATE;
4616 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004617 }
4618
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4620 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004621 status = PSA_ERROR_INVALID_ARGUMENT;
4622 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004623 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004624
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4626 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004627
Paul Elliott39dc6b82021-05-11 19:16:09 +01004628exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004630 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 } else {
4632 psa_aead_abort(operation);
4633 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004636}
4637
4638/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004639psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4640 size_t ad_length,
4641 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004642{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004643 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4644
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004646 status = PSA_ERROR_BAD_STATE;
4647 goto exit;
4648 }
4649
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 if (operation->lengths_set || operation->ad_started ||
4651 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004652 status = PSA_ERROR_BAD_STATE;
4653 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004654 }
4655
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004657#if defined(PSA_WANT_ALG_GCM)
4658 case PSA_ALG_GCM:
4659 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 * bits. Without the guard this code will generate warnings on 32bit
4661 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004662#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 if (((uint64_t) ad_length) >> 61 != 0 ||
4664 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004665 status = PSA_ERROR_INVALID_ARGUMENT;
4666 goto exit;
4667 }
Paul Elliott325d3742021-09-27 17:56:28 +01004668#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004669 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004670#endif /* PSA_WANT_ALG_GCM */
4671#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004672 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004674 status = PSA_ERROR_INVALID_ARGUMENT;
4675 goto exit;
4676 }
4677 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004678#endif /* PSA_WANT_ALG_CCM */
4679#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004680 case PSA_ALG_CHACHA20_POLY1305:
4681 /* No length restrictions for ChaChaPoly. */
4682 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004683#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004684 default:
4685 break;
4686 }
Paul Elliott325d3742021-09-27 17:56:28 +01004687
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4689 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004690
Paul Elliott39dc6b82021-05-11 19:16:09 +01004691exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004693 operation->ad_remaining = ad_length;
4694 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004695 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 } else {
4697 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004698 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004699
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004701}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004702
4703/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004704psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4705 const uint8_t *input,
4706 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004707{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004708 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4709
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004711 status = PSA_ERROR_BAD_STATE;
4712 goto exit;
4713 }
4714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004716 status = PSA_ERROR_BAD_STATE;
4717 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004718 }
4719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if (operation->lengths_set) {
4721 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004722 status = PSA_ERROR_INVALID_ARGUMENT;
4723 goto exit;
4724 }
4725
4726 operation->ad_remaining -= input_length;
4727 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004728#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004730 status = PSA_ERROR_BAD_STATE;
4731 goto exit;
4732 }
4733#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004734
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 status = psa_driver_wrapper_aead_update_ad(operation, input,
4736 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004737
Paul Elliott39dc6b82021-05-11 19:16:09 +01004738exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004740 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 } else {
4742 psa_aead_abort(operation);
4743 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004746}
4747
4748/* Encrypt or decrypt a message fragment in an active multipart AEAD
4749 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004750psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4751 const uint8_t *input,
4752 size_t input_length,
4753 uint8_t *output,
4754 size_t output_size,
4755 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004756{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004757 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004758
4759 *output_length = 0;
4760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004762 status = PSA_ERROR_BAD_STATE;
4763 goto exit;
4764 }
4765
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004767 status = PSA_ERROR_BAD_STATE;
4768 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004769 }
4770
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004772 /* Additional data length was supplied, but not all the additional
4773 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004775 status = PSA_ERROR_INVALID_ARGUMENT;
4776 goto exit;
4777 }
4778
4779 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004781 status = PSA_ERROR_INVALID_ARGUMENT;
4782 goto exit;
4783 }
4784
4785 operation->body_remaining -= input_length;
4786 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004787#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004789 status = PSA_ERROR_BAD_STATE;
4790 goto exit;
4791 }
4792#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004793
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4795 output, output_size,
4796 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004797
Paul Elliott39dc6b82021-05-11 19:16:09 +01004798exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004800 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 } else {
4802 psa_aead_abort(operation);
4803 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004804
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004806}
4807
Gilles Peskine449bd832023-01-11 14:50:10 +01004808static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004809{
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 if (operation->id == 0 || !operation->nonce_set) {
4811 return PSA_ERROR_BAD_STATE;
4812 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004813
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4815 operation->body_remaining != 0)) {
4816 return PSA_ERROR_INVALID_ARGUMENT;
4817 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004818
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004820}
4821
Paul Elliott302ff6b2021-04-20 18:10:30 +01004822/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004823psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4824 uint8_t *ciphertext,
4825 size_t ciphertext_size,
4826 size_t *ciphertext_length,
4827 uint8_t *tag,
4828 size_t tag_size,
4829 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004830{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004831 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4832
Paul Elliott302ff6b2021-04-20 18:10:30 +01004833 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004834 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004835
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 status = psa_aead_final_checks(operation);
4837 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004838 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004840
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004842 status = PSA_ERROR_BAD_STATE;
4843 goto exit;
4844 }
4845
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4847 ciphertext_size,
4848 ciphertext_length,
4849 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004850
Paul Elliott39dc6b82021-05-11 19:16:09 +01004851exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004852
4853
Paul Elliottf47b0952021-05-21 18:02:33 +01004854 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004855 * the zero tag size, make sure the tag is set to something implausible.
4856 * Even if the operation succeeds, make sure we clear the rest of the
4857 * buffer to prevent potential leakage of anything previously placed in
4858 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004859 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004860
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004864}
4865
4866/* Finish authenticating and decrypting a message in a multipart AEAD
4867 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004868psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4869 uint8_t *plaintext,
4870 size_t plaintext_size,
4871 size_t *plaintext_length,
4872 const uint8_t *tag,
4873 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004874{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004875 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4876
Paul Elliott302ff6b2021-04-20 18:10:30 +01004877 *plaintext_length = 0;
4878
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 status = psa_aead_final_checks(operation);
4880 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004881 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004885 status = PSA_ERROR_BAD_STATE;
4886 goto exit;
4887 }
4888
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4890 plaintext_size,
4891 plaintext_length,
4892 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004893
4894exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004896
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004898}
4899
4900/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004901psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004902{
4903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4904
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004906 /* The object has (apparently) been initialized but it is not (yet)
4907 * in use. It's ok to call abort on such an object, and there's
4908 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004910 }
4911
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004913
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004915
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004917}
4918
Gilles Peskinee59236f2018-01-27 23:32:46 +01004919/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004920/* Generators */
4921/****************************************************************/
4922
Przemek Stekiel69c46792022-06-10 12:59:51 +02004923#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004924 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004925 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05304926 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304927 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08004928#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004929#endif /* At least one builtin KDF */
4930
Przemek Stekiel69c46792022-06-10 12:59:51 +02004931#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004932 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4933 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4934static psa_status_t psa_key_derivation_start_hmac(
4935 psa_mac_operation_t *operation,
4936 psa_algorithm_t hash_alg,
4937 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02004939{
4940 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004942 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4943 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4944 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02004945
Steven Cooreman72f736a2021-05-07 14:14:37 +02004946 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02004948
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 status = psa_driver_wrapper_mac_sign_setup(operation,
4950 &attributes,
4951 hmac_key, hmac_key_length,
4952 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02004953
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 psa_reset_key_attributes(&attributes);
4955 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02004956}
4957#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004958
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004959#define HKDF_STATE_INIT 0 /* no input yet */
4960#define HKDF_STATE_STARTED 1 /* got salt */
4961#define HKDF_STATE_KEYED 2 /* got key */
4962#define HKDF_STATE_OUTPUT 3 /* output started */
4963
Gilles Peskinecbe66502019-05-16 16:59:18 +02004964static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004966{
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4968 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4969 } else {
4970 return operation->alg;
4971 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004972}
4973
Gilles Peskine449bd832023-01-11 14:50:10 +01004974psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004975{
4976 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4978 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004979 /* The object has (apparently) been initialized but it is not
4980 * in use. It's ok to call abort on such an object, and there's
4981 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004983#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01004984 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
4985 mbedtls_free(operation->ctx.hkdf.info);
4986 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4987 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004988#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004989#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4990 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4992 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4993 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4994 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004995 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02004997 }
4998
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005000 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005002 }
5003
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005005 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005007 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005008#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005010 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005012 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005013#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005014 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005015
5016 /* We leave the fields Ai and output_block to be erased safely by the
5017 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005019#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5020 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005021#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5023 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5024 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5025 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005026#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305027#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305028 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305029 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005030 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305031 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305032 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305033
5034 status = PSA_SUCCESS;
5035 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305036#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005037 {
5038 status = PSA_ERROR_BAD_STATE;
5039 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 mbedtls_platform_zeroize(operation, sizeof(*operation));
5041 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005042}
5043
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005044psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005046{
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005048 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005050 }
5051
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005052 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005054}
5055
Gilles Peskine449bd832023-01-11 14:50:10 +01005056psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5057 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005058{
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 if (operation->alg == 0) {
5060 return PSA_ERROR_BAD_STATE;
5061 }
5062 if (capacity > operation->capacity) {
5063 return PSA_ERROR_INVALID_ARGUMENT;
5064 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005065 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005067}
5068
Przemek Stekiel69c46792022-06-10 12:59:51 +02005069#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005070/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005071static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5072 psa_algorithm_t kdf_alg,
5073 uint8_t *output,
5074 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005075{
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5077 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005078 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005079 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005080#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005082#else
5083 const uint8_t last_block = 0xff;
5084#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005085
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 if (hkdf->state < HKDF_STATE_KEYED ||
5087 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005088#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005090#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 )) {
5092 return PSA_ERROR_BAD_STATE;
5093 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005094 hkdf->state = HKDF_STATE_OUTPUT;
5095
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005097 /* Copy what remains of the current block */
5098 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005100 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 }
5102 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005103 output += n;
5104 output_length -= n;
5105 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005107 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005109 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005110 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005111 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005112 * object was corrupted or if this function is called directly
5113 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 if (hkdf->block_number == last_block) {
5115 return PSA_ERROR_BAD_STATE;
5116 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005117
5118 /* We need a new block */
5119 ++hkdf->block_number;
5120 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5123 hash_alg,
5124 hkdf->prk,
5125 hash_length);
5126 if (status != PSA_SUCCESS) {
5127 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005128 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005129
5130 if (hkdf->block_number != 1) {
5131 status = psa_mac_update(&hkdf->hmac,
5132 hkdf->output_block,
5133 hash_length);
5134 if (status != PSA_SUCCESS) {
5135 return status;
5136 }
5137 }
5138 status = psa_mac_update(&hkdf->hmac,
5139 hkdf->info,
5140 hkdf->info_length);
5141 if (status != PSA_SUCCESS) {
5142 return status;
5143 }
5144 status = psa_mac_update(&hkdf->hmac,
5145 &hkdf->block_number, 1);
5146 if (status != PSA_SUCCESS) {
5147 return status;
5148 }
5149 status = psa_mac_sign_finish(&hkdf->hmac,
5150 hkdf->output_block,
5151 sizeof(hkdf->output_block),
5152 &hmac_output_length);
5153 if (status != PSA_SUCCESS) {
5154 return status;
5155 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005156 }
5157
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005159}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005160#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005161
John Durkop07cc04a2020-11-16 22:08:34 -08005162#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5163 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005164static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5165 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005167{
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5169 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005170 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5171 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005172 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005173
Janos Follath7742fee2019-06-17 12:58:10 +01005174 /* We can't be wanting more output after block 0xff, otherwise
5175 * the capacity check in psa_key_derivation_output_bytes() would have
5176 * prevented this call. It could happen only if the operation
5177 * object was corrupted or if this function is called directly
5178 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 if (tls12_prf->block_number == 0xff) {
5180 return PSA_ERROR_CORRUPTION_DETECTED;
5181 }
Janos Follath7742fee2019-06-17 12:58:10 +01005182
5183 /* We need a new block */
5184 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005185 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005186
5187 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5188 *
5189 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5190 *
5191 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5192 * HMAC_hash(secret, A(2) + seed) +
5193 * HMAC_hash(secret, A(3) + seed) + ...
5194 *
5195 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005196 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005197 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005198 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005199 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005200 * is currently extracted as `output_block` and where i is
5201 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005202 */
5203
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 status = psa_key_derivation_start_hmac(&hmac,
5205 hash_alg,
5206 tls12_prf->secret,
5207 tls12_prf->secret_length);
5208 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005209 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005211
5212 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005214 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5215 * the variable seed and in this instance means it in the context of the
5216 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 status = psa_mac_update(&hmac,
5218 tls12_prf->label,
5219 tls12_prf->label_length);
5220 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005221 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 }
5223 status = psa_mac_update(&hmac,
5224 tls12_prf->seed,
5225 tls12_prf->seed_length);
5226 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005227 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 }
5229 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005230 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5232 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005233 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005235 }
5236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 status = psa_mac_sign_finish(&hmac,
5238 tls12_prf->Ai, hash_length,
5239 &hmac_output_length);
5240 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005241 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 }
5243 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005244 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005246
5247 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 status = psa_key_derivation_start_hmac(&hmac,
5249 hash_alg,
5250 tls12_prf->secret,
5251 tls12_prf->secret_length);
5252 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005253 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 }
5255 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5256 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005257 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 }
5259 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5260 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005261 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 }
5263 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5264 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005265 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 }
5267 status = psa_mac_sign_finish(&hmac,
5268 tls12_prf->output_block, hash_length,
5269 &hmac_output_length);
5270 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005271 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005273
Janos Follath7742fee2019-06-17 12:58:10 +01005274
5275cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 cleanup_status = psa_mac_abort(&hmac);
5277 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005278 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005280
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005282}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005283
Janos Follath844eb0e2019-06-19 12:10:49 +01005284static psa_status_t psa_key_derivation_tls12_prf_read(
5285 psa_tls12_prf_key_derivation_t *tls12_prf,
5286 psa_algorithm_t alg,
5287 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005289{
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5291 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005292 psa_status_t status;
5293 uint8_t offset, length;
5294
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005296 case PSA_TLS12_PRF_STATE_LABEL_SET:
5297 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5298 break;
5299 case PSA_TLS12_PRF_STATE_OUTPUT:
5300 break;
5301 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005303 }
5304
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005306 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 if (tls12_prf->left_in_block == 0) {
5308 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5309 alg);
5310 if (status != PSA_SUCCESS) {
5311 return status;
5312 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005313
5314 continue;
5315 }
5316
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005318 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005320 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005322
5323 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005325 output += length;
5326 output_length -= length;
5327 tls12_prf->left_in_block -= length;
5328 }
5329
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005331}
John Durkop07cc04a2020-11-16 22:08:34 -08005332#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5333 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005334
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005335#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5336static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5337 psa_tls12_ecjpake_to_pms_t *ecjpake,
5338 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005340{
5341 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005342 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005343
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 if (output_length != 32) {
5345 return PSA_ERROR_INVALID_ARGUMENT;
5346 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005347
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5349 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5350 &output_size);
5351 if (status != PSA_SUCCESS) {
5352 return status;
5353 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005354
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 if (output_size != output_length) {
5356 return PSA_ERROR_GENERIC_ERROR;
5357 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005358
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005360}
5361#endif
5362
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305363#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305364static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5365 psa_pbkdf2_key_derivation_t *pbkdf2,
5366 psa_algorithm_t prf_alg,
5367 uint8_t prf_output_length,
5368 psa_key_attributes_t *attributes)
5369{
5370 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305371 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305372 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305373 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305374 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305375 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305376 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305377
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305378 mac_operation.is_sign = 1;
5379 mac_operation.mac_size = prf_output_length;
5380 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305381
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305382 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5383 attributes,
5384 pbkdf2->password,
5385 pbkdf2->password_length,
5386 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305387 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305388 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305389 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305390 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5391 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305392 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305393 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305394 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305395 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305396 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305397 }
5398 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5399 &mac_output_length);
5400 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305401 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305402 }
5403
5404 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305405 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305406 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305407 }
5408
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305409 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305410
5411 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305412 /* We are passing prf_output_length as mac_size because the driver
5413 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305414 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305415 status = psa_driver_wrapper_mac_compute(attributes,
5416 pbkdf2->password,
5417 pbkdf2->password_length,
5418 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305419 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305420 &mac_output_length);
5421 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305422 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305423 }
5424
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305425 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305426 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305427
5428cleanup:
5429 /* Zeroise buffers to clear sensitive data from memory. */
5430 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5431 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305432}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305433
5434static psa_status_t psa_key_derivation_pbkdf2_read(
5435 psa_pbkdf2_key_derivation_t *pbkdf2,
5436 psa_algorithm_t kdf_alg,
5437 uint8_t *output,
5438 size_t output_length)
5439{
5440 psa_status_t status;
5441 psa_algorithm_t prf_alg;
5442 uint8_t prf_output_length;
5443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5444 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5445 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5446
5447 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5448 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5449 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5450 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305451 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5452 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305453 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305454 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305455 } else {
5456 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305457 }
5458
5459 switch (pbkdf2->state) {
5460 case PSA_PBKDF2_STATE_PASSWORD_SET:
5461 /* Initially we need a new block so bytes_used is equal to block size*/
5462 pbkdf2->bytes_used = prf_output_length;
5463 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5464 break;
5465 case PSA_PBKDF2_STATE_OUTPUT:
5466 break;
5467 default:
5468 return PSA_ERROR_BAD_STATE;
5469 }
5470
5471 while (output_length != 0) {
5472 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5473 if (n > output_length) {
5474 n = (uint8_t) output_length;
5475 }
5476 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5477 output += n;
5478 output_length -= n;
5479 pbkdf2->bytes_used += n;
5480
5481 if (output_length == 0) {
5482 break;
5483 }
5484
5485 /* We need a new block */
5486 pbkdf2->bytes_used = 0;
5487 pbkdf2->block_number++;
5488
5489 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5490 prf_output_length,
5491 &attributes);
5492 if (status != PSA_SUCCESS) {
5493 return status;
5494 }
5495 }
5496
5497 return PSA_SUCCESS;
5498}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305499#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305500
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005501psa_status_t psa_key_derivation_output_bytes(
5502 psa_key_derivation_operation_t *operation,
5503 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005505{
5506 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005508
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005510 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005512 }
5513
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005515 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005516 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005517 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005518 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005519 goto exit;
5520 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005522 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005523 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005524 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5525 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005526 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005527 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005529 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005530 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005531
Przemek Stekiel69c46792022-06-10 12:59:51 +02005532#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5534 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5535 output, output_length);
5536 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005537#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005538#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5539 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5541 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5542 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5543 kdf_alg, output,
5544 output_length);
5545 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005546#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5547 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005548#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005550 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5552 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005553#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305554#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305555 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305556 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5557 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305558 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305559#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005560
Gilles Peskineeab56e42018-07-12 17:12:33 +02005561 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005562 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005564 }
5565
5566exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005568 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005569 * This allows us to differentiate between exhausted operations and
5570 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5571 * operations. */
5572 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005574 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005576 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005578}
5579
David Brown7807bf72021-02-09 16:28:23 -07005580#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005581static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005582{
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 if (data_size >= 8) {
5584 mbedtls_des_key_set_parity(data);
5585 }
5586 if (data_size >= 16) {
5587 mbedtls_des_key_set_parity(data + 8);
5588 }
5589 if (data_size >= 24) {
5590 mbedtls_des_key_set_parity(data + 16);
5591 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005592}
David Brown7807bf72021-02-09 16:28:23 -07005593#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005594
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005595/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 * ECC keys on a Weierstrass elliptic curve require the generation
5597 * of a private key which is an integer
5598 * in the range [1, N - 1], where N is the boundary of the private key domain:
5599 * N is the prime p for Diffie-Hellman, or the order of the
5600 * curve’s base point for ECC.
5601 *
5602 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5603 * This function generates the private key using the following process:
5604 *
5605 * 1. Draw a byte string of length ceiling(m/8) bytes.
5606 * 2. If m is not a multiple of 8, set the most significant
5607 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5608 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5609 * 4. If k > N - 2, discard the result and return to step 1.
5610 * 5. Output k + 1 as the private key.
5611 *
5612 * This method allows compliance to NIST standards, specifically the methods titled
5613 * Key-Pair Generation by Testing Candidates in the following publications:
5614 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5615 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5616 * Diffie-Hellman keys.
5617 *
5618 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5619 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5620 *
5621 * Note: Function allocates memory for *data buffer, so given *data should be
5622 * always NULL.
5623 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005624#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5625#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005626static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005627 psa_key_slot_t *slot,
5628 size_t bits,
5629 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005630 uint8_t **data
5631 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005632{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005633 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005634 mbedtls_mpi k;
5635 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005636 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005637 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005638 size_t m;
5639 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005640
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 mbedtls_mpi_init(&k);
5642 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005643
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005644 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005646 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01005647 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005648
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005650 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005651 goto cleanup;
5652 }
5653
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005654 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005656
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005658
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005659 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005660 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005661 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005662
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005663 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005664
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005665 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005667
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005668 /* Note: This function is always called with *data == NULL and it
5669 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 *data = mbedtls_calloc(1, m_bytes);
5671 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005672 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005673 goto cleanup;
5674 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005677 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005679 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005681
5682 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005684 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005685 * (8 * ceiling(m/8) - m) bits of the first byte in
5686 * the string to zero.
5687 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005689 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005690 }
5691
5692 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 * big-endian byte string.
5694 */
5695 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005696
5697 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 * Result of comparison is returned. When it indicates error
5699 * then this function is called again.
5700 */
5701 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005702 }
5703
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005704 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5706 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005707cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 if (ret != 0) {
5709 status = mbedtls_to_psa_error(ret);
5710 }
5711 if (status != PSA_SUCCESS) {
5712 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005713 *data = NULL;
5714 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 mbedtls_mpi_free(&k);
5716 mbedtls_mpi_free(&diff_N_2);
5717 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005718}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005719
5720/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5721 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5722 *
5723 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5724 * draw a 32-byte string and process it as specified in
5725 * Elliptic Curves for Security [RFC7748] §5.
5726 *
5727 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5728 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5729 *
5730 * Note: Function allocates memory for *data buffer, so given *data should be
5731 * always NULL.
5732 */
5733
5734static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5735 size_t bits,
5736 psa_key_derivation_operation_t *operation,
5737 uint8_t **data
5738 )
5739{
5740 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005741 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005744 case 255:
5745 output_length = 32;
5746 break;
5747 case 448:
5748 output_length = 56;
5749 break;
5750 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005752 break;
5753 }
5754
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 if (*data == NULL) {
5758 return PSA_ERROR_INSUFFICIENT_MEMORY;
5759 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005760
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005762
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005764 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005766
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005768 case 255:
5769 (*data)[0] &= 248;
5770 (*data)[31] &= 127;
5771 (*data)[31] |= 64;
5772 break;
5773 case 448:
5774 (*data)[0] &= 252;
5775 (*data)[55] |= 128;
5776 break;
5777 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005779 break;
5780 }
5781
5782 return status;
5783}
Valerio Setti86587ab2023-06-27 13:09:30 +02005784#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5785static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5786 psa_key_slot_t *slot, size_t bits,
5787 psa_key_derivation_operation_t *operation, uint8_t **data)
5788{
5789 (void) slot;
5790 (void) bits;
5791 (void) operation;
5792 (void) data;
5793 return PSA_ERROR_NOT_SUPPORTED;
5794}
5795
5796static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5797 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5798{
5799 (void) bits;
5800 (void) operation;
5801 (void) data;
5802 return PSA_ERROR_NOT_SUPPORTED;
5803}
5804#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5805#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005806
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005807static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005808 psa_key_slot_t *slot,
5809 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005811{
5812 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305814 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005815 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005816
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5818 return PSA_ERROR_INVALID_ARGUMENT;
5819 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005820
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005821#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005822 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5824 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5825 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005826 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5828 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005829 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 }
5831 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005832 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5834 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005835 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005837 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005838 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005839#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005840 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 if (key_type_is_raw_bytes(slot->attr.type)) {
5842 if (bits % 8 != 0) {
5843 return PSA_ERROR_INVALID_ARGUMENT;
5844 }
5845 data = mbedtls_calloc(1, bytes);
5846 if (data == NULL) {
5847 return PSA_ERROR_INSUFFICIENT_MEMORY;
5848 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005849
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 status = psa_key_derivation_output_bytes(operation, data, bytes);
5851 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005852 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 }
David Brown12ca5032021-02-11 11:02:00 -07005854#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5856 psa_des_set_key_parity(data, bytes);
5857 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005858#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 } else {
5860 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005861 }
Ronald Crondd04d422020-11-28 15:14:42 +01005862
Ronald Cronbf33c932020-11-28 18:06:53 +01005863 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005864
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005865 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005866 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 &storage_size);
5868 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305869 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 }
Archanad8a83dc2021-06-14 10:04:16 +05305871 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 status = psa_allocate_buffer_to_slot(slot, storage_size);
5873 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305874 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 }
Archanad8a83dc2021-06-14 10:04:16 +05305876
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005877 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 data, bytes,
5879 slot->key.data,
5880 slot->key.bytes,
5881 &slot->key.bytes, &bits);
5882 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005883 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005885
5886exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 mbedtls_free(data);
5888 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005889}
5890
Gilles Peskine092ce512024-02-20 12:31:24 +01005891static const psa_key_production_parameters_t default_production_parameters =
5892 PSA_KEY_PRODUCTION_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005893
Gilles Peskine092ce512024-02-20 12:31:24 +01005894int psa_key_production_parameters_are_default(
5895 const psa_key_production_parameters_t *params,
5896 size_t params_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005897{
Gilles Peskine092ce512024-02-20 12:31:24 +01005898 if (params->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005899 return 0;
5900 }
Gilles Peskine092ce512024-02-20 12:31:24 +01005901 if (params_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005902 return 0;
5903 }
5904 return 1;
5905}
5906
5907psa_status_t psa_key_derivation_output_key_ext(
5908 const psa_key_attributes_t *attributes,
5909 psa_key_derivation_operation_t *operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005910 const psa_key_production_parameters_t *params,
5911 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005912 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005913{
5914 psa_status_t status;
5915 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005916 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005917
Ronald Cron81709fc2020-11-14 12:10:32 +01005918 *key = MBEDTLS_SVC_KEY_ID_INIT;
5919
Gilles Peskine0f84d622019-09-12 19:03:13 +02005920 /* Reject any attempt to create a zero-length key so that we don't
5921 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 if (psa_get_key_bits(attributes) == 0) {
5923 return PSA_ERROR_INVALID_ARGUMENT;
5924 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005925
Gilles Peskine092ce512024-02-20 12:31:24 +01005926 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005927 return PSA_ERROR_INVALID_ARGUMENT;
5928 }
5929
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 if (operation->alg == PSA_ALG_NONE) {
5931 return PSA_ERROR_BAD_STATE;
5932 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005933
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 if (!operation->can_output_key) {
5935 return PSA_ERROR_NOT_PERMITTED;
5936 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005937
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5939 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005940#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005942 /* Deriving a key in a secure element is not implemented yet. */
5943 status = PSA_ERROR_NOT_SUPPORTED;
5944 }
5945#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 if (status == PSA_SUCCESS) {
5947 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005948 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005950 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 if (status == PSA_SUCCESS) {
5952 status = psa_finish_key_creation(slot, driver, key);
5953 }
5954 if (status != PSA_SUCCESS) {
5955 psa_fail_key_creation(slot, driver);
5956 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005959}
5960
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005961psa_status_t psa_key_derivation_output_key(
5962 const psa_key_attributes_t *attributes,
5963 psa_key_derivation_operation_t *operation,
5964 mbedtls_svc_key_id_t *key)
5965{
Gilles Peskinec81393b2024-02-14 20:51:28 +01005966 return psa_key_derivation_output_key_ext(attributes, operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005967 &default_production_parameters, 0,
Gilles Peskinec81393b2024-02-14 20:51:28 +01005968 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005969}
Gilles Peskineeab56e42018-07-12 17:12:33 +02005970
5971
5972/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005973/* Key derivation */
5974/****************************************************************/
5975
Steven Cooreman932ffb72021-02-15 12:14:32 +01005976#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005977static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005978{
5979#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5981 return 1;
5982 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005983#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005984#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5986 return 1;
5987 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005988#endif
5989#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5991 return 1;
5992 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005993#endif
5994#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5996 return 1;
5997 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005998#endif
5999#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6001 return 1;
6002 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006003#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006004#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6006 return 1;
6007 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006008#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306009#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6010 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6011 return 1;
6012 }
6013#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306014#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6015 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6016 return 1;
6017 }
6018#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006020}
6021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006023{
6024 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 psa_status_t status = psa_hash_setup(&operation, alg);
6026 psa_hash_abort(&operation);
6027 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006028}
6029
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306030static psa_status_t psa_key_derivation_set_maximum_capacity(
6031 psa_key_derivation_operation_t *operation,
6032 psa_algorithm_t kdf_alg)
6033{
6034#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6035 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6036 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6037 return PSA_SUCCESS;
6038 }
6039#endif
6040#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
6041 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6042#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05306043 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306044 PSA_KEY_TYPE_AES,
6045 128U,
6046 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306047#else
6048 operation->capacity = SIZE_MAX;
6049#endif
6050 return PSA_SUCCESS;
6051 }
6052#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6053
6054 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6055 * invalid or meaningless but it does not affect this function */
6056 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6057 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306058 if (hash_size == 0) {
6059 return PSA_ERROR_NOT_SUPPORTED;
6060 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306061
6062 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6063 * we might fail later, which is somewhat unfriendly and potentially
6064 * risk-prone. */
6065 psa_status_t status = psa_hash_try_support(hash_alg);
6066 if (status != PSA_SUCCESS) {
6067 return status;
6068 }
6069
6070#if defined(PSA_WANT_ALG_HKDF)
6071 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6072 operation->capacity = 255 * hash_size;
6073 } else
6074#endif
6075#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6076 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6077 operation->capacity = hash_size;
6078 } else
6079#endif
6080#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6081 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6082 operation->capacity = 255 * hash_size;
6083 } else
6084#endif
6085#if defined(PSA_WANT_ALG_TLS12_PRF)
6086 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6087 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6088 operation->capacity = SIZE_MAX;
6089 } else
6090#endif
6091#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6092 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6093 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6094 /* Master Secret is always 48 bytes
6095 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6096 operation->capacity = 48U;
6097 } else
6098#endif
6099#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6100 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6101#if (SIZE_MAX > UINT32_MAX)
6102 operation->capacity = UINT32_MAX * hash_size;
6103#else
6104 operation->capacity = SIZE_MAX;
6105#endif
6106 } else
6107#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6108 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306109 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306110 status = PSA_ERROR_NOT_SUPPORTED;
6111 }
6112 return status;
6113}
6114
Gilles Peskine969c5d62019-01-16 15:53:06 +01006115static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006116 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006118{
Janos Follath5fe19732019-06-20 15:09:30 +01006119 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6120 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006122
Gilles Peskine969c5d62019-01-16 15:53:06 +01006123 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 if (!is_kdf_alg_supported(kdf_alg)) {
6125 return PSA_ERROR_NOT_SUPPORTED;
6126 }
John Durkop07cc04a2020-11-16 22:08:34 -08006127
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306128 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6129 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306130 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006131}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006134{
6135#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 if (alg == PSA_ALG_ECDH) {
6137 return PSA_SUCCESS;
6138 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006139#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006140#if defined(PSA_WANT_ALG_FFDH)
6141 if (alg == PSA_ALG_FFDH) {
6142 return PSA_SUCCESS;
6143 }
6144#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006145 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006147}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006148
6149static int psa_key_derivation_allows_free_form_secret_input(
6150 psa_algorithm_t kdf_alg)
6151{
6152#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6153 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6154 return 0;
6155 }
6156#endif
6157 (void) kdf_alg;
6158 return 1;
6159}
John Durkop07cc04a2020-11-16 22:08:34 -08006160#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6163 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006164{
6165 psa_status_t status;
6166
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 if (operation->alg != 0) {
6168 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006169 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006170
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6172 return PSA_ERROR_INVALID_ARGUMENT;
6173 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6174#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6175 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6176 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6177 status = psa_key_agreement_try_support(ka_alg);
6178 if (status != PSA_SUCCESS) {
6179 return status;
6180 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006181 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6182 return PSA_ERROR_INVALID_ARGUMENT;
6183 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6185#else
6186 return PSA_ERROR_NOT_SUPPORTED;
6187#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6188 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6189#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6190 status = psa_key_derivation_setup_kdf(operation, alg);
6191#else
6192 return PSA_ERROR_NOT_SUPPORTED;
6193#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6194 } else {
6195 return PSA_ERROR_INVALID_ARGUMENT;
6196 }
6197
6198 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006199 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 }
6201 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006202}
6203
Przemek Stekiel69c46792022-06-10 12:59:51 +02006204#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006205static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6206 psa_algorithm_t kdf_alg,
6207 psa_key_derivation_step_t step,
6208 const uint8_t *data,
6209 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006210{
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006212 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006214 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006215#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6217 return PSA_ERROR_INVALID_ARGUMENT;
6218 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006219#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 if (hkdf->state != HKDF_STATE_INIT) {
6221 return PSA_ERROR_BAD_STATE;
6222 } else {
6223 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6224 hash_alg,
6225 data, data_length);
6226 if (status != PSA_SUCCESS) {
6227 return status;
6228 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006229 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006231 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006232 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006233#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006235 /* We shouldn't be in different state as HKDF_EXPAND only allows
6236 * two inputs: SECRET (this case) and INFO which does not modify
6237 * the state. It could happen only if the hkdf
6238 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 if (hkdf->state != HKDF_STATE_INIT) {
6240 return PSA_ERROR_BAD_STATE;
6241 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006242
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006243 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6245 return PSA_ERROR_INVALID_ARGUMENT;
6246 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006247
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 memcpy(hkdf->prk, data, data_length);
6249 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006250#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006251 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006252 /* HKDF: If no salt was provided, use an empty salt.
6253 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006255#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6257 return PSA_ERROR_BAD_STATE;
6258 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006259#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6261 hash_alg,
6262 NULL, 0);
6263 if (status != PSA_SUCCESS) {
6264 return status;
6265 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006266 hkdf->state = HKDF_STATE_STARTED;
6267 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 if (hkdf->state != HKDF_STATE_STARTED) {
6269 return PSA_ERROR_BAD_STATE;
6270 }
6271 status = psa_mac_update(&hkdf->hmac,
6272 data, data_length);
6273 if (status != PSA_SUCCESS) {
6274 return status;
6275 }
6276 status = psa_mac_sign_finish(&hkdf->hmac,
6277 hkdf->prk,
6278 sizeof(hkdf->prk),
6279 &data_length);
6280 if (status != PSA_SUCCESS) {
6281 return status;
6282 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006283 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006284
Gilles Peskine2b522db2019-04-12 15:11:49 +02006285 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006286 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006287#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006289 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006291 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006293#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006294 {
6295 /* Block 0 is empty, and the next block will be
6296 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006298 }
6299
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006301 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006302#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6304 return PSA_ERROR_INVALID_ARGUMENT;
6305 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006306#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006307#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6309 hkdf->state == HKDF_STATE_INIT) {
6310 return PSA_ERROR_BAD_STATE;
6311 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006312#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 if (hkdf->state == HKDF_STATE_OUTPUT) {
6314 return PSA_ERROR_BAD_STATE;
6315 }
6316 if (hkdf->info_set) {
6317 return PSA_ERROR_BAD_STATE;
6318 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006319 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 if (data_length != 0) {
6321 hkdf->info = mbedtls_calloc(1, data_length);
6322 if (hkdf->info == NULL) {
6323 return PSA_ERROR_INSUFFICIENT_MEMORY;
6324 }
6325 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006326 }
6327 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006329 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006331 }
6332}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006333#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006334
John Durkop07cc04a2020-11-16 22:08:34 -08006335#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6336 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006337static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6338 const uint8_t *data,
6339 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006340{
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6342 return PSA_ERROR_BAD_STATE;
6343 }
Janos Follathf08e2652019-06-13 09:05:41 +01006344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 if (data_length != 0) {
6346 prf->seed = mbedtls_calloc(1, data_length);
6347 if (prf->seed == NULL) {
6348 return PSA_ERROR_INSUFFICIENT_MEMORY;
6349 }
Janos Follathf08e2652019-06-13 09:05:41 +01006350
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006352 prf->seed_length = data_length;
6353 }
Janos Follathf08e2652019-06-13 09:05:41 +01006354
Gilles Peskine43f958b2020-12-13 14:55:14 +01006355 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006358}
6359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6361 const uint8_t *data,
6362 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006363{
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6365 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6366 return PSA_ERROR_BAD_STATE;
6367 }
Janos Follath81550542019-06-13 14:26:34 +01006368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 if (data_length != 0) {
6370 prf->secret = mbedtls_calloc(1, data_length);
6371 if (prf->secret == NULL) {
6372 return PSA_ERROR_INSUFFICIENT_MEMORY;
6373 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006376 prf->secret_length = data_length;
6377 }
Janos Follath81550542019-06-13 14:26:34 +01006378
Gilles Peskine43f958b2020-12-13 14:55:14 +01006379 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006382}
6383
Gilles Peskine449bd832023-01-11 14:50:10 +01006384static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6385 const uint8_t *data,
6386 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006387{
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6389 return PSA_ERROR_BAD_STATE;
6390 }
Janos Follath63028dd2019-06-13 09:15:47 +01006391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 if (data_length != 0) {
6393 prf->label = mbedtls_calloc(1, data_length);
6394 if (prf->label == NULL) {
6395 return PSA_ERROR_INSUFFICIENT_MEMORY;
6396 }
Janos Follath63028dd2019-06-13 09:15:47 +01006397
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006399 prf->label_length = data_length;
6400 }
Janos Follath63028dd2019-06-13 09:15:47 +01006401
Gilles Peskine43f958b2020-12-13 14:55:14 +01006402 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006403
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006405}
6406
Gilles Peskine449bd832023-01-11 14:50:10 +01006407static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6408 psa_key_derivation_step_t step,
6409 const uint8_t *data,
6410 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006411{
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006413 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006415 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006417 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006419 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006421 }
6422}
John Durkop07cc04a2020-11-16 22:08:34 -08006423#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6424 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6425
6426#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6427static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6428 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006429 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006431{
6432 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6434 4 + data_length + prf->other_secret_length :
6435 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006436
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6438 return PSA_ERROR_INVALID_ARGUMENT;
6439 }
John Durkop07cc04a2020-11-16 22:08:34 -08006440
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 uint8_t *pms = mbedtls_calloc(1, pms_len);
6442 if (pms == NULL) {
6443 return PSA_ERROR_INSUFFICIENT_MEMORY;
6444 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006445 uint8_t *cur = pms;
6446
6447 /* pure-PSK:
6448 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006449 *
6450 * The premaster secret is formed as follows: if the PSK is N octets
6451 * long, concatenate a uint16 with the value N, N zero octets, a second
6452 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006453 *
6454 * mixed-PSK:
6455 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6456 * follows: concatenate a uint16 with the length of the other secret,
6457 * the other secret itself, uint16 with the length of PSK, and the
6458 * PSK itself.
6459 * For details please check:
6460 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6461 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6462 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006463 */
6464
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6466 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6467 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6468 if (prf->other_secret_length != 0) {
6469 memcpy(cur, prf->other_secret, prf->other_secret_length);
6470 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006471 cur += prf->other_secret_length;
6472 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 } else {
6474 *cur++ = MBEDTLS_BYTE_1(data_length);
6475 *cur++ = MBEDTLS_BYTE_0(data_length);
6476 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006477 cur += data_length;
6478 }
6479
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 *cur++ = MBEDTLS_BYTE_1(data_length);
6481 *cur++ = MBEDTLS_BYTE_0(data_length);
6482 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006483 cur += data_length;
6484
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006485 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006486
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006487 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006489}
Janos Follath6660f0e2019-06-17 08:44:03 +01006490
Przemek Stekiele47201b2022-04-19 13:53:28 +02006491static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6492 psa_tls12_prf_key_derivation_t *prf,
6493 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006495{
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6497 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006498 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006499
6500 if (data_length != 0) {
6501 prf->other_secret = mbedtls_calloc(1, data_length);
6502 if (prf->other_secret == NULL) {
6503 return PSA_ERROR_INSUFFICIENT_MEMORY;
6504 }
6505
6506 memcpy(prf->other_secret, data, data_length);
6507 prf->other_secret_length = data_length;
6508 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006509 prf->other_secret_length = 0;
6510 }
6511
6512 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6513
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006515}
6516
Janos Follath6660f0e2019-06-17 08:44:03 +01006517static psa_status_t psa_tls12_prf_psk_to_ms_input(
6518 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006519 psa_key_derivation_step_t step,
6520 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006522{
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006524 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006525 return psa_tls12_prf_psk_to_ms_set_key(prf,
6526 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006527 break;
6528 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6530 data,
6531 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006532 break;
6533 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006534 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006535 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006536
Przemek Stekiele47201b2022-04-19 13:53:28 +02006537 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006538}
John Durkop07cc04a2020-11-16 22:08:34 -08006539#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006540
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006541#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6542static psa_status_t psa_tls12_ecjpake_to_pms_input(
6543 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006544 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006545 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006547{
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6549 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6550 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006551 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006552
6553 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 if (data[0] != 0x04) {
6555 return PSA_ERROR_INVALID_ARGUMENT;
6556 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006557
6558 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006560
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006562}
6563#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306564
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306565#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306566static psa_status_t psa_pbkdf2_set_input_cost(
6567 psa_pbkdf2_key_derivation_t *pbkdf2,
6568 psa_key_derivation_step_t step,
6569 uint64_t data)
6570{
6571 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6572 return PSA_ERROR_INVALID_ARGUMENT;
6573 }
6574
6575 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6576 return PSA_ERROR_BAD_STATE;
6577 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306578
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306579 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306580 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306581 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306582
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306583 if (data == 0) {
6584 return PSA_ERROR_INVALID_ARGUMENT;
6585 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306586
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306587 pbkdf2->input_cost = data;
6588 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6589
6590 return PSA_SUCCESS;
6591}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306592
6593static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6594 const uint8_t *data,
6595 size_t data_length)
6596{
Gilles Peskineead17662023-08-20 22:02:51 +02006597 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6598 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6599 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6600 /* Appending to existing salt. No state change. */
6601 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306602 return PSA_ERROR_BAD_STATE;
6603 }
6604
Gilles Peskineca57d782023-07-20 19:08:06 +02006605 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006606 /* Appending an empty string, nothing to do. */
6607 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306608 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306609
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306610 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6611 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306612 return PSA_ERROR_INSUFFICIENT_MEMORY;
6613 }
6614
Gilles Peskineead17662023-08-20 22:02:51 +02006615 if (pbkdf2->salt_length != 0) {
6616 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6617 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306618 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306619 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306620 mbedtls_free(pbkdf2->salt);
6621 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306622 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306623 return PSA_SUCCESS;
6624}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306625
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306626#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306627static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306628 const uint8_t *input,
6629 size_t input_len,
6630 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306631 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306632{
6633 psa_status_t status = PSA_SUCCESS;
6634 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6635 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306636 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306637 } else {
6638 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306639 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306640 }
6641 return status;
6642}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306643#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306644
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306645#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306646static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6647 size_t input_len,
6648 uint8_t *output,
6649 size_t *output_len)
6650{
6651 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306652 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306653 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306654 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306655 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6656 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6657 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306658 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306659 * mac_size as the driver function sets mac_output_length = mac_size
6660 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306661 status = psa_driver_wrapper_mac_compute(&attributes,
6662 zeros, sizeof(zeros),
6663 PSA_ALG_CMAC, input, input_len,
6664 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306665 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6666 128U,
6667 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306668 output_len);
6669 } else {
6670 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306671 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306672 }
6673 return status;
6674}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306675#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306676
6677static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306678 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306679 const uint8_t *data,
6680 size_t data_length)
6681{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306682 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306683 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6684 return PSA_ERROR_BAD_STATE;
6685 }
6686
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306687#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306688 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6689 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6690 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6691 pbkdf2->password,
6692 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306693 } else
6694#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6695#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6696 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306697 status = psa_pbkdf2_cmac_set_password(data, data_length,
6698 pbkdf2->password,
6699 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306700 } else
6701#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6702 {
6703 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306704 }
6705
6706 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6707
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306708 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306709}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306710
6711static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306712 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306713 psa_key_derivation_step_t step,
6714 const uint8_t *data,
6715 size_t data_length)
6716{
6717 switch (step) {
6718 case PSA_KEY_DERIVATION_INPUT_SALT:
6719 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6720 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306721 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306722 default:
6723 return PSA_ERROR_INVALID_ARGUMENT;
6724 }
6725}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306726#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306727
Gilles Peskineb8965192019-09-24 16:21:10 +02006728/** Check whether the given key type is acceptable for the given
6729 * input step of a key derivation.
6730 *
6731 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6732 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6733 * Both secret and non-secret inputs can alternatively have the type
6734 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6735 * that the input was passed as a buffer rather than via a key object.
6736 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006737static int psa_key_derivation_check_input_type(
6738 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006739 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006740{
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006742 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 if (key_type == PSA_KEY_TYPE_DERIVE) {
6744 return PSA_SUCCESS;
6745 }
6746 if (key_type == PSA_KEY_TYPE_NONE) {
6747 return PSA_SUCCESS;
6748 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006749 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006750 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006751 if (key_type == PSA_KEY_TYPE_DERIVE) {
6752 return PSA_SUCCESS;
6753 }
6754 if (key_type == PSA_KEY_TYPE_NONE) {
6755 return PSA_SUCCESS;
6756 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006757 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006758 case PSA_KEY_DERIVATION_INPUT_LABEL:
6759 case PSA_KEY_DERIVATION_INPUT_SALT:
6760 case PSA_KEY_DERIVATION_INPUT_INFO:
6761 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006762 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6763 return PSA_SUCCESS;
6764 }
6765 if (key_type == PSA_KEY_TYPE_NONE) {
6766 return PSA_SUCCESS;
6767 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006768 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306769 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6770 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6771 return PSA_SUCCESS;
6772 }
6773 if (key_type == PSA_KEY_TYPE_DERIVE) {
6774 return PSA_SUCCESS;
6775 }
6776 if (key_type == PSA_KEY_TYPE_NONE) {
6777 return PSA_SUCCESS;
6778 }
6779 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006780 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006782}
6783
Janos Follathb80a94e2019-06-12 15:54:46 +01006784static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006785 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006786 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006787 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006788 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006789 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006790{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006791 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006792 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006793
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 status = psa_key_derivation_check_input_type(step, key_type);
6795 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006796 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006798
Przemek Stekiel69c46792022-06-10 12:59:51 +02006799#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006800 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6801 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6802 step, data, data_length);
6803 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006804#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006805#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006806 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6807 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6808 step, data, data_length);
6809 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006810#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6811#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6813 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6814 step, data, data_length);
6815 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006816#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006817#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006818 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006819 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6821 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006822#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306823#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306824 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306825 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6826 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306827 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306828#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006829 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006830 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006831 (void) data;
6832 (void) data_length;
6833 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006835 }
6836
Gilles Peskine224b0d62019-09-23 18:13:17 +02006837exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 if (status != PSA_SUCCESS) {
6839 psa_key_derivation_abort(operation);
6840 }
6841 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006842}
6843
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306844static psa_status_t psa_key_derivation_input_integer_internal(
6845 psa_key_derivation_operation_t *operation,
6846 psa_key_derivation_step_t step,
6847 uint64_t value)
6848{
6849 psa_status_t status;
6850 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6851
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306852#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306853 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306854 status = psa_pbkdf2_set_input_cost(
6855 &operation->ctx.pbkdf2, step, value);
6856 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306857#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306858 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306859 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306860 (void) value;
6861 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306862 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306863 }
6864
6865 if (status != PSA_SUCCESS) {
6866 psa_key_derivation_abort(operation);
6867 }
6868 return status;
6869}
6870
Janos Follath51f4a0f2019-06-14 11:35:55 +01006871psa_status_t psa_key_derivation_input_bytes(
6872 psa_key_derivation_operation_t *operation,
6873 psa_key_derivation_step_t step,
6874 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006876{
Gilles Peskine449bd832023-01-11 14:50:10 +01006877 return psa_key_derivation_input_internal(operation, step,
6878 PSA_KEY_TYPE_NONE,
6879 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006880}
6881
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306882psa_status_t psa_key_derivation_input_integer(
6883 psa_key_derivation_operation_t *operation,
6884 psa_key_derivation_step_t step,
6885 uint64_t value)
6886{
6887 return psa_key_derivation_input_integer_internal(operation, step, value);
6888}
6889
Janos Follath51f4a0f2019-06-14 11:35:55 +01006890psa_status_t psa_key_derivation_input_key(
6891 psa_key_derivation_operation_t *operation,
6892 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006894{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006895 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006896 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006897 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006898
Ronald Cron5c522922020-11-14 16:35:34 +01006899 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6901 if (status != PSA_SUCCESS) {
6902 psa_key_derivation_abort(operation);
6903 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006904 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006905
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306906 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6907 * permission to output to a key object. */
6908 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6909 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006910 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006912
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 status = psa_key_derivation_input_internal(operation,
6914 step, slot->attr.type,
6915 slot->key.data,
6916 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006917
Ryan Everett5ac6fa72024-02-14 17:11:36 +00006918 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006919
Gilles Peskine449bd832023-01-11 14:50:10 +01006920 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006921}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006922
6923
6924
6925/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006926/* Key agreement */
6927/****************************************************************/
6928
Gilles Peskine449bd832023-01-11 14:50:10 +01006929psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6930 const uint8_t *key_buffer,
6931 size_t key_buffer_size,
6932 psa_algorithm_t alg,
6933 const uint8_t *peer_key,
6934 size_t peer_key_length,
6935 uint8_t *shared_secret,
6936 size_t shared_secret_size,
6937 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006938{
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006940#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006941 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006942 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6943 key_buffer_size, alg,
6944 peer_key, peer_key_length,
6945 shared_secret,
6946 shared_secret_size,
6947 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006948#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006949
6950#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6951 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006952 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006953 peer_key,
6954 peer_key_length,
6955 key_buffer,
6956 key_buffer_size,
6957 shared_secret,
6958 shared_secret_size,
6959 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006960#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6961
Gilles Peskine01d718c2018-09-18 12:01:02 +02006962 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006963 (void) attributes;
6964 (void) key_buffer;
6965 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006966 (void) peer_key;
6967 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006968 (void) shared_secret;
6969 (void) shared_secret_size;
6970 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006972 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006973}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006974
Aditya Deshpande17845b82022-10-13 17:21:01 +01006975/** Internal function for raw key agreement
6976 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006977 * to the driver's implementation if a driver is present.
6978 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006979 * (psa_key_agreement_raw_builtin).
6980 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006981static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6982 psa_key_slot_t *private_key,
6983 const uint8_t *peer_key,
6984 size_t peer_key_length,
6985 uint8_t *shared_secret,
6986 size_t shared_secret_size,
6987 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006988{
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6990 return PSA_ERROR_NOT_SUPPORTED;
6991 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006992
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006993 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006994 private_key->key.data,
6995 private_key->key.bytes, alg,
6996 peer_key, peer_key_length,
6997 shared_secret,
6998 shared_secret_size,
6999 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007000}
7001
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007002/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007003 * to potentially free embedded data structures and wipe confidential data.
7004 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007005static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7006 psa_key_derivation_step_t step,
7007 psa_key_slot_t *private_key,
7008 const uint8_t *peer_key,
7009 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007010{
7011 psa_status_t status;
Moritz Fischer967f8cd2024-03-02 00:55:06 +00007012 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
Gilles Peskine01d718c2018-09-18 12:01:02 +02007013 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007015
7016 /* Step 1: run the secret agreement algorithm to generate the shared
7017 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 status = psa_key_agreement_raw_internal(ka_alg,
7019 private_key,
7020 peer_key, peer_key_length,
7021 shared_secret,
7022 sizeof(shared_secret),
7023 &shared_secret_length);
7024 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007025 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007027
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007028 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007029 * the shared secret. A shared secret is permitted wherever a key
7030 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 status = psa_key_derivation_input_internal(operation, step,
7032 PSA_KEY_TYPE_DERIVE,
7033 shared_secret,
7034 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007035exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7037 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007038}
7039
Gilles Peskine449bd832023-01-11 14:50:10 +01007040psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7041 psa_key_derivation_step_t step,
7042 mbedtls_svc_key_id_t private_key,
7043 const uint8_t *peer_key,
7044 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007045{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007046 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007047 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007048 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007049
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7051 return PSA_ERROR_INVALID_ARGUMENT;
7052 }
Ronald Cron5c522922020-11-14 16:35:34 +01007053 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7055 if (status != PSA_SUCCESS) {
7056 return status;
7057 }
7058 status = psa_key_agreement_internal(operation, step,
7059 slot,
7060 peer_key, peer_key_length);
7061 if (status != PSA_SUCCESS) {
7062 psa_key_derivation_abort(operation);
7063 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007064 /* If a private key has been added as SECRET, we allow the derived
7065 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007067 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007068 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007069 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007070
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007071 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007072
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007074}
7075
Gilles Peskine449bd832023-01-11 14:50:10 +01007076psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7077 mbedtls_svc_key_id_t private_key,
7078 const uint8_t *peer_key,
7079 size_t peer_key_length,
7080 uint8_t *output,
7081 size_t output_size,
7082 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007083{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007084 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007085 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007086 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007087 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007088
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007090 status = PSA_ERROR_INVALID_ARGUMENT;
7091 goto exit;
7092 }
Ronald Cron5c522922020-11-14 16:35:34 +01007093 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007094 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7095 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007096 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007098
Gilles Peskine3e561302022-04-14 00:17:15 +02007099 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7100 * for the output size. The PSA specification only guarantees that this
7101 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7102 * but it might be nice to allow smaller buffers if the output fits.
7103 * At the time of writing this comment, with only ECDH implemented,
7104 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7105 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7106 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007107 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7109 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007110 status = PSA_ERROR_BUFFER_TOO_SMALL;
7111 goto exit;
7112 }
7113
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 status = psa_key_agreement_raw_internal(alg, slot,
7115 peer_key, peer_key_length,
7116 output, output_size,
7117 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007118
7119exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007120 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007121 /* If an error happens and is not handled properly, the output
7122 * may be used as a key to protect sensitive data. Arrange for such
7123 * a key to be random, which is likely to result in decryption or
7124 * verification errors. This is better than filling the buffer with
7125 * some constant data such as zeros, which would result in the data
7126 * being protected with a reproducible, easily knowable key.
7127 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007129 *output_length = output_size;
7130 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007131
Ryan Everetta103ec92024-01-31 13:59:57 +00007132 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007133
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007135}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007136
7137
Gilles Peskine30524eb2020-11-13 17:02:26 +01007138
Gilles Peskine86a440b2018-11-12 18:39:40 +01007139/****************************************************************/
7140/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007141/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007142
Gilles Peskine672a7712023-04-28 21:00:28 +02007143#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7144#include "entropy_poll.h"
7145#endif
7146
Gilles Peskine30524eb2020-11-13 17:02:26 +01007147/** Initialize the PSA random generator.
Paul Elliottd35dce62024-03-15 13:57:16 +00007148 *
7149 * Note: the mbedtls_threading_psa_rngdata_mutex should be held when calling
7150 * this function if mutexes are enabled.
Gilles Peskine30524eb2020-11-13 17:02:26 +01007151 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007152static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007153{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007154#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007155 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007156#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7157
Gilles Peskine30524eb2020-11-13 17:02:26 +01007158 /* Set default configuration if
7159 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007160 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007161 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007162 }
7163 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007164 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007165 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007166
Gilles Peskine449bd832023-01-11 14:50:10 +01007167 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007168#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7169 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7170 /* The PSA entropy injection feature depends on using NV seed as an entropy
7171 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007172 mbedtls_entropy_add_source(&rng->entropy,
7173 mbedtls_nv_seed_poll, NULL,
7174 MBEDTLS_ENTROPY_BLOCK_SIZE,
7175 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007176#endif
7177
Valerio Setti061d4e42024-02-26 12:52:44 +01007178 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007179#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007180}
7181
7182/** Deinitialize the PSA random generator.
Paul Elliottd35dce62024-03-15 13:57:16 +00007183 *
7184 * Note: the mbedtls_threading_psa_rngdata_mutex should be held when calling
7185 * this function if mutexes are enabled.
Gilles Peskine30524eb2020-11-13 17:02:26 +01007186 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007187static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007188{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007189#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007190 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007191#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01007192 mbedtls_psa_drbg_free(&rng->drbg);
Valerio Setti83e0de82023-11-24 12:13:05 +01007193 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007194#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007195}
7196
7197/** Seed the PSA random generator.
7198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007199static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007200{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007201#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7202 /* Do nothing: the external RNG seeds itself. */
7203 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007204 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007205#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007206 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01007207 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01007208 drbg_seed, sizeof(drbg_seed) - 1);
7209 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007210#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007211}
7212
Gilles Peskine449bd832023-01-11 14:50:10 +01007213psa_status_t psa_generate_random(uint8_t *output,
7214 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007215{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007216 GUARD_MODULE_INITIALIZED;
7217
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007218#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7219
7220 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7222 output, output_size,
7223 &output_length);
7224 if (status != PSA_SUCCESS) {
7225 return status;
7226 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007227 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007228 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 if (output_length != output_size) {
7230 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7231 }
7232 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007233
7234#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7235
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 while (output_size > 0) {
Valerio Setti718180c2024-02-27 11:58:39 +01007237 int ret = MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
Gilles Peskine71ddab92021-01-04 21:01:07 +01007238 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7240 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7241 output_size);
Valerio Setti718180c2024-02-27 11:58:39 +01007242#if defined(MBEDTLS_CTR_DRBG_C)
7243 ret = mbedtls_ctr_drbg_random(&global_data.rng.drbg, output, request_size);
7244#elif defined(MBEDTLS_HMAC_DRBG_C)
7245 ret = mbedtls_hmac_drbg_random(&global_data.rng.drbg, output, request_size);
7246#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01007247 if (ret != 0) {
7248 return mbedtls_to_psa_error(ret);
7249 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007250 output_size -= request_size;
7251 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007252 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007254#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007255}
7256
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007257#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007258psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7259 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007260{
Paul Elliott600472b2024-02-23 17:26:58 +00007261 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 return PSA_ERROR_NOT_PERMITTED;
7263 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007264
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7266 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7267 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7268 return PSA_ERROR_INVALID_ARGUMENT;
7269 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007270
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007272}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007273#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007274
Ronald Cron3772afe2021-02-08 16:10:05 +01007275/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007276 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007277 * \param type The key type
7278 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007279 *
7280 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007281 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007282 * \retval #PSA_ERROR_INVALID_ARGUMENT
7283 * The size in bits of the key is not valid.
7284 * \retval #PSA_ERROR_NOT_SUPPORTED
7285 * The type and/or the size in bits of the key or the combination of
7286 * the two is not supported.
7287 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007288static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007290{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007291 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007292
Gilles Peskine449bd832023-01-11 14:50:10 +01007293 if (key_type_is_raw_bytes(type)) {
7294 status = psa_validate_unstructured_key_bit_size(type, bits);
7295 if (status != PSA_SUCCESS) {
7296 return status;
7297 }
7298 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007299#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007300 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7301 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7302 return PSA_ERROR_NOT_SUPPORTED;
7303 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007304 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007305 return PSA_ERROR_NOT_SUPPORTED;
7306 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007307
Ronald Cron01b2aba2020-10-05 09:42:02 +02007308 /* Accept only byte-aligned keys, for the same reasons as
7309 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 if (bits % 8 != 0) {
7311 return PSA_ERROR_NOT_SUPPORTED;
7312 }
7313 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007314#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007315
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007316#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007318 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 return PSA_SUCCESS;
7320 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007321#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007322
Valerio Settia55f0422023-07-10 15:34:41 +02007323#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007324 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007325 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007326 return PSA_ERROR_NOT_SUPPORTED;
7327 }
7328 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007329#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007330 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007331 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007332 }
7333
Gilles Peskine449bd832023-01-11 14:50:10 +01007334 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007335}
7336
Ronald Cron55ed0592020-10-05 10:30:40 +02007337psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007338 const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007339 const psa_key_production_parameters_t *params, size_t params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007340 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007341{
7342 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007343 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007344
Gilles Peskine7a18f962024-02-12 16:48:11 +01007345 /* Only used for RSA */
Gilles Peskine092ce512024-02-20 12:31:24 +01007346 (void) params;
7347 (void) params_data_length;
Gilles Peskine7a18f962024-02-12 16:48:11 +01007348
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 if (key_type_is_raw_bytes(type)) {
7350 status = psa_generate_random(key_buffer, key_buffer_size);
7351 if (status != PSA_SUCCESS) {
7352 return status;
7353 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007354
David Brown12ca5032021-02-11 11:02:00 -07007355#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007356 if (type == PSA_KEY_TYPE_DES) {
7357 psa_des_set_key_parity(key_buffer, key_buffer_size);
7358 }
David Brown8107e312021-02-12 08:08:46 -07007359#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007361
Valerio Setti76df8c12023-07-11 14:11:28 +02007362#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007363 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskinee22f6a92024-02-26 15:45:33 +01007364 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskine4c32b692024-02-16 00:11:09 +01007365 params, params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007366 key_buffer,
7367 key_buffer_size,
7368 key_buffer_length);
7369 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007370#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007371
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007372#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007373 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7374 return mbedtls_psa_ecp_generate_key(attributes,
7375 key_buffer,
7376 key_buffer_size,
7377 key_buffer_length);
7378 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007379#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007380
Valerio Settia55f0422023-07-10 15:34:41 +02007381#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007382 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7383 return mbedtls_psa_ffdh_generate_key(attributes,
7384 key_buffer,
7385 key_buffer_size,
7386 key_buffer_length);
7387 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007388#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007389 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007390 (void) key_buffer_length;
7391 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007392 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007393
Gilles Peskine449bd832023-01-11 14:50:10 +01007394 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007395}
Darryl Green0c6575a2018-11-07 16:05:30 +00007396
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007397psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007398 const psa_key_production_parameters_t *params,
7399 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007400 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007401{
7402 psa_status_t status;
7403 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007404 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007405 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007406
Ronald Cron81709fc2020-11-14 12:10:32 +01007407 *key = MBEDTLS_SVC_KEY_ID_INIT;
7408
Gilles Peskine0f84d622019-09-12 19:03:13 +02007409 /* Reject any attempt to create a zero-length key so that we don't
7410 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007411 if (psa_get_key_bits(attributes) == 0) {
7412 return PSA_ERROR_INVALID_ARGUMENT;
7413 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007414
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007415 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007416 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007417 return PSA_ERROR_INVALID_ARGUMENT;
7418 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007419
Gilles Peskine7a18f962024-02-12 16:48:11 +01007420#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007421 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +01007422 if (params->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01007423 return PSA_ERROR_INVALID_ARGUMENT;
7424 }
7425 } else
7426#endif
Gilles Peskine092ce512024-02-20 12:31:24 +01007427 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007428 return PSA_ERROR_INVALID_ARGUMENT;
7429 }
7430
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7432 &slot, &driver);
7433 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007434 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 }
Gilles Peskine11792082019-08-06 18:36:36 +02007436
Ronald Cron977c2472020-10-13 08:32:21 +02007437 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307438 * storage ( thus not in the case of generating a key in a secure element
7439 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7440 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007442 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007444 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007445 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007446 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007447 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007449
7450 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007451 attributes->type,
7452 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007454 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 attributes, &key_buffer_size);
7456 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007457 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007458 }
Ronald Cron977c2472020-10-13 08:32:21 +02007459 }
Ronald Cron977c2472020-10-13 08:32:21 +02007460
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7462 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007463 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007464 }
Ronald Cron977c2472020-10-13 08:32:21 +02007465 }
7466
Gilles Peskine449bd832023-01-11 14:50:10 +01007467 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007468 params, params_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01007469 slot->key.data, slot->key.bytes,
7470 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 if (status != PSA_SUCCESS) {
7472 psa_remove_key_data_from_memory(slot);
7473 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007474
Gilles Peskine11792082019-08-06 18:36:36 +02007475exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 if (status == PSA_SUCCESS) {
7477 status = psa_finish_key_creation(slot, driver, key);
7478 }
7479 if (status != PSA_SUCCESS) {
7480 psa_fail_key_creation(slot, driver);
7481 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007482
Gilles Peskine449bd832023-01-11 14:50:10 +01007483 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007484}
7485
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007486psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7487 mbedtls_svc_key_id_t *key)
7488{
7489 return psa_generate_key_ext(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007490 &default_production_parameters, 0,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007491 key);
7492}
7493
Gilles Peskinee59236f2018-01-27 23:32:46 +01007494/****************************************************************/
7495/* Module setup */
7496/****************************************************************/
7497
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007498#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007499psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 void (* entropy_init)(mbedtls_entropy_context *ctx),
7501 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007502{
Paul Elliott8e151532024-02-23 17:35:29 +00007503 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7504
7505#if defined(MBEDTLS_THREADING_C)
7506 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
7507#endif /* defined(MBEDTLS_THREADING_C) */
7508
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
Paul Elliott8e151532024-02-23 17:35:29 +00007510 status = PSA_ERROR_BAD_STATE;
7511 } else {
7512 global_data.rng.entropy_init = entropy_init;
7513 global_data.rng.entropy_free = entropy_free;
7514 status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01007515 }
Paul Elliott8e151532024-02-23 17:35:29 +00007516
7517#if defined(MBEDTLS_THREADING_C)
7518 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
7519#endif /* defined(MBEDTLS_THREADING_C) */
7520
7521 return status;
Gilles Peskine5e769522018-11-20 21:59:56 +01007522}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007523#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007524
Gilles Peskine449bd832023-01-11 14:50:10 +01007525void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007526{
Paul Elliott47cee8e2024-03-08 21:38:02 +00007527
Paul Elliott600472b2024-02-23 17:26:58 +00007528#if defined(MBEDTLS_THREADING_C)
7529 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
7530#endif /* defined(MBEDTLS_THREADING_C) */
7531
Paul Elliott47cee8e2024-03-08 21:38:02 +00007532 /* Nothing to do to free transaction. */
7533 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED) {
7534 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
Valerio Setti83e0de82023-11-24 12:13:05 +01007535 }
Paul Elliott600472b2024-02-23 17:26:58 +00007536
Paul Elliott47cee8e2024-03-08 21:38:02 +00007537 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED) {
7538 psa_wipe_all_key_slots();
7539 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
7540 }
Ronald Cron9ba76912021-04-10 16:57:30 +02007541
Paul Elliott600472b2024-02-23 17:26:58 +00007542#if defined(MBEDTLS_THREADING_C)
7543 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
7544#endif /* defined(MBEDTLS_THREADING_C) */
7545
Paul Elliott47cee8e2024-03-08 21:38:02 +00007546#if defined(MBEDTLS_THREADING_C)
7547 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
7548#endif /* defined(MBEDTLS_THREADING_C) */
7549
7550 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7551 mbedtls_psa_random_free(&global_data.rng);
7552 }
7553 global_data.rng_state = RNG_NOT_INITIALIZED;
7554 mbedtls_platform_zeroize(&global_data.rng, sizeof(global_data.rng));
7555
7556#if defined(MBEDTLS_THREADING_C)
7557 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
7558#endif /* defined(MBEDTLS_THREADING_C) */
7559
7560#if defined(MBEDTLS_THREADING_C)
7561 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
7562#endif /* defined(MBEDTLS_THREADING_C) */
7563
Ronald Cron9ba76912021-04-10 16:57:30 +02007564 /* Terminate drivers */
Paul Elliott47cee8e2024-03-08 21:38:02 +00007565 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED) {
7566 psa_driver_wrapper_free();
7567 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
7568 }
7569
7570#if defined(MBEDTLS_THREADING_C)
7571 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
7572#endif /* defined(MBEDTLS_THREADING_C) */
7573
Gilles Peskinee59236f2018-01-27 23:32:46 +01007574}
7575
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007576#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7577/** Recover a transaction that was interrupted by a power failure.
7578 *
7579 * This function is called during initialization, before psa_crypto_init()
7580 * returns. If this function returns a failure status, the initialization
7581 * fails.
7582 */
7583static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007584 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007585{
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007587 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7588 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 /* TODO - fall through to the failure case until this
7590 * is implemented.
7591 * https://github.com/ARMmbed/mbed-crypto/issues/218
7592 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007593 default:
7594 /* We found an unsupported transaction in the storage.
7595 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007597 }
7598}
7599#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7600
Paul Elliott47cee8e2024-03-08 21:38:02 +00007601static psa_status_t mbedtls_psa_crypto_init_subsystem(mbedtls_psa_crypto_subsystem subsystem)
7602{
7603 psa_status_t status = PSA_SUCCESS;
7604 uint8_t driver_wrappers_initialized = 0;
7605
7606 switch (subsystem) {
7607 case PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS:
7608
7609#if defined(MBEDTLS_THREADING_C)
7610 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
7611#endif /* defined(MBEDTLS_THREADING_C) */
7612
7613 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED)) {
7614 /* Init drivers */
7615 status = psa_driver_wrapper_init();
7616
7617 /* Drivers need shutdown regardless of startup errors. */
7618 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
7619
7620
7621 }
7622#if defined(MBEDTLS_THREADING_C)
7623 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7624 &mbedtls_threading_psa_globaldata_mutex));
7625#endif /* defined(MBEDTLS_THREADING_C) */
7626
7627 break;
7628
7629 case PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS:
7630
7631#if defined(MBEDTLS_THREADING_C)
7632 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
7633#endif /* defined(MBEDTLS_THREADING_C) */
7634
7635 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED)) {
7636 status = psa_initialize_key_slots();
7637
7638 /* Need to wipe keys even if initialization fails. */
7639 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
7640
7641 }
7642#if defined(MBEDTLS_THREADING_C)
7643 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7644 &mbedtls_threading_psa_globaldata_mutex));
7645#endif /* defined(MBEDTLS_THREADING_C) */
7646
7647 break;
7648
7649 case PSA_CRYPTO_SUBSYSTEM_RNG:
7650
7651#if defined(MBEDTLS_THREADING_C)
7652 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
7653#endif /* defined(MBEDTLS_THREADING_C) */
7654
7655 driver_wrappers_initialized =
7656 (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED);
7657
7658#if defined(MBEDTLS_THREADING_C)
7659 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7660 &mbedtls_threading_psa_globaldata_mutex));
7661#endif /* defined(MBEDTLS_THREADING_C) */
7662
7663 /* Need to use separate mutex here, as initialisation can require
7664 * testing of init flags, which requires locking the global data
7665 * mutex. */
7666#if defined(MBEDTLS_THREADING_C)
7667 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex));
7668#endif /* defined(MBEDTLS_THREADING_C) */
7669
7670 /* Initialize and seed the random generator. */
7671 if (global_data.rng_state == RNG_NOT_INITIALIZED && driver_wrappers_initialized) {
7672 mbedtls_psa_random_init(&global_data.rng);
7673 global_data.rng_state = RNG_INITIALIZED;
7674
7675 status = mbedtls_psa_random_seed(&global_data.rng);
7676 if (status == PSA_SUCCESS) {
7677 global_data.rng_state = RNG_SEEDED;
7678 }
7679 }
7680
7681#if defined(MBEDTLS_THREADING_C)
7682 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7683 &mbedtls_threading_psa_rngdata_mutex));
7684#endif /* defined(MBEDTLS_THREADING_C) */
7685
Paul Elliott47cee8e2024-03-08 21:38:02 +00007686 break;
7687
7688 case PSA_CRYPTO_SUBSYSTEM_TRANSACTION:
7689
7690#if defined(MBEDTLS_THREADING_C)
7691 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
7692#endif /* defined(MBEDTLS_THREADING_C) */
7693
7694 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED)) {
7695#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7696 status = psa_crypto_load_transaction();
7697 if (status == PSA_SUCCESS) {
7698 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7699 if (status == PSA_SUCCESS) {
7700 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
7701 }
7702 status = psa_crypto_stop_transaction();
7703 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
7704 /* There's no transaction to complete. It's all good. */
7705 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
7706 status = PSA_SUCCESS;
7707 }
7708#else /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
7709 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
7710 status = PSA_SUCCESS;
7711#endif /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
7712 }
7713
7714#if defined(MBEDTLS_THREADING_C)
7715 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
7716 &mbedtls_threading_psa_globaldata_mutex));
7717#endif /* defined(MBEDTLS_THREADING_C) */
7718
7719 break;
7720
7721 default:
7722 status = PSA_ERROR_CORRUPTION_DETECTED;
7723 }
7724
7725 /* Exit label only required when using threading macros. */
7726#if defined(MBEDTLS_THREADING_C)
7727exit:
7728#endif /* defined(MBEDTLS_THREADING_C) */
7729
7730 return status;
7731}
7732
Gilles Peskine449bd832023-01-11 14:50:10 +01007733psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007734{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007735 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007736
Paul Elliott47cee8e2024-03-08 21:38:02 +00007737 /* Double initialization is explicitly allowed. Early out if everything is
7738 * done. */
7739 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007740 return PSA_SUCCESS;
7741 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007742
Paul Elliott47cee8e2024-03-08 21:38:02 +00007743 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS);
Valerio Setti402cfba2023-11-13 10:24:32 +01007744 if (status != PSA_SUCCESS) {
7745 goto exit;
7746 }
7747
Paul Elliott47cee8e2024-03-08 21:38:02 +00007748 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS);
Gilles Peskine449bd832023-01-11 14:50:10 +01007749 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007750 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007751 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007752
Paul Elliott47cee8e2024-03-08 21:38:02 +00007753 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_RNG);
7754 if (status != PSA_SUCCESS) {
7755 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02007756 }
Gilles Peskinefc762652019-07-22 19:30:34 +02007757
Paul Elliott47cee8e2024-03-08 21:38:02 +00007758 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_TRANSACTION);
Gilles Peskinee59236f2018-01-27 23:32:46 +01007759
7760exit:
Paul Elliott600472b2024-02-23 17:26:58 +00007761
Gilles Peskine449bd832023-01-11 14:50:10 +01007762 if (status != PSA_SUCCESS) {
7763 mbedtls_psa_crypto_free();
7764 }
Paul Elliott47cee8e2024-03-08 21:38:02 +00007765
Gilles Peskine449bd832023-01-11 14:50:10 +01007766 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007767}
7768
Yanray Wangffc3c482023-07-11 12:01:04 +08007769#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007770psa_status_t psa_crypto_driver_pake_get_password_len(
7771 const psa_crypto_driver_pake_inputs_t *inputs,
7772 size_t *password_len)
7773{
7774 if (inputs->password_len == 0) {
7775 return PSA_ERROR_BAD_STATE;
7776 }
7777
7778 *password_len = inputs->password_len;
7779
7780 return PSA_SUCCESS;
7781}
7782
7783psa_status_t psa_crypto_driver_pake_get_password(
7784 const psa_crypto_driver_pake_inputs_t *inputs,
7785 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7786{
7787 if (inputs->password_len == 0) {
7788 return PSA_ERROR_BAD_STATE;
7789 }
7790
7791 if (buffer_size < inputs->password_len) {
7792 return PSA_ERROR_BUFFER_TOO_SMALL;
7793 }
7794
7795 memcpy(buffer, inputs->password, inputs->password_len);
7796 *buffer_length = inputs->password_len;
7797
7798 return PSA_SUCCESS;
7799}
7800
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007801psa_status_t psa_crypto_driver_pake_get_user_len(
7802 const psa_crypto_driver_pake_inputs_t *inputs,
7803 size_t *user_len)
7804{
7805 if (inputs->user_len == 0) {
7806 return PSA_ERROR_BAD_STATE;
7807 }
7808
7809 *user_len = inputs->user_len;
7810
7811 return PSA_SUCCESS;
7812}
7813
7814psa_status_t psa_crypto_driver_pake_get_user(
7815 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007816 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007817{
7818 if (inputs->user_len == 0) {
7819 return PSA_ERROR_BAD_STATE;
7820 }
7821
Przemek Stekielc0e62502023-03-14 11:49:36 +01007822 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007823 return PSA_ERROR_BUFFER_TOO_SMALL;
7824 }
7825
Przemek Stekielc0e62502023-03-14 11:49:36 +01007826 memcpy(user_id, inputs->user, inputs->user_len);
7827 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007828
7829 return PSA_SUCCESS;
7830}
7831
7832psa_status_t psa_crypto_driver_pake_get_peer_len(
7833 const psa_crypto_driver_pake_inputs_t *inputs,
7834 size_t *peer_len)
7835{
7836 if (inputs->peer_len == 0) {
7837 return PSA_ERROR_BAD_STATE;
7838 }
7839
7840 *peer_len = inputs->peer_len;
7841
7842 return PSA_SUCCESS;
7843}
7844
7845psa_status_t psa_crypto_driver_pake_get_peer(
7846 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007847 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007848{
7849 if (inputs->peer_len == 0) {
7850 return PSA_ERROR_BAD_STATE;
7851 }
7852
Przemek Stekielc0e62502023-03-14 11:49:36 +01007853 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007854 return PSA_ERROR_BUFFER_TOO_SMALL;
7855 }
7856
Przemek Stekielc0e62502023-03-14 11:49:36 +01007857 memcpy(peer_id, inputs->peer, inputs->peer_len);
7858 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007859
7860 return PSA_SUCCESS;
7861}
7862
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007863psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7864 const psa_crypto_driver_pake_inputs_t *inputs,
7865 psa_pake_cipher_suite_t *cipher_suite)
7866{
7867 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7868 return PSA_ERROR_BAD_STATE;
7869 }
7870
7871 *cipher_suite = inputs->cipher_suite;
7872
7873 return PSA_SUCCESS;
7874}
7875
Neil Armstronga7d08c32022-06-01 18:21:20 +02007876psa_status_t psa_pake_setup(
7877 psa_pake_operation_t *operation,
7878 const psa_pake_cipher_suite_t *cipher_suite)
7879{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007880 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007881
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007882 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007883 status = PSA_ERROR_BAD_STATE;
7884 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007885 }
7886
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007887 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007888 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007889 status = PSA_ERROR_INVALID_ARGUMENT;
7890 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007891 }
7892
Przemek Stekiel51eac532022-12-07 11:04:51 +01007893 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7894
Przemek Stekiele12ed362022-12-21 12:54:46 +01007895 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007896 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7897 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007898 operation->data.inputs.cipher_suite = *cipher_suite;
7899
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007900#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007901 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007902 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007903 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007904
David Horstmann16f01512023-06-14 17:21:07 +01007905 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007906 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007907 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007908#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007909 {
7910 status = PSA_ERROR_NOT_SUPPORTED;
7911 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007912 }
7913
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007914 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7915
Przemek Stekiel51eac532022-12-07 11:04:51 +01007916 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007917exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007918 psa_pake_abort(operation);
7919 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007920}
7921
7922psa_status_t psa_pake_set_password_key(
7923 psa_pake_operation_t *operation,
7924 mbedtls_svc_key_id_t password)
7925{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007927 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7928 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007929 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007930
Przemek Stekiel51eac532022-12-07 11:04:51 +01007931 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007932 status = PSA_ERROR_BAD_STATE;
7933 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007934 }
7935
Przemek Stekiel6c764412022-11-22 14:05:12 +01007936 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7937 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007938 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007939 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007940 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007941 }
7942
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007943 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007944
Przemek Stekiel51eac532022-12-07 11:04:51 +01007945 if (type != PSA_KEY_TYPE_PASSWORD &&
7946 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7947 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007948 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007949 }
7950
Przemek Stekiel51eac532022-12-07 11:04:51 +01007951 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7952 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007953 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007954 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007955 }
7956
7957 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7958 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01007959 operation->data.inputs.attributes = slot->attr;
7960
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007961exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007962 if (status != PSA_SUCCESS) {
7963 psa_pake_abort(operation);
7964 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00007965 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007966 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007967}
7968
7969psa_status_t psa_pake_set_user(
7970 psa_pake_operation_t *operation,
7971 const uint8_t *user_id,
7972 size_t user_id_len)
7973{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007974 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007975
7976 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007977 status = PSA_ERROR_BAD_STATE;
7978 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007979 }
7980
Przemek Stekiel51eac532022-12-07 11:04:51 +01007981 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007982 status = PSA_ERROR_INVALID_ARGUMENT;
7983 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007984 }
7985
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007986 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007987 status = PSA_ERROR_BAD_STATE;
7988 goto exit;
7989 }
7990
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007991 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7992 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007993 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7994 goto exit;
7995 }
7996
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007997 memcpy(operation->data.inputs.user, user_id, user_id_len);
7998 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007999
8000 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008001exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008002 psa_pake_abort(operation);
8003 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008004}
8005
8006psa_status_t psa_pake_set_peer(
8007 psa_pake_operation_t *operation,
8008 const uint8_t *peer_id,
8009 size_t peer_id_len)
8010{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008011 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008012
8013 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008014 status = PSA_ERROR_BAD_STATE;
8015 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008016 }
8017
Przemek Stekiel51eac532022-12-07 11:04:51 +01008018 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008019 status = PSA_ERROR_INVALID_ARGUMENT;
8020 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008021 }
8022
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008023 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008024 status = PSA_ERROR_BAD_STATE;
8025 goto exit;
8026 }
8027
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008028 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8029 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008030 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8031 goto exit;
8032 }
8033
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008034 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8035 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008036
8037 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008038exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008039 psa_pake_abort(operation);
8040 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008041}
8042
8043psa_status_t psa_pake_set_role(
8044 psa_pake_operation_t *operation,
8045 psa_pake_role_t role)
8046{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008047 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008048
Przemek Stekiel51eac532022-12-07 11:04:51 +01008049 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008050 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008051 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008052 }
8053
Przemek Stekiel09104b82023-03-06 14:11:51 +01008054 switch (operation->alg) {
8055#if defined(PSA_WANT_ALG_JPAKE)
8056 case PSA_ALG_JPAKE:
8057 if (role == PSA_PAKE_ROLE_NONE) {
8058 return PSA_SUCCESS;
8059 }
8060 status = PSA_ERROR_INVALID_ARGUMENT;
8061 break;
8062#endif
8063 default:
8064 (void) role;
8065 status = PSA_ERROR_NOT_SUPPORTED;
8066 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008067 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008068exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008069 psa_pake_abort(operation);
8070 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008071}
8072
David Horstmanne7f21e62023-05-12 18:17:21 +01008073/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008074#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008075static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008076 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008077{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008078 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008079 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008080 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008081
David Horstmann024e5c52023-06-14 15:48:21 +01008082 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008083 is_x1 = (stage->outputs < 1);
8084 } else {
8085 is_x1 = (stage->inputs < 1);
8086 }
8087
David Horstmann74a3d8c2023-06-14 18:28:19 +01008088 key_share_step = is_x1 ?
8089 PSA_JPAKE_X1_STEP_KEY_SHARE :
8090 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008091 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008092 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8093 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8094 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8095 } else {
8096 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008097 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008098 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008099}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008100#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008101
Przemek Stekiel2797d372022-12-22 11:19:22 +01008102static psa_status_t psa_pake_complete_inputs(
8103 psa_pake_operation_t *operation)
8104{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008105 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008106 /* Create copy of the inputs on stack as inputs share memory
8107 with the driver context which will be setup by the driver. */
8108 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008109
Przemek Stekielfde11282023-03-13 16:06:09 +01008110 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008111 return PSA_ERROR_BAD_STATE;
8112 }
8113
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008114 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008115 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8116 return PSA_ERROR_BAD_STATE;
8117 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008118 }
8119
Przemek Stekiel18620a32023-01-17 16:34:52 +01008120 /* Clear driver context */
8121 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8122
8123 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008124
8125 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008126 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008127
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008128 /* User and peer are translated to role. */
8129 mbedtls_free(inputs.user);
8130 mbedtls_free(inputs.peer);
8131
Przemek Stekiel2797d372022-12-22 11:19:22 +01008132 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008133#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008134 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008135 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008136 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008137#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008138 {
8139 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008140 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008141 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008142 return status;
8143}
8144
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008145#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008146static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008147 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008148 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008149 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008150{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008151 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8152 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8153 step != PSA_PAKE_STEP_ZK_PROOF) {
8154 return PSA_ERROR_INVALID_ARGUMENT;
8155 }
8156
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008157 psa_jpake_computation_stage_t *computation_stage =
8158 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008159
David Horstmann5da95602023-06-08 15:37:12 +01008160 if (computation_stage->round != PSA_JPAKE_FIRST &&
8161 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008162 return PSA_ERROR_BAD_STATE;
8163 }
8164
David Horstmanne7f21e62023-05-12 18:17:21 +01008165 /* Check that the step we are given is the one we were expecting */
8166 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008167 return PSA_ERROR_BAD_STATE;
8168 }
8169
David Horstmanne7f21e62023-05-12 18:17:21 +01008170 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8171 computation_stage->inputs == 0 &&
8172 computation_stage->outputs == 0) {
8173 /* Start of the round, so function decides whether we are inputting
8174 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008175 computation_stage->io_mode = io_mode;
8176 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008177 /* Middle of the round so the mode we are in must match the function
8178 * called by the user */
8179 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008180 }
8181
8182 return PSA_SUCCESS;
8183}
8184
David Horstmanne7f21e62023-05-12 18:17:21 +01008185static psa_status_t psa_jpake_epilogue(
8186 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008187 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008188{
David Horstmanne7f21e62023-05-12 18:17:21 +01008189 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008190 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008191
David Horstmanne7f21e62023-05-12 18:17:21 +01008192 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8193 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008194 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008195 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008196 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008197 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008198 }
8199 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008200 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008201 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008202 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008203 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008204 }
8205 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008206 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8207 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008208 /* End of a round, move to the next round */
8209 stage->inputs = 0;
8210 stage->outputs = 0;
8211 stage->round++;
8212 }
8213 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008214 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008215 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008216 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008217 return PSA_SUCCESS;
8218}
David Horstmanne7f21e62023-05-12 18:17:21 +01008219
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008220#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008221
Neil Armstronga7d08c32022-06-01 18:21:20 +02008222psa_status_t psa_pake_output(
8223 psa_pake_operation_t *operation,
8224 psa_pake_step_t step,
8225 uint8_t *output,
8226 size_t output_size,
8227 size_t *output_length)
8228{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008229 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008230 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008231 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008232
8233 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008234 status = psa_pake_complete_inputs(operation);
8235 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008236 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008237 }
8238 }
8239
8240 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008241 status = PSA_ERROR_BAD_STATE;
8242 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008243 }
8244
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008245 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008246 status = PSA_ERROR_INVALID_ARGUMENT;
8247 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008248 }
8249
Przemek Stekiele12ed362022-12-21 12:54:46 +01008250 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008251#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008252 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008253 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008254 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008255 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008256 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008257 driver_step = convert_jpake_computation_stage_to_driver_step(
8258 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008259 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008260#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008261 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008262 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008263 status = PSA_ERROR_NOT_SUPPORTED;
8264 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008265 }
8266
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008267 status = psa_driver_wrapper_pake_output(operation, driver_step,
8268 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008269
8270 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008271 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008272 }
8273
8274 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008275#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008276 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008277 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008278 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008279 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008280 }
8281 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008282#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008283 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008284 status = PSA_ERROR_NOT_SUPPORTED;
8285 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008286 }
8287
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008288 return PSA_SUCCESS;
8289exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008290 psa_pake_abort(operation);
8291 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008292}
8293
8294psa_status_t psa_pake_input(
8295 psa_pake_operation_t *operation,
8296 psa_pake_step_t step,
8297 const uint8_t *input,
8298 size_t input_length)
8299{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008300 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008301 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008302 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8303 operation->primitive,
8304 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008305
8306 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008307 status = psa_pake_complete_inputs(operation);
8308 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008309 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008310 }
8311 }
8312
8313 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008314 status = PSA_ERROR_BAD_STATE;
8315 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008316 }
8317
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008318 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008319 status = PSA_ERROR_INVALID_ARGUMENT;
8320 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008321 }
8322
Przemek Stekiele12ed362022-12-21 12:54:46 +01008323 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008324#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008325 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008326 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008327 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008328 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008329 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008330 driver_step = convert_jpake_computation_stage_to_driver_step(
8331 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008332 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008333#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008334 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008335 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008336 status = PSA_ERROR_NOT_SUPPORTED;
8337 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008338 }
8339
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008340 status = psa_driver_wrapper_pake_input(operation, driver_step,
8341 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008342
8343 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008344 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008345 }
8346
8347 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008348#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008349 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008350 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008351 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008352 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008353 }
8354 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008355#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008356 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008357 status = PSA_ERROR_NOT_SUPPORTED;
8358 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008359 }
8360
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008361 return PSA_SUCCESS;
8362exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008363 psa_pake_abort(operation);
8364 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008365}
8366
8367psa_status_t psa_pake_get_implicit_key(
8368 psa_pake_operation_t *operation,
8369 psa_key_derivation_operation_t *output)
8370{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008371 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008372 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008373 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008374 size_t shared_key_len = 0;
8375
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008376 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008377 status = PSA_ERROR_BAD_STATE;
8378 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008379 }
8380
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008381#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008382 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008383 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008384 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008385 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008386 status = PSA_ERROR_BAD_STATE;
8387 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008388 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008389 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008390#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008391 {
8392 status = PSA_ERROR_NOT_SUPPORTED;
8393 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008394 }
8395
Przemek Stekiel0c781802022-11-29 14:53:13 +01008396 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8397 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008398 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008399 &shared_key_len);
8400
8401 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008402 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008403 }
8404
8405 status = psa_key_derivation_input_bytes(output,
8406 PSA_KEY_DERIVATION_INPUT_SECRET,
8407 shared_key,
8408 shared_key_len);
8409
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008410 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008411exit:
8412 abort_status = psa_pake_abort(operation);
8413 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008414}
8415
8416psa_status_t psa_pake_abort(
8417 psa_pake_operation_t *operation)
8418{
Przemek Stekield69dca92023-01-31 19:59:20 +01008419 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008420
Przemek Stekield69dca92023-01-31 19:59:20 +01008421 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008422 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008423 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008424
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008425 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8426 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008427 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008428 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008429 }
8430 if (operation->data.inputs.user != NULL) {
8431 mbedtls_free(operation->data.inputs.user);
8432 }
8433 if (operation->data.inputs.peer != NULL) {
8434 mbedtls_free(operation->data.inputs.peer);
8435 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008436 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008437 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008438
Przemek Stekield69dca92023-01-31 19:59:20 +01008439 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008440}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008441#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008442
Gilles Peskinee59236f2018-01-27 23:32:46 +01008443#endif /* MBEDTLS_PSA_CRYPTO_C */