blob: 63fd05c59b6dc0b02ffb9cf7d268ba672554d049 [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
Gilles Peskine449bd832023-01-11 14:50:10 +010096typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +010097 uint8_t initialized;
98 uint8_t rng_state;
99 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100100 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100101} psa_global_data_t;
102
103static psa_global_data_t global_data;
104
Paul Elliott600472b2024-02-23 17:26:58 +0000105static uint8_t psa_get_initialized(void)
106{
107 uint8_t initialized;
108
109#if defined(MBEDTLS_THREADING_C)
110 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
111#endif /* defined(MBEDTLS_THREADING_C) */
112
113 initialized = global_data.initialized;
114
115#if defined(MBEDTLS_THREADING_C)
116 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
117#endif /* defined(MBEDTLS_THREADING_C) */
118
119 return initialized;
120}
121
Paul Elliott35816522024-02-23 17:47:42 +0000122static uint8_t psa_get_drivers_initialized(void)
123{
124 uint8_t initialized;
125
126#if defined(MBEDTLS_THREADING_C)
127 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
128#endif /* defined(MBEDTLS_THREADING_C) */
129
130 initialized = global_data.drivers_initialized;
131
132#if defined(MBEDTLS_THREADING_C)
133 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
134#endif /* defined(MBEDTLS_THREADING_C) */
135
136 return initialized;
137}
138
itayzafrir0adf0fc2018-09-06 16:24:41 +0300139#define GUARD_MODULE_INITIALIZED \
Paul Elliott600472b2024-02-23 17:26:58 +0000140 if (psa_get_initialized() == 0) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300142
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100143int psa_can_do_hash(psa_algorithm_t hash_alg)
144{
145 (void) hash_alg;
Paul Elliott35816522024-02-23 17:47:42 +0000146 return psa_get_drivers_initialized();
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100147}
Valerio Settic6f004f2023-12-12 11:27:36 +0100148
Valerio Setti1fff4f22023-12-28 14:19:34 +0100149int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
Valerio Settic6f004f2023-12-12 11:27:36 +0100150{
Valerio Setti1fff4f22023-12-28 14:19:34 +0100151 (void) key_type;
Valerio Settic6f004f2023-12-12 11:27:36 +0100152 (void) cipher_alg;
Paul Elliott35816522024-02-23 17:47:42 +0000153 return psa_get_drivers_initialized();
Valerio Settic6f004f2023-12-12 11:27:36 +0100154}
155
156
Valerio Settia55f0422023-07-10 15:34:41 +0200157#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200158 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200159 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200160static int psa_is_dh_key_size_valid(size_t bits)
161{
Valerio Setti504a1022024-01-17 15:19:30 +0100162 switch (bits) {
163#if defined(PSA_WANT_DH_RFC7919_2048)
164 case 2048:
165 return 1;
166#endif /* PSA_WANT_DH_RFC7919_2048 */
167#if defined(PSA_WANT_DH_RFC7919_3072)
168 case 3072:
169 return 1;
170#endif /* PSA_WANT_DH_RFC7919_3072 */
171#if defined(PSA_WANT_DH_RFC7919_4096)
172 case 4096:
173 return 1;
174#endif /* PSA_WANT_DH_RFC7919_4096 */
175#if defined(PSA_WANT_DH_RFC7919_6144)
176 case 6144:
177 return 1;
178#endif /* PSA_WANT_DH_RFC7919_6144 */
179#if defined(PSA_WANT_DH_RFC7919_8192)
180 case 8192:
181 return 1;
182#endif /* PSA_WANT_DH_RFC7919_8192 */
183 default:
184 return 0;
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200185 }
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200186}
Valerio Settia55f0422023-07-10 15:34:41 +0200187#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200188 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200189 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100192{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100193 /* Mbed TLS error codes can combine a high-level error code and a
194 * low-level error code. The low-level error usually reflects the
195 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 int low_level_ret = -(-ret & 0x007f);
197 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100198 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100200
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100201#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
203 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100205 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
206 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100207#endif
208
209#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200210 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
211 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
212 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
213 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
214 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200216 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200218 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100220#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200221
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100222#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000223 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100224 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100226#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100227
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100228#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100231 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100233#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100234
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100235#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200236 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100238#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200239
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100240#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200241 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200243 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100245#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200246
Dave Rodgman509b5672023-08-16 19:26:23 +0100247#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100248 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100250 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100252 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100254 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100256 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100258 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100260 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100262#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
265 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100266 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
267 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100268 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100270 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
271 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100273 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100275#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100276
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100277#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100278 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100280#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100281
Gilles Peskinee59236f2018-01-27 23:32:46 +0100282 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
283 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
284 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100286
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100287#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200290 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100294#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100295
Gilles Peskine82e57d12020-11-13 21:31:17 +0100296#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100298 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
299 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100300 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100302 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
303 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100305 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100307#endif
308
Dave Rodgmandea266f2023-08-31 11:52:43 +0100309#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100312 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100314 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100316#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100319#endif
320#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100321
Dave Rodgman509b5672023-08-16 19:26:23 +0100322#if defined(MBEDTLS_BIGNUM_C)
323#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100324 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100326#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100327 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100329 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100331 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100333 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100335 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100337 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100339 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100341#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100342
Dave Rodgman509b5672023-08-16 19:26:23 +0100343#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100344 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100346 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
347 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100349#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
350 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100351 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100353#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100354 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
355 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100359 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
360 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100364 case MBEDTLS_ERR_PK_INVALID_ALG:
365 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
366 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100368 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200370 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100372#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100373
Gilles Peskineff2d2002019-05-06 15:26:23 +0200374 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200376 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200378
Dave Rodgman509b5672023-08-16 19:26:23 +0100379#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100380 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100382 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100384 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100386 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100388 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
389 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100391 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100393 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100395 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100397#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200398
Dave Rodgman1dab4452023-09-01 09:59:51 +0100399#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300400 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300401 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300403 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300405 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300407 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
408 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300410 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100412 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100414
Dave Rodgman509b5672023-08-16 19:26:23 +0100415#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000416 case MBEDTLS_ERR_ECP_IN_PROGRESS:
417 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100418#endif
419#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000420
Janos Follatha13b9052019-11-22 12:48:59 +0000421 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300423
Gilles Peskinee59236f2018-01-27 23:32:46 +0100424 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100426 }
427}
428
Paul Elliottdc42ca82023-02-24 18:11:59 +0000429/**
430 * \brief For output buffers which contain "tags"
431 * (outputs that may be checked for validity like
432 * hashes, MACs and signatures), fill the unused
433 * part of the output buffer (the whole buffer on
434 * error, the trailing part on success) with
435 * something that isn't a valid tag (barring an
436 * attack on the tag and deliberately-crafted
437 * input), in case the caller doesn't check the
438 * return status properly.
439 *
440 * \param output_buffer Pointer to buffer to wipe. May not be NULL
441 * unless \p output_buffer_size is zero.
442 * \param status Status of function called to generate
443 * output_buffer originally
444 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
445 * could be NULL.
446 * \param output_buffer_length Length of data written to output_buffer, must be
447 * less than \p output_buffer_size
448 */
449static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000450 size_t output_buffer_size, size_t output_buffer_length)
451{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000452 size_t offset = 0;
453
454 if (output_buffer_size == 0) {
455 /* If output_buffer_size is 0 then we have nothing to do. We must not
456 call memset because output_buffer may be NULL in this case */
457 return;
458 }
459
460 if (status == PSA_SUCCESS) {
461 offset = output_buffer_length;
462 }
463
464 memset(output_buffer + offset, '!', output_buffer_size - offset);
465}
466
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
469 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200470{
471 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200473 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200474 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200475 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200476 case PSA_KEY_TYPE_PASSWORD:
477 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200478 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100479#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200480 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (bits != 128 && bits != 192 && bits != 256) {
482 return PSA_ERROR_INVALID_ARGUMENT;
483 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200484 break;
485#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200486#if defined(PSA_WANT_KEY_TYPE_ARIA)
487 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (bits != 128 && bits != 192 && bits != 256) {
489 return PSA_ERROR_INVALID_ARGUMENT;
490 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200491 break;
492#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100493#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200494 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (bits != 128 && bits != 192 && bits != 256) {
496 return PSA_ERROR_INVALID_ARGUMENT;
497 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200498 break;
499#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100500#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (bits != 64 && bits != 128 && bits != 192) {
503 return PSA_ERROR_INVALID_ARGUMENT;
504 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200505 break;
506#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100507#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200508 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (bits != 256) {
510 return PSA_ERROR_INVALID_ARGUMENT;
511 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200512 break;
513#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200514 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200516 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (bits % 8 != 0) {
518 return PSA_ERROR_INVALID_ARGUMENT;
519 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522}
523
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100524/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100525 *
Steven Cooremancd640932021-03-08 12:00:27 +0100526 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
527 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100528 *
529 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100530 * \param[in] key_type The key type of the key to be used with the
531 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100532 *
533 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100534 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100535 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100536 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100537 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100538MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100539 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100541{
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (PSA_ALG_IS_HMAC(algorithm)) {
543 if (key_type == PSA_KEY_TYPE_HMAC) {
544 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100545 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100546 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
549 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
550 * key. */
551 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
552 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
553 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
554 * the block length (larger than 1) for block ciphers. */
555 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
556 return PSA_SUCCESS;
557 }
558 }
559 }
560
561 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100562}
563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
565 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200566{
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if (slot->key.data != NULL) {
568 return PSA_ERROR_ALREADY_EXISTS;
569 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 slot->key.data = mbedtls_calloc(1, buffer_length);
572 if (slot->key.data == NULL) {
573 return PSA_ERROR_INSUFFICIENT_MEMORY;
574 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200575
Ronald Cronea0f8a62020-11-25 17:52:23 +0100576 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200578}
579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
581 const uint8_t *data,
582 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200583{
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 psa_status_t status = psa_allocate_buffer_to_slot(slot,
585 data_length);
586 if (status != PSA_SUCCESS) {
587 return status;
588 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 memcpy(slot->key.data, data, data_length);
591 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200592}
593
Ronald Crona0fe59f2020-11-29 11:14:53 +0100594psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100595 const psa_key_attributes_t *attributes,
596 const uint8_t *data, size_t data_length,
597 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100599{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100601 psa_key_type_t type = attributes->type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100602
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200603 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (data_length == 0) {
605 return PSA_ERROR_NOT_SUPPORTED;
606 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (key_type_is_raw_bytes(type)) {
609 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200610
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100611 status = psa_validate_unstructured_key_bit_size(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 *bits);
613 if (status != PSA_SUCCESS) {
614 return status;
615 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200616
Ronald Crondd04d422020-11-28 15:14:42 +0100617 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100619 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return PSA_SUCCESS;
623 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200624#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200625 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100626 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200627 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Valerio Setti504a1022024-01-17 15:19:30 +0100628 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100629 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200630 return mbedtls_psa_ffdh_import_key(attributes,
631 data, data_length,
632 key_buffer, key_buffer_size,
633 key_buffer_length,
634 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100635 }
Valerio Settia55f0422023-07-10 15:34:41 +0200636#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200637 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200638#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
640 if (PSA_KEY_TYPE_IS_ECC(type)) {
641 return mbedtls_psa_ecp_import_key(attributes,
642 data, data_length,
643 key_buffer, key_buffer_size,
644 key_buffer_length,
645 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100646 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200647#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800648 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200649#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
650 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
652 if (PSA_KEY_TYPE_IS_RSA(type)) {
653 return mbedtls_psa_rsa_import_key(attributes,
654 data, data_length,
655 key_buffer, key_buffer_size,
656 key_buffer_length,
657 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200658 }
Valerio Settife478902023-07-25 12:27:19 +0200659#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
660 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800661 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100662 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100665}
666
Gilles Peskinef603c712019-01-19 13:40:11 +0100667/** Calculate the intersection of two algorithm usage policies.
668 *
669 * Return 0 (which allows no operation) on incompatibility.
670 */
671static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100672 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100673 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100675{
Gilles Peskine549ea862019-05-22 11:45:59 +0200676 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (alg1 == alg2) {
678 return alg1;
679 }
Steven Cooreman03488022021-02-18 12:07:20 +0100680 /* If the policies are from the same hash-and-sign family, check
681 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
683 PSA_ALG_IS_SIGN_HASH(alg2) &&
684 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
685 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
686 return alg2;
687 }
688 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
689 return alg1;
690 }
Steven Cooreman03488022021-02-18 12:07:20 +0100691 }
692 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100693 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100694 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
696 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
697 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
698 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
699 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100700 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100701
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100702 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
704 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
705 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
706 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100707 }
708 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
710 (alg1_len <= alg2_len)) {
711 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100712 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
714 (alg2_len <= alg1_len)) {
715 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100716 }
717 }
718 /* If the policies are from the same MAC family, check whether one
719 * of them is a minimum-MAC-length policy. Calculate the most
720 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
722 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
723 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100724 /* Validate the combination of key type and algorithm. Since the base
725 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
727 return 0;
728 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100729
Steven Cooreman31a876d2021-03-03 20:47:40 +0100730 /* Get the (exact or at-least) output lengths for both sides of the
731 * requested intersection. None of the currently supported algorithms
732 * have an output length dependent on the actual key size, so setting it
733 * to a bogus value of 0 is currently OK.
734 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100735 * Note that for at-least-this-length wildcard algorithms, the output
736 * length is set to the shortest allowed length, which allows us to
737 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
739 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100740 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100741
Steven Cooremana1d83222021-02-25 10:20:29 +0100742 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
744 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
745 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100746 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100747
748 /* If only one is an at-least-this-length policy, the intersection would
749 * be the other (fixed-length) policy as long as said fixed length is
750 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
752 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100753 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
755 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100756 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100757
Steven Cooremancd640932021-03-08 12:00:27 +0100758 /* If none of them are wildcards, check whether they define the same tag
759 * length. This is still possible here when one is default-length and
760 * the other specific-length. Ensure to always return the
761 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if (alg1_len == alg2_len) {
763 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
764 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100765 }
766 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100768}
769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770static int psa_key_algorithm_permits(psa_key_type_t key_type,
771 psa_algorithm_t policy_alg,
772 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200773{
Gilles Peskine549ea862019-05-22 11:45:59 +0200774 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (requested_alg == policy_alg) {
776 return 1;
777 }
Steven Cooreman03488022021-02-18 12:07:20 +0100778 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
779 * and requested_alg is the same hash-and-sign family with any hash,
780 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
782 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
783 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
784 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100785 }
786 /* If policy_alg is a wildcard AEAD algorithm of the same base as
787 * the requested algorithm, check the requested tag length to be
788 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if (PSA_ALG_IS_AEAD(policy_alg) &&
790 PSA_ALG_IS_AEAD(requested_alg) &&
791 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
792 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
793 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
794 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
795 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100796 }
Steven Cooremancd640932021-03-08 12:00:27 +0100797 /* If policy_alg is a MAC algorithm of the same base as the requested
798 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (PSA_ALG_IS_MAC(policy_alg) &&
800 PSA_ALG_IS_MAC(requested_alg) &&
801 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
802 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100803 /* Validate the combination of key type and algorithm. Since the policy
804 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
806 return 0;
807 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100808
Steven Cooreman31a876d2021-03-03 20:47:40 +0100809 /* Get both the requested output length for the algorithm which is to be
810 * verified, and the default output length for the base algorithm.
811 * Note that none of the currently supported algorithms have an output
812 * length dependent on actual key size, so setting it to a bogus value
813 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100814 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100816 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 key_type, 0,
818 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100819
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100820 /* If the policy is default-length, only allow an algorithm with
821 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
823 return requested_output_length == default_output_length;
824 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100825
826 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100827 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
829 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
830 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100831 }
832
Steven Cooremancd640932021-03-08 12:00:27 +0100833 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
834 * check for the requested MAC length to be equal to or longer than the
835 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
837 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
838 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100839 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200840 }
Steven Cooremance48e852020-10-05 16:02:45 +0200841 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200842 * a key derivation with that key agreement should also be allowed. This
843 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
845 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
846 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
847 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200848 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100849 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200851}
852
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100853/** Test whether a policy permits an algorithm.
854 *
855 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100856 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100857 * \note This function requires providing the key type for which the policy is
858 * being validated, since some algorithm policy definitions (e.g. MAC)
859 * have different properties depending on what kind of cipher it is
860 * combined with.
861 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100862 * \retval PSA_SUCCESS When \p alg is a specific algorithm
863 * allowed by the \p policy.
864 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
865 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
866 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100867 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100868static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
869 psa_key_type_t key_type,
870 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100871{
Steven Cooremand788fab2021-02-25 11:29:17 +0100872 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if (alg == 0) {
874 return PSA_ERROR_INVALID_ARGUMENT;
875 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100876
Steven Cooreman7e39f052021-02-23 14:18:32 +0100877 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if (PSA_ALG_IS_WILDCARD(alg)) {
879 return PSA_ERROR_INVALID_ARGUMENT;
880 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
883 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
884 return PSA_SUCCESS;
885 } else {
886 return PSA_ERROR_NOT_PERMITTED;
887 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100888}
889
Gilles Peskinef603c712019-01-19 13:40:11 +0100890/** Restrict a key policy based on a constraint.
891 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100892 * \note This function requires providing the key type for which the policy is
893 * being restricted, since some algorithm policy definitions (e.g. MAC)
894 * have different properties depending on what kind of cipher it is
895 * combined with.
896 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100897 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100898 * \param[in,out] policy The policy to restrict.
899 * \param[in] constraint The policy constraint to apply.
900 *
901 * \retval #PSA_SUCCESS
902 * \c *policy contains the intersection of the original value of
903 * \c *policy and \c *constraint.
904 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100905 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100906 * \c *policy is unchanged.
907 */
908static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100909 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100910 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100912{
913 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 psa_key_policy_algorithm_intersection(key_type, policy->alg,
915 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200916 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
918 constraint->alg2);
919 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
920 return PSA_ERROR_INVALID_ARGUMENT;
921 }
922 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
923 return PSA_ERROR_INVALID_ARGUMENT;
924 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100925 policy->usage &= constraint->usage;
926 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200927 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100929}
930
Przemek Stekiel061f6942022-11-30 10:51:35 +0100931/** Get the description of a key given its identifier and policy constraints
932 * and lock it.
933 *
934 * The key must have allow all the usage flags set in \p usage. If \p alg is
935 * nonzero, the key must allow operations with this algorithm. If \p alg is
936 * zero, the algorithm is not checked.
937 *
938 * In case of a persistent key, the function loads the description of the key
939 * into a key slot if not already done.
940 *
Ryan Everett098c6652024-01-03 13:03:36 +0000941 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +0000942 * It is the responsibility of the caller to then unregister
943 * once they have finished reading the contents of the slot.
944 * The caller unregisters by calling psa_unregister_read() or
945 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
946 * if and only if the caller already holds the global key slot mutex
947 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
948 * the unregister with mutex lock and unlock operations.
Przemek Stekiel061f6942022-11-30 10:51:35 +0100949 */
950static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100951 mbedtls_svc_key_id_t key,
952 psa_key_slot_t **p_slot,
953 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100955{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200956 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100957 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 status = psa_get_and_lock_key_slot(key, p_slot);
960 if (status != PSA_SUCCESS) {
961 return status;
962 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200963 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100964
965 /* Enforce that usage policy for the key slot contains all the flags
966 * required by the usage parameter. There is one exception: public
967 * keys can always be exported, so we treat public key objects as
968 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100970 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100974 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200975 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100976 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100977
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800978 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if (alg != 0) {
980 status = psa_key_policy_permits(&slot->attr.policy,
981 slot->attr.type,
982 alg);
983 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100984 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100986 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200989
990error:
991 *p_slot = NULL;
Ryan Everettfb792ca2024-01-31 13:40:05 +0000992 psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100995}
Darryl Green940d72c2018-07-13 13:18:51 +0100996
Ronald Cron5c522922020-11-14 16:35:34 +0100997/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200998 *
999 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001000 * available, as opposed to a key in a secure element and/or to be used
1001 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001002 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001003 * This is a temporary function that may be used instead of
1004 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1005 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001006 *
Ryan Everett098c6652024-01-03 13:03:36 +00001007 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +00001008 * It is the responsibility of the caller to then unregister
1009 * once they have finished reading the contents of the slot.
1010 * The caller unregisters by calling psa_unregister_read() or
1011 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
1012 * if and only if the caller already holds the global key slot mutex
1013 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
1014 * psa_unregister_read() with mutex lock and unlock operations.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001015 */
Ronald Cron5c522922020-11-14 16:35:34 +01001016static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1017 mbedtls_svc_key_id_t key,
1018 psa_key_slot_t **p_slot,
1019 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001021{
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1023 usage, alg);
1024 if (status != PSA_SUCCESS) {
1025 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001026 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everettfb792ca2024-01-31 13:40:05 +00001029 psa_unregister_read_under_mutex(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 *p_slot = NULL;
1031 return PSA_ERROR_NOT_SUPPORTED;
1032 }
1033
1034 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001035}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001038{
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001040 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001042
Ronald Cronea0f8a62020-11-25 17:52:23 +01001043 slot->key.data = NULL;
1044 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001047}
1048
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001049/** Completely wipe a slot in memory, including its policy.
1050 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001051psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001052{
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 /*
1056 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +00001057 * do our best to report an unexpected amount of registered readers or
1058 * an unexpected state.
1059 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1060 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1062 * function is called as part of the execution of a test suite, the
1063 * execution of the test suite is stopped in error if the assertion fails.
1064 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001065 switch (slot->state) {
1066 case PSA_SLOT_FULL:
1067 /* In this state psa_wipe_key_slot() must only be called if the
1068 * caller is the last reader. */
1069 case PSA_SLOT_PENDING_DELETION:
1070 /* In this state psa_wipe_key_slot() must only be called if the
1071 * caller is the last reader. */
1072 if (slot->registered_readers != 1) {
1073 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1);
1074 status = PSA_ERROR_CORRUPTION_DETECTED;
1075 }
1076 break;
1077 case PSA_SLOT_FILLING:
1078 /* In this state registered_readers must be 0. */
1079 if (slot->registered_readers != 0) {
1080 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0);
1081 status = PSA_ERROR_CORRUPTION_DETECTED;
1082 }
1083 break;
1084 case PSA_SLOT_EMPTY:
1085 /* The slot is already empty, it cannot be wiped. */
1086 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1087 status = PSA_ERROR_CORRUPTION_DETECTED;
1088 break;
1089 default:
1090 /* The slot's state is invalid. */
1091 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001092 }
1093
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001094 /* Multipart operations may still be using the key. This is safe
1095 * because all multipart operation objects are independent from
1096 * the key slot: if they need to access the key after the setup
1097 * phase, they have a copy of the key. Note that this means that
1098 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001099 /* At this point, key material and other type-specific content has
1100 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001101 * zeroize because the metadata is not particularly sensitive.
1102 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 memset(slot, 0, sizeof(*slot));
1104 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001105}
1106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001108{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001109 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001110 psa_status_t status; /* status of the last operation */
1111 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001112#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1113 psa_se_drv_table_entry_t *driver;
1114#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (mbedtls_svc_key_id_is_null(key)) {
1117 return PSA_SUCCESS;
1118 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001119
Ronald Cronf2911112020-10-29 17:51:10 +01001120 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001121 * Get the description of the key in a key slot, and register to read it.
1122 * In the case of a persistent key, this will load the key description
1123 * from persistent memory if not done yet.
1124 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001125 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001126 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 status = psa_get_and_lock_key_slot(key, &slot);
1128 if (status != PSA_SUCCESS) {
1129 return status;
1130 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001131
Ryan Everettc053d962024-01-25 17:56:32 +00001132#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001133 /* We cannot unlock between setting the state to PENDING_DELETION
1134 * and destroying the key in storage, as otherwise another thread
1135 * could load the key into a new slot and the key will not be
1136 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001137 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1138 &mbedtls_threading_key_slot_mutex));
Ryan Everettd868b742024-03-08 18:35:09 +00001139
1140 if (slot->state == PSA_SLOT_PENDING_DELETION) {
1141 /* Another thread has destroyed the key between us locking the slot
1142 * and us gaining the mutex. Unregister from the slot,
1143 * and report that the key does not exist. */
1144 status = psa_unregister_read(slot);
1145
1146 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1147 &mbedtls_threading_key_slot_mutex));
1148 return (status == PSA_SUCCESS) ? PSA_ERROR_INVALID_HANDLE : status;
1149 }
Ryan Everettc053d962024-01-25 17:56:32 +00001150#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001151 /* Set the key slot containing the key description's state to
1152 * PENDING_DELETION. This stops new operations from registering
1153 * to read the slot. Current readers can safely continue to access
1154 * the key within the slot; the last registered reader will
1155 * automatically wipe the slot when they call psa_unregister_read().
1156 * If the key is persistent, we can now delete the copy of the key
1157 * from memory. If the key is opaque, we require the driver to
1158 * deal with the deletion. */
Ryan Everettd868b742024-03-08 18:35:09 +00001159 overall_status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1160 PSA_SLOT_PENDING_DELETION);
Ryan Everettc053d962024-01-25 17:56:32 +00001161
Ryan Everettd868b742024-03-08 18:35:09 +00001162 if (overall_status != PSA_SUCCESS) {
Ryan Everettc053d962024-01-25 17:56:32 +00001163 goto exit;
1164 }
Ronald Cronf2911112020-10-29 17:51:10 +01001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001167 /* Refuse the destruction of a read-only key (which may or may not work
1168 * if we attempt it, depending on whether the key is merely read-only
1169 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001170 * Just do the best we can, which is to wipe the copy in memory
1171 * (done in this function's cleanup code). */
1172 overall_status = PSA_ERROR_NOT_PERMITTED;
1173 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001174 }
1175
Gilles Peskine354f7672019-07-12 23:46:38 +02001176#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1178 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001179 /* For a key in a secure element, we need to do three things:
1180 * remove the key file in internal storage, destroy the
1181 * key inside the secure element, and update the driver's
1182 * persistent data. Start a transaction that will encompass these
1183 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001185 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001187 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 status = psa_crypto_save_transaction();
1189 if (status != PSA_SUCCESS) {
1190 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001191 /* We should still try to destroy the key in the secure
1192 * element and the key metadata in storage. This is especially
1193 * important if the error is that the storage is full.
1194 * But how to do it exactly without risking an inconsistent
1195 * state after a reset?
1196 * https://github.com/ARMmbed/mbed-crypto/issues/215
1197 */
1198 overall_status = status;
1199 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001200 }
1201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 status = psa_destroy_se_key(driver,
1203 psa_key_slot_get_slot_number(slot));
1204 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001205 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001207 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001208#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1209
Darryl Greend49a4992018-06-18 17:27:26 +01001210#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001212 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001213 * The slot will still hold a copy of the key until the last reader
1214 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 status = psa_destroy_persistent_key(slot->attr.id);
1216 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001217 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 }
Darryl Greend49a4992018-06-18 17:27:26 +01001219 }
1220#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001221
Gilles Peskinefc762652019-07-22 19:30:34 +02001222#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if (driver != NULL) {
1224 status = psa_save_se_persistent_data(driver);
1225 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001226 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 }
1228 status = psa_crypto_stop_transaction();
1229 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001230 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001232 }
1233#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1234
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001235exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001236 /* Unregister from reading the slot. If we are the last active reader
1237 * then this will wipe the slot. */
1238 status = psa_unregister_read(slot);
1239 /* Prioritize CORRUPTION_DETECTED from unregistering over
1240 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001242 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 }
Ryan Everettc053d962024-01-25 17:56:32 +00001244
1245#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001246 /* Don't overwrite existing errors if the unlock fails. */
1247 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001248 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1249 &mbedtls_threading_key_slot_mutex));
1250#endif
1251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001253}
1254
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001255/** Retrieve all the publicly-accessible attributes of a key.
1256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001257psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1258 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001259{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001260 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001261 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1266 if (status != PSA_SUCCESS) {
1267 return status;
1268 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001269
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001270 *attributes = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001271
1272#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1274 psa_set_key_slot_number(attributes,
1275 psa_key_slot_get_slot_number(slot));
1276 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001277#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001278
Gilles Peskine97c0b2f2024-02-16 00:49:46 +01001279 return psa_unregister_read_under_mutex(slot);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280}
1281
Gilles Peskinec8000c02019-08-02 20:15:51 +02001282#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1283psa_status_t psa_get_key_slot_number(
1284 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001286{
Gilles Peskine972539c2024-02-28 01:49:45 +01001287 if (attributes->has_slot_number) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001288 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 return PSA_SUCCESS;
1290 } else {
1291 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001292 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001293}
1294#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1297 size_t key_buffer_size,
1298 uint8_t *data,
1299 size_t data_size,
1300 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001301{
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (key_buffer_size > data_size) {
1303 return PSA_ERROR_BUFFER_TOO_SMALL;
1304 }
1305 memcpy(data, key_buffer, key_buffer_size);
1306 memset(data + key_buffer_size, 0,
1307 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001308 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001310}
1311
Ronald Cron7285cda2020-11-26 14:46:37 +01001312psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001313 const psa_key_attributes_t *attributes,
1314 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001316{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001317 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (key_type_is_raw_bytes(type) ||
1320 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001321 PSA_KEY_TYPE_IS_ECC(type) ||
1322 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 return psa_export_key_buffer_internal(
1324 key_buffer, key_buffer_size,
1325 data, data_size, data_length);
1326 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001327 /* This shouldn't happen in the reference implementation, but
1328 it is valid for a special-purpose implementation to omit
1329 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001331 }
1332}
1333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1335 uint8_t *data,
1336 size_t data_size,
1337 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001338{
1339 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1340 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1341 psa_key_slot_t *slot;
1342
Ronald Crone907e552021-01-18 13:22:38 +01001343 /* Reject a zero-length output buffer now, since this can never be a
1344 * valid key representation. This way we know that data must be a valid
1345 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (data_size == 0) {
1347 return PSA_ERROR_BUFFER_TOO_SMALL;
1348 }
Ronald Crone907e552021-01-18 13:22:38 +01001349
Ronald Cron9486f9d2020-11-26 13:36:23 +01001350 /* Set the key to empty now, so that even when there are errors, we always
1351 * set data_length to a value between 0 and data_size. On error, setting
1352 * the key to empty is a good choice because an empty key representation is
1353 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001354 *data_length = 0;
1355
Ronald Cron9486f9d2020-11-26 13:36:23 +01001356 /* Export requires the EXPORT flag. There is an exception for public keys,
1357 * which don't require any flag, but
1358 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1359 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1361 PSA_KEY_USAGE_EXPORT, 0);
1362 if (status != PSA_SUCCESS) {
1363 return status;
1364 }
mohammad160306e79202018-03-28 13:17:44 +03001365
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001366 status = psa_driver_wrapper_export_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 slot->key.data, slot->key.bytes,
1368 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001369
Ryan Everetta103ec92024-01-31 13:59:57 +00001370 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001373}
1374
Ronald Cron7285cda2020-11-26 14:46:37 +01001375psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001376 const psa_key_attributes_t *attributes,
1377 const uint8_t *key_buffer,
1378 size_t key_buffer_size,
1379 uint8_t *data,
1380 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001382{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001383 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001384
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001385 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001386 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1387 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001388 /* Exporting public -> public */
1389 return psa_export_key_buffer_internal(
1390 key_buffer, key_buffer_size,
1391 data, data_size, data_length);
1392 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001393#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001394 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1395 return mbedtls_psa_rsa_export_public_key(attributes,
1396 key_buffer,
1397 key_buffer_size,
1398 data,
1399 data_size,
1400 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001401#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001402 /* We don't know how to convert a private RSA key to public. */
1403 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001404#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001405 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001406 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001407#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001408 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1409 return mbedtls_psa_ecp_export_public_key(attributes,
1410 key_buffer,
1411 key_buffer_size,
1412 data,
1413 data_size,
1414 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001415#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001416 /* We don't know how to convert a private ECC key to public */
1417 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001418#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001419 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001420 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001421#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001422 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001423 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001424 key_buffer,
1425 key_buffer_size,
1426 data, data_size,
1427 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001428#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001429 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001430#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001431 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001433 (void) key_buffer;
1434 (void) key_buffer_size;
1435 (void) data;
1436 (void) data_size;
1437 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001439 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001440}
Ronald Crond18b5f82020-11-25 16:46:05 +01001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1443 uint8_t *data,
1444 size_t data_size,
1445 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001446{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001447 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001448 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001449 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001450
Ronald Crone907e552021-01-18 13:22:38 +01001451 /* Reject a zero-length output buffer now, since this can never be a
1452 * valid key representation. This way we know that data must be a valid
1453 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if (data_size == 0) {
1455 return PSA_ERROR_BUFFER_TOO_SMALL;
1456 }
Ronald Crone907e552021-01-18 13:22:38 +01001457
Darryl Greendd8fb772018-11-07 16:00:44 +00001458 /* Set the key to empty now, so that even when there are errors, we always
1459 * set data_length to a value between 0 and data_size. On error, setting
1460 * the key to empty is a good choice because an empty key representation is
1461 * unlikely to be accepted anywhere. */
1462 *data_length = 0;
1463
1464 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1466 if (status != PSA_SUCCESS) {
1467 return status;
1468 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1471 status = PSA_ERROR_INVALID_ARGUMENT;
1472 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001473 }
1474
Ronald Cron67227982020-11-26 15:16:05 +01001475 status = psa_driver_wrapper_export_public_key(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001476 &slot->attr, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001478
1479exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001480 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001483}
1484
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001485/** Validate that a key policy is internally well-formed.
1486 *
1487 * This function only rejects invalid policies. It does not validate the
1488 * consistency of the policy with respect to other attributes of the key
1489 * such as the key type.
1490 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001492{
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1494 PSA_KEY_USAGE_COPY |
1495 PSA_KEY_USAGE_ENCRYPT |
1496 PSA_KEY_USAGE_DECRYPT |
1497 PSA_KEY_USAGE_SIGN_MESSAGE |
1498 PSA_KEY_USAGE_VERIFY_MESSAGE |
1499 PSA_KEY_USAGE_SIGN_HASH |
1500 PSA_KEY_USAGE_VERIFY_HASH |
1501 PSA_KEY_USAGE_VERIFY_DERIVATION |
1502 PSA_KEY_USAGE_DERIVE)) != 0) {
1503 return PSA_ERROR_INVALID_ARGUMENT;
1504 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001507}
1508
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001509/** Validate the internal consistency of key attributes.
1510 *
1511 * This function only rejects invalid attribute values. If does not
1512 * validate the consistency of the attributes with any key data that may
1513 * be involved in the creation of the key.
1514 *
1515 * Call this function early in the key creation process.
1516 *
1517 * \param[in] attributes Key attributes for the new key.
1518 * \param[out] p_drv On any return, the driver for the key, if any.
1519 * NULL for a transparent key.
1520 *
1521 */
1522static psa_status_t psa_validate_key_attributes(
1523 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001525{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001526 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1528 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 status = psa_validate_key_location(lifetime, p_drv);
1531 if (status != PSA_SUCCESS) {
1532 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001533 }
1534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 status = psa_validate_key_persistence(lifetime);
1536 if (status != PSA_SUCCESS) {
1537 return status;
1538 }
1539
1540 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1541 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1542 return PSA_ERROR_INVALID_ARGUMENT;
1543 }
1544 } else {
1545 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1546 return PSA_ERROR_INVALID_ARGUMENT;
1547 }
1548 }
1549
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001550 status = psa_validate_key_policy(&attributes->policy);
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 if (status != PSA_SUCCESS) {
1552 return status;
1553 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001554
1555 /* Refuse to create overly large keys.
1556 * Note that this doesn't trigger on import if the attributes don't
1557 * explicitly specify a size (so psa_get_key_bits returns 0), so
1558 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1560 return PSA_ERROR_NOT_SUPPORTED;
1561 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001564}
1565
Gilles Peskine4747d192019-04-17 15:05:45 +02001566/** Prepare a key slot to receive key material.
1567 *
1568 * This function allocates a key slot and sets its metadata.
1569 *
1570 * If this function fails, call psa_fail_key_creation().
1571 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001572 * This function is intended to be used as follows:
1573 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001574 * it with the specified attributes, and in case of a volatile key assign it
1575 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001576 * -# Populate the slot with the key material.
1577 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1578 * In case of failure at any step, stop the sequence and call
1579 * psa_fail_key_creation().
1580 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001581 * On success, the key slot's state is PSA_SLOT_FILLING.
1582 * It is the responsibility of the caller to change the slot's state to
1583 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001584 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001585 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001586 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001587 * \param[out] p_slot On success, a pointer to the prepared slot.
1588 * \param[out] p_drv On any return, the driver for the key, if any.
1589 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001590 *
1591 * \retval #PSA_SUCCESS
1592 * The key slot is ready to receive key material.
1593 * \return If this function fails, the key slot is an invalid state.
1594 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001595 */
1596static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001597 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001598 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001599 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001601{
1602 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001603 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001604 psa_key_slot_t *slot;
1605
Gilles Peskinedf179142019-07-15 22:02:14 +02001606 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001607 *p_drv = NULL;
1608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 status = psa_validate_key_attributes(attributes, p_drv);
1610 if (status != PSA_SUCCESS) {
1611 return status;
1612 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001613
Ryan Everett3d8118d2024-01-30 16:58:47 +00001614#if defined(MBEDTLS_THREADING_C)
1615 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1616 &mbedtls_threading_key_slot_mutex));
1617#endif
Ryan Everettb69118e2024-01-02 15:54:32 +00001618 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001619#if defined(MBEDTLS_THREADING_C)
1620 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1621 &mbedtls_threading_key_slot_mutex));
1622#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (status != PSA_SUCCESS) {
1624 return status;
1625 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001626 slot = *p_slot;
1627
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001628 /* We're storing the declared bit-size of the key. It's up to each
1629 * creation mechanism to verify that this information is correct.
1630 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001631 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001632 * is optional (import, copy). In case of a volatile key, assign it the
1633 * volatile key identifier associated to the slot returned to contain its
1634 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001635
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001636 slot->attr = *attributes;
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001638#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1639 slot->attr.id = volatile_key_id;
1640#else
1641 slot->attr.id.key_id = volatile_key_id;
1642#endif
1643 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001644
Gilles Peskinecbaff462019-07-12 23:46:04 +02001645#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001646 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001647 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001648 * create the key file in internal storage, create the
1649 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001650 * persistent data. This is done by starting a transaction that will
1651 * encompass these three actions.
1652 * For registering a volatile key, we just need to find an appropriate
1653 * slot number inside the SE. Since the key is designated volatile, creating
1654 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001655 /* The first thing to do is to find a slot number for the new key.
1656 * We save the slot number in persistent storage as part of the
1657 * transaction data. It will be needed to recover if the power
1658 * fails during the key creation process, to clean up on the secure
1659 * element side after restarting. Obtaining a slot number from the
1660 * secure element driver updates its persistent state, but we do not yet
1661 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001662 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001664 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1666 &slot_number);
1667 if (status != PSA_SUCCESS) {
1668 return status;
1669 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001670
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001671 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001673 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001674 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001675 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 status = psa_crypto_save_transaction();
1677 if (status != PSA_SUCCESS) {
1678 (void) psa_crypto_stop_transaction();
1679 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001680 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001681 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001682
1683 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001685 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001688 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001690 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001691#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001694}
Gilles Peskine4747d192019-04-17 15:05:45 +02001695
1696/** Finalize the creation of a key once its key material has been set.
1697 *
1698 * This entails writing the key to persistent storage.
1699 *
1700 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001701 * See the documentation of psa_start_key_creation() for the intended use
1702 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001703 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001704 * If the finalization succeeds, the function sets the key slot's state to
1705 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1706 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001707 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001708 * \param[in,out] slot Pointer to the slot with key material.
1709 * \param[in] driver The secure element driver for the key,
1710 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001711 * \param[out] key On success, identifier of the key. Note that the
1712 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001713 *
1714 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001715 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001716 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1717 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1718 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1719 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1720 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1721 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001722 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001723 * \return If this function fails, the key slot is an invalid state.
1724 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001725 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001726static psa_status_t psa_finish_key_creation(
1727 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001728 psa_se_drv_table_entry_t *driver,
1729 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001730{
1731 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001732 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001733 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001734
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001735#if defined(MBEDTLS_THREADING_C)
1736 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1737 &mbedtls_threading_key_slot_mutex));
1738#endif
1739
Gilles Peskine4747d192019-04-17 15:05:45 +02001740#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001742#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001744 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001745 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001747
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001748 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1749 sizeof(data.slot_number),
1750 "Slot number size does not match psa_se_key_data_storage_t");
1751
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1753 status = psa_save_persistent_key(&slot->attr,
1754 (uint8_t *) &data,
1755 sizeof(data));
1756 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001757#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1758 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001759 /* Key material is saved in export representation in the slot, so
1760 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 status = psa_save_persistent_key(&slot->attr,
1762 slot->key.data,
1763 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001764 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001765 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001766#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1767
Gilles Peskinecbaff462019-07-12 23:46:04 +02001768#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001769 /* Finish the transaction for a key creation. This does not
1770 * happen when registering an existing key. Detect this case
1771 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001772 * creation of a persistent key in a secure element requires a transaction,
1773 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 if (driver != NULL &&
1775 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1776 status = psa_save_se_persistent_data(driver);
1777 if (status != PSA_SUCCESS) {
1778 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001779
1780#if defined(MBEDTLS_THREADING_C)
1781 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1782 &mbedtls_threading_key_slot_mutex));
1783#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001785 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001787 }
1788#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001791 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001792 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1793 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001795 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001797 }
Ronald Cron50972942020-11-14 11:28:25 +01001798
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001799#if defined(MBEDTLS_THREADING_C)
1800 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1801 &mbedtls_threading_key_slot_mutex));
1802#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001804}
1805
1806/** Abort the creation of a key.
1807 *
1808 * You may call this function after calling psa_start_key_creation(),
1809 * or after psa_finish_key_creation() fails. In other circumstances, this
1810 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001811 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001812 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001813 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001814 * \param[in,out] slot Pointer to the slot with key material.
1815 * \param[in] driver The secure element driver for the key,
1816 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001817 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001818static void psa_fail_key_creation(psa_key_slot_t *slot,
1819 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001820{
Gilles Peskine011e4282019-06-26 18:34:38 +02001821 (void) driver;
1822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001824 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001826
Ryan Everettb7101442024-01-23 20:09:49 +00001827#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00001828 /* If the lock operation fails we still wipe the slot.
1829 * Operations will no longer work after a failed lock,
1830 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00001831 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
1832#endif
1833
Gilles Peskine011e4282019-06-26 18:34:38 +02001834#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001835 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001836 * element, and the failure happened later (when saving metadata
1837 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001838 * element.
1839 * https://github.com/ARMmbed/mbed-crypto/issues/217
1840 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001841
Gilles Peskined7729582019-08-05 15:55:54 +02001842 /* Abort the ongoing transaction if any (there may not be one if
1843 * the creation process failed before starting one, or if the
1844 * key creation is a registration of a key in a secure element).
1845 * Earlier functions must already have done what it takes to undo any
1846 * partial creation. All that's left is to update the transaction data
1847 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001849#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00001852
1853#if defined(MBEDTLS_THREADING_C)
1854 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
1855#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02001856}
1857
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001858/** Validate optional attributes during key creation.
1859 *
1860 * Some key attributes are optional during key creation. If they are
1861 * specified in the attributes structure, check that they are consistent
1862 * with the data in the slot.
1863 *
1864 * This function should be called near the end of key creation, after
1865 * the slot in memory is fully populated but before saving persistent data.
1866 */
1867static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001868 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001870{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001871 if (attributes->type != 0) {
1872 if (attributes->type != slot->attr.type) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 return PSA_ERROR_INVALID_ARGUMENT;
1874 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001875 }
1876
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001877 if (attributes->bits != 0) {
1878 if (attributes->bits != slot->attr.bits) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 return PSA_ERROR_INVALID_ARGUMENT;
1880 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001881 }
1882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001884}
1885
Gilles Peskine449bd832023-01-11 14:50:10 +01001886psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1887 const uint8_t *data,
1888 size_t data_length,
1889 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001890{
1891 psa_status_t status;
1892 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001893 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001894 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301895 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896
Ronald Cron81709fc2020-11-14 12:10:32 +01001897 *key = MBEDTLS_SVC_KEY_ID_INIT;
1898
Gilles Peskine0f84d622019-09-12 19:03:13 +02001899 /* Reject zero-length symmetric keys (including raw data key objects).
1900 * This also rejects any key which might be encoded as an empty string,
1901 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 if (data_length == 0) {
1903 return PSA_ERROR_INVALID_ARGUMENT;
1904 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001905
Archana449608b2021-09-08 15:36:05 +05301906 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 if (data_length > SIZE_MAX / 8) {
1908 return PSA_ERROR_NOT_SUPPORTED;
1909 }
Archana6ed4bda2021-08-04 10:47:15 +05301910
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1912 &slot, &driver);
1913 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001914 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001916
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001917 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301918 * storage ( thus not in the case of importing a key in a secure element
1919 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1920 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001922 if (psa_key_lifetime_is_external(attributes->lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301923 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 attributes, data, data_length, &storage_size);
1925 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301926 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 }
Archanad8a83dc2021-06-14 10:04:16 +05301928 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 status = psa_allocate_buffer_to_slot(slot, storage_size);
1930 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001931 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001933 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001934
1935 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 status = psa_driver_wrapper_import_key(attributes,
1937 data, data_length,
1938 slot->key.data,
1939 slot->key.bytes,
1940 &slot->key.bytes, &bits);
1941 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001942 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001944
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001946 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001948 status = PSA_ERROR_INVALID_ARGUMENT;
1949 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001950 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001951
Archana6ed4bda2021-08-04 10:47:15 +05301952 /* Enforce a size limit, and in particular ensure that the bit
1953 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301955 status = PSA_ERROR_NOT_SUPPORTED;
1956 goto exit;
1957 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 status = psa_validate_optional_attributes(slot, attributes);
1959 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001960 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001964exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 if (status != PSA_SUCCESS) {
1966 psa_fail_key_creation(slot, driver);
1967 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001970}
1971
Gilles Peskined7729582019-08-05 15:55:54 +02001972#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1973psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02001975{
1976 psa_status_t status;
1977 psa_key_slot_t *slot = NULL;
1978 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001979 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001980
1981 /* Leaving attributes unspecified is not currently supported.
1982 * It could make sense to query the key type and size from the
1983 * secure element, but not all secure elements support this
1984 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
1986 return PSA_ERROR_NOT_SUPPORTED;
1987 }
1988 if (psa_get_key_bits(attributes) == 0) {
1989 return PSA_ERROR_NOT_SUPPORTED;
1990 }
Gilles Peskined7729582019-08-05 15:55:54 +02001991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
1993 &slot, &driver);
1994 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02001995 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 }
Gilles Peskined7729582019-08-05 15:55:54 +02001997
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02001999
2000exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 if (status != PSA_SUCCESS) {
2002 psa_fail_key_creation(slot, driver);
2003 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002004
Gilles Peskined7729582019-08-05 15:55:54 +02002005 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 psa_close_key(key);
2007 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002008}
2009#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2012 const psa_key_attributes_t *specified_attributes,
2013 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002014{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002015 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002016 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002017 psa_key_slot_t *source_slot = NULL;
2018 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002019 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002020 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302021 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002022
Ronald Cron81709fc2020-11-14 12:10:32 +01002023 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2024
Archana8a180362021-07-05 02:18:48 +05302025 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2027 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002028 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 status = psa_validate_optional_attributes(source_slot,
2032 specified_attributes);
2033 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002034 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002036
Archana9d17bf42021-09-10 06:22:44 +05302037 /* The target key type and number of bits have been validated by
2038 * psa_validate_optional_attributes() to be either equal to zero or
2039 * equal to the ones of the source key. So it is safe to inherit
2040 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302041 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002042 actual_attributes.bits = source_slot->attr.bits;
2043 actual_attributes.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302044
2045
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 status = psa_restrict_key_policy(source_slot->attr.type,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002047 &actual_attributes.policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 &source_slot->attr.policy);
2049 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002050 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2054 &target_slot, &driver);
2055 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002056 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 }
2058 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2059 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302060 /*
Archana9d17bf42021-09-10 06:22:44 +05302061 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302062 * the source key would need to be exported as plaintext and re-imported
2063 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302064 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302065 * appropriate API invocations from the application, if needed.
2066 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002067 status = PSA_ERROR_NOT_SUPPORTED;
2068 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002069 }
Archana8a180362021-07-05 02:18:48 +05302070 /*
2071 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302072 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302073 * - For opaque keys this translates to an invocation of the drivers'
2074 * copy_key entry point through the dispatch layer.
2075 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002076 if (psa_key_lifetime_is_external(actual_attributes.lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2078 &storage_size);
2079 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302080 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 }
Archana374fe5b2021-09-08 15:50:28 +05302082
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2084 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302085 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 }
Archana374fe5b2021-09-08 15:50:28 +05302087
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 status = psa_driver_wrapper_copy_key(&actual_attributes,
2089 source_slot->key.data,
2090 source_slot->key.bytes,
2091 target_slot->key.data,
2092 target_slot->key.bytes,
2093 &target_slot->key.bytes);
2094 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302095 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 }
2097 } else {
2098 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302099 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 source_slot->key.bytes);
2101 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302102 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 }
Archana8a180362021-07-05 02:18:48 +05302104 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002106exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 if (status != PSA_SUCCESS) {
2108 psa_fail_key_creation(target_slot, driver);
2109 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002110
Ryan Everetta103ec92024-01-31 13:59:57 +00002111 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002114}
2115
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002116
2117
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002118/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002119/* Message digests */
2120/****************************************************************/
2121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002123{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002124 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if (operation->id == 0) {
2126 return PSA_SUCCESS;
2127 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002130 operation->id = 0;
2131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002133}
2134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2136 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002137{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002138 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002139
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002140 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002142 status = PSA_ERROR_BAD_STATE;
2143 goto exit;
2144 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002147 status = PSA_ERROR_INVALID_ARGUMENT;
2148 goto exit;
2149 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002150
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002151 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2152 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002156
2157exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 if (status != PSA_SUCCESS) {
2159 psa_hash_abort(operation);
2160 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002161
2162 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002163}
2164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2166 const uint8_t *input,
2167 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002168{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002169 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002172 status = PSA_ERROR_BAD_STATE;
2173 goto exit;
2174 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002175
Steven Cooremanc8288352021-03-04 14:02:19 +01002176 /* Don't require hash implementations to behave correctly on a
2177 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 if (input_length == 0) {
2179 return PSA_SUCCESS;
2180 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002183
2184exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 if (status != PSA_SUCCESS) {
2186 psa_hash_abort(operation);
2187 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002188
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002190}
2191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2193 uint8_t *hash,
2194 size_t hash_size,
2195 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002196{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002197 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if (operation->id == 0) {
2199 return PSA_ERROR_BAD_STATE;
2200 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002201
Steven Cooreman1e582352021-02-18 17:24:37 +01002202 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 operation, hash, hash_size, hash_length);
2204 psa_hash_abort(operation);
2205 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002206}
2207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2209 const uint8_t *hash,
2210 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002211{
Ronald Cron69a63422021-10-18 09:47:58 +02002212 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002213 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002214 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 operation,
2216 actual_hash, sizeof(actual_hash),
2217 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002224 status = PSA_ERROR_INVALID_SIGNATURE;
2225 goto exit;
2226 }
2227
Dave Rodgman78701152023-08-29 14:20:18 +01002228 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002229 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002231
2232exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2234 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002235 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239}
2240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241psa_status_t psa_hash_compute(psa_algorithm_t alg,
2242 const uint8_t *input, size_t input_length,
2243 uint8_t *hash, size_t hash_size,
2244 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002245{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002246 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 if (!PSA_ALG_IS_HASH(alg)) {
2248 return PSA_ERROR_INVALID_ARGUMENT;
2249 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2252 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002253}
2254
Gilles Peskine449bd832023-01-11 14:50:10 +01002255psa_status_t psa_hash_compare(psa_algorithm_t alg,
2256 const uint8_t *input, size_t input_length,
2257 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002258{
Ronald Cron69a63422021-10-18 09:47:58 +02002259 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002260 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 if (!PSA_ALG_IS_HASH(alg)) {
2263 return PSA_ERROR_INVALID_ARGUMENT;
2264 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002265
Steven Cooreman1e582352021-02-18 17:24:37 +01002266 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 alg, input, input_length,
2268 actual_hash, sizeof(actual_hash),
2269 &actual_hash_length);
2270 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002271 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 }
2273 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002274 status = PSA_ERROR_INVALID_SIGNATURE;
2275 goto exit;
2276 }
Dave Rodgman78701152023-08-29 14:20:18 +01002277 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002278 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002280
2281exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2283 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002284}
2285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2287 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002288{
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 if (source_operation->id == 0 ||
2290 target_operation->id != 0) {
2291 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002292 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2295 target_operation);
2296 if (status != PSA_SUCCESS) {
2297 psa_hash_abort(target_operation);
2298 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002301}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002302
2303
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002304/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002305/* MAC */
2306/****************************************************************/
2307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002309{
Steven Cooremane6804192021-03-19 18:28:56 +01002310 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 if (operation->id == 0) {
2312 return PSA_SUCCESS;
2313 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002316 operation->mac_size = 0;
2317 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002318 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002321}
2322
Ronald Cron2dff3b22021-06-17 16:33:22 +02002323static psa_status_t psa_mac_finalize_alg_and_key_validation(
2324 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002325 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002327{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002328 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 psa_key_type_t key_type = psa_get_key_type(attributes);
2330 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (!PSA_ALG_IS_MAC(alg)) {
2333 return PSA_ERROR_INVALID_ARGUMENT;
2334 }
Steven Cooremane6804192021-03-19 18:28:56 +01002335
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002336 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 status = psa_mac_key_can_do(alg, key_type);
2338 if (status != PSA_SUCCESS) {
2339 return status;
2340 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002341
Steven Cooremand1a68f12021-05-10 11:13:41 +02002342 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002346 /* A very short MAC is too short for security since it can be
2347 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2348 * so we make this our minimum, even though 32 bits is still
2349 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002351 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2354 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002355 /* It's impossible to "truncate" to a larger length than the full length
2356 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002358 }
2359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002361 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2362 * that is disabled in the compile-time configuration. The result can
2363 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2364 * configuration into account. In this case, force a return of
2365 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2366 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2367 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2368 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2369 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002371 }
2372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002374}
2375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2377 mbedtls_svc_key_id_t key,
2378 psa_algorithm_t alg,
2379 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002380{
2381 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2382 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002383 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002384
2385 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002387 status = PSA_ERROR_BAD_STATE;
2388 goto exit;
2389 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002390
2391 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 key,
2393 &slot,
2394 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2395 alg);
2396 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002397 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002399
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002400 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 &operation->mac_size);
2402 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002403 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002405
2406 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002407 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 if (is_sign) {
2409 status = psa_driver_wrapper_mac_sign_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002410 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 slot->key.data,
2412 slot->key.bytes,
2413 alg);
2414 } else {
2415 status = psa_driver_wrapper_mac_verify_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002416 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 slot->key.data,
2418 slot->key.bytes,
2419 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002420 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002421
Gilles Peskinefbfac682018-07-08 20:51:54 +02002422exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (status != PSA_SUCCESS) {
2424 psa_mac_abort(operation);
2425 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002426
Ryan Everettfb9857f2024-02-14 12:16:41 +00002427 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430}
2431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2433 mbedtls_svc_key_id_t key,
2434 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002435{
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002437}
2438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2440 mbedtls_svc_key_id_t key,
2441 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002442{
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002444}
2445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2447 const uint8_t *input,
2448 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002449{
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 if (operation->id == 0) {
2451 return PSA_ERROR_BAD_STATE;
2452 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002454 /* Don't require hash implementations to behave correctly on a
2455 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 if (input_length == 0) {
2457 return PSA_SUCCESS;
2458 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2461 input, input_length);
2462 if (status != PSA_SUCCESS) {
2463 psa_mac_abort(operation);
2464 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002467}
2468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2470 uint8_t *mac,
2471 size_t mac_size,
2472 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002473{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002474 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2475 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002478 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002479 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002480 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002483 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002484 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002485 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002486
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002487 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2488 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002490 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002491 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002492 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002495 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002496 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002497 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 status = psa_driver_wrapper_mac_sign_finish(operation,
2500 mac, operation->mac_size,
2501 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002502
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002503exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002504 /* In case of success, set the potential excess room in the output buffer
2505 * to an invalid value, to avoid potentially leaking a longer MAC.
2506 * In case of error, set the output length and content to a safe default,
2507 * such that in case the caller misses an error check, the output would be
2508 * an unachievable MAC.
2509 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002511 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002512 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002513 }
mohammad16036df908f2018-04-02 08:34:15 -07002514
Paul Elliottdc42ca82023-02-24 18:11:59 +00002515 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002518
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002520}
2521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2523 const uint8_t *mac,
2524 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002525{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002526 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2527 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002530 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002531 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002532 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002535 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002536 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002537 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002538
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002540 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002541 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002542 }
2543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 status = psa_driver_wrapper_mac_verify_finish(operation,
2545 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002546
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002547exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002551}
2552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2554 psa_algorithm_t alg,
2555 const uint8_t *input,
2556 size_t input_length,
2557 uint8_t *mac,
2558 size_t mac_size,
2559 size_t *mac_length,
2560 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002561{
2562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002563 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2564 psa_key_slot_t *slot;
2565 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002566
Ronald Cron51131b52021-06-17 17:17:20 +02002567 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 key,
2569 &slot,
2570 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2571 alg);
2572 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002573 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002575
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002576 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 &operation_mac_size);
2578 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002579 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002583 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002584 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002585 }
2586
2587 status = psa_driver_wrapper_mac_compute(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002588 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 slot->key.data, slot->key.bytes,
2590 alg,
2591 input, input_length,
2592 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002593
2594exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002595 /* In case of success, set the potential excess room in the output buffer
2596 * to an invalid value, to avoid potentially leaking a longer MAC.
2597 * In case of error, set the output length and content to a safe default,
2598 * such that in case the caller misses an error check, the output would be
2599 * an unachievable MAC.
2600 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002602 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002603 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002604 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002605
2606 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002607
Ryan Everetta103ec92024-01-31 13:59:57 +00002608 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002611}
2612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002614 psa_algorithm_t alg,
2615 const uint8_t *input,
2616 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 uint8_t *mac,
2618 size_t mac_size,
2619 size_t *mac_length)
2620{
2621 return psa_mac_compute_internal(key, alg,
2622 input, input_length,
2623 mac, mac_size, mac_length, 1);
2624}
2625
2626psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2627 psa_algorithm_t alg,
2628 const uint8_t *input,
2629 size_t input_length,
2630 const uint8_t *mac,
2631 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002632{
2633 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002634 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2635 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 status = psa_mac_compute_internal(key, alg,
2638 input, input_length,
2639 actual_mac, sizeof(actual_mac),
2640 &actual_mac_length, 0);
2641 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002642 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002644
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002646 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002647 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002648 }
Dave Rodgman78701152023-08-29 14:20:18 +01002649 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002650 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002651 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002652 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002653
2654exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002658}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002659
Gilles Peskinee59236f2018-01-27 23:32:46 +01002660/****************************************************************/
2661/* Asymmetric cryptography */
2662/****************************************************************/
2663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2665 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002666{
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 if (input_is_message) {
2668 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2669 return PSA_ERROR_INVALID_ARGUMENT;
2670 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2673 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2674 return PSA_ERROR_INVALID_ARGUMENT;
2675 }
2676 }
2677 } else {
2678 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2679 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002680 }
2681 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002684}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2687 int input_is_message,
2688 psa_algorithm_t alg,
2689 const uint8_t *input,
2690 size_t input_length,
2691 uint8_t *signature,
2692 size_t signature_size,
2693 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002694{
2695 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2696 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2697 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002698
2699 *signature_length = 0;
2700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 status = psa_sign_verify_check_alg(input_is_message, alg);
2702 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002703 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002705
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002706 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002707 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002708 * buffer can in principle be empty since it doesn't actually have
2709 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 if (signature_size == 0) {
2711 return PSA_ERROR_BUFFER_TOO_SMALL;
2712 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002713
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002714 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 key, &slot,
2716 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2717 PSA_KEY_USAGE_SIGN_HASH,
2718 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002721 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002725 status = PSA_ERROR_INVALID_ARGUMENT;
2726 goto exit;
2727 }
2728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002730 status = psa_driver_wrapper_sign_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002731 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002732 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 signature, signature_size, signature_length);
2734 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002735
2736 status = psa_driver_wrapper_sign_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002737 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002738 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002740 }
2741
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002742
2743exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002744 psa_wipe_tag_output_buffer(signature, status, signature_size,
2745 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002746
Ryan Everetta103ec92024-01-31 13:59:57 +00002747 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002750}
2751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2753 int input_is_message,
2754 psa_algorithm_t alg,
2755 const uint8_t *input,
2756 size_t input_length,
2757 const uint8_t *signature,
2758 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002759{
2760 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2761 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2762 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002763
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 status = psa_sign_verify_check_alg(input_is_message, alg);
2765 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002766 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002768
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002769 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 key, &slot,
2771 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2772 PSA_KEY_USAGE_VERIFY_HASH,
2773 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 if (status != PSA_SUCCESS) {
2776 return status;
2777 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002780 status = psa_driver_wrapper_verify_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002781 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002782 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 signature, signature_length);
2784 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002785 status = psa_driver_wrapper_verify_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002786 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002787 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002789 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002790
Ryan Everetta103ec92024-01-31 13:59:57 +00002791 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002794
2795}
2796
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002797psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002798 const psa_key_attributes_t *attributes,
2799 const uint8_t *key_buffer,
2800 size_t key_buffer_size,
2801 psa_algorithm_t alg,
2802 const uint8_t *input,
2803 size_t input_length,
2804 uint8_t *signature,
2805 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002807{
2808 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002811 size_t hash_length;
2812 uint8_t hash[PSA_HASH_MAX_SIZE];
2813
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002814 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 PSA_ALG_SIGN_GET_HASH(alg),
2816 input, input_length,
2817 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002820 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002822
2823 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 attributes, key_buffer, key_buffer_size,
2825 alg, hash, hash_length,
2826 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002827 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002828
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002830}
2831
Gilles Peskine449bd832023-01-11 14:50:10 +01002832psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2833 psa_algorithm_t alg,
2834 const uint8_t *input,
2835 size_t input_length,
2836 uint8_t *signature,
2837 size_t signature_size,
2838 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002839{
2840 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002841 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002843}
2844
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002845psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002846 const psa_key_attributes_t *attributes,
2847 const uint8_t *key_buffer,
2848 size_t key_buffer_size,
2849 psa_algorithm_t alg,
2850 const uint8_t *input,
2851 size_t input_length,
2852 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002854{
2855 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002858 size_t hash_length;
2859 uint8_t hash[PSA_HASH_MAX_SIZE];
2860
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002861 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 PSA_ALG_SIGN_GET_HASH(alg),
2863 input, input_length,
2864 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002867 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002869
2870 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 attributes, key_buffer, key_buffer_size,
2872 alg, hash, hash_length,
2873 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002874 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002875
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002877}
2878
Gilles Peskine449bd832023-01-11 14:50:10 +01002879psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2880 psa_algorithm_t alg,
2881 const uint8_t *input,
2882 size_t input_length,
2883 const uint8_t *signature,
2884 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002885{
2886 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002887 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002889}
2890
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002891psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002892 const psa_key_attributes_t *attributes,
2893 const uint8_t *key_buffer, size_t key_buffer_size,
2894 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002896{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002897 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2899 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002900#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2902 return mbedtls_psa_rsa_sign_hash(
2903 attributes,
2904 key_buffer, key_buffer_size,
2905 alg, hash, hash_length,
2906 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002907#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2908 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 } else {
2910 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002911 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002912 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002914#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2916 return mbedtls_psa_ecdsa_sign_hash(
2917 attributes,
2918 key_buffer, key_buffer_size,
2919 alg, hash, hash_length,
2920 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002921#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2922 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 } else {
2924 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002925 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002926 }
Ronald Cron4501c982021-03-19 20:09:32 +01002927
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 (void) key_buffer;
2929 (void) key_buffer_size;
2930 (void) hash;
2931 (void) hash_length;
2932 (void) signature;
2933 (void) signature_size;
2934 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002935
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002937}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002938
Gilles Peskine449bd832023-01-11 14:50:10 +01002939psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2940 psa_algorithm_t alg,
2941 const uint8_t *hash,
2942 size_t hash_length,
2943 uint8_t *signature,
2944 size_t signature_size,
2945 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002946{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002947 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002948 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002950}
2951
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002952psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002953 const psa_key_attributes_t *attributes,
2954 const uint8_t *key_buffer, size_t key_buffer_size,
2955 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002957{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002958 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2960 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002961#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2963 return mbedtls_psa_rsa_verify_hash(
2964 attributes,
2965 key_buffer, key_buffer_size,
2966 alg, hash, hash_length,
2967 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002968#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2969 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 } else {
2971 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002972 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002973 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002975#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2977 return mbedtls_psa_ecdsa_verify_hash(
2978 attributes,
2979 key_buffer, key_buffer_size,
2980 alg, hash, hash_length,
2981 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002982#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2983 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 } else {
2985 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002986 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002987 }
Ronald Cron4501c982021-03-19 20:09:32 +01002988
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 (void) key_buffer;
2990 (void) key_buffer_size;
2991 (void) hash;
2992 (void) hash_length;
2993 (void) signature;
2994 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002995
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002997}
2998
Gilles Peskine449bd832023-01-11 14:50:10 +01002999psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3000 psa_algorithm_t alg,
3001 const uint8_t *hash,
3002 size_t hash_length,
3003 const uint8_t *signature,
3004 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003005{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003006 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003007 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003009}
3010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3012 psa_algorithm_t alg,
3013 const uint8_t *input,
3014 size_t input_length,
3015 const uint8_t *salt,
3016 size_t salt_length,
3017 uint8_t *output,
3018 size_t output_size,
3019 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003020{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003021 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003022 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003023 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003024
Darryl Green5cc689a2018-07-24 15:34:10 +01003025 (void) input;
3026 (void) input_length;
3027 (void) salt;
3028 (void) output;
3029 (void) output_size;
3030
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003031 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3034 return PSA_ERROR_INVALID_ARGUMENT;
3035 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003036
Valerio Setti5bb454a2024-01-15 10:43:16 +01003037 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3039 if (status != PSA_SUCCESS) {
3040 return status;
3041 }
3042 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3043 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003044 status = PSA_ERROR_INVALID_ARGUMENT;
3045 goto exit;
3046 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003047
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003048 status = psa_driver_wrapper_asymmetric_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003049 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003050 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003052exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003053 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003054
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003056}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3059 psa_algorithm_t alg,
3060 const uint8_t *input,
3061 size_t input_length,
3062 const uint8_t *salt,
3063 size_t salt_length,
3064 uint8_t *output,
3065 size_t output_size,
3066 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003067{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003068 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003069 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003070 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003071
Darryl Green5cc689a2018-07-24 15:34:10 +01003072 (void) input;
3073 (void) input_length;
3074 (void) salt;
3075 (void) output;
3076 (void) output_size;
3077
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003078 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3081 return PSA_ERROR_INVALID_ARGUMENT;
3082 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003083
Valerio Setti5bb454a2024-01-15 10:43:16 +01003084 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3086 if (status != PSA_SUCCESS) {
3087 return status;
3088 }
3089 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003090 status = PSA_ERROR_INVALID_ARGUMENT;
3091 goto exit;
3092 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003094 status = psa_driver_wrapper_asymmetric_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003095 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003096 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003098
3099exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003100 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003101
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003103}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003104
Paul Elliott9fe12f62022-11-30 19:16:02 +00003105/****************************************************************/
3106/* Asymmetric interruptible cryptography */
3107/****************************************************************/
3108
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003109static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3110
Paul Elliott9fe12f62022-11-30 19:16:02 +00003111void psa_interruptible_set_max_ops(uint32_t max_ops)
3112{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003113 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003114}
3115
3116uint32_t psa_interruptible_get_max_ops(void)
3117{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003118 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003119}
3120
Paul Elliott9fe12f62022-11-30 19:16:02 +00003121uint32_t psa_sign_hash_get_num_ops(
3122 const psa_sign_hash_interruptible_operation_t *operation)
3123{
Paul Elliott296ede92022-12-15 17:00:30 +00003124 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003125}
3126
3127uint32_t psa_verify_hash_get_num_ops(
3128 const psa_verify_hash_interruptible_operation_t *operation)
3129{
Paul Elliott296ede92022-12-15 17:00:30 +00003130 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003131}
3132
Paul Elliott59ad9452022-12-18 15:09:02 +00003133static psa_status_t psa_sign_hash_abort_internal(
3134 psa_sign_hash_interruptible_operation_t *operation)
3135{
3136 if (operation->id == 0) {
3137 /* The object has (apparently) been initialized but it is not (yet)
3138 * in use. It's ok to call abort on such an object, and there's
3139 * nothing to do. */
3140 return PSA_SUCCESS;
3141 }
3142
3143 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3144
3145 status = psa_driver_wrapper_sign_hash_abort(operation);
3146
3147 operation->id = 0;
3148
Paul Elliotte6145dc2023-02-07 12:51:21 +00003149 /* Do not clear either the error_occurred or num_ops elements here as they
3150 * only want to be cleared by the application calling abort, not by abort
3151 * being called at completion of an operation. */
3152
Paul Elliott59ad9452022-12-18 15:09:02 +00003153 return status;
3154}
3155
Paul Elliott9fe12f62022-11-30 19:16:02 +00003156psa_status_t psa_sign_hash_start(
3157 psa_sign_hash_interruptible_operation_t *operation,
3158 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3159 const uint8_t *hash, size_t hash_length)
3160{
3161 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3162 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3163 psa_key_slot_t *slot;
3164
Paul Elliottc9774412023-02-06 15:14:07 +00003165 /* Check that start has not been previously called, or operation has not
3166 * previously errored. */
3167 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003168 return PSA_ERROR_BAD_STATE;
3169 }
3170
Paul Elliott9fe12f62022-11-30 19:16:02 +00003171 status = psa_sign_verify_check_alg(0, alg);
3172 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003173 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003174 return status;
3175 }
3176
3177 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3178 PSA_KEY_USAGE_SIGN_HASH,
3179 alg);
3180
3181 if (status != PSA_SUCCESS) {
3182 goto exit;
3183 }
3184
3185 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3186 status = PSA_ERROR_INVALID_ARGUMENT;
3187 goto exit;
3188 }
3189
Paul Elliott296ede92022-12-15 17:00:30 +00003190 /* Ensure ops count gets reset, in case of operation re-use. */
3191 operation->num_ops = 0;
3192
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003193 status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003194 slot->key.data,
3195 slot->key.bytes, alg,
3196 hash, hash_length);
3197exit:
3198
3199 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003200 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003201 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003202 }
3203
Ryan Everettdcc03d52024-02-14 15:44:13 +00003204 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003205
Paul Elliottc9774412023-02-06 15:14:07 +00003206 if (unlock_status != PSA_SUCCESS) {
3207 operation->error_occurred = 1;
3208 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003209
Paul Elliottc9774412023-02-06 15:14:07 +00003210 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003211}
3212
3213
3214psa_status_t psa_sign_hash_complete(
3215 psa_sign_hash_interruptible_operation_t *operation,
3216 uint8_t *signature, size_t signature_size,
3217 size_t *signature_length)
3218{
3219 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3220
3221 *signature_length = 0;
3222
Paul Elliottc9774412023-02-06 15:14:07 +00003223 /* Check that start has been called first, and that operation has not
3224 * previously errored. */
3225 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003226 status = PSA_ERROR_BAD_STATE;
3227 goto exit;
3228 }
3229
Paul Elliott096abc42023-02-03 18:33:23 +00003230 /* Immediately reject a zero-length signature buffer. This guarantees that
3231 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003232 if (signature_size == 0) {
3233 status = PSA_ERROR_BUFFER_TOO_SMALL;
3234 goto exit;
3235 }
3236
3237 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3238 signature_size,
3239 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003240
Paul Elliott296ede92022-12-15 17:00:30 +00003241 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003242 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003243
Paul Elliottebe225c2023-02-10 14:32:53 +00003244exit:
3245
Paul Elliott59200a22023-02-21 15:07:40 +00003246 psa_wipe_tag_output_buffer(signature, status, signature_size,
3247 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003248
Paul Elliott53bb3122023-02-10 14:22:22 +00003249 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003250 if (status != PSA_SUCCESS) {
3251 operation->error_occurred = 1;
3252 }
3253
Paul Elliott59ad9452022-12-18 15:09:02 +00003254 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003255 }
3256
3257 return status;
3258}
3259
3260psa_status_t psa_sign_hash_abort(
3261 psa_sign_hash_interruptible_operation_t *operation)
3262{
Paul Elliott59ad9452022-12-18 15:09:02 +00003263 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3264
3265 status = psa_sign_hash_abort_internal(operation);
3266
3267 /* We clear the number of ops done here, so that it is not cleared when
3268 * the operation fails or succeeds, only on manual abort. */
3269 operation->num_ops = 0;
3270
Paul Elliottc9774412023-02-06 15:14:07 +00003271 /* Likewise, failure state. */
3272 operation->error_occurred = 0;
3273
Paul Elliott59ad9452022-12-18 15:09:02 +00003274 return status;
3275}
3276
3277static psa_status_t psa_verify_hash_abort_internal(
3278 psa_verify_hash_interruptible_operation_t *operation)
3279{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003280 if (operation->id == 0) {
3281 /* The object has (apparently) been initialized but it is not (yet)
3282 * in use. It's ok to call abort on such an object, and there's
3283 * nothing to do. */
3284 return PSA_SUCCESS;
3285 }
3286
Paul Elliott59ad9452022-12-18 15:09:02 +00003287 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3288
3289 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003290
3291 operation->id = 0;
3292
Paul Elliotte6145dc2023-02-07 12:51:21 +00003293 /* Do not clear either the error_occurred or num_ops elements here as they
3294 * only want to be cleared by the application calling abort, not by abort
3295 * being called at completion of an operation. */
3296
Paul Elliott59ad9452022-12-18 15:09:02 +00003297 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003298}
3299
3300psa_status_t psa_verify_hash_start(
3301 psa_verify_hash_interruptible_operation_t *operation,
3302 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3303 const uint8_t *hash, size_t hash_length,
3304 const uint8_t *signature, size_t signature_length)
3305{
3306 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3307 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3308 psa_key_slot_t *slot;
3309
Paul Elliottc9774412023-02-06 15:14:07 +00003310 /* Check that start has not been previously called, or operation has not
3311 * previously errored. */
3312 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003313 return PSA_ERROR_BAD_STATE;
3314 }
3315
3316 status = psa_sign_verify_check_alg(0, alg);
3317 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003318 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003319 return status;
3320 }
3321
3322 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3323 PSA_KEY_USAGE_VERIFY_HASH,
3324 alg);
3325
3326 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003327 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003328 return status;
3329 }
3330
Paul Elliott296ede92022-12-15 17:00:30 +00003331 /* Ensure ops count gets reset, in case of operation re-use. */
3332 operation->num_ops = 0;
3333
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003334 status = psa_driver_wrapper_verify_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003335 slot->key.data,
3336 slot->key.bytes,
3337 alg, hash, hash_length,
3338 signature, signature_length);
3339
3340 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003341 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003342 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003343 }
3344
Ryan Everett291267f2024-02-14 15:59:15 +00003345 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003346
Paul Elliottc9774412023-02-06 15:14:07 +00003347 if (unlock_status != PSA_SUCCESS) {
3348 operation->error_occurred = 1;
3349 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003350
Paul Elliottc9774412023-02-06 15:14:07 +00003351 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003352}
3353
3354psa_status_t psa_verify_hash_complete(
3355 psa_verify_hash_interruptible_operation_t *operation)
3356{
3357 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3358
Paul Elliottc9774412023-02-06 15:14:07 +00003359 /* Check that start has been called first, and that operation has not
3360 * previously errored. */
3361 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003362 status = PSA_ERROR_BAD_STATE;
3363 goto exit;
3364 }
3365
3366 status = psa_driver_wrapper_verify_hash_complete(operation);
3367
Paul Elliott296ede92022-12-15 17:00:30 +00003368 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003369 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003370 operation);
3371
Paul Elliottebe225c2023-02-10 14:32:53 +00003372exit:
3373
Paul Elliott9fe12f62022-11-30 19:16:02 +00003374 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003375 if (status != PSA_SUCCESS) {
3376 operation->error_occurred = 1;
3377 }
3378
Paul Elliott59ad9452022-12-18 15:09:02 +00003379 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003380 }
3381
3382 return status;
3383}
3384
3385psa_status_t psa_verify_hash_abort(
3386 psa_verify_hash_interruptible_operation_t *operation)
3387{
Paul Elliott59ad9452022-12-18 15:09:02 +00003388 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003389
Paul Elliott59ad9452022-12-18 15:09:02 +00003390 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003391
Paul Elliott59ad9452022-12-18 15:09:02 +00003392 /* We clear the number of ops done here, so that it is not cleared when
3393 * the operation fails or succeeds, only on manual abort. */
3394 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003395
Paul Elliottc9774412023-02-06 15:14:07 +00003396 /* Likewise, failure state. */
3397 operation->error_occurred = 0;
3398
Paul Elliott59ad9452022-12-18 15:09:02 +00003399 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003400}
3401
Paul Elliott588f8ed2022-12-02 18:10:26 +00003402/****************************************************************/
3403/* Asymmetric interruptible cryptography internal */
3404/* implementations */
3405/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003406
Paul Elliott588f8ed2022-12-02 18:10:26 +00003407void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3408{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003409
Paul Elliott588f8ed2022-12-02 18:10:26 +00003410#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3411 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3412 defined(MBEDTLS_ECP_RESTARTABLE)
3413
3414 /* Internal implementation uses zero to indicate infinite number max ops,
3415 * therefore avoid this value, and set to minimum possible. */
3416 if (max_ops == 0) {
3417 max_ops = 1;
3418 }
3419
Paul Elliott588f8ed2022-12-02 18:10:26 +00003420 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003421#else
3422 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003423#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3424 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3425 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3426}
3427
Paul Elliott588f8ed2022-12-02 18:10:26 +00003428uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003429 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003430{
3431#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3432 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3433 defined(MBEDTLS_ECP_RESTARTABLE)
3434
Paul Elliott93d9ca82023-02-15 18:14:21 +00003435 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003436#else
3437 (void) operation;
3438 return 0;
3439#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3440 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3441 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3442}
3443
3444uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003445 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003446{
3447 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3448 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3449 defined(MBEDTLS_ECP_RESTARTABLE)
3450
Paul Elliott93d9ca82023-02-15 18:14:21 +00003451 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003452#else
3453 (void) operation;
3454 return 0;
3455#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3456 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3457 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3458}
3459
3460psa_status_t mbedtls_psa_sign_hash_start(
3461 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3462 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3463 size_t key_buffer_size, psa_algorithm_t alg,
3464 const uint8_t *hash, size_t hash_length)
3465{
3466 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003467 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003468
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003469 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003470 return PSA_ERROR_NOT_SUPPORTED;
3471 }
3472
3473 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003474 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003475 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003476
3477#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003478 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3479 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003480
Paul Elliotta1c94092023-02-15 16:38:04 +00003481 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3482
Paul Elliott93d9ca82023-02-15 18:14:21 +00003483 /* Ensure num_ops is zero'ed in case of context re-use. */
3484 operation->num_ops = 0;
3485
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003486 status = mbedtls_psa_ecp_load_representation(attributes->type,
3487 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003488 key_buffer,
3489 key_buffer_size,
3490 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003491
Paul Elliott068fe072023-01-16 13:59:15 +00003492 if (status != PSA_SUCCESS) {
3493 return status;
3494 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003495
Paul Elliott1bc59df2023-02-05 13:41:57 +00003496 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003497 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003498
Paul Elliott068fe072023-01-16 13:59:15 +00003499 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003500 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003501 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003502
Paul Elliott0290a762023-02-09 14:30:24 +00003503 /* We only need to store the same length of hash as the private key size
3504 * here, it would be truncated by the internal implementation anyway. */
3505 required_hash_length = (hash_length < operation->coordinate_bytes ?
3506 hash_length : operation->coordinate_bytes);
3507
Paul Elliottba70ad42023-02-15 18:23:53 +00003508 if (required_hash_length > sizeof(operation->hash)) {
3509 /* Shouldn't happen, but better safe than sorry. */
3510 return PSA_ERROR_CORRUPTION_DETECTED;
3511 }
3512
Paul Elliott0290a762023-02-09 14:30:24 +00003513 memcpy(operation->hash, hash, required_hash_length);
3514 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003515
3516 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003517
3518#else
Paul Elliott068fe072023-01-16 13:59:15 +00003519 (void) operation;
3520 (void) key_buffer;
3521 (void) key_buffer_size;
3522 (void) alg;
3523 (void) hash;
3524 (void) hash_length;
3525 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003526 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003527
Paul Elliott068fe072023-01-16 13:59:15 +00003528 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003529#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3530 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3531 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003532}
3533
3534psa_status_t mbedtls_psa_sign_hash_complete(
3535 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3536 uint8_t *signature, size_t signature_size,
3537 size_t *signature_length)
3538{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003539#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3540 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3541 defined(MBEDTLS_ECP_RESTARTABLE)
3542
Paul Elliotta1c94092023-02-15 16:38:04 +00003543 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003544 mbedtls_mpi r;
3545 mbedtls_mpi s;
3546
Paul Elliott46845252023-02-03 14:59:11 +00003547 mbedtls_mpi_init(&r);
3548 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003549
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003550 /* Ensure max_ops is set to the current value (or default). */
3551 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3552
Paul Elliotta1c94092023-02-15 16:38:04 +00003553 if (signature_size < 2 * operation->coordinate_bytes) {
3554 status = PSA_ERROR_BUFFER_TOO_SMALL;
3555 goto exit;
3556 }
3557
Paul Elliott588f8ed2022-12-02 18:10:26 +00003558 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003559
Paul Elliott588f8ed2022-12-02 18:10:26 +00003560#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3561 status = mbedtls_to_psa_error(
3562 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003563 &r,
3564 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003565 &operation->ctx->d,
3566 operation->hash,
3567 operation->hash_length,
3568 operation->md_alg,
3569 mbedtls_psa_get_random,
3570 MBEDTLS_PSA_RANDOM_STATE,
3571 &operation->restart_ctx));
3572#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003573 status = PSA_ERROR_NOT_SUPPORTED;
3574 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003575#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3576 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003577 status = mbedtls_to_psa_error(
3578 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003579 &r,
3580 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003581 &operation->ctx->d,
3582 operation->hash,
3583 operation->hash_length,
3584 mbedtls_psa_get_random,
3585 MBEDTLS_PSA_RANDOM_STATE,
3586 mbedtls_psa_get_random,
3587 MBEDTLS_PSA_RANDOM_STATE,
3588 &operation->restart_ctx));
3589 }
3590
Paul Elliottf8e5b562023-02-19 18:43:45 +00003591 /* Hide the fact that the restart context only holds a delta of number of
3592 * ops done during the last operation, not an absolute value. */
3593 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3594
Paul Elliott724bd252023-02-08 12:35:08 +00003595 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003596 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003597 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003598 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003599 operation->coordinate_bytes)
3600 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003601
3602 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003603 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003604 }
3605
3606 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003607 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003608 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003609 operation->coordinate_bytes,
3610 operation->coordinate_bytes)
3611 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003612
3613 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003614 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003615 }
3616
Paul Elliott1bc59df2023-02-05 13:41:57 +00003617 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003618
Paul Elliott724bd252023-02-08 12:35:08 +00003619 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003620 }
Paul Elliott724bd252023-02-08 12:35:08 +00003621
3622exit:
3623
3624 mbedtls_mpi_free(&r);
3625 mbedtls_mpi_free(&s);
3626 return status;
3627
Paul Elliott588f8ed2022-12-02 18:10:26 +00003628 #else
3629
3630 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003631 (void) signature;
3632 (void) signature_size;
3633 (void) signature_length;
3634
3635 return PSA_ERROR_NOT_SUPPORTED;
3636
3637#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3638 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3639 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3640}
3641
3642psa_status_t mbedtls_psa_sign_hash_abort(
3643 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3644{
3645
3646#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3647 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3648 defined(MBEDTLS_ECP_RESTARTABLE)
3649
3650 if (operation->ctx) {
3651 mbedtls_ecdsa_free(operation->ctx);
3652 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003653 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003654 }
3655
3656 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3657
Paul Elliott93d9ca82023-02-15 18:14:21 +00003658 operation->num_ops = 0;
3659
Paul Elliott588f8ed2022-12-02 18:10:26 +00003660 return PSA_SUCCESS;
3661
3662#else
3663
3664 (void) operation;
3665
3666 return PSA_ERROR_NOT_SUPPORTED;
3667
3668#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3669 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3670 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3671}
3672
3673psa_status_t mbedtls_psa_verify_hash_start(
3674 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3675 const psa_key_attributes_t *attributes,
3676 const uint8_t *key_buffer, size_t key_buffer_size,
3677 psa_algorithm_t alg,
3678 const uint8_t *hash, size_t hash_length,
3679 const uint8_t *signature, size_t signature_length)
3680{
3681 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003682 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003683 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003684
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003685 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003686 return PSA_ERROR_NOT_SUPPORTED;
3687 }
3688
3689 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003690 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003691 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003692
3693#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003694 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3695 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003696
Paul Elliotta1c94092023-02-15 16:38:04 +00003697 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3698 mbedtls_mpi_init(&operation->r);
3699 mbedtls_mpi_init(&operation->s);
3700
Paul Elliott93d9ca82023-02-15 18:14:21 +00003701 /* Ensure num_ops is zero'ed in case of context re-use. */
3702 operation->num_ops = 0;
3703
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003704 status = mbedtls_psa_ecp_load_representation(attributes->type,
3705 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003706 key_buffer,
3707 key_buffer_size,
3708 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003709
Paul Elliott068fe072023-01-16 13:59:15 +00003710 if (status != PSA_SUCCESS) {
3711 return status;
3712 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003713
Paul Elliottc569fc22023-02-10 13:02:54 +00003714 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715
Paul Elliott1bc59df2023-02-05 13:41:57 +00003716 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003717 return PSA_ERROR_INVALID_SIGNATURE;
3718 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003719
Paul Elliott068fe072023-01-16 13:59:15 +00003720 status = mbedtls_to_psa_error(
3721 mbedtls_mpi_read_binary(&operation->r,
3722 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003723 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003724
Paul Elliott068fe072023-01-16 13:59:15 +00003725 if (status != PSA_SUCCESS) {
3726 return status;
3727 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003728
Paul Elliott068fe072023-01-16 13:59:15 +00003729 status = mbedtls_to_psa_error(
3730 mbedtls_mpi_read_binary(&operation->s,
3731 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003732 coordinate_bytes,
3733 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003734
Paul Elliott068fe072023-01-16 13:59:15 +00003735 if (status != PSA_SUCCESS) {
3736 return status;
3737 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003738
Paul Elliott2c9843f2023-02-15 17:32:42 +00003739 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003740
Paul Elliott2c9843f2023-02-15 17:32:42 +00003741 if (status != PSA_SUCCESS) {
3742 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003743 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003744
Paul Elliott0290a762023-02-09 14:30:24 +00003745 /* We only need to store the same length of hash as the private key size
3746 * here, it would be truncated by the internal implementation anyway. */
3747 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3748 coordinate_bytes);
3749
Paul Elliottba70ad42023-02-15 18:23:53 +00003750 if (required_hash_length > sizeof(operation->hash)) {
3751 /* Shouldn't happen, but better safe than sorry. */
3752 return PSA_ERROR_CORRUPTION_DETECTED;
3753 }
3754
Paul Elliott0290a762023-02-09 14:30:24 +00003755 memcpy(operation->hash, hash, required_hash_length);
3756 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003757
3758 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003759#else
Paul Elliott068fe072023-01-16 13:59:15 +00003760 (void) operation;
3761 (void) key_buffer;
3762 (void) key_buffer_size;
3763 (void) alg;
3764 (void) hash;
3765 (void) hash_length;
3766 (void) signature;
3767 (void) signature_length;
3768 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003769 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003770 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003771
Paul Elliott068fe072023-01-16 13:59:15 +00003772 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3774 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3775 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003776}
3777
3778psa_status_t mbedtls_psa_verify_hash_complete(
3779 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3780{
3781
3782#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3783 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3784 defined(MBEDTLS_ECP_RESTARTABLE)
3785
Paul Elliottf8e5b562023-02-19 18:43:45 +00003786 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3787
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003788 /* Ensure max_ops is set to the current value (or default). */
3789 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3790
Paul Elliottf8e5b562023-02-19 18:43:45 +00003791 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003792 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3793 operation->hash,
3794 operation->hash_length,
3795 &operation->ctx->Q,
3796 &operation->r,
3797 &operation->s,
3798 &operation->restart_ctx));
3799
Paul Elliottf8e5b562023-02-19 18:43:45 +00003800 /* Hide the fact that the restart context only holds a delta of number of
3801 * ops done during the last operation, not an absolute value. */
3802 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3803
3804 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805#else
3806 (void) operation;
3807
3808 return PSA_ERROR_NOT_SUPPORTED;
3809
3810#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3811 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3812 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3813}
3814
3815psa_status_t mbedtls_psa_verify_hash_abort(
3816 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3817{
3818
3819#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3820 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3821 defined(MBEDTLS_ECP_RESTARTABLE)
3822
3823 if (operation->ctx) {
3824 mbedtls_ecdsa_free(operation->ctx);
3825 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003826 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003827 }
3828
3829 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3830
Paul Elliott93d9ca82023-02-15 18:14:21 +00003831 operation->num_ops = 0;
3832
Paul Elliott588f8ed2022-12-02 18:10:26 +00003833 mbedtls_mpi_free(&operation->r);
3834 mbedtls_mpi_free(&operation->s);
3835
3836 return PSA_SUCCESS;
3837
3838#else
3839 (void) operation;
3840
3841 return PSA_ERROR_NOT_SUPPORTED;
3842
3843#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3844 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3845 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3846}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003847
mohammad1603503973b2018-03-12 15:59:30 +02003848/****************************************************************/
3849/* Symmetric cryptography */
3850/****************************************************************/
3851
Gilles Peskine449bd832023-01-11 14:50:10 +01003852static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3853 mbedtls_svc_key_id_t key,
3854 psa_algorithm_t alg,
3855 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003856{
3857 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3858 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003859 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003860 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3861 PSA_KEY_USAGE_ENCRYPT :
3862 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003863
3864 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003865 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003866 status = PSA_ERROR_BAD_STATE;
3867 goto exit;
3868 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003869
Gilles Peskine449bd832023-01-11 14:50:10 +01003870 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003871 status = PSA_ERROR_INVALID_ARGUMENT;
3872 goto exit;
3873 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003874
Gilles Peskine449bd832023-01-11 14:50:10 +01003875 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3876 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003877 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003878 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003879
Ronald Cron49fafa92021-03-10 08:34:23 +01003880 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003881 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003882 * so we only set it (in the driver wrapper) after resources have been
3883 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003884 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003885 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003886 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003887 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003888 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003889 }
3890 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003891
3892 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003893 if (cipher_operation == MBEDTLS_ENCRYPT) {
3894 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003895 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003896 slot->key.data,
3897 slot->key.bytes,
3898 alg);
3899 } else {
3900 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003901 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003902 slot->key.data,
3903 slot->key.bytes,
3904 alg);
3905 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003906
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003907exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003908 if (status != PSA_SUCCESS) {
3909 psa_cipher_abort(operation);
3910 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003911
Ryan Everettc0053cc2024-02-14 16:27:13 +00003912 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003913
Gilles Peskine449bd832023-01-11 14:50:10 +01003914 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003915}
3916
Gilles Peskine449bd832023-01-11 14:50:10 +01003917psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3918 mbedtls_svc_key_id_t key,
3919 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003920{
Gilles Peskine449bd832023-01-11 14:50:10 +01003921 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003922}
3923
Gilles Peskine449bd832023-01-11 14:50:10 +01003924psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3925 mbedtls_svc_key_id_t key,
3926 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003927{
Gilles Peskine449bd832023-01-11 14:50:10 +01003928 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003929}
3930
Gilles Peskine449bd832023-01-11 14:50:10 +01003931psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3932 uint8_t *iv,
3933 size_t iv_size,
3934 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003935{
Ronald Cron6d051732020-10-01 14:10:20 +02003936 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003937 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07003938 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003939
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003941 status = PSA_ERROR_BAD_STATE;
3942 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003943 }
3944
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003946 status = PSA_ERROR_BAD_STATE;
3947 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003948 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003949
Ronald Cron2fb90522021-07-05 12:31:44 +02003950 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003951 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003952 status = PSA_ERROR_BUFFER_TOO_SMALL;
3953 goto exit;
3954 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003955
Gilles Peskine449bd832023-01-11 14:50:10 +01003956 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02003957 status = PSA_ERROR_GENERIC_ERROR;
3958 goto exit;
3959 }
3960
Gilles Peskine449bd832023-01-11 14:50:10 +01003961 status = psa_generate_random(local_iv, default_iv_length);
3962 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003963 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003964 }
Ronald Cron5618a392021-03-26 09:52:26 +01003965
Gilles Peskine449bd832023-01-11 14:50:10 +01003966 status = psa_driver_wrapper_cipher_set_iv(operation,
3967 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003968
3969exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003970 if (status == PSA_SUCCESS) {
3971 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02003972 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003973 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02003975 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02003977 }
Ronald Cron6d051732020-10-01 14:10:20 +02003978
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003980}
3981
Gilles Peskine449bd832023-01-11 14:50:10 +01003982psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3983 const uint8_t *iv,
3984 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003985{
Ronald Cron6d051732020-10-01 14:10:20 +02003986 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003987
Gilles Peskine449bd832023-01-11 14:50:10 +01003988 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003989 status = PSA_ERROR_BAD_STATE;
3990 goto exit;
3991 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003992
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003994 status = PSA_ERROR_BAD_STATE;
3995 goto exit;
3996 }
Ronald Crona0d68172021-03-26 10:15:08 +01003997
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003999 status = PSA_ERROR_INVALID_ARGUMENT;
4000 goto exit;
4001 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004002
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 status = psa_driver_wrapper_cipher_set_iv(operation,
4004 iv,
4005 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004006
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004007exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004009 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 } else {
4011 psa_cipher_abort(operation);
4012 }
4013 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004014}
4015
Gilles Peskine449bd832023-01-11 14:50:10 +01004016psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4017 const uint8_t *input,
4018 size_t input_length,
4019 uint8_t *output,
4020 size_t output_size,
4021 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004022{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004023 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004024
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004026 status = PSA_ERROR_BAD_STATE;
4027 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004028 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004029
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004031 status = PSA_ERROR_BAD_STATE;
4032 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004033 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 status = psa_driver_wrapper_cipher_update(operation,
4036 input,
4037 input_length,
4038 output,
4039 output_size,
4040 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004041
4042exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 if (status != PSA_SUCCESS) {
4044 psa_cipher_abort(operation);
4045 }
Ronald Cron6d051732020-10-01 14:10:20 +02004046
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004048}
4049
Gilles Peskine449bd832023-01-11 14:50:10 +01004050psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4051 uint8_t *output,
4052 size_t output_size,
4053 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004054{
David Saadab4ecc272019-02-14 13:48:10 +02004055 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004058 status = PSA_ERROR_BAD_STATE;
4059 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004060 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004061
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004063 status = PSA_ERROR_BAD_STATE;
4064 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004065 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004066
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 status = psa_driver_wrapper_cipher_finish(operation,
4068 output,
4069 output_size,
4070 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004071
4072exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 if (status == PSA_SUCCESS) {
4074 return psa_cipher_abort(operation);
4075 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004076 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004078
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004080 }
mohammad1603503973b2018-03-12 15:59:30 +02004081}
4082
Gilles Peskine449bd832023-01-11 14:50:10 +01004083psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004084{
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004086 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004087 * in use. It's ok to call abort on such an object, and there's
4088 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004090 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004091
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004093
Ronald Cron49fafa92021-03-10 08:34:23 +01004094 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004095 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004096 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004097
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004099}
4100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4102 psa_algorithm_t alg,
4103 const uint8_t *input,
4104 size_t input_length,
4105 uint8_t *output,
4106 size_t output_size,
4107 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004108{
4109 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004110 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004111 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004112 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4113 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004114
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004116 status = PSA_ERROR_INVALID_ARGUMENT;
4117 goto exit;
4118 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004119
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4121 PSA_KEY_USAGE_ENCRYPT,
4122 alg);
4123 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004126
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4128 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004129 status = PSA_ERROR_GENERIC_ERROR;
4130 goto exit;
4131 }
4132
Gilles Peskine449bd832023-01-11 14:50:10 +01004133 if (default_iv_length > 0) {
4134 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004135 status = PSA_ERROR_BUFFER_TOO_SMALL;
4136 goto exit;
4137 }
4138
Gilles Peskine449bd832023-01-11 14:50:10 +01004139 status = psa_generate_random(local_iv, default_iv_length);
4140 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004143 }
4144
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004145 status = psa_driver_wrapper_cipher_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004146 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004147 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004148 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004150
4151exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004152 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004154 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004155 }
Ronald Cron23919522021-07-08 19:00:07 +02004156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 if (status == PSA_SUCCESS) {
4158 if (default_iv_length > 0) {
4159 memcpy(output, local_iv, default_iv_length);
4160 }
4161 *output_length += default_iv_length;
4162 } else {
4163 *output_length = 0;
4164 }
4165
4166 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004167}
4168
Gilles Peskine449bd832023-01-11 14:50:10 +01004169psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4170 psa_algorithm_t alg,
4171 const uint8_t *input,
4172 size_t input_length,
4173 uint8_t *output,
4174 size_t output_size,
4175 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004176{
4177 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004178 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004179 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004180
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004182 status = PSA_ERROR_INVALID_ARGUMENT;
4183 goto exit;
4184 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004185
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4187 PSA_KEY_USAGE_DECRYPT,
4188 alg);
4189 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004190 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004192
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4194 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004195 status = PSA_ERROR_INVALID_ARGUMENT;
4196 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004198 status = PSA_ERROR_INVALID_ARGUMENT;
4199 goto exit;
4200 }
4201
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004202 status = psa_driver_wrapper_cipher_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004203 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004204 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004206
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004207exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004208 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004210 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004212
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004214 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 }
Ronald Cron23919522021-07-08 19:00:07 +02004216
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004218}
4219
4220
mohammad16035955c982018-04-26 00:53:03 +03004221/****************************************************************/
4222/* AEAD */
4223/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004224
Paul Elliottbaff51c2021-09-28 17:44:45 +01004225/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004226static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004227{
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004229}
4230
4231/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004232static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4233 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004234{
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004236
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004238#if defined(PSA_WANT_ALG_GCM)
4239 case PSA_ALG_GCM:
4240 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 * arbitrarily large nonces. Please note that we do not generally
4242 * recommend the usage of nonces of greater length than
4243 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4244 * size, which can then lead to collisions if you encrypt a very
4245 * large number of messages.*/
4246 if (nonce_length != 0) {
4247 return PSA_SUCCESS;
4248 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004249 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004250#endif /* PSA_WANT_ALG_GCM */
4251#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004252 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 if (nonce_length >= 7 && nonce_length <= 13) {
4254 return PSA_SUCCESS;
4255 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004256 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004257#endif /* PSA_WANT_ALG_CCM */
4258#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004259 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 if (nonce_length == 12) {
4261 return PSA_SUCCESS;
4262 } else if (nonce_length == 8) {
4263 return PSA_ERROR_NOT_SUPPORTED;
4264 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004265 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004266#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004267 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004268 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004270 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004271
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004273}
4274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004276{
Gilles Peskine449bd832023-01-11 14:50:10 +01004277 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4278 return PSA_ERROR_INVALID_ARGUMENT;
4279 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004280
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004282}
4283
Gilles Peskine449bd832023-01-11 14:50:10 +01004284psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4285 psa_algorithm_t alg,
4286 const uint8_t *nonce,
4287 size_t nonce_length,
4288 const uint8_t *additional_data,
4289 size_t additional_data_length,
4290 const uint8_t *plaintext,
4291 size_t plaintext_length,
4292 uint8_t *ciphertext,
4293 size_t ciphertext_size,
4294 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004295{
Ronald Cron215633c2021-03-16 17:15:37 +01004296 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4297 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004298
mohammad1603f08a5502018-06-03 15:05:47 +03004299 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004300
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 status = psa_aead_check_algorithm(alg);
4302 if (status != PSA_SUCCESS) {
4303 return status;
4304 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004305
Ronald Cron9a986162021-03-26 12:40:07 +01004306 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4308 if (status != PSA_SUCCESS) {
4309 return status;
4310 }
mohammad16035955c982018-04-26 00:53:03 +03004311
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 status = psa_aead_check_nonce_length(alg, nonce_length);
4313 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004314 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004316
Ronald Cronde822812021-03-17 16:08:20 +01004317 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004318 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004319 alg,
4320 nonce, nonce_length,
4321 additional_data, additional_data_length,
4322 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004324
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4326 memset(ciphertext, 0, ciphertext_size);
4327 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004328
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004329exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004330 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004331
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004333}
4334
Gilles Peskine449bd832023-01-11 14:50:10 +01004335psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4336 psa_algorithm_t alg,
4337 const uint8_t *nonce,
4338 size_t nonce_length,
4339 const uint8_t *additional_data,
4340 size_t additional_data_length,
4341 const uint8_t *ciphertext,
4342 size_t ciphertext_length,
4343 uint8_t *plaintext,
4344 size_t plaintext_size,
4345 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004346{
Ronald Cron215633c2021-03-16 17:15:37 +01004347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4348 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004349
Gilles Peskineee652a32018-06-01 19:23:52 +02004350 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004351
Gilles Peskine449bd832023-01-11 14:50:10 +01004352 status = psa_aead_check_algorithm(alg);
4353 if (status != PSA_SUCCESS) {
4354 return status;
4355 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004356
Ronald Cron9a986162021-03-26 12:40:07 +01004357 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4359 if (status != PSA_SUCCESS) {
4360 return status;
4361 }
mohammad16035955c982018-04-26 00:53:03 +03004362
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 status = psa_aead_check_nonce_length(alg, nonce_length);
4364 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004365 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004367
Ronald Cronde822812021-03-17 16:08:20 +01004368 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004369 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004370 alg,
4371 nonce, nonce_length,
4372 additional_data, additional_data_length,
4373 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004375
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 if (status != PSA_SUCCESS && plaintext_size != 0) {
4377 memset(plaintext, 0, plaintext_size);
4378 }
mohammad160360a64d02018-06-03 17:20:42 +03004379
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004380exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004381 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004382
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 return status;
mohammad16035955c982018-04-26 00:53:03 +03004384}
4385
Gilles Peskine449bd832023-01-11 14:50:10 +01004386static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4387{
4388 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004389
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004391#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004393 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4395 return PSA_ERROR_INVALID_ARGUMENT;
4396 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004397 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004398#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004399
Przemek Stekiel86679c72022-10-06 17:06:56 +02004400#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004402 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4404 return PSA_ERROR_INVALID_ARGUMENT;
4405 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004406 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004407#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004408
Przemek Stekiel86679c72022-10-06 17:06:56 +02004409#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004411 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 if (tag_len != 16) {
4413 return PSA_ERROR_INVALID_ARGUMENT;
4414 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004415 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004416#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004417
4418 default:
4419 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004421 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004423}
4424
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004425/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004426static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4427 int is_encrypt,
4428 mbedtls_svc_key_id_t key,
4429 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004430{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004431 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4432 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4433 psa_key_slot_t *slot = NULL;
4434 psa_key_usage_t key_usage = 0;
4435
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 status = psa_aead_check_algorithm(alg);
4437 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004438 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004442 status = PSA_ERROR_BAD_STATE;
4443 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004444 }
4445
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 if (operation->nonce_set || operation->lengths_set ||
4447 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004448 status = PSA_ERROR_BAD_STATE;
4449 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004450 }
4451
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004453 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004455 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004457
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4459 alg);
4460 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004461 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004465 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 if (is_encrypt) {
4469 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004470 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 slot->key.data,
4472 slot->key.bytes,
4473 alg);
4474 } else {
4475 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004476 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 slot->key.data,
4478 slot->key.bytes,
4479 alg);
4480 }
4481 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004482 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004484
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004485 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004486
4487exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00004488 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004489
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004491 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004493 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 } else {
4495 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004496 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004497
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004499}
4500
Paul Elliott302ff6b2021-04-20 18:10:30 +01004501/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004502psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4503 mbedtls_svc_key_id_t key,
4504 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004505{
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004507}
4508
4509/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004510psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4511 mbedtls_svc_key_id_t key,
4512 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004513{
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004515}
4516
4517/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004518psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4519 uint8_t *nonce,
4520 size_t nonce_size,
4521 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004522{
4523 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004524 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004525 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004526
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004527 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004530 status = PSA_ERROR_BAD_STATE;
4531 goto exit;
4532 }
4533
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004535 status = PSA_ERROR_BAD_STATE;
4536 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004537 }
4538
Paul Elliotte193ea82021-10-01 13:00:16 +01004539 /* For CCM, this size may not be correct according to the PSA
4540 * specification. The PSA Crypto 1.0.1 specification states:
4541 *
4542 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4543 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4544 *
4545 * However this restriction that L has to be the smallest integer is not
4546 * applied in practice, and it is not implementable here since the
4547 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4549 operation->alg);
4550 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004551 status = PSA_ERROR_BUFFER_TOO_SMALL;
4552 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004553 }
4554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 status = psa_generate_random(local_nonce, required_nonce_size);
4556 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004557 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004559
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004561
Paul Elliott39dc6b82021-05-11 19:16:09 +01004562exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 if (status == PSA_SUCCESS) {
4564 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004565 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 } else {
4567 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004568 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004571}
4572
4573/* Set the nonce for a multipart authenticated encryption or decryption
4574 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004575psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4576 const uint8_t *nonce,
4577 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004578{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004579 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4580
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004582 status = PSA_ERROR_BAD_STATE;
4583 goto exit;
4584 }
4585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004587 status = PSA_ERROR_BAD_STATE;
4588 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004589 }
4590
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4592 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004593 status = PSA_ERROR_INVALID_ARGUMENT;
4594 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004595 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4598 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004599
Paul Elliott39dc6b82021-05-11 19:16:09 +01004600exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004602 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 } else {
4604 psa_aead_abort(operation);
4605 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004608}
4609
4610/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004611psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4612 size_t ad_length,
4613 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004614{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004615 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4616
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004618 status = PSA_ERROR_BAD_STATE;
4619 goto exit;
4620 }
4621
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 if (operation->lengths_set || operation->ad_started ||
4623 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004624 status = PSA_ERROR_BAD_STATE;
4625 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004626 }
4627
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004629#if defined(PSA_WANT_ALG_GCM)
4630 case PSA_ALG_GCM:
4631 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 * bits. Without the guard this code will generate warnings on 32bit
4633 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004634#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 if (((uint64_t) ad_length) >> 61 != 0 ||
4636 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004637 status = PSA_ERROR_INVALID_ARGUMENT;
4638 goto exit;
4639 }
Paul Elliott325d3742021-09-27 17:56:28 +01004640#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004641 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004642#endif /* PSA_WANT_ALG_GCM */
4643#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004644 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004646 status = PSA_ERROR_INVALID_ARGUMENT;
4647 goto exit;
4648 }
4649 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004650#endif /* PSA_WANT_ALG_CCM */
4651#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004652 case PSA_ALG_CHACHA20_POLY1305:
4653 /* No length restrictions for ChaChaPoly. */
4654 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004655#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004656 default:
4657 break;
4658 }
Paul Elliott325d3742021-09-27 17:56:28 +01004659
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4661 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004662
Paul Elliott39dc6b82021-05-11 19:16:09 +01004663exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004665 operation->ad_remaining = ad_length;
4666 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004667 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 } else {
4669 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004670 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004671
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004673}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004674
4675/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004676psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4677 const uint8_t *input,
4678 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004679{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004680 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004683 status = PSA_ERROR_BAD_STATE;
4684 goto exit;
4685 }
4686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004688 status = PSA_ERROR_BAD_STATE;
4689 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004690 }
4691
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 if (operation->lengths_set) {
4693 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004694 status = PSA_ERROR_INVALID_ARGUMENT;
4695 goto exit;
4696 }
4697
4698 operation->ad_remaining -= input_length;
4699 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004700#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004702 status = PSA_ERROR_BAD_STATE;
4703 goto exit;
4704 }
4705#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004706
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 status = psa_driver_wrapper_aead_update_ad(operation, input,
4708 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004709
Paul Elliott39dc6b82021-05-11 19:16:09 +01004710exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004712 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 } else {
4714 psa_aead_abort(operation);
4715 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004718}
4719
4720/* Encrypt or decrypt a message fragment in an active multipart AEAD
4721 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004722psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4723 const uint8_t *input,
4724 size_t input_length,
4725 uint8_t *output,
4726 size_t output_size,
4727 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004728{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004729 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004730
4731 *output_length = 0;
4732
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004734 status = PSA_ERROR_BAD_STATE;
4735 goto exit;
4736 }
4737
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004739 status = PSA_ERROR_BAD_STATE;
4740 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004741 }
4742
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004744 /* Additional data length was supplied, but not all the additional
4745 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004747 status = PSA_ERROR_INVALID_ARGUMENT;
4748 goto exit;
4749 }
4750
4751 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004753 status = PSA_ERROR_INVALID_ARGUMENT;
4754 goto exit;
4755 }
4756
4757 operation->body_remaining -= input_length;
4758 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004759#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004761 status = PSA_ERROR_BAD_STATE;
4762 goto exit;
4763 }
4764#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004765
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4767 output, output_size,
4768 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004769
Paul Elliott39dc6b82021-05-11 19:16:09 +01004770exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004772 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 } else {
4774 psa_aead_abort(operation);
4775 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004776
Gilles Peskine449bd832023-01-11 14:50:10 +01004777 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004778}
4779
Gilles Peskine449bd832023-01-11 14:50:10 +01004780static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004781{
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 if (operation->id == 0 || !operation->nonce_set) {
4783 return PSA_ERROR_BAD_STATE;
4784 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004785
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4787 operation->body_remaining != 0)) {
4788 return PSA_ERROR_INVALID_ARGUMENT;
4789 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004790
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004792}
4793
Paul Elliott302ff6b2021-04-20 18:10:30 +01004794/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004795psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4796 uint8_t *ciphertext,
4797 size_t ciphertext_size,
4798 size_t *ciphertext_length,
4799 uint8_t *tag,
4800 size_t tag_size,
4801 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004802{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004803 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4804
Paul Elliott302ff6b2021-04-20 18:10:30 +01004805 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004806 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004807
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 status = psa_aead_final_checks(operation);
4809 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004810 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004812
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004814 status = PSA_ERROR_BAD_STATE;
4815 goto exit;
4816 }
4817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4819 ciphertext_size,
4820 ciphertext_length,
4821 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004822
Paul Elliott39dc6b82021-05-11 19:16:09 +01004823exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004824
4825
Paul Elliottf47b0952021-05-21 18:02:33 +01004826 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004827 * the zero tag size, make sure the tag is set to something implausible.
4828 * Even if the operation succeeds, make sure we clear the rest of the
4829 * buffer to prevent potential leakage of anything previously placed in
4830 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004831 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004832
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004834
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004836}
4837
4838/* Finish authenticating and decrypting a message in a multipart AEAD
4839 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004840psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4841 uint8_t *plaintext,
4842 size_t plaintext_size,
4843 size_t *plaintext_length,
4844 const uint8_t *tag,
4845 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004846{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004847 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4848
Paul Elliott302ff6b2021-04-20 18:10:30 +01004849 *plaintext_length = 0;
4850
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 status = psa_aead_final_checks(operation);
4852 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004853 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004855
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004857 status = PSA_ERROR_BAD_STATE;
4858 goto exit;
4859 }
4860
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4862 plaintext_size,
4863 plaintext_length,
4864 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004865
4866exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004870}
4871
4872/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004873psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004874{
4875 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4876
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004878 /* The object has (apparently) been initialized but it is not (yet)
4879 * in use. It's ok to call abort on such an object, and there's
4880 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004882 }
4883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004885
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004887
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004889}
4890
Gilles Peskinee59236f2018-01-27 23:32:46 +01004891/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004892/* Generators */
4893/****************************************************************/
4894
Przemek Stekiel69c46792022-06-10 12:59:51 +02004895#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004896 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004897 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05304898 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304899 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08004900#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004901#endif /* At least one builtin KDF */
4902
Przemek Stekiel69c46792022-06-10 12:59:51 +02004903#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004904 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4905 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4906static psa_status_t psa_key_derivation_start_hmac(
4907 psa_mac_operation_t *operation,
4908 psa_algorithm_t hash_alg,
4909 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02004911{
4912 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4913 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4915 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4916 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02004917
Steven Cooreman72f736a2021-05-07 14:14:37 +02004918 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02004920
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 status = psa_driver_wrapper_mac_sign_setup(operation,
4922 &attributes,
4923 hmac_key, hmac_key_length,
4924 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 psa_reset_key_attributes(&attributes);
4927 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02004928}
4929#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004930
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004931#define HKDF_STATE_INIT 0 /* no input yet */
4932#define HKDF_STATE_STARTED 1 /* got salt */
4933#define HKDF_STATE_KEYED 2 /* got key */
4934#define HKDF_STATE_OUTPUT 3 /* output started */
4935
Gilles Peskinecbe66502019-05-16 16:59:18 +02004936static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004938{
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4940 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4941 } else {
4942 return operation->alg;
4943 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004944}
4945
Gilles Peskine449bd832023-01-11 14:50:10 +01004946psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004947{
4948 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4950 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004951 /* The object has (apparently) been initialized but it is not
4952 * in use. It's ok to call abort on such an object, and there's
4953 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004955#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
4957 mbedtls_free(operation->ctx.hkdf.info);
4958 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4959 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004960#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004961#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4962 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4964 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4965 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4966 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004967 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02004969 }
4970
Gilles Peskine449bd832023-01-11 14:50:10 +01004971 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004972 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01004974 }
4975
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004977 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01004979 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01004980#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004981 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004982 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02004984 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01004985#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02004986 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004987
4988 /* We leave the fields Ai and output_block to be erased safely by the
4989 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004991#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4992 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004993#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
4995 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
4996 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
4997 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004998#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304999#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305000 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305001 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005002 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305003 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305004 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305005
5006 status = PSA_SUCCESS;
5007 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305008#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005009 {
5010 status = PSA_ERROR_BAD_STATE;
5011 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 mbedtls_platform_zeroize(operation, sizeof(*operation));
5013 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005014}
5015
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005016psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005018{
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005020 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005022 }
5023
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005024 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005026}
5027
Gilles Peskine449bd832023-01-11 14:50:10 +01005028psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5029 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005030{
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 if (operation->alg == 0) {
5032 return PSA_ERROR_BAD_STATE;
5033 }
5034 if (capacity > operation->capacity) {
5035 return PSA_ERROR_INVALID_ARGUMENT;
5036 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005037 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005039}
5040
Przemek Stekiel69c46792022-06-10 12:59:51 +02005041#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005042/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005043static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5044 psa_algorithm_t kdf_alg,
5045 uint8_t *output,
5046 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005047{
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5049 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005050 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005051 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005052#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005054#else
5055 const uint8_t last_block = 0xff;
5056#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005057
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 if (hkdf->state < HKDF_STATE_KEYED ||
5059 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005060#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005062#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 )) {
5064 return PSA_ERROR_BAD_STATE;
5065 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005066 hkdf->state = HKDF_STATE_OUTPUT;
5067
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005069 /* Copy what remains of the current block */
5070 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005072 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 }
5074 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005075 output += n;
5076 output_length -= n;
5077 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005079 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005081 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005082 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005083 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005084 * object was corrupted or if this function is called directly
5085 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 if (hkdf->block_number == last_block) {
5087 return PSA_ERROR_BAD_STATE;
5088 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005089
5090 /* We need a new block */
5091 ++hkdf->block_number;
5092 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005093
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5095 hash_alg,
5096 hkdf->prk,
5097 hash_length);
5098 if (status != PSA_SUCCESS) {
5099 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005100 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005101
5102 if (hkdf->block_number != 1) {
5103 status = psa_mac_update(&hkdf->hmac,
5104 hkdf->output_block,
5105 hash_length);
5106 if (status != PSA_SUCCESS) {
5107 return status;
5108 }
5109 }
5110 status = psa_mac_update(&hkdf->hmac,
5111 hkdf->info,
5112 hkdf->info_length);
5113 if (status != PSA_SUCCESS) {
5114 return status;
5115 }
5116 status = psa_mac_update(&hkdf->hmac,
5117 &hkdf->block_number, 1);
5118 if (status != PSA_SUCCESS) {
5119 return status;
5120 }
5121 status = psa_mac_sign_finish(&hkdf->hmac,
5122 hkdf->output_block,
5123 sizeof(hkdf->output_block),
5124 &hmac_output_length);
5125 if (status != PSA_SUCCESS) {
5126 return status;
5127 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005128 }
5129
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005131}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005132#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005133
John Durkop07cc04a2020-11-16 22:08:34 -08005134#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5135 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005136static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5137 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005139{
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5141 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005142 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5143 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005144 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005145
Janos Follath7742fee2019-06-17 12:58:10 +01005146 /* We can't be wanting more output after block 0xff, otherwise
5147 * the capacity check in psa_key_derivation_output_bytes() would have
5148 * prevented this call. It could happen only if the operation
5149 * object was corrupted or if this function is called directly
5150 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 if (tls12_prf->block_number == 0xff) {
5152 return PSA_ERROR_CORRUPTION_DETECTED;
5153 }
Janos Follath7742fee2019-06-17 12:58:10 +01005154
5155 /* We need a new block */
5156 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005157 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005158
5159 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5160 *
5161 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5162 *
5163 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5164 * HMAC_hash(secret, A(2) + seed) +
5165 * HMAC_hash(secret, A(3) + seed) + ...
5166 *
5167 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005168 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005169 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005170 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005171 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005172 * is currently extracted as `output_block` and where i is
5173 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005174 */
5175
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 status = psa_key_derivation_start_hmac(&hmac,
5177 hash_alg,
5178 tls12_prf->secret,
5179 tls12_prf->secret_length);
5180 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005181 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005183
5184 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005186 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5187 * the variable seed and in this instance means it in the context of the
5188 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 status = psa_mac_update(&hmac,
5190 tls12_prf->label,
5191 tls12_prf->label_length);
5192 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005193 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 }
5195 status = psa_mac_update(&hmac,
5196 tls12_prf->seed,
5197 tls12_prf->seed_length);
5198 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005199 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 }
5201 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005202 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5204 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005205 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005207 }
5208
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 status = psa_mac_sign_finish(&hmac,
5210 tls12_prf->Ai, hash_length,
5211 &hmac_output_length);
5212 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005213 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 }
5215 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005216 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005218
5219 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 status = psa_key_derivation_start_hmac(&hmac,
5221 hash_alg,
5222 tls12_prf->secret,
5223 tls12_prf->secret_length);
5224 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005225 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 }
5227 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5228 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005229 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 }
5231 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5232 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005233 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 }
5235 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5236 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005237 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 }
5239 status = psa_mac_sign_finish(&hmac,
5240 tls12_prf->output_block, hash_length,
5241 &hmac_output_length);
5242 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005243 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005245
Janos Follath7742fee2019-06-17 12:58:10 +01005246
5247cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 cleanup_status = psa_mac_abort(&hmac);
5249 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005250 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005252
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005254}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005255
Janos Follath844eb0e2019-06-19 12:10:49 +01005256static psa_status_t psa_key_derivation_tls12_prf_read(
5257 psa_tls12_prf_key_derivation_t *tls12_prf,
5258 psa_algorithm_t alg,
5259 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005261{
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5263 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005264 psa_status_t status;
5265 uint8_t offset, length;
5266
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005268 case PSA_TLS12_PRF_STATE_LABEL_SET:
5269 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5270 break;
5271 case PSA_TLS12_PRF_STATE_OUTPUT:
5272 break;
5273 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005275 }
5276
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005278 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (tls12_prf->left_in_block == 0) {
5280 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5281 alg);
5282 if (status != PSA_SUCCESS) {
5283 return status;
5284 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005285
5286 continue;
5287 }
5288
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005290 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005292 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005294
5295 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005297 output += length;
5298 output_length -= length;
5299 tls12_prf->left_in_block -= length;
5300 }
5301
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005303}
John Durkop07cc04a2020-11-16 22:08:34 -08005304#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5305 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005306
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005307#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5308static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5309 psa_tls12_ecjpake_to_pms_t *ecjpake,
5310 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005312{
5313 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005314 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005315
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 if (output_length != 32) {
5317 return PSA_ERROR_INVALID_ARGUMENT;
5318 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005319
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5321 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5322 &output_size);
5323 if (status != PSA_SUCCESS) {
5324 return status;
5325 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005326
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 if (output_size != output_length) {
5328 return PSA_ERROR_GENERIC_ERROR;
5329 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005330
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005332}
5333#endif
5334
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305335#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305336static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5337 psa_pbkdf2_key_derivation_t *pbkdf2,
5338 psa_algorithm_t prf_alg,
5339 uint8_t prf_output_length,
5340 psa_key_attributes_t *attributes)
5341{
5342 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305343 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305344 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305345 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305346 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305347 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305348 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305349
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305350 mac_operation.is_sign = 1;
5351 mac_operation.mac_size = prf_output_length;
5352 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305353
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305354 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5355 attributes,
5356 pbkdf2->password,
5357 pbkdf2->password_length,
5358 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305359 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305360 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305361 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305362 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5363 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305364 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305365 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305366 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305367 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305368 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305369 }
5370 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5371 &mac_output_length);
5372 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305373 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305374 }
5375
5376 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305377 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305378 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305379 }
5380
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305381 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305382
5383 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305384 /* We are passing prf_output_length as mac_size because the driver
5385 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305386 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305387 status = psa_driver_wrapper_mac_compute(attributes,
5388 pbkdf2->password,
5389 pbkdf2->password_length,
5390 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305391 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305392 &mac_output_length);
5393 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305394 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305395 }
5396
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305397 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305398 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305399
5400cleanup:
5401 /* Zeroise buffers to clear sensitive data from memory. */
5402 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5403 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305404}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305405
5406static psa_status_t psa_key_derivation_pbkdf2_read(
5407 psa_pbkdf2_key_derivation_t *pbkdf2,
5408 psa_algorithm_t kdf_alg,
5409 uint8_t *output,
5410 size_t output_length)
5411{
5412 psa_status_t status;
5413 psa_algorithm_t prf_alg;
5414 uint8_t prf_output_length;
5415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5416 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5417 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5418
5419 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5420 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5421 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5422 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305423 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5424 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305425 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305426 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305427 } else {
5428 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305429 }
5430
5431 switch (pbkdf2->state) {
5432 case PSA_PBKDF2_STATE_PASSWORD_SET:
5433 /* Initially we need a new block so bytes_used is equal to block size*/
5434 pbkdf2->bytes_used = prf_output_length;
5435 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5436 break;
5437 case PSA_PBKDF2_STATE_OUTPUT:
5438 break;
5439 default:
5440 return PSA_ERROR_BAD_STATE;
5441 }
5442
5443 while (output_length != 0) {
5444 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5445 if (n > output_length) {
5446 n = (uint8_t) output_length;
5447 }
5448 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5449 output += n;
5450 output_length -= n;
5451 pbkdf2->bytes_used += n;
5452
5453 if (output_length == 0) {
5454 break;
5455 }
5456
5457 /* We need a new block */
5458 pbkdf2->bytes_used = 0;
5459 pbkdf2->block_number++;
5460
5461 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5462 prf_output_length,
5463 &attributes);
5464 if (status != PSA_SUCCESS) {
5465 return status;
5466 }
5467 }
5468
5469 return PSA_SUCCESS;
5470}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305471#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305472
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005473psa_status_t psa_key_derivation_output_bytes(
5474 psa_key_derivation_operation_t *operation,
5475 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005477{
5478 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005480
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005482 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005484 }
5485
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005487 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005488 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005489 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005490 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005491 goto exit;
5492 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005494 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005495 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005496 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5497 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005498 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005499 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005501 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005502 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005503
Przemek Stekiel69c46792022-06-10 12:59:51 +02005504#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5506 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5507 output, output_length);
5508 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005509#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005510#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5511 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5513 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5514 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5515 kdf_alg, output,
5516 output_length);
5517 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005518#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5519 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005520#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005522 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5524 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005525#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305526#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305527 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305528 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5529 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305530 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305531#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005532
Gilles Peskineeab56e42018-07-12 17:12:33 +02005533 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005534 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005536 }
5537
5538exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005540 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005541 * This allows us to differentiate between exhausted operations and
5542 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5543 * operations. */
5544 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005546 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005548 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005550}
5551
David Brown7807bf72021-02-09 16:28:23 -07005552#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005553static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005554{
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 if (data_size >= 8) {
5556 mbedtls_des_key_set_parity(data);
5557 }
5558 if (data_size >= 16) {
5559 mbedtls_des_key_set_parity(data + 8);
5560 }
5561 if (data_size >= 24) {
5562 mbedtls_des_key_set_parity(data + 16);
5563 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005564}
David Brown7807bf72021-02-09 16:28:23 -07005565#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005566
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005567/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 * ECC keys on a Weierstrass elliptic curve require the generation
5569 * of a private key which is an integer
5570 * in the range [1, N - 1], where N is the boundary of the private key domain:
5571 * N is the prime p for Diffie-Hellman, or the order of the
5572 * curve’s base point for ECC.
5573 *
5574 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5575 * This function generates the private key using the following process:
5576 *
5577 * 1. Draw a byte string of length ceiling(m/8) bytes.
5578 * 2. If m is not a multiple of 8, set the most significant
5579 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5580 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5581 * 4. If k > N - 2, discard the result and return to step 1.
5582 * 5. Output k + 1 as the private key.
5583 *
5584 * This method allows compliance to NIST standards, specifically the methods titled
5585 * Key-Pair Generation by Testing Candidates in the following publications:
5586 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5587 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5588 * Diffie-Hellman keys.
5589 *
5590 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5591 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5592 *
5593 * Note: Function allocates memory for *data buffer, so given *data should be
5594 * always NULL.
5595 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005596#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5597#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005598static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005599 psa_key_slot_t *slot,
5600 size_t bits,
5601 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005602 uint8_t **data
5603 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005604{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005605 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005606 mbedtls_mpi k;
5607 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005608 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005609 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005610 size_t m;
5611 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005612
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 mbedtls_mpi_init(&k);
5614 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005615
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005616 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005618 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01005619 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005622 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005623 goto cleanup;
5624 }
5625
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005626 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005628
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005630
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005631 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005632 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005633 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005634
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005635 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005636
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005637 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005639
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005640 /* Note: This function is always called with *data == NULL and it
5641 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 *data = mbedtls_calloc(1, m_bytes);
5643 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005644 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005645 goto cleanup;
5646 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005647
Gilles Peskine449bd832023-01-11 14:50:10 +01005648 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005649 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005651 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005652 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005653
5654 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005656 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005657 * (8 * ceiling(m/8) - m) bits of the first byte in
5658 * the string to zero.
5659 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005661 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005662 }
5663
5664 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005665 * big-endian byte string.
5666 */
5667 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005668
5669 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 * Result of comparison is returned. When it indicates error
5671 * then this function is called again.
5672 */
5673 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005674 }
5675
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005676 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5678 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005679cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 if (ret != 0) {
5681 status = mbedtls_to_psa_error(ret);
5682 }
5683 if (status != PSA_SUCCESS) {
5684 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005685 *data = NULL;
5686 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 mbedtls_mpi_free(&k);
5688 mbedtls_mpi_free(&diff_N_2);
5689 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005690}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005691
5692/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5693 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5694 *
5695 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5696 * draw a 32-byte string and process it as specified in
5697 * Elliptic Curves for Security [RFC7748] §5.
5698 *
5699 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5700 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5701 *
5702 * Note: Function allocates memory for *data buffer, so given *data should be
5703 * always NULL.
5704 */
5705
5706static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5707 size_t bits,
5708 psa_key_derivation_operation_t *operation,
5709 uint8_t **data
5710 )
5711{
5712 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005713 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005714
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005716 case 255:
5717 output_length = 32;
5718 break;
5719 case 448:
5720 output_length = 56;
5721 break;
5722 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005724 break;
5725 }
5726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005728
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 if (*data == NULL) {
5730 return PSA_ERROR_INSUFFICIENT_MEMORY;
5731 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005732
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005736 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005738
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005740 case 255:
5741 (*data)[0] &= 248;
5742 (*data)[31] &= 127;
5743 (*data)[31] |= 64;
5744 break;
5745 case 448:
5746 (*data)[0] &= 252;
5747 (*data)[55] |= 128;
5748 break;
5749 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005751 break;
5752 }
5753
5754 return status;
5755}
Valerio Setti86587ab2023-06-27 13:09:30 +02005756#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5757static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5758 psa_key_slot_t *slot, size_t bits,
5759 psa_key_derivation_operation_t *operation, uint8_t **data)
5760{
5761 (void) slot;
5762 (void) bits;
5763 (void) operation;
5764 (void) data;
5765 return PSA_ERROR_NOT_SUPPORTED;
5766}
5767
5768static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5769 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5770{
5771 (void) bits;
5772 (void) operation;
5773 (void) data;
5774 return PSA_ERROR_NOT_SUPPORTED;
5775}
5776#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5777#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005778
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005779static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005780 psa_key_slot_t *slot,
5781 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005783{
5784 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305786 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005787 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5790 return PSA_ERROR_INVALID_ARGUMENT;
5791 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005792
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005793#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005794 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5796 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5797 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005798 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5800 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005801 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 }
5803 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005804 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5806 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005807 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005809 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005810 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005811#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005812 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 if (key_type_is_raw_bytes(slot->attr.type)) {
5814 if (bits % 8 != 0) {
5815 return PSA_ERROR_INVALID_ARGUMENT;
5816 }
5817 data = mbedtls_calloc(1, bytes);
5818 if (data == NULL) {
5819 return PSA_ERROR_INSUFFICIENT_MEMORY;
5820 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 status = psa_key_derivation_output_bytes(operation, data, bytes);
5823 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005824 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 }
David Brown12ca5032021-02-11 11:02:00 -07005826#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5828 psa_des_set_key_parity(data, bytes);
5829 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005830#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 } else {
5832 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005833 }
Ronald Crondd04d422020-11-28 15:14:42 +01005834
Ronald Cronbf33c932020-11-28 18:06:53 +01005835 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005836
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005837 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005838 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 &storage_size);
5840 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305841 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 }
Archanad8a83dc2021-06-14 10:04:16 +05305843 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 status = psa_allocate_buffer_to_slot(slot, storage_size);
5845 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305846 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 }
Archanad8a83dc2021-06-14 10:04:16 +05305848
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005849 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 data, bytes,
5851 slot->key.data,
5852 slot->key.bytes,
5853 &slot->key.bytes, &bits);
5854 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005855 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005857
5858exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 mbedtls_free(data);
5860 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005861}
5862
Gilles Peskine092ce512024-02-20 12:31:24 +01005863static const psa_key_production_parameters_t default_production_parameters =
5864 PSA_KEY_PRODUCTION_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005865
Gilles Peskine092ce512024-02-20 12:31:24 +01005866int psa_key_production_parameters_are_default(
5867 const psa_key_production_parameters_t *params,
5868 size_t params_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005869{
Gilles Peskine092ce512024-02-20 12:31:24 +01005870 if (params->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005871 return 0;
5872 }
Gilles Peskine092ce512024-02-20 12:31:24 +01005873 if (params_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005874 return 0;
5875 }
5876 return 1;
5877}
5878
5879psa_status_t psa_key_derivation_output_key_ext(
5880 const psa_key_attributes_t *attributes,
5881 psa_key_derivation_operation_t *operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005882 const psa_key_production_parameters_t *params,
5883 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005884 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005885{
5886 psa_status_t status;
5887 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005888 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005889
Ronald Cron81709fc2020-11-14 12:10:32 +01005890 *key = MBEDTLS_SVC_KEY_ID_INIT;
5891
Gilles Peskine0f84d622019-09-12 19:03:13 +02005892 /* Reject any attempt to create a zero-length key so that we don't
5893 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 if (psa_get_key_bits(attributes) == 0) {
5895 return PSA_ERROR_INVALID_ARGUMENT;
5896 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005897
Gilles Peskine092ce512024-02-20 12:31:24 +01005898 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005899 return PSA_ERROR_INVALID_ARGUMENT;
5900 }
5901
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 if (operation->alg == PSA_ALG_NONE) {
5903 return PSA_ERROR_BAD_STATE;
5904 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005905
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 if (!operation->can_output_key) {
5907 return PSA_ERROR_NOT_PERMITTED;
5908 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005909
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5911 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005912#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005914 /* Deriving a key in a secure element is not implemented yet. */
5915 status = PSA_ERROR_NOT_SUPPORTED;
5916 }
5917#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 if (status == PSA_SUCCESS) {
5919 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005920 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005922 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 if (status == PSA_SUCCESS) {
5924 status = psa_finish_key_creation(slot, driver, key);
5925 }
5926 if (status != PSA_SUCCESS) {
5927 psa_fail_key_creation(slot, driver);
5928 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005929
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005931}
5932
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005933psa_status_t psa_key_derivation_output_key(
5934 const psa_key_attributes_t *attributes,
5935 psa_key_derivation_operation_t *operation,
5936 mbedtls_svc_key_id_t *key)
5937{
Gilles Peskinec81393b2024-02-14 20:51:28 +01005938 return psa_key_derivation_output_key_ext(attributes, operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005939 &default_production_parameters, 0,
Gilles Peskinec81393b2024-02-14 20:51:28 +01005940 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005941}
Gilles Peskineeab56e42018-07-12 17:12:33 +02005942
5943
5944/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005945/* Key derivation */
5946/****************************************************************/
5947
Steven Cooreman932ffb72021-02-15 12:14:32 +01005948#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005949static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005950{
5951#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5953 return 1;
5954 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005955#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005956#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5958 return 1;
5959 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005960#endif
5961#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5963 return 1;
5964 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005965#endif
5966#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5968 return 1;
5969 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005970#endif
5971#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5973 return 1;
5974 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005975#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005976#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5978 return 1;
5979 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005980#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05305981#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5982 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5983 return 1;
5984 }
5985#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05305986#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
5987 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5988 return 1;
5989 }
5990#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005992}
5993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005995{
5996 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 psa_status_t status = psa_hash_setup(&operation, alg);
5998 psa_hash_abort(&operation);
5999 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006000}
6001
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306002static psa_status_t psa_key_derivation_set_maximum_capacity(
6003 psa_key_derivation_operation_t *operation,
6004 psa_algorithm_t kdf_alg)
6005{
6006#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6007 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6008 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6009 return PSA_SUCCESS;
6010 }
6011#endif
6012#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
6013 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6014#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05306015 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306016 PSA_KEY_TYPE_AES,
6017 128U,
6018 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306019#else
6020 operation->capacity = SIZE_MAX;
6021#endif
6022 return PSA_SUCCESS;
6023 }
6024#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6025
6026 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6027 * invalid or meaningless but it does not affect this function */
6028 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6029 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306030 if (hash_size == 0) {
6031 return PSA_ERROR_NOT_SUPPORTED;
6032 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306033
6034 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6035 * we might fail later, which is somewhat unfriendly and potentially
6036 * risk-prone. */
6037 psa_status_t status = psa_hash_try_support(hash_alg);
6038 if (status != PSA_SUCCESS) {
6039 return status;
6040 }
6041
6042#if defined(PSA_WANT_ALG_HKDF)
6043 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6044 operation->capacity = 255 * hash_size;
6045 } else
6046#endif
6047#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6048 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6049 operation->capacity = hash_size;
6050 } else
6051#endif
6052#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6053 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6054 operation->capacity = 255 * hash_size;
6055 } else
6056#endif
6057#if defined(PSA_WANT_ALG_TLS12_PRF)
6058 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6059 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6060 operation->capacity = SIZE_MAX;
6061 } else
6062#endif
6063#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6064 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6065 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6066 /* Master Secret is always 48 bytes
6067 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6068 operation->capacity = 48U;
6069 } else
6070#endif
6071#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6072 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6073#if (SIZE_MAX > UINT32_MAX)
6074 operation->capacity = UINT32_MAX * hash_size;
6075#else
6076 operation->capacity = SIZE_MAX;
6077#endif
6078 } else
6079#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6080 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306081 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306082 status = PSA_ERROR_NOT_SUPPORTED;
6083 }
6084 return status;
6085}
6086
Gilles Peskine969c5d62019-01-16 15:53:06 +01006087static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006088 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006090{
Janos Follath5fe19732019-06-20 15:09:30 +01006091 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6092 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006094
Gilles Peskine969c5d62019-01-16 15:53:06 +01006095 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 if (!is_kdf_alg_supported(kdf_alg)) {
6097 return PSA_ERROR_NOT_SUPPORTED;
6098 }
John Durkop07cc04a2020-11-16 22:08:34 -08006099
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306100 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6101 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306102 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006103}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006106{
6107#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (alg == PSA_ALG_ECDH) {
6109 return PSA_SUCCESS;
6110 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006111#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006112#if defined(PSA_WANT_ALG_FFDH)
6113 if (alg == PSA_ALG_FFDH) {
6114 return PSA_SUCCESS;
6115 }
6116#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006117 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006119}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006120
6121static int psa_key_derivation_allows_free_form_secret_input(
6122 psa_algorithm_t kdf_alg)
6123{
6124#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6125 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6126 return 0;
6127 }
6128#endif
6129 (void) kdf_alg;
6130 return 1;
6131}
John Durkop07cc04a2020-11-16 22:08:34 -08006132#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6135 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006136{
6137 psa_status_t status;
6138
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 if (operation->alg != 0) {
6140 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006141 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006142
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6144 return PSA_ERROR_INVALID_ARGUMENT;
6145 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6146#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6147 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6148 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6149 status = psa_key_agreement_try_support(ka_alg);
6150 if (status != PSA_SUCCESS) {
6151 return status;
6152 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006153 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6154 return PSA_ERROR_INVALID_ARGUMENT;
6155 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6157#else
6158 return PSA_ERROR_NOT_SUPPORTED;
6159#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6160 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6161#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6162 status = psa_key_derivation_setup_kdf(operation, alg);
6163#else
6164 return PSA_ERROR_NOT_SUPPORTED;
6165#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6166 } else {
6167 return PSA_ERROR_INVALID_ARGUMENT;
6168 }
6169
6170 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006171 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 }
6173 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006174}
6175
Przemek Stekiel69c46792022-06-10 12:59:51 +02006176#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006177static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6178 psa_algorithm_t kdf_alg,
6179 psa_key_derivation_step_t step,
6180 const uint8_t *data,
6181 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006182{
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006184 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006186 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006187#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6189 return PSA_ERROR_INVALID_ARGUMENT;
6190 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006191#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 if (hkdf->state != HKDF_STATE_INIT) {
6193 return PSA_ERROR_BAD_STATE;
6194 } else {
6195 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6196 hash_alg,
6197 data, data_length);
6198 if (status != PSA_SUCCESS) {
6199 return status;
6200 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006201 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006203 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006204 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006205#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006207 /* We shouldn't be in different state as HKDF_EXPAND only allows
6208 * two inputs: SECRET (this case) and INFO which does not modify
6209 * the state. It could happen only if the hkdf
6210 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 if (hkdf->state != HKDF_STATE_INIT) {
6212 return PSA_ERROR_BAD_STATE;
6213 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006214
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006215 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6217 return PSA_ERROR_INVALID_ARGUMENT;
6218 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006219
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 memcpy(hkdf->prk, data, data_length);
6221 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006222#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006223 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006224 /* HKDF: If no salt was provided, use an empty salt.
6225 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006227#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6229 return PSA_ERROR_BAD_STATE;
6230 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006231#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6233 hash_alg,
6234 NULL, 0);
6235 if (status != PSA_SUCCESS) {
6236 return status;
6237 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006238 hkdf->state = HKDF_STATE_STARTED;
6239 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 if (hkdf->state != HKDF_STATE_STARTED) {
6241 return PSA_ERROR_BAD_STATE;
6242 }
6243 status = psa_mac_update(&hkdf->hmac,
6244 data, data_length);
6245 if (status != PSA_SUCCESS) {
6246 return status;
6247 }
6248 status = psa_mac_sign_finish(&hkdf->hmac,
6249 hkdf->prk,
6250 sizeof(hkdf->prk),
6251 &data_length);
6252 if (status != PSA_SUCCESS) {
6253 return status;
6254 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006255 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006256
Gilles Peskine2b522db2019-04-12 15:11:49 +02006257 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006258 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006259#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006261 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006263 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006265#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006266 {
6267 /* Block 0 is empty, and the next block will be
6268 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006270 }
6271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006273 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006274#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6276 return PSA_ERROR_INVALID_ARGUMENT;
6277 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006278#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006279#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6281 hkdf->state == HKDF_STATE_INIT) {
6282 return PSA_ERROR_BAD_STATE;
6283 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006284#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 if (hkdf->state == HKDF_STATE_OUTPUT) {
6286 return PSA_ERROR_BAD_STATE;
6287 }
6288 if (hkdf->info_set) {
6289 return PSA_ERROR_BAD_STATE;
6290 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006291 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 if (data_length != 0) {
6293 hkdf->info = mbedtls_calloc(1, data_length);
6294 if (hkdf->info == NULL) {
6295 return PSA_ERROR_INSUFFICIENT_MEMORY;
6296 }
6297 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006298 }
6299 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006301 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006303 }
6304}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006305#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006306
John Durkop07cc04a2020-11-16 22:08:34 -08006307#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6308 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006309static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6310 const uint8_t *data,
6311 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006312{
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6314 return PSA_ERROR_BAD_STATE;
6315 }
Janos Follathf08e2652019-06-13 09:05:41 +01006316
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 if (data_length != 0) {
6318 prf->seed = mbedtls_calloc(1, data_length);
6319 if (prf->seed == NULL) {
6320 return PSA_ERROR_INSUFFICIENT_MEMORY;
6321 }
Janos Follathf08e2652019-06-13 09:05:41 +01006322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006324 prf->seed_length = data_length;
6325 }
Janos Follathf08e2652019-06-13 09:05:41 +01006326
Gilles Peskine43f958b2020-12-13 14:55:14 +01006327 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006330}
6331
Gilles Peskine449bd832023-01-11 14:50:10 +01006332static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6333 const uint8_t *data,
6334 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006335{
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6337 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6338 return PSA_ERROR_BAD_STATE;
6339 }
Janos Follath81550542019-06-13 14:26:34 +01006340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 if (data_length != 0) {
6342 prf->secret = mbedtls_calloc(1, data_length);
6343 if (prf->secret == NULL) {
6344 return PSA_ERROR_INSUFFICIENT_MEMORY;
6345 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006348 prf->secret_length = data_length;
6349 }
Janos Follath81550542019-06-13 14:26:34 +01006350
Gilles Peskine43f958b2020-12-13 14:55:14 +01006351 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006354}
6355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6357 const uint8_t *data,
6358 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006359{
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6361 return PSA_ERROR_BAD_STATE;
6362 }
Janos Follath63028dd2019-06-13 09:15:47 +01006363
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 if (data_length != 0) {
6365 prf->label = mbedtls_calloc(1, data_length);
6366 if (prf->label == NULL) {
6367 return PSA_ERROR_INSUFFICIENT_MEMORY;
6368 }
Janos Follath63028dd2019-06-13 09:15:47 +01006369
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006371 prf->label_length = data_length;
6372 }
Janos Follath63028dd2019-06-13 09:15:47 +01006373
Gilles Peskine43f958b2020-12-13 14:55:14 +01006374 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006377}
6378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6380 psa_key_derivation_step_t step,
6381 const uint8_t *data,
6382 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006383{
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006385 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006387 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006389 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006391 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006393 }
6394}
John Durkop07cc04a2020-11-16 22:08:34 -08006395#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6396 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6397
6398#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6399static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6400 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006401 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006403{
6404 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6406 4 + data_length + prf->other_secret_length :
6407 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006408
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6410 return PSA_ERROR_INVALID_ARGUMENT;
6411 }
John Durkop07cc04a2020-11-16 22:08:34 -08006412
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 uint8_t *pms = mbedtls_calloc(1, pms_len);
6414 if (pms == NULL) {
6415 return PSA_ERROR_INSUFFICIENT_MEMORY;
6416 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006417 uint8_t *cur = pms;
6418
6419 /* pure-PSK:
6420 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006421 *
6422 * The premaster secret is formed as follows: if the PSK is N octets
6423 * long, concatenate a uint16 with the value N, N zero octets, a second
6424 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006425 *
6426 * mixed-PSK:
6427 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6428 * follows: concatenate a uint16 with the length of the other secret,
6429 * the other secret itself, uint16 with the length of PSK, and the
6430 * PSK itself.
6431 * For details please check:
6432 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6433 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6434 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006435 */
6436
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6438 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6439 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6440 if (prf->other_secret_length != 0) {
6441 memcpy(cur, prf->other_secret, prf->other_secret_length);
6442 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006443 cur += prf->other_secret_length;
6444 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 } else {
6446 *cur++ = MBEDTLS_BYTE_1(data_length);
6447 *cur++ = MBEDTLS_BYTE_0(data_length);
6448 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006449 cur += data_length;
6450 }
6451
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 *cur++ = MBEDTLS_BYTE_1(data_length);
6453 *cur++ = MBEDTLS_BYTE_0(data_length);
6454 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006455 cur += data_length;
6456
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006457 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006458
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006459 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006461}
Janos Follath6660f0e2019-06-17 08:44:03 +01006462
Przemek Stekiele47201b2022-04-19 13:53:28 +02006463static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6464 psa_tls12_prf_key_derivation_t *prf,
6465 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006467{
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6469 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006470 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006471
6472 if (data_length != 0) {
6473 prf->other_secret = mbedtls_calloc(1, data_length);
6474 if (prf->other_secret == NULL) {
6475 return PSA_ERROR_INSUFFICIENT_MEMORY;
6476 }
6477
6478 memcpy(prf->other_secret, data, data_length);
6479 prf->other_secret_length = data_length;
6480 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006481 prf->other_secret_length = 0;
6482 }
6483
6484 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6485
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006487}
6488
Janos Follath6660f0e2019-06-17 08:44:03 +01006489static psa_status_t psa_tls12_prf_psk_to_ms_input(
6490 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006491 psa_key_derivation_step_t step,
6492 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006494{
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006496 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 return psa_tls12_prf_psk_to_ms_set_key(prf,
6498 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006499 break;
6500 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6502 data,
6503 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006504 break;
6505 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006507 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006508
Przemek Stekiele47201b2022-04-19 13:53:28 +02006509 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006510}
John Durkop07cc04a2020-11-16 22:08:34 -08006511#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006512
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006513#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6514static psa_status_t psa_tls12_ecjpake_to_pms_input(
6515 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006516 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006517 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006519{
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6521 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6522 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006523 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006524
6525 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 if (data[0] != 0x04) {
6527 return PSA_ERROR_INVALID_ARGUMENT;
6528 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006529
6530 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006532
Gilles Peskine449bd832023-01-11 14:50:10 +01006533 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006534}
6535#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306536
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306537#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306538static psa_status_t psa_pbkdf2_set_input_cost(
6539 psa_pbkdf2_key_derivation_t *pbkdf2,
6540 psa_key_derivation_step_t step,
6541 uint64_t data)
6542{
6543 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6544 return PSA_ERROR_INVALID_ARGUMENT;
6545 }
6546
6547 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6548 return PSA_ERROR_BAD_STATE;
6549 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306550
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306551 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306552 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306553 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306554
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306555 if (data == 0) {
6556 return PSA_ERROR_INVALID_ARGUMENT;
6557 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306558
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306559 pbkdf2->input_cost = data;
6560 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6561
6562 return PSA_SUCCESS;
6563}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306564
6565static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6566 const uint8_t *data,
6567 size_t data_length)
6568{
Gilles Peskineead17662023-08-20 22:02:51 +02006569 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6570 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6571 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6572 /* Appending to existing salt. No state change. */
6573 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306574 return PSA_ERROR_BAD_STATE;
6575 }
6576
Gilles Peskineca57d782023-07-20 19:08:06 +02006577 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006578 /* Appending an empty string, nothing to do. */
6579 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306580 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306581
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306582 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6583 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306584 return PSA_ERROR_INSUFFICIENT_MEMORY;
6585 }
6586
Gilles Peskineead17662023-08-20 22:02:51 +02006587 if (pbkdf2->salt_length != 0) {
6588 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6589 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306590 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306591 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306592 mbedtls_free(pbkdf2->salt);
6593 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306594 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306595 return PSA_SUCCESS;
6596}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306597
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306598#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306599static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306600 const uint8_t *input,
6601 size_t input_len,
6602 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306603 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306604{
6605 psa_status_t status = PSA_SUCCESS;
6606 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6607 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306608 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306609 } else {
6610 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306611 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306612 }
6613 return status;
6614}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306615#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306616
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306617#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306618static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6619 size_t input_len,
6620 uint8_t *output,
6621 size_t *output_len)
6622{
6623 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306624 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306626 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306627 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6628 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6629 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306630 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306631 * mac_size as the driver function sets mac_output_length = mac_size
6632 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306633 status = psa_driver_wrapper_mac_compute(&attributes,
6634 zeros, sizeof(zeros),
6635 PSA_ALG_CMAC, input, input_len,
6636 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306637 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6638 128U,
6639 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306640 output_len);
6641 } else {
6642 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306643 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306644 }
6645 return status;
6646}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306647#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306648
6649static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306650 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306651 const uint8_t *data,
6652 size_t data_length)
6653{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306654 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306655 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6656 return PSA_ERROR_BAD_STATE;
6657 }
6658
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306659#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306660 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6661 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6662 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6663 pbkdf2->password,
6664 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306665 } else
6666#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6667#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6668 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306669 status = psa_pbkdf2_cmac_set_password(data, data_length,
6670 pbkdf2->password,
6671 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306672 } else
6673#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6674 {
6675 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306676 }
6677
6678 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6679
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306680 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306681}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306682
6683static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306684 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306685 psa_key_derivation_step_t step,
6686 const uint8_t *data,
6687 size_t data_length)
6688{
6689 switch (step) {
6690 case PSA_KEY_DERIVATION_INPUT_SALT:
6691 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6692 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306693 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306694 default:
6695 return PSA_ERROR_INVALID_ARGUMENT;
6696 }
6697}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306698#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306699
Gilles Peskineb8965192019-09-24 16:21:10 +02006700/** Check whether the given key type is acceptable for the given
6701 * input step of a key derivation.
6702 *
6703 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6704 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6705 * Both secret and non-secret inputs can alternatively have the type
6706 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6707 * that the input was passed as a buffer rather than via a key object.
6708 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006709static int psa_key_derivation_check_input_type(
6710 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006712{
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006714 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 if (key_type == PSA_KEY_TYPE_DERIVE) {
6716 return PSA_SUCCESS;
6717 }
6718 if (key_type == PSA_KEY_TYPE_NONE) {
6719 return PSA_SUCCESS;
6720 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006721 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006722 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006723 if (key_type == PSA_KEY_TYPE_DERIVE) {
6724 return PSA_SUCCESS;
6725 }
6726 if (key_type == PSA_KEY_TYPE_NONE) {
6727 return PSA_SUCCESS;
6728 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006729 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006730 case PSA_KEY_DERIVATION_INPUT_LABEL:
6731 case PSA_KEY_DERIVATION_INPUT_SALT:
6732 case PSA_KEY_DERIVATION_INPUT_INFO:
6733 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6735 return PSA_SUCCESS;
6736 }
6737 if (key_type == PSA_KEY_TYPE_NONE) {
6738 return PSA_SUCCESS;
6739 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006740 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306741 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6742 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6743 return PSA_SUCCESS;
6744 }
6745 if (key_type == PSA_KEY_TYPE_DERIVE) {
6746 return PSA_SUCCESS;
6747 }
6748 if (key_type == PSA_KEY_TYPE_NONE) {
6749 return PSA_SUCCESS;
6750 }
6751 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006752 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006753 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006754}
6755
Janos Follathb80a94e2019-06-12 15:54:46 +01006756static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006757 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006758 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006759 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006760 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006762{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006763 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006764 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006765
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 status = psa_key_derivation_check_input_type(step, key_type);
6767 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006768 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006770
Przemek Stekiel69c46792022-06-10 12:59:51 +02006771#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006772 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6773 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6774 step, data, data_length);
6775 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006776#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006777#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006778 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6779 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6780 step, data, data_length);
6781 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006782#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6783#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006784 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6785 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6786 step, data, data_length);
6787 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006788#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006789#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006791 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006792 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6793 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006794#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306795#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306796 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306797 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6798 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306799 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306800#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006801 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006802 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006803 (void) data;
6804 (void) data_length;
6805 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006806 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006807 }
6808
Gilles Peskine224b0d62019-09-23 18:13:17 +02006809exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006810 if (status != PSA_SUCCESS) {
6811 psa_key_derivation_abort(operation);
6812 }
6813 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006814}
6815
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306816static psa_status_t psa_key_derivation_input_integer_internal(
6817 psa_key_derivation_operation_t *operation,
6818 psa_key_derivation_step_t step,
6819 uint64_t value)
6820{
6821 psa_status_t status;
6822 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6823
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306824#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306825 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306826 status = psa_pbkdf2_set_input_cost(
6827 &operation->ctx.pbkdf2, step, value);
6828 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306829#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306830 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306831 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306832 (void) value;
6833 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306834 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306835 }
6836
6837 if (status != PSA_SUCCESS) {
6838 psa_key_derivation_abort(operation);
6839 }
6840 return status;
6841}
6842
Janos Follath51f4a0f2019-06-14 11:35:55 +01006843psa_status_t psa_key_derivation_input_bytes(
6844 psa_key_derivation_operation_t *operation,
6845 psa_key_derivation_step_t step,
6846 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006847 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006848{
Gilles Peskine449bd832023-01-11 14:50:10 +01006849 return psa_key_derivation_input_internal(operation, step,
6850 PSA_KEY_TYPE_NONE,
6851 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006852}
6853
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306854psa_status_t psa_key_derivation_input_integer(
6855 psa_key_derivation_operation_t *operation,
6856 psa_key_derivation_step_t step,
6857 uint64_t value)
6858{
6859 return psa_key_derivation_input_integer_internal(operation, step, value);
6860}
6861
Janos Follath51f4a0f2019-06-14 11:35:55 +01006862psa_status_t psa_key_derivation_input_key(
6863 psa_key_derivation_operation_t *operation,
6864 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006866{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006867 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006868 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006869 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006870
Ronald Cron5c522922020-11-14 16:35:34 +01006871 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6873 if (status != PSA_SUCCESS) {
6874 psa_key_derivation_abort(operation);
6875 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006876 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006877
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306878 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6879 * permission to output to a key object. */
6880 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6881 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006882 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006883 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006884
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 status = psa_key_derivation_input_internal(operation,
6886 step, slot->attr.type,
6887 slot->key.data,
6888 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006889
Ryan Everett5ac6fa72024-02-14 17:11:36 +00006890 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006891
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006893}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006894
6895
6896
6897/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006898/* Key agreement */
6899/****************************************************************/
6900
Gilles Peskine449bd832023-01-11 14:50:10 +01006901psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6902 const uint8_t *key_buffer,
6903 size_t key_buffer_size,
6904 psa_algorithm_t alg,
6905 const uint8_t *peer_key,
6906 size_t peer_key_length,
6907 uint8_t *shared_secret,
6908 size_t shared_secret_size,
6909 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006910{
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006912#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006913 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006914 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6915 key_buffer_size, alg,
6916 peer_key, peer_key_length,
6917 shared_secret,
6918 shared_secret_size,
6919 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006920#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006921
6922#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6923 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006924 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006925 peer_key,
6926 peer_key_length,
6927 key_buffer,
6928 key_buffer_size,
6929 shared_secret,
6930 shared_secret_size,
6931 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006932#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6933
Gilles Peskine01d718c2018-09-18 12:01:02 +02006934 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006935 (void) attributes;
6936 (void) key_buffer;
6937 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006938 (void) peer_key;
6939 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006940 (void) shared_secret;
6941 (void) shared_secret_size;
6942 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006944 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006945}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006946
Aditya Deshpande17845b82022-10-13 17:21:01 +01006947/** Internal function for raw key agreement
6948 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006949 * to the driver's implementation if a driver is present.
6950 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006951 * (psa_key_agreement_raw_builtin).
6952 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006953static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6954 psa_key_slot_t *private_key,
6955 const uint8_t *peer_key,
6956 size_t peer_key_length,
6957 uint8_t *shared_secret,
6958 size_t shared_secret_size,
6959 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006960{
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6962 return PSA_ERROR_NOT_SUPPORTED;
6963 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006964
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006965 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 private_key->key.data,
6967 private_key->key.bytes, alg,
6968 peer_key, peer_key_length,
6969 shared_secret,
6970 shared_secret_size,
6971 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006972}
6973
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006974/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006975 * to potentially free embedded data structures and wipe confidential data.
6976 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006977static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6978 psa_key_derivation_step_t step,
6979 psa_key_slot_t *private_key,
6980 const uint8_t *peer_key,
6981 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006982{
6983 psa_status_t status;
Moritz Fischer967f8cd2024-03-02 00:55:06 +00006984 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
Gilles Peskine01d718c2018-09-18 12:01:02 +02006985 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006987
6988 /* Step 1: run the secret agreement algorithm to generate the shared
6989 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 status = psa_key_agreement_raw_internal(ka_alg,
6991 private_key,
6992 peer_key, peer_key_length,
6993 shared_secret,
6994 sizeof(shared_secret),
6995 &shared_secret_length);
6996 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006997 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006998 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006999
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007000 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007001 * the shared secret. A shared secret is permitted wherever a key
7002 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 status = psa_key_derivation_input_internal(operation, step,
7004 PSA_KEY_TYPE_DERIVE,
7005 shared_secret,
7006 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007007exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007008 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7009 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007010}
7011
Gilles Peskine449bd832023-01-11 14:50:10 +01007012psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7013 psa_key_derivation_step_t step,
7014 mbedtls_svc_key_id_t private_key,
7015 const uint8_t *peer_key,
7016 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007017{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007018 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007019 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007020 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007021
Gilles Peskine449bd832023-01-11 14:50:10 +01007022 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7023 return PSA_ERROR_INVALID_ARGUMENT;
7024 }
Ronald Cron5c522922020-11-14 16:35:34 +01007025 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7027 if (status != PSA_SUCCESS) {
7028 return status;
7029 }
7030 status = psa_key_agreement_internal(operation, step,
7031 slot,
7032 peer_key, peer_key_length);
7033 if (status != PSA_SUCCESS) {
7034 psa_key_derivation_abort(operation);
7035 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007036 /* If a private key has been added as SECRET, we allow the derived
7037 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007038 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007039 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007041 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007042
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007043 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007044
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007046}
7047
Gilles Peskine449bd832023-01-11 14:50:10 +01007048psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7049 mbedtls_svc_key_id_t private_key,
7050 const uint8_t *peer_key,
7051 size_t peer_key_length,
7052 uint8_t *output,
7053 size_t output_size,
7054 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007055{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007056 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007057 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007058 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007059 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007060
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007062 status = PSA_ERROR_INVALID_ARGUMENT;
7063 goto exit;
7064 }
Ronald Cron5c522922020-11-14 16:35:34 +01007065 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7067 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007068 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007070
Gilles Peskine3e561302022-04-14 00:17:15 +02007071 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7072 * for the output size. The PSA specification only guarantees that this
7073 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7074 * but it might be nice to allow smaller buffers if the output fits.
7075 * At the time of writing this comment, with only ECDH implemented,
7076 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7077 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7078 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007079 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7081 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007082 status = PSA_ERROR_BUFFER_TOO_SMALL;
7083 goto exit;
7084 }
7085
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 status = psa_key_agreement_raw_internal(alg, slot,
7087 peer_key, peer_key_length,
7088 output, output_size,
7089 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007090
7091exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007093 /* If an error happens and is not handled properly, the output
7094 * may be used as a key to protect sensitive data. Arrange for such
7095 * a key to be random, which is likely to result in decryption or
7096 * verification errors. This is better than filling the buffer with
7097 * some constant data such as zeros, which would result in the data
7098 * being protected with a reproducible, easily knowable key.
7099 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007101 *output_length = output_size;
7102 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007103
Ryan Everetta103ec92024-01-31 13:59:57 +00007104 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007105
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007107}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007108
7109
Gilles Peskine30524eb2020-11-13 17:02:26 +01007110
Gilles Peskine86a440b2018-11-12 18:39:40 +01007111/****************************************************************/
7112/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007113/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007114
Gilles Peskine672a7712023-04-28 21:00:28 +02007115#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7116#include "entropy_poll.h"
7117#endif
7118
Gilles Peskine30524eb2020-11-13 17:02:26 +01007119/** Initialize the PSA random generator.
7120 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007121static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007122{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007123#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007124 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007125#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7126
Gilles Peskine30524eb2020-11-13 17:02:26 +01007127 /* Set default configuration if
7128 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007129 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007130 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 }
7132 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007133 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007135
Gilles Peskine449bd832023-01-11 14:50:10 +01007136 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007137#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7138 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7139 /* The PSA entropy injection feature depends on using NV seed as an entropy
7140 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 mbedtls_entropy_add_source(&rng->entropy,
7142 mbedtls_nv_seed_poll, NULL,
7143 MBEDTLS_ENTROPY_BLOCK_SIZE,
7144 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007145#endif
7146
Valerio Setti061d4e42024-02-26 12:52:44 +01007147 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007148#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007149}
7150
7151/** Deinitialize the PSA random generator.
7152 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007153static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007154{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007155#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007156 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007157#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01007158 mbedtls_psa_drbg_free(&rng->drbg);
Valerio Setti83e0de82023-11-24 12:13:05 +01007159 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007160#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007161}
7162
7163/** Seed the PSA random generator.
7164 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007165static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007166{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007167#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7168 /* Do nothing: the external RNG seeds itself. */
7169 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007171#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007172 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01007173 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 drbg_seed, sizeof(drbg_seed) - 1);
7175 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007176#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007177}
7178
Gilles Peskine449bd832023-01-11 14:50:10 +01007179psa_status_t psa_generate_random(uint8_t *output,
7180 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007181{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007182 GUARD_MODULE_INITIALIZED;
7183
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007184#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7185
7186 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007187 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7188 output, output_size,
7189 &output_length);
7190 if (status != PSA_SUCCESS) {
7191 return status;
7192 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007193 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007194 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 if (output_length != output_size) {
7196 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7197 }
7198 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007199
7200#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7201
Gilles Peskine449bd832023-01-11 14:50:10 +01007202 while (output_size > 0) {
Valerio Setti718180c2024-02-27 11:58:39 +01007203 int ret = MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
Gilles Peskine71ddab92021-01-04 21:01:07 +01007204 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007205 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7206 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7207 output_size);
Valerio Setti718180c2024-02-27 11:58:39 +01007208#if defined(MBEDTLS_CTR_DRBG_C)
7209 ret = mbedtls_ctr_drbg_random(&global_data.rng.drbg, output, request_size);
7210#elif defined(MBEDTLS_HMAC_DRBG_C)
7211 ret = mbedtls_hmac_drbg_random(&global_data.rng.drbg, output, request_size);
7212#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 if (ret != 0) {
7214 return mbedtls_to_psa_error(ret);
7215 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007216 output_size -= request_size;
7217 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007218 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007219 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007220#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007221}
7222
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007223#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007224psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7225 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007226{
Paul Elliott600472b2024-02-23 17:26:58 +00007227 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007228 return PSA_ERROR_NOT_PERMITTED;
7229 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007230
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7232 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7233 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7234 return PSA_ERROR_INVALID_ARGUMENT;
7235 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007236
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007238}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007239#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007240
Ronald Cron3772afe2021-02-08 16:10:05 +01007241/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007242 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007243 * \param type The key type
7244 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007245 *
7246 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007247 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007248 * \retval #PSA_ERROR_INVALID_ARGUMENT
7249 * The size in bits of the key is not valid.
7250 * \retval #PSA_ERROR_NOT_SUPPORTED
7251 * The type and/or the size in bits of the key or the combination of
7252 * the two is not supported.
7253 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007254static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007256{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007257 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007258
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 if (key_type_is_raw_bytes(type)) {
7260 status = psa_validate_unstructured_key_bit_size(type, bits);
7261 if (status != PSA_SUCCESS) {
7262 return status;
7263 }
7264 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007265#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7267 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7268 return PSA_ERROR_NOT_SUPPORTED;
7269 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007270 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007271 return PSA_ERROR_NOT_SUPPORTED;
7272 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007273
Ronald Cron01b2aba2020-10-05 09:42:02 +02007274 /* Accept only byte-aligned keys, for the same reasons as
7275 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007276 if (bits % 8 != 0) {
7277 return PSA_ERROR_NOT_SUPPORTED;
7278 }
7279 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007280#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007281
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007282#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007284 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007285 return PSA_SUCCESS;
7286 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007287#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007288
Valerio Settia55f0422023-07-10 15:34:41 +02007289#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007290 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007291 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007292 return PSA_ERROR_NOT_SUPPORTED;
7293 }
7294 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007295#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007296 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007297 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007298 }
7299
Gilles Peskine449bd832023-01-11 14:50:10 +01007300 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007301}
7302
Ronald Cron55ed0592020-10-05 10:30:40 +02007303psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007304 const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007305 const psa_key_production_parameters_t *params, size_t params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007306 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007307{
7308 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007309 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007310
Gilles Peskine7a18f962024-02-12 16:48:11 +01007311 /* Only used for RSA */
Gilles Peskine092ce512024-02-20 12:31:24 +01007312 (void) params;
7313 (void) params_data_length;
Gilles Peskine7a18f962024-02-12 16:48:11 +01007314
Gilles Peskine449bd832023-01-11 14:50:10 +01007315 if (key_type_is_raw_bytes(type)) {
7316 status = psa_generate_random(key_buffer, key_buffer_size);
7317 if (status != PSA_SUCCESS) {
7318 return status;
7319 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007320
David Brown12ca5032021-02-11 11:02:00 -07007321#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 if (type == PSA_KEY_TYPE_DES) {
7323 psa_des_set_key_parity(key_buffer, key_buffer_size);
7324 }
David Brown8107e312021-02-12 08:08:46 -07007325#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007327
Valerio Setti76df8c12023-07-11 14:11:28 +02007328#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007329 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskinee22f6a92024-02-26 15:45:33 +01007330 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskine4c32b692024-02-16 00:11:09 +01007331 params, params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 key_buffer,
7333 key_buffer_size,
7334 key_buffer_length);
7335 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007336#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007337
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007338#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007339 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7340 return mbedtls_psa_ecp_generate_key(attributes,
7341 key_buffer,
7342 key_buffer_size,
7343 key_buffer_length);
7344 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007345#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007346
Valerio Settia55f0422023-07-10 15:34:41 +02007347#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007348 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7349 return mbedtls_psa_ffdh_generate_key(attributes,
7350 key_buffer,
7351 key_buffer_size,
7352 key_buffer_length);
7353 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007354#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007355 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007356 (void) key_buffer_length;
7357 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007358 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007359
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007361}
Darryl Green0c6575a2018-11-07 16:05:30 +00007362
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007363psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007364 const psa_key_production_parameters_t *params,
7365 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007366 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007367{
7368 psa_status_t status;
7369 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007370 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007371 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007372
Ronald Cron81709fc2020-11-14 12:10:32 +01007373 *key = MBEDTLS_SVC_KEY_ID_INIT;
7374
Gilles Peskine0f84d622019-09-12 19:03:13 +02007375 /* Reject any attempt to create a zero-length key so that we don't
7376 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 if (psa_get_key_bits(attributes) == 0) {
7378 return PSA_ERROR_INVALID_ARGUMENT;
7379 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007380
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007381 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007382 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007383 return PSA_ERROR_INVALID_ARGUMENT;
7384 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007385
Gilles Peskine7a18f962024-02-12 16:48:11 +01007386#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007387 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +01007388 if (params->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01007389 return PSA_ERROR_INVALID_ARGUMENT;
7390 }
7391 } else
7392#endif
Gilles Peskine092ce512024-02-20 12:31:24 +01007393 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007394 return PSA_ERROR_INVALID_ARGUMENT;
7395 }
7396
Gilles Peskine449bd832023-01-11 14:50:10 +01007397 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7398 &slot, &driver);
7399 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007400 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007401 }
Gilles Peskine11792082019-08-06 18:36:36 +02007402
Ronald Cron977c2472020-10-13 08:32:21 +02007403 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307404 * storage ( thus not in the case of generating a key in a secure element
7405 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7406 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007407 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007408 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01007409 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007410 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007411 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007412 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007413 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007414 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007415
7416 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007417 attributes->type,
7418 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007419 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007420 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007421 attributes, &key_buffer_size);
7422 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007423 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007424 }
Ronald Cron977c2472020-10-13 08:32:21 +02007425 }
Ronald Cron977c2472020-10-13 08:32:21 +02007426
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7428 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007429 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007430 }
Ronald Cron977c2472020-10-13 08:32:21 +02007431 }
7432
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007434 params, params_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01007435 slot->key.data, slot->key.bytes,
7436 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 if (status != PSA_SUCCESS) {
7438 psa_remove_key_data_from_memory(slot);
7439 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007440
Gilles Peskine11792082019-08-06 18:36:36 +02007441exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007442 if (status == PSA_SUCCESS) {
7443 status = psa_finish_key_creation(slot, driver, key);
7444 }
7445 if (status != PSA_SUCCESS) {
7446 psa_fail_key_creation(slot, driver);
7447 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007448
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007450}
7451
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007452psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7453 mbedtls_svc_key_id_t *key)
7454{
7455 return psa_generate_key_ext(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007456 &default_production_parameters, 0,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007457 key);
7458}
7459
Gilles Peskinee59236f2018-01-27 23:32:46 +01007460/****************************************************************/
7461/* Module setup */
7462/****************************************************************/
7463
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007464#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007465psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007466 void (* entropy_init)(mbedtls_entropy_context *ctx),
7467 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007468{
Paul Elliott8e151532024-02-23 17:35:29 +00007469 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7470
7471#if defined(MBEDTLS_THREADING_C)
7472 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
7473#endif /* defined(MBEDTLS_THREADING_C) */
7474
Gilles Peskine449bd832023-01-11 14:50:10 +01007475 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
Paul Elliott8e151532024-02-23 17:35:29 +00007476 status = PSA_ERROR_BAD_STATE;
7477 } else {
7478 global_data.rng.entropy_init = entropy_init;
7479 global_data.rng.entropy_free = entropy_free;
7480 status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01007481 }
Paul Elliott8e151532024-02-23 17:35:29 +00007482
7483#if defined(MBEDTLS_THREADING_C)
7484 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
7485#endif /* defined(MBEDTLS_THREADING_C) */
7486
7487 return status;
Gilles Peskine5e769522018-11-20 21:59:56 +01007488}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007489#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007490
Gilles Peskine449bd832023-01-11 14:50:10 +01007491void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007492{
Paul Elliott600472b2024-02-23 17:26:58 +00007493 /* Need to hold the mutex here to prevent this going ahead before
7494 * psa_crypto_init() has completed, and to ensure integrity of
7495 * global_data. */
7496#if defined(MBEDTLS_THREADING_C)
7497 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
7498#endif /* defined(MBEDTLS_THREADING_C) */
7499
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 psa_wipe_all_key_slots();
Valerio Setti83e0de82023-11-24 12:13:05 +01007501 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7502 mbedtls_psa_random_free(&global_data.rng);
7503 }
Paul Elliott600472b2024-02-23 17:26:58 +00007504
Gilles Peskinec6b69072018-11-20 21:42:52 +01007505 /* Wipe all remaining data, including configuration.
7506 * In particular, this sets all state indicator to the value
7507 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007509
Paul Elliott600472b2024-02-23 17:26:58 +00007510#if defined(MBEDTLS_THREADING_C)
7511 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
7512#endif /* defined(MBEDTLS_THREADING_C) */
7513
Ronald Cron9ba76912021-04-10 16:57:30 +02007514 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007515 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007516}
7517
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007518#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7519/** Recover a transaction that was interrupted by a power failure.
7520 *
7521 * This function is called during initialization, before psa_crypto_init()
7522 * returns. If this function returns a failure status, the initialization
7523 * fails.
7524 */
7525static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007527{
Gilles Peskine449bd832023-01-11 14:50:10 +01007528 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007529 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7530 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 /* TODO - fall through to the failure case until this
7532 * is implemented.
7533 * https://github.com/ARMmbed/mbed-crypto/issues/218
7534 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007535 default:
7536 /* We found an unsupported transaction in the storage.
7537 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007539 }
7540}
7541#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7542
Gilles Peskine449bd832023-01-11 14:50:10 +01007543psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007544{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007545 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007546
Paul Elliott600472b2024-02-23 17:26:58 +00007547 /* Need to hold the mutex for the entire function to prevent incomplete
7548 * initialisation before someone calls psa_crypto_free() or calls this
7549 * function again before we set global_data.initialised to 1. */
7550 #if defined(MBEDTLS_THREADING_C)
7551 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
7552#endif /* defined(MBEDTLS_THREADING_C) */
7553
7554 /* We cannot use psa_get_initialized() here as we already hold the mutex. */
7555 if (global_data.initialized == 1) {
7556#if defined(MBEDTLS_THREADING_C)
7557 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
7558#endif /* defined(MBEDTLS_THREADING_C) */
7559
7560 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007561 return PSA_SUCCESS;
7562 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007563
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007564 /* Init drivers */
7565 status = psa_driver_wrapper_init();
7566 if (status != PSA_SUCCESS) {
7567 goto exit;
7568 }
7569 global_data.drivers_initialized = 1;
7570
Valerio Setti402cfba2023-11-13 10:24:32 +01007571 status = psa_initialize_key_slots();
7572 if (status != PSA_SUCCESS) {
7573 goto exit;
7574 }
7575
Gilles Peskine30524eb2020-11-13 17:02:26 +01007576 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007578 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 status = mbedtls_psa_random_seed(&global_data.rng);
7580 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007581 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007582 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007583 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007584
Gilles Peskine4b734222019-07-24 15:56:31 +02007585#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 status = psa_crypto_load_transaction();
7587 if (status == PSA_SUCCESS) {
7588 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7589 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007590 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 }
7592 status = psa_crypto_stop_transaction();
7593 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007594 /* There's no transaction to complete. It's all good. */
7595 status = PSA_SUCCESS;
7596 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007597#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007598
Gilles Peskinec6b69072018-11-20 21:42:52 +01007599 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007600 global_data.initialized = 1;
7601
7602exit:
Paul Elliott600472b2024-02-23 17:26:58 +00007603
7604#if defined(MBEDTLS_THREADING_C)
7605 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
7606#endif /* defined(MBEDTLS_THREADING_C) */
7607
Gilles Peskine449bd832023-01-11 14:50:10 +01007608 if (status != PSA_SUCCESS) {
7609 mbedtls_psa_crypto_free();
7610 }
7611 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007612}
7613
Yanray Wangffc3c482023-07-11 12:01:04 +08007614#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007615psa_status_t psa_crypto_driver_pake_get_password_len(
7616 const psa_crypto_driver_pake_inputs_t *inputs,
7617 size_t *password_len)
7618{
7619 if (inputs->password_len == 0) {
7620 return PSA_ERROR_BAD_STATE;
7621 }
7622
7623 *password_len = inputs->password_len;
7624
7625 return PSA_SUCCESS;
7626}
7627
7628psa_status_t psa_crypto_driver_pake_get_password(
7629 const psa_crypto_driver_pake_inputs_t *inputs,
7630 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7631{
7632 if (inputs->password_len == 0) {
7633 return PSA_ERROR_BAD_STATE;
7634 }
7635
7636 if (buffer_size < inputs->password_len) {
7637 return PSA_ERROR_BUFFER_TOO_SMALL;
7638 }
7639
7640 memcpy(buffer, inputs->password, inputs->password_len);
7641 *buffer_length = inputs->password_len;
7642
7643 return PSA_SUCCESS;
7644}
7645
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007646psa_status_t psa_crypto_driver_pake_get_user_len(
7647 const psa_crypto_driver_pake_inputs_t *inputs,
7648 size_t *user_len)
7649{
7650 if (inputs->user_len == 0) {
7651 return PSA_ERROR_BAD_STATE;
7652 }
7653
7654 *user_len = inputs->user_len;
7655
7656 return PSA_SUCCESS;
7657}
7658
7659psa_status_t psa_crypto_driver_pake_get_user(
7660 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007661 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007662{
7663 if (inputs->user_len == 0) {
7664 return PSA_ERROR_BAD_STATE;
7665 }
7666
Przemek Stekielc0e62502023-03-14 11:49:36 +01007667 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007668 return PSA_ERROR_BUFFER_TOO_SMALL;
7669 }
7670
Przemek Stekielc0e62502023-03-14 11:49:36 +01007671 memcpy(user_id, inputs->user, inputs->user_len);
7672 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007673
7674 return PSA_SUCCESS;
7675}
7676
7677psa_status_t psa_crypto_driver_pake_get_peer_len(
7678 const psa_crypto_driver_pake_inputs_t *inputs,
7679 size_t *peer_len)
7680{
7681 if (inputs->peer_len == 0) {
7682 return PSA_ERROR_BAD_STATE;
7683 }
7684
7685 *peer_len = inputs->peer_len;
7686
7687 return PSA_SUCCESS;
7688}
7689
7690psa_status_t psa_crypto_driver_pake_get_peer(
7691 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007692 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007693{
7694 if (inputs->peer_len == 0) {
7695 return PSA_ERROR_BAD_STATE;
7696 }
7697
Przemek Stekielc0e62502023-03-14 11:49:36 +01007698 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007699 return PSA_ERROR_BUFFER_TOO_SMALL;
7700 }
7701
Przemek Stekielc0e62502023-03-14 11:49:36 +01007702 memcpy(peer_id, inputs->peer, inputs->peer_len);
7703 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007704
7705 return PSA_SUCCESS;
7706}
7707
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007708psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7709 const psa_crypto_driver_pake_inputs_t *inputs,
7710 psa_pake_cipher_suite_t *cipher_suite)
7711{
7712 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7713 return PSA_ERROR_BAD_STATE;
7714 }
7715
7716 *cipher_suite = inputs->cipher_suite;
7717
7718 return PSA_SUCCESS;
7719}
7720
Neil Armstronga7d08c32022-06-01 18:21:20 +02007721psa_status_t psa_pake_setup(
7722 psa_pake_operation_t *operation,
7723 const psa_pake_cipher_suite_t *cipher_suite)
7724{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007725 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007726
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007727 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007728 status = PSA_ERROR_BAD_STATE;
7729 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007730 }
7731
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007732 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007733 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007734 status = PSA_ERROR_INVALID_ARGUMENT;
7735 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007736 }
7737
Przemek Stekiel51eac532022-12-07 11:04:51 +01007738 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7739
Przemek Stekiele12ed362022-12-21 12:54:46 +01007740 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007741 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7742 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007743 operation->data.inputs.cipher_suite = *cipher_suite;
7744
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007745#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007746 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007747 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007748 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007749
David Horstmann16f01512023-06-14 17:21:07 +01007750 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007751 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007752 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007753#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007754 {
7755 status = PSA_ERROR_NOT_SUPPORTED;
7756 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007757 }
7758
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007759 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7760
Przemek Stekiel51eac532022-12-07 11:04:51 +01007761 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007762exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007763 psa_pake_abort(operation);
7764 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007765}
7766
7767psa_status_t psa_pake_set_password_key(
7768 psa_pake_operation_t *operation,
7769 mbedtls_svc_key_id_t password)
7770{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007771 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007772 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7773 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007774 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007775
Przemek Stekiel51eac532022-12-07 11:04:51 +01007776 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007777 status = PSA_ERROR_BAD_STATE;
7778 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007779 }
7780
Przemek Stekiel6c764412022-11-22 14:05:12 +01007781 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7782 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007783 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007784 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007785 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007786 }
7787
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007788 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007789
Przemek Stekiel51eac532022-12-07 11:04:51 +01007790 if (type != PSA_KEY_TYPE_PASSWORD &&
7791 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7792 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007793 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007794 }
7795
Przemek Stekiel51eac532022-12-07 11:04:51 +01007796 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7797 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007798 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007799 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007800 }
7801
7802 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7803 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01007804 operation->data.inputs.attributes = slot->attr;
7805
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007806exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007807 if (status != PSA_SUCCESS) {
7808 psa_pake_abort(operation);
7809 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00007810 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007811 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007812}
7813
7814psa_status_t psa_pake_set_user(
7815 psa_pake_operation_t *operation,
7816 const uint8_t *user_id,
7817 size_t user_id_len)
7818{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007819 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007820
7821 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007822 status = PSA_ERROR_BAD_STATE;
7823 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007824 }
7825
Przemek Stekiel51eac532022-12-07 11:04:51 +01007826 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007827 status = PSA_ERROR_INVALID_ARGUMENT;
7828 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007829 }
7830
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007831 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007832 status = PSA_ERROR_BAD_STATE;
7833 goto exit;
7834 }
7835
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007836 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7837 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007838 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7839 goto exit;
7840 }
7841
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007842 memcpy(operation->data.inputs.user, user_id, user_id_len);
7843 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007844
7845 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007846exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007847 psa_pake_abort(operation);
7848 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007849}
7850
7851psa_status_t psa_pake_set_peer(
7852 psa_pake_operation_t *operation,
7853 const uint8_t *peer_id,
7854 size_t peer_id_len)
7855{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007856 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007857
7858 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007859 status = PSA_ERROR_BAD_STATE;
7860 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007861 }
7862
Przemek Stekiel51eac532022-12-07 11:04:51 +01007863 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007864 status = PSA_ERROR_INVALID_ARGUMENT;
7865 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007866 }
7867
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007868 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007869 status = PSA_ERROR_BAD_STATE;
7870 goto exit;
7871 }
7872
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007873 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7874 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007875 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7876 goto exit;
7877 }
7878
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007879 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7880 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007881
7882 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007883exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007884 psa_pake_abort(operation);
7885 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007886}
7887
7888psa_status_t psa_pake_set_role(
7889 psa_pake_operation_t *operation,
7890 psa_pake_role_t role)
7891{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007892 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007893
Przemek Stekiel51eac532022-12-07 11:04:51 +01007894 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007895 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007896 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007897 }
7898
Przemek Stekiel09104b82023-03-06 14:11:51 +01007899 switch (operation->alg) {
7900#if defined(PSA_WANT_ALG_JPAKE)
7901 case PSA_ALG_JPAKE:
7902 if (role == PSA_PAKE_ROLE_NONE) {
7903 return PSA_SUCCESS;
7904 }
7905 status = PSA_ERROR_INVALID_ARGUMENT;
7906 break;
7907#endif
7908 default:
7909 (void) role;
7910 status = PSA_ERROR_NOT_SUPPORTED;
7911 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007912 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007913exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007914 psa_pake_abort(operation);
7915 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007916}
7917
David Horstmanne7f21e62023-05-12 18:17:21 +01007918/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007919#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007920static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007921 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007922{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007923 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007924 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007925 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007926
David Horstmann024e5c52023-06-14 15:48:21 +01007927 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007928 is_x1 = (stage->outputs < 1);
7929 } else {
7930 is_x1 = (stage->inputs < 1);
7931 }
7932
David Horstmann74a3d8c2023-06-14 18:28:19 +01007933 key_share_step = is_x1 ?
7934 PSA_JPAKE_X1_STEP_KEY_SHARE :
7935 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007936 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007937 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7938 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7939 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7940 } else {
7941 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007942 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01007943 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01007944}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007945#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007946
Przemek Stekiel2797d372022-12-22 11:19:22 +01007947static psa_status_t psa_pake_complete_inputs(
7948 psa_pake_operation_t *operation)
7949{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007950 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007951 /* Create copy of the inputs on stack as inputs share memory
7952 with the driver context which will be setup by the driver. */
7953 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007954
Przemek Stekielfde11282023-03-13 16:06:09 +01007955 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007956 return PSA_ERROR_BAD_STATE;
7957 }
7958
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007959 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007960 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7961 return PSA_ERROR_BAD_STATE;
7962 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007963 }
7964
Przemek Stekiel18620a32023-01-17 16:34:52 +01007965 /* Clear driver context */
7966 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7967
7968 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007969
7970 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01007971 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007972
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007973 /* User and peer are translated to role. */
7974 mbedtls_free(inputs.user);
7975 mbedtls_free(inputs.peer);
7976
Przemek Stekiel2797d372022-12-22 11:19:22 +01007977 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007978#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007979 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007980 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007981 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007982#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007983 {
7984 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007985 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007986 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007987 return status;
7988}
7989
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007990#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01007991static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01007992 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01007993 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01007994 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007995{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007996 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7997 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7998 step != PSA_PAKE_STEP_ZK_PROOF) {
7999 return PSA_ERROR_INVALID_ARGUMENT;
8000 }
8001
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008002 psa_jpake_computation_stage_t *computation_stage =
8003 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008004
David Horstmann5da95602023-06-08 15:37:12 +01008005 if (computation_stage->round != PSA_JPAKE_FIRST &&
8006 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008007 return PSA_ERROR_BAD_STATE;
8008 }
8009
David Horstmanne7f21e62023-05-12 18:17:21 +01008010 /* Check that the step we are given is the one we were expecting */
8011 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008012 return PSA_ERROR_BAD_STATE;
8013 }
8014
David Horstmanne7f21e62023-05-12 18:17:21 +01008015 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8016 computation_stage->inputs == 0 &&
8017 computation_stage->outputs == 0) {
8018 /* Start of the round, so function decides whether we are inputting
8019 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008020 computation_stage->io_mode = io_mode;
8021 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008022 /* Middle of the round so the mode we are in must match the function
8023 * called by the user */
8024 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008025 }
8026
8027 return PSA_SUCCESS;
8028}
8029
David Horstmanne7f21e62023-05-12 18:17:21 +01008030static psa_status_t psa_jpake_epilogue(
8031 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008032 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008033{
David Horstmanne7f21e62023-05-12 18:17:21 +01008034 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008035 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008036
David Horstmanne7f21e62023-05-12 18:17:21 +01008037 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8038 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008039 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008040 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008041 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008042 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008043 }
8044 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008045 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008046 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008047 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008048 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008049 }
8050 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008051 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8052 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008053 /* End of a round, move to the next round */
8054 stage->inputs = 0;
8055 stage->outputs = 0;
8056 stage->round++;
8057 }
8058 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008059 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008060 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008061 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008062 return PSA_SUCCESS;
8063}
David Horstmanne7f21e62023-05-12 18:17:21 +01008064
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008065#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008066
Neil Armstronga7d08c32022-06-01 18:21:20 +02008067psa_status_t psa_pake_output(
8068 psa_pake_operation_t *operation,
8069 psa_pake_step_t step,
8070 uint8_t *output,
8071 size_t output_size,
8072 size_t *output_length)
8073{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008074 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008075 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008076 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008077
8078 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008079 status = psa_pake_complete_inputs(operation);
8080 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008081 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008082 }
8083 }
8084
8085 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008086 status = PSA_ERROR_BAD_STATE;
8087 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008088 }
8089
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008090 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008091 status = PSA_ERROR_INVALID_ARGUMENT;
8092 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008093 }
8094
Przemek Stekiele12ed362022-12-21 12:54:46 +01008095 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008096#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008097 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008098 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008099 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008100 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008101 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008102 driver_step = convert_jpake_computation_stage_to_driver_step(
8103 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008104 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008105#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008106 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008107 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008108 status = PSA_ERROR_NOT_SUPPORTED;
8109 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008110 }
8111
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008112 status = psa_driver_wrapper_pake_output(operation, driver_step,
8113 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008114
8115 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008116 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008117 }
8118
8119 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008120#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008121 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008122 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008123 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008124 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008125 }
8126 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008127#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008128 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008129 status = PSA_ERROR_NOT_SUPPORTED;
8130 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008131 }
8132
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008133 return PSA_SUCCESS;
8134exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008135 psa_pake_abort(operation);
8136 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008137}
8138
8139psa_status_t psa_pake_input(
8140 psa_pake_operation_t *operation,
8141 psa_pake_step_t step,
8142 const uint8_t *input,
8143 size_t input_length)
8144{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008145 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008146 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008147 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8148 operation->primitive,
8149 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008150
8151 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008152 status = psa_pake_complete_inputs(operation);
8153 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008154 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008155 }
8156 }
8157
8158 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008159 status = PSA_ERROR_BAD_STATE;
8160 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008161 }
8162
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008163 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008164 status = PSA_ERROR_INVALID_ARGUMENT;
8165 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008166 }
8167
Przemek Stekiele12ed362022-12-21 12:54:46 +01008168 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008169#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008170 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008171 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008172 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008173 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008174 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008175 driver_step = convert_jpake_computation_stage_to_driver_step(
8176 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008177 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008178#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008179 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008180 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008181 status = PSA_ERROR_NOT_SUPPORTED;
8182 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008183 }
8184
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008185 status = psa_driver_wrapper_pake_input(operation, driver_step,
8186 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008187
8188 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008189 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008190 }
8191
8192 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008193#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008194 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008195 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008196 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008197 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008198 }
8199 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008200#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008201 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008202 status = PSA_ERROR_NOT_SUPPORTED;
8203 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008204 }
8205
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008206 return PSA_SUCCESS;
8207exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008208 psa_pake_abort(operation);
8209 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008210}
8211
8212psa_status_t psa_pake_get_implicit_key(
8213 psa_pake_operation_t *operation,
8214 psa_key_derivation_operation_t *output)
8215{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008216 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008217 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008218 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008219 size_t shared_key_len = 0;
8220
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008221 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008222 status = PSA_ERROR_BAD_STATE;
8223 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008224 }
8225
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008226#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008227 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008228 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008229 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008230 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008231 status = PSA_ERROR_BAD_STATE;
8232 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008233 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008234 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008235#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008236 {
8237 status = PSA_ERROR_NOT_SUPPORTED;
8238 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008239 }
8240
Przemek Stekiel0c781802022-11-29 14:53:13 +01008241 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8242 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008243 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008244 &shared_key_len);
8245
8246 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008247 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008248 }
8249
8250 status = psa_key_derivation_input_bytes(output,
8251 PSA_KEY_DERIVATION_INPUT_SECRET,
8252 shared_key,
8253 shared_key_len);
8254
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008255 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008256exit:
8257 abort_status = psa_pake_abort(operation);
8258 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008259}
8260
8261psa_status_t psa_pake_abort(
8262 psa_pake_operation_t *operation)
8263{
Przemek Stekield69dca92023-01-31 19:59:20 +01008264 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008265
Przemek Stekield69dca92023-01-31 19:59:20 +01008266 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008267 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008268 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008269
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008270 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8271 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008272 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008273 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008274 }
8275 if (operation->data.inputs.user != NULL) {
8276 mbedtls_free(operation->data.inputs.user);
8277 }
8278 if (operation->data.inputs.peer != NULL) {
8279 mbedtls_free(operation->data.inputs.peer);
8280 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008281 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008282 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008283
Przemek Stekield69dca92023-01-31 19:59:20 +01008284 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008285}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008286#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008287
Gilles Peskinee59236f2018-01-27 23:32:46 +01008288#endif /* MBEDTLS_PSA_CRYPTO_C */