blob: 3019522f299608776af44b49e214511b5c6f818b [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
itayzafrir0adf0fc2018-09-06 16:24:41 +0300122#define GUARD_MODULE_INITIALIZED \
Paul Elliott600472b2024-02-23 17:26:58 +0000123 if (psa_get_initialized() == 0) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300125
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100126int psa_can_do_hash(psa_algorithm_t hash_alg)
127{
128 (void) hash_alg;
129 return global_data.drivers_initialized;
130}
Valerio Settic6f004f2023-12-12 11:27:36 +0100131
Valerio Setti1fff4f22023-12-28 14:19:34 +0100132int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
Valerio Settic6f004f2023-12-12 11:27:36 +0100133{
Valerio Setti1fff4f22023-12-28 14:19:34 +0100134 (void) key_type;
Valerio Settic6f004f2023-12-12 11:27:36 +0100135 (void) cipher_alg;
136 return global_data.drivers_initialized;
137}
138
139
Valerio Settia55f0422023-07-10 15:34:41 +0200140#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200141 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200142 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200143static int psa_is_dh_key_size_valid(size_t bits)
144{
Valerio Setti504a1022024-01-17 15:19:30 +0100145 switch (bits) {
146#if defined(PSA_WANT_DH_RFC7919_2048)
147 case 2048:
148 return 1;
149#endif /* PSA_WANT_DH_RFC7919_2048 */
150#if defined(PSA_WANT_DH_RFC7919_3072)
151 case 3072:
152 return 1;
153#endif /* PSA_WANT_DH_RFC7919_3072 */
154#if defined(PSA_WANT_DH_RFC7919_4096)
155 case 4096:
156 return 1;
157#endif /* PSA_WANT_DH_RFC7919_4096 */
158#if defined(PSA_WANT_DH_RFC7919_6144)
159 case 6144:
160 return 1;
161#endif /* PSA_WANT_DH_RFC7919_6144 */
162#if defined(PSA_WANT_DH_RFC7919_8192)
163 case 8192:
164 return 1;
165#endif /* PSA_WANT_DH_RFC7919_8192 */
166 default:
167 return 0;
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200168 }
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200169}
Valerio Settia55f0422023-07-10 15:34:41 +0200170#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200171 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200172 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100175{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100176 /* Mbed TLS error codes can combine a high-level error code and a
177 * low-level error code. The low-level error usually reflects the
178 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 int low_level_ret = -(-ret & 0x007f);
180 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100181 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100183
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100184#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
186 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100188 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
189 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100190#endif
191
192#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200193 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
194 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
195 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
196 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
197 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200199 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200201 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100203#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200204
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100205#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000206 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100209#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100210
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100211#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100212 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100214 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100216#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100217
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100218#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200219 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100221#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200222
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100223#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200224 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200226 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100228#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200229
Dave Rodgman509b5672023-08-16 19:26:23 +0100230#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100231 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100233 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100239 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100241 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100243 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100245#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
248 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100249 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
250 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
254 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100256 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100258#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100259
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100260#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100261 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100263#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100264
Gilles Peskinee59236f2018-01-27 23:32:46 +0100265 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
266 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
267 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100269
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100270#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100271 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200273 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100275 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100277#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100278
Gilles Peskine82e57d12020-11-13 21:31:17 +0100279#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100281 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
282 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100283 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100285 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
286 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100288 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100290#endif
291
Dave Rodgmandea266f2023-08-31 11:52:43 +0100292#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100299#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100300 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100302#endif
303#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100304
Dave Rodgman509b5672023-08-16 19:26:23 +0100305#if defined(MBEDTLS_BIGNUM_C)
306#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100307 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100309#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100310 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100312 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100314 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100316 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100318 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100320 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100322 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100324#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100325
Dave Rodgman509b5672023-08-16 19:26:23 +0100326#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100327 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100329 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
330 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100332#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
333 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100334 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100336#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100337 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
338 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100340 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100342 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
343 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100345 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100347 case MBEDTLS_ERR_PK_INVALID_ALG:
348 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
349 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100351 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200353 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100355#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100356
Gilles Peskineff2d2002019-05-06 15:26:23 +0200357 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200359 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200361
Dave Rodgman509b5672023-08-16 19:26:23 +0100362#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100363 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100365 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100367 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100369 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100371 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
372 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100374 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100376 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100378 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100380#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200381
Dave Rodgman1dab4452023-09-01 09:59:51 +0100382#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300383 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300384 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300386 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300388 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300390 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
391 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300393 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100395 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100397
Dave Rodgman509b5672023-08-16 19:26:23 +0100398#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000399 case MBEDTLS_ERR_ECP_IN_PROGRESS:
400 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100401#endif
402#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000403
Janos Follatha13b9052019-11-22 12:48:59 +0000404 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300406
Gilles Peskinee59236f2018-01-27 23:32:46 +0100407 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100409 }
410}
411
Paul Elliottdc42ca82023-02-24 18:11:59 +0000412/**
413 * \brief For output buffers which contain "tags"
414 * (outputs that may be checked for validity like
415 * hashes, MACs and signatures), fill the unused
416 * part of the output buffer (the whole buffer on
417 * error, the trailing part on success) with
418 * something that isn't a valid tag (barring an
419 * attack on the tag and deliberately-crafted
420 * input), in case the caller doesn't check the
421 * return status properly.
422 *
423 * \param output_buffer Pointer to buffer to wipe. May not be NULL
424 * unless \p output_buffer_size is zero.
425 * \param status Status of function called to generate
426 * output_buffer originally
427 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
428 * could be NULL.
429 * \param output_buffer_length Length of data written to output_buffer, must be
430 * less than \p output_buffer_size
431 */
432static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000433 size_t output_buffer_size, size_t output_buffer_length)
434{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000435 size_t offset = 0;
436
437 if (output_buffer_size == 0) {
438 /* If output_buffer_size is 0 then we have nothing to do. We must not
439 call memset because output_buffer may be NULL in this case */
440 return;
441 }
442
443 if (status == PSA_SUCCESS) {
444 offset = output_buffer_length;
445 }
446
447 memset(output_buffer + offset, '!', output_buffer_size - offset);
448}
449
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
452 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200453{
454 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200456 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200457 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200458 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200459 case PSA_KEY_TYPE_PASSWORD:
460 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200461 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100462#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200463 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (bits != 128 && bits != 192 && bits != 256) {
465 return PSA_ERROR_INVALID_ARGUMENT;
466 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200467 break;
468#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200469#if defined(PSA_WANT_KEY_TYPE_ARIA)
470 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (bits != 128 && bits != 192 && bits != 256) {
472 return PSA_ERROR_INVALID_ARGUMENT;
473 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200474 break;
475#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100476#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 if (bits != 128 && bits != 192 && bits != 256) {
479 return PSA_ERROR_INVALID_ARGUMENT;
480 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481 break;
482#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100483#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200484 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (bits != 64 && bits != 128 && bits != 192) {
486 return PSA_ERROR_INVALID_ARGUMENT;
487 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200488 break;
489#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100490#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200491 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if (bits != 256) {
493 return PSA_ERROR_INVALID_ARGUMENT;
494 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200495 break;
496#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (bits % 8 != 0) {
501 return PSA_ERROR_INVALID_ARGUMENT;
502 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200505}
506
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100507/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100508 *
Steven Cooremancd640932021-03-08 12:00:27 +0100509 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
510 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100511 *
512 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100513 * \param[in] key_type The key type of the key to be used with the
514 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100515 *
516 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100517 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100518 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100519 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100520 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100521MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100522 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100524{
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (PSA_ALG_IS_HMAC(algorithm)) {
526 if (key_type == PSA_KEY_TYPE_HMAC) {
527 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100528 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100529 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
532 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
533 * key. */
534 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
535 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
536 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
537 * the block length (larger than 1) for block ciphers. */
538 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
539 return PSA_SUCCESS;
540 }
541 }
542 }
543
544 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100545}
546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
548 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200549{
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (slot->key.data != NULL) {
551 return PSA_ERROR_ALREADY_EXISTS;
552 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 slot->key.data = mbedtls_calloc(1, buffer_length);
555 if (slot->key.data == NULL) {
556 return PSA_ERROR_INSUFFICIENT_MEMORY;
557 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200558
Ronald Cronea0f8a62020-11-25 17:52:23 +0100559 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200561}
562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
564 const uint8_t *data,
565 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200566{
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 psa_status_t status = psa_allocate_buffer_to_slot(slot,
568 data_length);
569 if (status != PSA_SUCCESS) {
570 return status;
571 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 memcpy(slot->key.data, data, data_length);
574 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200575}
576
Ronald Crona0fe59f2020-11-29 11:14:53 +0100577psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100578 const psa_key_attributes_t *attributes,
579 const uint8_t *data, size_t data_length,
580 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100582{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100583 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100584 psa_key_type_t type = attributes->type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100585
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200586 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (data_length == 0) {
588 return PSA_ERROR_NOT_SUPPORTED;
589 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if (key_type_is_raw_bytes(type)) {
592 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200593
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100594 status = psa_validate_unstructured_key_bit_size(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 *bits);
596 if (status != PSA_SUCCESS) {
597 return status;
598 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200599
Ronald Crondd04d422020-11-28 15:14:42 +0100600 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100602 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 return PSA_SUCCESS;
606 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200607#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200608 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100609 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200610 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Valerio Setti504a1022024-01-17 15:19:30 +0100611 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100612 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200613 return mbedtls_psa_ffdh_import_key(attributes,
614 data, data_length,
615 key_buffer, key_buffer_size,
616 key_buffer_length,
617 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100618 }
Valerio Settia55f0422023-07-10 15:34:41 +0200619#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200620 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200621#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
623 if (PSA_KEY_TYPE_IS_ECC(type)) {
624 return mbedtls_psa_ecp_import_key(attributes,
625 data, data_length,
626 key_buffer, key_buffer_size,
627 key_buffer_length,
628 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100629 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200630#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800631 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200632#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
633 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
635 if (PSA_KEY_TYPE_IS_RSA(type)) {
636 return mbedtls_psa_rsa_import_key(attributes,
637 data, data_length,
638 key_buffer, key_buffer_size,
639 key_buffer_length,
640 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200641 }
Valerio Settife478902023-07-25 12:27:19 +0200642#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
643 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800644 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100645 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100648}
649
Gilles Peskinef603c712019-01-19 13:40:11 +0100650/** Calculate the intersection of two algorithm usage policies.
651 *
652 * Return 0 (which allows no operation) on incompatibility.
653 */
654static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100655 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100656 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100658{
Gilles Peskine549ea862019-05-22 11:45:59 +0200659 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (alg1 == alg2) {
661 return alg1;
662 }
Steven Cooreman03488022021-02-18 12:07:20 +0100663 /* If the policies are from the same hash-and-sign family, check
664 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
666 PSA_ALG_IS_SIGN_HASH(alg2) &&
667 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
668 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
669 return alg2;
670 }
671 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
672 return alg1;
673 }
Steven Cooreman03488022021-02-18 12:07:20 +0100674 }
675 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100676 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100677 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
679 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
680 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
681 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
682 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100683 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100684
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100685 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
687 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
688 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
689 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100690 }
691 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
693 (alg1_len <= alg2_len)) {
694 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100695 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
697 (alg2_len <= alg1_len)) {
698 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100699 }
700 }
701 /* If the policies are from the same MAC family, check whether one
702 * of them is a minimum-MAC-length policy. Calculate the most
703 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
705 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
706 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100707 /* Validate the combination of key type and algorithm. Since the base
708 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
710 return 0;
711 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100712
Steven Cooreman31a876d2021-03-03 20:47:40 +0100713 /* Get the (exact or at-least) output lengths for both sides of the
714 * requested intersection. None of the currently supported algorithms
715 * have an output length dependent on the actual key size, so setting it
716 * to a bogus value of 0 is currently OK.
717 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100718 * Note that for at-least-this-length wildcard algorithms, the output
719 * length is set to the shortest allowed length, which allows us to
720 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
722 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100723 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100724
Steven Cooremana1d83222021-02-25 10:20:29 +0100725 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
727 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
728 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100729 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100730
731 /* If only one is an at-least-this-length policy, the intersection would
732 * be the other (fixed-length) policy as long as said fixed length is
733 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
735 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100736 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
738 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100739 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100740
Steven Cooremancd640932021-03-08 12:00:27 +0100741 /* If none of them are wildcards, check whether they define the same tag
742 * length. This is still possible here when one is default-length and
743 * the other specific-length. Ensure to always return the
744 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (alg1_len == alg2_len) {
746 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
747 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100748 }
749 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100751}
752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753static int psa_key_algorithm_permits(psa_key_type_t key_type,
754 psa_algorithm_t policy_alg,
755 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200756{
Gilles Peskine549ea862019-05-22 11:45:59 +0200757 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if (requested_alg == policy_alg) {
759 return 1;
760 }
Steven Cooreman03488022021-02-18 12:07:20 +0100761 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
762 * and requested_alg is the same hash-and-sign family with any hash,
763 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
765 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
766 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
767 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100768 }
769 /* If policy_alg is a wildcard AEAD algorithm of the same base as
770 * the requested algorithm, check the requested tag length to be
771 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (PSA_ALG_IS_AEAD(policy_alg) &&
773 PSA_ALG_IS_AEAD(requested_alg) &&
774 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
775 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
776 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
777 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
778 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100779 }
Steven Cooremancd640932021-03-08 12:00:27 +0100780 /* If policy_alg is a MAC algorithm of the same base as the requested
781 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (PSA_ALG_IS_MAC(policy_alg) &&
783 PSA_ALG_IS_MAC(requested_alg) &&
784 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
785 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100786 /* Validate the combination of key type and algorithm. Since the policy
787 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
789 return 0;
790 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100791
Steven Cooreman31a876d2021-03-03 20:47:40 +0100792 /* Get both the requested output length for the algorithm which is to be
793 * verified, and the default output length for the base algorithm.
794 * Note that none of the currently supported algorithms have an output
795 * length dependent on actual key size, so setting it to a bogus value
796 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100797 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100799 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 key_type, 0,
801 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100802
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100803 /* If the policy is default-length, only allow an algorithm with
804 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
806 return requested_output_length == default_output_length;
807 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100808
809 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100810 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
812 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
813 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100814 }
815
Steven Cooremancd640932021-03-08 12:00:27 +0100816 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
817 * check for the requested MAC length to be equal to or longer than the
818 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
820 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
821 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100822 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200823 }
Steven Cooremance48e852020-10-05 16:02:45 +0200824 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200825 * a key derivation with that key agreement should also be allowed. This
826 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
828 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
829 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
830 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200831 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100832 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200834}
835
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100836/** Test whether a policy permits an algorithm.
837 *
838 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100839 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100840 * \note This function requires providing the key type for which the policy is
841 * being validated, since some algorithm policy definitions (e.g. MAC)
842 * have different properties depending on what kind of cipher it is
843 * combined with.
844 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100845 * \retval PSA_SUCCESS When \p alg is a specific algorithm
846 * allowed by the \p policy.
847 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
848 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
849 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100850 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
852 psa_key_type_t key_type,
853 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100854{
Steven Cooremand788fab2021-02-25 11:29:17 +0100855 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 if (alg == 0) {
857 return PSA_ERROR_INVALID_ARGUMENT;
858 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100859
Steven Cooreman7e39f052021-02-23 14:18:32 +0100860 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if (PSA_ALG_IS_WILDCARD(alg)) {
862 return PSA_ERROR_INVALID_ARGUMENT;
863 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
866 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
867 return PSA_SUCCESS;
868 } else {
869 return PSA_ERROR_NOT_PERMITTED;
870 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100871}
872
Gilles Peskinef603c712019-01-19 13:40:11 +0100873/** Restrict a key policy based on a constraint.
874 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100875 * \note This function requires providing the key type for which the policy is
876 * being restricted, since some algorithm policy definitions (e.g. MAC)
877 * have different properties depending on what kind of cipher it is
878 * combined with.
879 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100880 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100881 * \param[in,out] policy The policy to restrict.
882 * \param[in] constraint The policy constraint to apply.
883 *
884 * \retval #PSA_SUCCESS
885 * \c *policy contains the intersection of the original value of
886 * \c *policy and \c *constraint.
887 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100888 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100889 * \c *policy is unchanged.
890 */
891static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100892 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100893 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100895{
896 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 psa_key_policy_algorithm_intersection(key_type, policy->alg,
898 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200899 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
901 constraint->alg2);
902 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
903 return PSA_ERROR_INVALID_ARGUMENT;
904 }
905 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
906 return PSA_ERROR_INVALID_ARGUMENT;
907 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100908 policy->usage &= constraint->usage;
909 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200910 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100912}
913
Przemek Stekiel061f6942022-11-30 10:51:35 +0100914/** Get the description of a key given its identifier and policy constraints
915 * and lock it.
916 *
917 * The key must have allow all the usage flags set in \p usage. If \p alg is
918 * nonzero, the key must allow operations with this algorithm. If \p alg is
919 * zero, the algorithm is not checked.
920 *
921 * In case of a persistent key, the function loads the description of the key
922 * into a key slot if not already done.
923 *
Ryan Everett098c6652024-01-03 13:03:36 +0000924 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +0000925 * It is the responsibility of the caller to then unregister
926 * once they have finished reading the contents of the slot.
927 * The caller unregisters by calling psa_unregister_read() or
928 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
929 * if and only if the caller already holds the global key slot mutex
930 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
931 * the unregister with mutex lock and unlock operations.
Przemek Stekiel061f6942022-11-30 10:51:35 +0100932 */
933static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100934 mbedtls_svc_key_id_t key,
935 psa_key_slot_t **p_slot,
936 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100938{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200939 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100940 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 status = psa_get_and_lock_key_slot(key, p_slot);
943 if (status != PSA_SUCCESS) {
944 return status;
945 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200946 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100947
948 /* Enforce that usage policy for the key slot contains all the flags
949 * required by the usage parameter. There is one exception: public
950 * keys can always be exported, so we treat public key objects as
951 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100953 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100957 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200958 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100959 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100960
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800961 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (alg != 0) {
963 status = psa_key_policy_permits(&slot->attr.policy,
964 slot->attr.type,
965 alg);
966 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100967 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100969 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200972
973error:
974 *p_slot = NULL;
Ryan Everettfb792ca2024-01-31 13:40:05 +0000975 psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100978}
Darryl Green940d72c2018-07-13 13:18:51 +0100979
Ronald Cron5c522922020-11-14 16:35:34 +0100980/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200981 *
982 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200983 * available, as opposed to a key in a secure element and/or to be used
984 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200985 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200986 * This is a temporary function that may be used instead of
987 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
988 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200989 *
Ryan Everett098c6652024-01-03 13:03:36 +0000990 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +0000991 * It is the responsibility of the caller to then unregister
992 * once they have finished reading the contents of the slot.
993 * The caller unregisters by calling psa_unregister_read() or
994 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
995 * if and only if the caller already holds the global key slot mutex
996 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
997 * psa_unregister_read() with mutex lock and unlock operations.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200998 */
Ronald Cron5c522922020-11-14 16:35:34 +0100999static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1000 mbedtls_svc_key_id_t key,
1001 psa_key_slot_t **p_slot,
1002 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001004{
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1006 usage, alg);
1007 if (status != PSA_SUCCESS) {
1008 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001009 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everettfb792ca2024-01-31 13:40:05 +00001012 psa_unregister_read_under_mutex(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 *p_slot = NULL;
1014 return PSA_ERROR_NOT_SUPPORTED;
1015 }
1016
1017 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001018}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001021{
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001023 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001025
Ronald Cronea0f8a62020-11-25 17:52:23 +01001026 slot->key.data = NULL;
1027 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001030}
1031
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001032/** Completely wipe a slot in memory, including its policy.
1033 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001034psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001035{
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001037
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 /*
1039 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +00001040 * do our best to report an unexpected amount of registered readers or
1041 * an unexpected state.
1042 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1043 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1045 * function is called as part of the execution of a test suite, the
1046 * execution of the test suite is stopped in error if the assertion fails.
1047 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001048 switch (slot->state) {
1049 case PSA_SLOT_FULL:
1050 /* In this state psa_wipe_key_slot() must only be called if the
1051 * caller is the last reader. */
1052 case PSA_SLOT_PENDING_DELETION:
1053 /* In this state psa_wipe_key_slot() must only be called if the
1054 * caller is the last reader. */
1055 if (slot->registered_readers != 1) {
1056 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1);
1057 status = PSA_ERROR_CORRUPTION_DETECTED;
1058 }
1059 break;
1060 case PSA_SLOT_FILLING:
1061 /* In this state registered_readers must be 0. */
1062 if (slot->registered_readers != 0) {
1063 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0);
1064 status = PSA_ERROR_CORRUPTION_DETECTED;
1065 }
1066 break;
1067 case PSA_SLOT_EMPTY:
1068 /* The slot is already empty, it cannot be wiped. */
1069 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1070 status = PSA_ERROR_CORRUPTION_DETECTED;
1071 break;
1072 default:
1073 /* The slot's state is invalid. */
1074 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001075 }
1076
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001077 /* Multipart operations may still be using the key. This is safe
1078 * because all multipart operation objects are independent from
1079 * the key slot: if they need to access the key after the setup
1080 * phase, they have a copy of the key. Note that this means that
1081 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001082 /* At this point, key material and other type-specific content has
1083 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001084 * zeroize because the metadata is not particularly sensitive.
1085 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 memset(slot, 0, sizeof(*slot));
1087 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001088}
1089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001092 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001093 psa_status_t status; /* status of the last operation */
1094 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001095#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1096 psa_se_drv_table_entry_t *driver;
1097#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if (mbedtls_svc_key_id_is_null(key)) {
1100 return PSA_SUCCESS;
1101 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001102
Ronald Cronf2911112020-10-29 17:51:10 +01001103 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001104 * Get the description of the key in a key slot, and register to read it.
1105 * In the case of a persistent key, this will load the key description
1106 * from persistent memory if not done yet.
1107 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001108 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001109 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 status = psa_get_and_lock_key_slot(key, &slot);
1111 if (status != PSA_SUCCESS) {
1112 return status;
1113 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001114
Ryan Everettc053d962024-01-25 17:56:32 +00001115#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001116 /* We cannot unlock between setting the state to PENDING_DELETION
1117 * and destroying the key in storage, as otherwise another thread
1118 * could load the key into a new slot and the key will not be
1119 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001120 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1121 &mbedtls_threading_key_slot_mutex));
Ryan Everettd868b742024-03-08 18:35:09 +00001122
1123 if (slot->state == PSA_SLOT_PENDING_DELETION) {
1124 /* Another thread has destroyed the key between us locking the slot
1125 * and us gaining the mutex. Unregister from the slot,
1126 * and report that the key does not exist. */
1127 status = psa_unregister_read(slot);
1128
1129 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1130 &mbedtls_threading_key_slot_mutex));
1131 return (status == PSA_SUCCESS) ? PSA_ERROR_INVALID_HANDLE : status;
1132 }
Ryan Everettc053d962024-01-25 17:56:32 +00001133#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001134 /* Set the key slot containing the key description's state to
1135 * PENDING_DELETION. This stops new operations from registering
1136 * to read the slot. Current readers can safely continue to access
1137 * the key within the slot; the last registered reader will
1138 * automatically wipe the slot when they call psa_unregister_read().
1139 * If the key is persistent, we can now delete the copy of the key
1140 * from memory. If the key is opaque, we require the driver to
1141 * deal with the deletion. */
Ryan Everettd868b742024-03-08 18:35:09 +00001142 overall_status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1143 PSA_SLOT_PENDING_DELETION);
Ryan Everettc053d962024-01-25 17:56:32 +00001144
Ryan Everettd868b742024-03-08 18:35:09 +00001145 if (overall_status != PSA_SUCCESS) {
Ryan Everettc053d962024-01-25 17:56:32 +00001146 goto exit;
1147 }
Ronald Cronf2911112020-10-29 17:51:10 +01001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001150 /* Refuse the destruction of a read-only key (which may or may not work
1151 * if we attempt it, depending on whether the key is merely read-only
1152 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001153 * Just do the best we can, which is to wipe the copy in memory
1154 * (done in this function's cleanup code). */
1155 overall_status = PSA_ERROR_NOT_PERMITTED;
1156 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001157 }
1158
Gilles Peskine354f7672019-07-12 23:46:38 +02001159#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1161 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001162 /* For a key in a secure element, we need to do three things:
1163 * remove the key file in internal storage, destroy the
1164 * key inside the secure element, and update the driver's
1165 * persistent data. Start a transaction that will encompass these
1166 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001168 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001170 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 status = psa_crypto_save_transaction();
1172 if (status != PSA_SUCCESS) {
1173 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001174 /* We should still try to destroy the key in the secure
1175 * element and the key metadata in storage. This is especially
1176 * important if the error is that the storage is full.
1177 * But how to do it exactly without risking an inconsistent
1178 * state after a reset?
1179 * https://github.com/ARMmbed/mbed-crypto/issues/215
1180 */
1181 overall_status = status;
1182 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001183 }
1184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 status = psa_destroy_se_key(driver,
1186 psa_key_slot_get_slot_number(slot));
1187 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001188 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001190 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001191#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1192
Darryl Greend49a4992018-06-18 17:27:26 +01001193#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001195 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001196 * The slot will still hold a copy of the key until the last reader
1197 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 status = psa_destroy_persistent_key(slot->attr.id);
1199 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001200 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 }
Darryl Greend49a4992018-06-18 17:27:26 +01001202 }
1203#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001204
Gilles Peskinefc762652019-07-22 19:30:34 +02001205#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (driver != NULL) {
1207 status = psa_save_se_persistent_data(driver);
1208 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001209 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 }
1211 status = psa_crypto_stop_transaction();
1212 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001213 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001215 }
1216#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1217
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001218exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001219 /* Unregister from reading the slot. If we are the last active reader
1220 * then this will wipe the slot. */
1221 status = psa_unregister_read(slot);
1222 /* Prioritize CORRUPTION_DETECTED from unregistering over
1223 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001225 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 }
Ryan Everettc053d962024-01-25 17:56:32 +00001227
1228#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001229 /* Don't overwrite existing errors if the unlock fails. */
1230 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001231 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1232 &mbedtls_threading_key_slot_mutex));
1233#endif
1234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001236}
1237
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001238/** Retrieve all the publicly-accessible attributes of a key.
1239 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001240psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1241 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001242{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001243 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001244 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001247
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1249 if (status != PSA_SUCCESS) {
1250 return status;
1251 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001252
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001253 *attributes = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001254
1255#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1257 psa_set_key_slot_number(attributes,
1258 psa_key_slot_get_slot_number(slot));
1259 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001261
Gilles Peskine97c0b2f2024-02-16 00:49:46 +01001262 return psa_unregister_read_under_mutex(slot);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001263}
1264
Gilles Peskinec8000c02019-08-02 20:15:51 +02001265#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1266psa_status_t psa_get_key_slot_number(
1267 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001269{
Gilles Peskine972539c2024-02-28 01:49:45 +01001270 if (attributes->has_slot_number) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001271 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 return PSA_SUCCESS;
1273 } else {
1274 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001275 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001276}
1277#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1280 size_t key_buffer_size,
1281 uint8_t *data,
1282 size_t data_size,
1283 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001284{
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if (key_buffer_size > data_size) {
1286 return PSA_ERROR_BUFFER_TOO_SMALL;
1287 }
1288 memcpy(data, key_buffer, key_buffer_size);
1289 memset(data + key_buffer_size, 0,
1290 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001291 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001293}
1294
Ronald Cron7285cda2020-11-26 14:46:37 +01001295psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001296 const psa_key_attributes_t *attributes,
1297 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001299{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001300 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (key_type_is_raw_bytes(type) ||
1303 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001304 PSA_KEY_TYPE_IS_ECC(type) ||
1305 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 return psa_export_key_buffer_internal(
1307 key_buffer, key_buffer_size,
1308 data, data_size, data_length);
1309 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001310 /* This shouldn't happen in the reference implementation, but
1311 it is valid for a special-purpose implementation to omit
1312 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001314 }
1315}
1316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1318 uint8_t *data,
1319 size_t data_size,
1320 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001321{
1322 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1323 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1324 psa_key_slot_t *slot;
1325
Ronald Crone907e552021-01-18 13:22:38 +01001326 /* Reject a zero-length output buffer now, since this can never be a
1327 * valid key representation. This way we know that data must be a valid
1328 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 if (data_size == 0) {
1330 return PSA_ERROR_BUFFER_TOO_SMALL;
1331 }
Ronald Crone907e552021-01-18 13:22:38 +01001332
Ronald Cron9486f9d2020-11-26 13:36:23 +01001333 /* Set the key to empty now, so that even when there are errors, we always
1334 * set data_length to a value between 0 and data_size. On error, setting
1335 * the key to empty is a good choice because an empty key representation is
1336 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001337 *data_length = 0;
1338
Ronald Cron9486f9d2020-11-26 13:36:23 +01001339 /* Export requires the EXPORT flag. There is an exception for public keys,
1340 * which don't require any flag, but
1341 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1344 PSA_KEY_USAGE_EXPORT, 0);
1345 if (status != PSA_SUCCESS) {
1346 return status;
1347 }
mohammad160306e79202018-03-28 13:17:44 +03001348
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001349 status = psa_driver_wrapper_export_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 slot->key.data, slot->key.bytes,
1351 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001352
Ryan Everetta103ec92024-01-31 13:59:57 +00001353 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001356}
1357
Ronald Cron7285cda2020-11-26 14:46:37 +01001358psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001359 const psa_key_attributes_t *attributes,
1360 const uint8_t *key_buffer,
1361 size_t key_buffer_size,
1362 uint8_t *data,
1363 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001365{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001366 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001367
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001368 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001369 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1370 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001371 /* Exporting public -> public */
1372 return psa_export_key_buffer_internal(
1373 key_buffer, key_buffer_size,
1374 data, data_size, data_length);
1375 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001376#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001377 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1378 return mbedtls_psa_rsa_export_public_key(attributes,
1379 key_buffer,
1380 key_buffer_size,
1381 data,
1382 data_size,
1383 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001384#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001385 /* We don't know how to convert a private RSA key to public. */
1386 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001387#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001388 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001389 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001390#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001391 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1392 return mbedtls_psa_ecp_export_public_key(attributes,
1393 key_buffer,
1394 key_buffer_size,
1395 data,
1396 data_size,
1397 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001398#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001399 /* We don't know how to convert a private ECC key to public */
1400 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001401#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001402 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001403 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001404#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001405 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001406 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001407 key_buffer,
1408 key_buffer_size,
1409 data, data_size,
1410 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001411#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001412 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001413#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001414 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001416 (void) key_buffer;
1417 (void) key_buffer_size;
1418 (void) data;
1419 (void) data_size;
1420 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001422 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001423}
Ronald Crond18b5f82020-11-25 16:46:05 +01001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1426 uint8_t *data,
1427 size_t data_size,
1428 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001429{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001430 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001431 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001432 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001433
Ronald Crone907e552021-01-18 13:22:38 +01001434 /* Reject a zero-length output buffer now, since this can never be a
1435 * valid key representation. This way we know that data must be a valid
1436 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 if (data_size == 0) {
1438 return PSA_ERROR_BUFFER_TOO_SMALL;
1439 }
Ronald Crone907e552021-01-18 13:22:38 +01001440
Darryl Greendd8fb772018-11-07 16:00:44 +00001441 /* Set the key to empty now, so that even when there are errors, we always
1442 * set data_length to a value between 0 and data_size. On error, setting
1443 * the key to empty is a good choice because an empty key representation is
1444 * unlikely to be accepted anywhere. */
1445 *data_length = 0;
1446
1447 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1449 if (status != PSA_SUCCESS) {
1450 return status;
1451 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1454 status = PSA_ERROR_INVALID_ARGUMENT;
1455 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001456 }
1457
Ronald Cron67227982020-11-26 15:16:05 +01001458 status = psa_driver_wrapper_export_public_key(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001459 &slot->attr, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001461
1462exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001463 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001466}
1467
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001468/** Validate that a key policy is internally well-formed.
1469 *
1470 * This function only rejects invalid policies. It does not validate the
1471 * consistency of the policy with respect to other attributes of the key
1472 * such as the key type.
1473 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001475{
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1477 PSA_KEY_USAGE_COPY |
1478 PSA_KEY_USAGE_ENCRYPT |
1479 PSA_KEY_USAGE_DECRYPT |
1480 PSA_KEY_USAGE_SIGN_MESSAGE |
1481 PSA_KEY_USAGE_VERIFY_MESSAGE |
1482 PSA_KEY_USAGE_SIGN_HASH |
1483 PSA_KEY_USAGE_VERIFY_HASH |
1484 PSA_KEY_USAGE_VERIFY_DERIVATION |
1485 PSA_KEY_USAGE_DERIVE)) != 0) {
1486 return PSA_ERROR_INVALID_ARGUMENT;
1487 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001490}
1491
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001492/** Validate the internal consistency of key attributes.
1493 *
1494 * This function only rejects invalid attribute values. If does not
1495 * validate the consistency of the attributes with any key data that may
1496 * be involved in the creation of the key.
1497 *
1498 * Call this function early in the key creation process.
1499 *
1500 * \param[in] attributes Key attributes for the new key.
1501 * \param[out] p_drv On any return, the driver for the key, if any.
1502 * NULL for a transparent key.
1503 *
1504 */
1505static psa_status_t psa_validate_key_attributes(
1506 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001508{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001509 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1511 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 status = psa_validate_key_location(lifetime, p_drv);
1514 if (status != PSA_SUCCESS) {
1515 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001516 }
1517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 status = psa_validate_key_persistence(lifetime);
1519 if (status != PSA_SUCCESS) {
1520 return status;
1521 }
1522
1523 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1524 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1525 return PSA_ERROR_INVALID_ARGUMENT;
1526 }
1527 } else {
1528 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1529 return PSA_ERROR_INVALID_ARGUMENT;
1530 }
1531 }
1532
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001533 status = psa_validate_key_policy(&attributes->policy);
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 if (status != PSA_SUCCESS) {
1535 return status;
1536 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001537
1538 /* Refuse to create overly large keys.
1539 * Note that this doesn't trigger on import if the attributes don't
1540 * explicitly specify a size (so psa_get_key_bits returns 0), so
1541 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1543 return PSA_ERROR_NOT_SUPPORTED;
1544 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001547}
1548
Gilles Peskine4747d192019-04-17 15:05:45 +02001549/** Prepare a key slot to receive key material.
1550 *
1551 * This function allocates a key slot and sets its metadata.
1552 *
1553 * If this function fails, call psa_fail_key_creation().
1554 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001555 * This function is intended to be used as follows:
1556 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001557 * it with the specified attributes, and in case of a volatile key assign it
1558 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001559 * -# Populate the slot with the key material.
1560 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1561 * In case of failure at any step, stop the sequence and call
1562 * psa_fail_key_creation().
1563 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001564 * On success, the key slot's state is PSA_SLOT_FILLING.
1565 * It is the responsibility of the caller to change the slot's state to
1566 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001567 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001568 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001569 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001570 * \param[out] p_slot On success, a pointer to the prepared slot.
1571 * \param[out] p_drv On any return, the driver for the key, if any.
1572 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001573 *
1574 * \retval #PSA_SUCCESS
1575 * The key slot is ready to receive key material.
1576 * \return If this function fails, the key slot is an invalid state.
1577 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001578 */
1579static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001580 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001581 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001582 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001584{
1585 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001586 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001587 psa_key_slot_t *slot;
1588
Gilles Peskinedf179142019-07-15 22:02:14 +02001589 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001590 *p_drv = NULL;
1591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 status = psa_validate_key_attributes(attributes, p_drv);
1593 if (status != PSA_SUCCESS) {
1594 return status;
1595 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001596
Ryan Everett3d8118d2024-01-30 16:58:47 +00001597#if defined(MBEDTLS_THREADING_C)
1598 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1599 &mbedtls_threading_key_slot_mutex));
1600#endif
Ryan Everettb69118e2024-01-02 15:54:32 +00001601 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001602#if defined(MBEDTLS_THREADING_C)
1603 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1604 &mbedtls_threading_key_slot_mutex));
1605#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if (status != PSA_SUCCESS) {
1607 return status;
1608 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001609 slot = *p_slot;
1610
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001611 /* We're storing the declared bit-size of the key. It's up to each
1612 * creation mechanism to verify that this information is correct.
1613 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001614 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001615 * is optional (import, copy). In case of a volatile key, assign it the
1616 * volatile key identifier associated to the slot returned to contain its
1617 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001618
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001619 slot->attr = *attributes;
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001621#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1622 slot->attr.id = volatile_key_id;
1623#else
1624 slot->attr.id.key_id = volatile_key_id;
1625#endif
1626 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001627
Gilles Peskinecbaff462019-07-12 23:46:04 +02001628#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001629 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001630 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001631 * create the key file in internal storage, create the
1632 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001633 * persistent data. This is done by starting a transaction that will
1634 * encompass these three actions.
1635 * For registering a volatile key, we just need to find an appropriate
1636 * slot number inside the SE. Since the key is designated volatile, creating
1637 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001638 /* The first thing to do is to find a slot number for the new key.
1639 * We save the slot number in persistent storage as part of the
1640 * transaction data. It will be needed to recover if the power
1641 * fails during the key creation process, to clean up on the secure
1642 * element side after restarting. Obtaining a slot number from the
1643 * secure element driver updates its persistent state, but we do not yet
1644 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001645 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001647 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1649 &slot_number);
1650 if (status != PSA_SUCCESS) {
1651 return status;
1652 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001653
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001654 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001656 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001657 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001658 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 status = psa_crypto_save_transaction();
1660 if (status != PSA_SUCCESS) {
1661 (void) psa_crypto_stop_transaction();
1662 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001663 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001664 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001665
1666 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001668 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001671 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001673 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001674#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001677}
Gilles Peskine4747d192019-04-17 15:05:45 +02001678
1679/** Finalize the creation of a key once its key material has been set.
1680 *
1681 * This entails writing the key to persistent storage.
1682 *
1683 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001684 * See the documentation of psa_start_key_creation() for the intended use
1685 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001686 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001687 * If the finalization succeeds, the function sets the key slot's state to
1688 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1689 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001690 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001691 * \param[in,out] slot Pointer to the slot with key material.
1692 * \param[in] driver The secure element driver for the key,
1693 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001694 * \param[out] key On success, identifier of the key. Note that the
1695 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001696 *
1697 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001698 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001699 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1700 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1701 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1702 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1703 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1704 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001705 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001706 * \return If this function fails, the key slot is an invalid state.
1707 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001708 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001709static psa_status_t psa_finish_key_creation(
1710 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001711 psa_se_drv_table_entry_t *driver,
1712 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001713{
1714 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001715 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001716 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001717
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001718#if defined(MBEDTLS_THREADING_C)
1719 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1720 &mbedtls_threading_key_slot_mutex));
1721#endif
1722
Gilles Peskine4747d192019-04-17 15:05:45 +02001723#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001725#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001727 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001728 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001730
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001731 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1732 sizeof(data.slot_number),
1733 "Slot number size does not match psa_se_key_data_storage_t");
1734
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1736 status = psa_save_persistent_key(&slot->attr,
1737 (uint8_t *) &data,
1738 sizeof(data));
1739 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001740#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1741 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001742 /* Key material is saved in export representation in the slot, so
1743 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 status = psa_save_persistent_key(&slot->attr,
1745 slot->key.data,
1746 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001747 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001748 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001749#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1750
Gilles Peskinecbaff462019-07-12 23:46:04 +02001751#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001752 /* Finish the transaction for a key creation. This does not
1753 * happen when registering an existing key. Detect this case
1754 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001755 * creation of a persistent key in a secure element requires a transaction,
1756 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 if (driver != NULL &&
1758 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1759 status = psa_save_se_persistent_data(driver);
1760 if (status != PSA_SUCCESS) {
1761 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001762
1763#if defined(MBEDTLS_THREADING_C)
1764 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1765 &mbedtls_threading_key_slot_mutex));
1766#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001768 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001770 }
1771#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001774 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001775 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1776 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001778 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001780 }
Ronald Cron50972942020-11-14 11:28:25 +01001781
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001782#if defined(MBEDTLS_THREADING_C)
1783 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1784 &mbedtls_threading_key_slot_mutex));
1785#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001787}
1788
1789/** Abort the creation of a key.
1790 *
1791 * You may call this function after calling psa_start_key_creation(),
1792 * or after psa_finish_key_creation() fails. In other circumstances, this
1793 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001794 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001795 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001796 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001797 * \param[in,out] slot Pointer to the slot with key material.
1798 * \param[in] driver The secure element driver for the key,
1799 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001800 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001801static void psa_fail_key_creation(psa_key_slot_t *slot,
1802 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001803{
Gilles Peskine011e4282019-06-26 18:34:38 +02001804 (void) driver;
1805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001807 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001809
Ryan Everettb7101442024-01-23 20:09:49 +00001810#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00001811 /* If the lock operation fails we still wipe the slot.
1812 * Operations will no longer work after a failed lock,
1813 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00001814 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
1815#endif
1816
Gilles Peskine011e4282019-06-26 18:34:38 +02001817#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001818 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001819 * element, and the failure happened later (when saving metadata
1820 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001821 * element.
1822 * https://github.com/ARMmbed/mbed-crypto/issues/217
1823 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001824
Gilles Peskined7729582019-08-05 15:55:54 +02001825 /* Abort the ongoing transaction if any (there may not be one if
1826 * the creation process failed before starting one, or if the
1827 * key creation is a registration of a key in a secure element).
1828 * Earlier functions must already have done what it takes to undo any
1829 * partial creation. All that's left is to update the transaction data
1830 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001832#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1833
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00001835
1836#if defined(MBEDTLS_THREADING_C)
1837 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
1838#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02001839}
1840
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001841/** Validate optional attributes during key creation.
1842 *
1843 * Some key attributes are optional during key creation. If they are
1844 * specified in the attributes structure, check that they are consistent
1845 * with the data in the slot.
1846 *
1847 * This function should be called near the end of key creation, after
1848 * the slot in memory is fully populated but before saving persistent data.
1849 */
1850static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001851 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001853{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001854 if (attributes->type != 0) {
1855 if (attributes->type != slot->attr.type) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 return PSA_ERROR_INVALID_ARGUMENT;
1857 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001858 }
1859
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001860 if (attributes->bits != 0) {
1861 if (attributes->bits != slot->attr.bits) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 return PSA_ERROR_INVALID_ARGUMENT;
1863 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001864 }
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001867}
1868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1870 const uint8_t *data,
1871 size_t data_length,
1872 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001873{
1874 psa_status_t status;
1875 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001876 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001877 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301878 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001879
Ronald Cron81709fc2020-11-14 12:10:32 +01001880 *key = MBEDTLS_SVC_KEY_ID_INIT;
1881
Gilles Peskine0f84d622019-09-12 19:03:13 +02001882 /* Reject zero-length symmetric keys (including raw data key objects).
1883 * This also rejects any key which might be encoded as an empty string,
1884 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 if (data_length == 0) {
1886 return PSA_ERROR_INVALID_ARGUMENT;
1887 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001888
Archana449608b2021-09-08 15:36:05 +05301889 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 if (data_length > SIZE_MAX / 8) {
1891 return PSA_ERROR_NOT_SUPPORTED;
1892 }
Archana6ed4bda2021-08-04 10:47:15 +05301893
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1895 &slot, &driver);
1896 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001897 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001899
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001900 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301901 * storage ( thus not in the case of importing a key in a secure element
1902 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1903 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001905 if (psa_key_lifetime_is_external(attributes->lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301906 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 attributes, data, data_length, &storage_size);
1908 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301909 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 }
Archanad8a83dc2021-06-14 10:04:16 +05301911 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 status = psa_allocate_buffer_to_slot(slot, storage_size);
1913 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001914 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001916 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001917
1918 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 status = psa_driver_wrapper_import_key(attributes,
1920 data, data_length,
1921 slot->key.data,
1922 slot->key.bytes,
1923 &slot->key.bytes, &bits);
1924 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001925 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001929 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001931 status = PSA_ERROR_INVALID_ARGUMENT;
1932 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001933 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001934
Archana6ed4bda2021-08-04 10:47:15 +05301935 /* Enforce a size limit, and in particular ensure that the bit
1936 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301938 status = PSA_ERROR_NOT_SUPPORTED;
1939 goto exit;
1940 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 status = psa_validate_optional_attributes(slot, attributes);
1942 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001943 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001947exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 if (status != PSA_SUCCESS) {
1949 psa_fail_key_creation(slot, driver);
1950 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001953}
1954
Gilles Peskined7729582019-08-05 15:55:54 +02001955#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1956psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02001958{
1959 psa_status_t status;
1960 psa_key_slot_t *slot = NULL;
1961 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001962 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001963
1964 /* Leaving attributes unspecified is not currently supported.
1965 * It could make sense to query the key type and size from the
1966 * secure element, but not all secure elements support this
1967 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
1969 return PSA_ERROR_NOT_SUPPORTED;
1970 }
1971 if (psa_get_key_bits(attributes) == 0) {
1972 return PSA_ERROR_NOT_SUPPORTED;
1973 }
Gilles Peskined7729582019-08-05 15:55:54 +02001974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
1976 &slot, &driver);
1977 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02001978 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 }
Gilles Peskined7729582019-08-05 15:55:54 +02001980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02001982
1983exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (status != PSA_SUCCESS) {
1985 psa_fail_key_creation(slot, driver);
1986 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001987
Gilles Peskined7729582019-08-05 15:55:54 +02001988 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 psa_close_key(key);
1990 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02001991}
1992#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
1995 const psa_key_attributes_t *specified_attributes,
1996 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01001997{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001998 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001999 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002000 psa_key_slot_t *source_slot = NULL;
2001 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002002 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002003 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302004 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002005
Ronald Cron81709fc2020-11-14 12:10:32 +01002006 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2007
Archana8a180362021-07-05 02:18:48 +05302008 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2010 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002011 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 status = psa_validate_optional_attributes(source_slot,
2015 specified_attributes);
2016 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002017 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002019
Archana9d17bf42021-09-10 06:22:44 +05302020 /* The target key type and number of bits have been validated by
2021 * psa_validate_optional_attributes() to be either equal to zero or
2022 * equal to the ones of the source key. So it is safe to inherit
2023 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302024 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002025 actual_attributes.bits = source_slot->attr.bits;
2026 actual_attributes.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302027
2028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 status = psa_restrict_key_policy(source_slot->attr.type,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002030 &actual_attributes.policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 &source_slot->attr.policy);
2032 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002033 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2037 &target_slot, &driver);
2038 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002039 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 }
2041 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2042 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302043 /*
Archana9d17bf42021-09-10 06:22:44 +05302044 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302045 * the source key would need to be exported as plaintext and re-imported
2046 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302047 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302048 * appropriate API invocations from the application, if needed.
2049 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002050 status = PSA_ERROR_NOT_SUPPORTED;
2051 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002052 }
Archana8a180362021-07-05 02:18:48 +05302053 /*
2054 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302055 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302056 * - For opaque keys this translates to an invocation of the drivers'
2057 * copy_key entry point through the dispatch layer.
2058 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002059 if (psa_key_lifetime_is_external(actual_attributes.lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2061 &storage_size);
2062 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302063 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 }
Archana374fe5b2021-09-08 15:50:28 +05302065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2067 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302068 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 }
Archana374fe5b2021-09-08 15:50:28 +05302070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 status = psa_driver_wrapper_copy_key(&actual_attributes,
2072 source_slot->key.data,
2073 source_slot->key.bytes,
2074 target_slot->key.data,
2075 target_slot->key.bytes,
2076 &target_slot->key.bytes);
2077 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302078 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 }
2080 } else {
2081 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302082 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 source_slot->key.bytes);
2084 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302085 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 }
Archana8a180362021-07-05 02:18:48 +05302087 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002089exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 if (status != PSA_SUCCESS) {
2091 psa_fail_key_creation(target_slot, driver);
2092 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002093
Ryan Everetta103ec92024-01-31 13:59:57 +00002094 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002097}
2098
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002099
2100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002101/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002102/* Message digests */
2103/****************************************************************/
2104
Gilles Peskine449bd832023-01-11 14:50:10 +01002105psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002106{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002107 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 if (operation->id == 0) {
2109 return PSA_SUCCESS;
2110 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002113 operation->id = 0;
2114
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002116}
2117
Gilles Peskine449bd832023-01-11 14:50:10 +01002118psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2119 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002120{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002121 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002122
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002123 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002125 status = PSA_ERROR_BAD_STATE;
2126 goto exit;
2127 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002130 status = PSA_ERROR_INVALID_ARGUMENT;
2131 goto exit;
2132 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002133
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002134 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2135 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002139
2140exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (status != PSA_SUCCESS) {
2142 psa_hash_abort(operation);
2143 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002144
2145 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002146}
2147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2149 const uint8_t *input,
2150 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002151{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002152 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002155 status = PSA_ERROR_BAD_STATE;
2156 goto exit;
2157 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002158
Steven Cooremanc8288352021-03-04 14:02:19 +01002159 /* Don't require hash implementations to behave correctly on a
2160 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 if (input_length == 0) {
2162 return PSA_SUCCESS;
2163 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002166
2167exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 if (status != PSA_SUCCESS) {
2169 psa_hash_abort(operation);
2170 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002173}
2174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2176 uint8_t *hash,
2177 size_t hash_size,
2178 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002179{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002180 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (operation->id == 0) {
2182 return PSA_ERROR_BAD_STATE;
2183 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002184
Steven Cooreman1e582352021-02-18 17:24:37 +01002185 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 operation, hash, hash_size, hash_length);
2187 psa_hash_abort(operation);
2188 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002189}
2190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2192 const uint8_t *hash,
2193 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194{
Ronald Cron69a63422021-10-18 09:47:58 +02002195 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002196 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002197 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 operation,
2199 actual_hash, sizeof(actual_hash),
2200 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002203 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002207 status = PSA_ERROR_INVALID_SIGNATURE;
2208 goto exit;
2209 }
2210
Dave Rodgman78701152023-08-29 14:20:18 +01002211 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002212 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002214
2215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2217 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002218 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002222}
2223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224psa_status_t psa_hash_compute(psa_algorithm_t alg,
2225 const uint8_t *input, size_t input_length,
2226 uint8_t *hash, size_t hash_size,
2227 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002228{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002229 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 if (!PSA_ALG_IS_HASH(alg)) {
2231 return PSA_ERROR_INVALID_ARGUMENT;
2232 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2235 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002236}
2237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238psa_status_t psa_hash_compare(psa_algorithm_t alg,
2239 const uint8_t *input, size_t input_length,
2240 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002241{
Ronald Cron69a63422021-10-18 09:47:58 +02002242 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002243 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 if (!PSA_ALG_IS_HASH(alg)) {
2246 return PSA_ERROR_INVALID_ARGUMENT;
2247 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002248
Steven Cooreman1e582352021-02-18 17:24:37 +01002249 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 alg, input, input_length,
2251 actual_hash, sizeof(actual_hash),
2252 &actual_hash_length);
2253 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002254 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 }
2256 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002257 status = PSA_ERROR_INVALID_SIGNATURE;
2258 goto exit;
2259 }
Dave Rodgman78701152023-08-29 14:20:18 +01002260 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002261 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002263
2264exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2266 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002267}
2268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2270 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002271{
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 if (source_operation->id == 0 ||
2273 target_operation->id != 0) {
2274 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002275 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2278 target_operation);
2279 if (status != PSA_SUCCESS) {
2280 psa_hash_abort(target_operation);
2281 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002284}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002285
2286
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002287/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002288/* MAC */
2289/****************************************************************/
2290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002292{
Steven Cooremane6804192021-03-19 18:28:56 +01002293 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (operation->id == 0) {
2295 return PSA_SUCCESS;
2296 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002299 operation->mac_size = 0;
2300 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002301 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002304}
2305
Ronald Cron2dff3b22021-06-17 16:33:22 +02002306static psa_status_t psa_mac_finalize_alg_and_key_validation(
2307 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002308 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002310{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002311 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 psa_key_type_t key_type = psa_get_key_type(attributes);
2313 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (!PSA_ALG_IS_MAC(alg)) {
2316 return PSA_ERROR_INVALID_ARGUMENT;
2317 }
Steven Cooremane6804192021-03-19 18:28:56 +01002318
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002319 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 status = psa_mac_key_can_do(alg, key_type);
2321 if (status != PSA_SUCCESS) {
2322 return status;
2323 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002324
Steven Cooremand1a68f12021-05-10 11:13:41 +02002325 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002329 /* A very short MAC is too short for security since it can be
2330 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2331 * so we make this our minimum, even though 32 bits is still
2332 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002334 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2337 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002338 /* It's impossible to "truncate" to a larger length than the full length
2339 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002341 }
2342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002344 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2345 * that is disabled in the compile-time configuration. The result can
2346 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2347 * configuration into account. In this case, force a return of
2348 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2349 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2350 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2351 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2352 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002354 }
2355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002357}
2358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2360 mbedtls_svc_key_id_t key,
2361 psa_algorithm_t alg,
2362 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002363{
2364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2365 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002366 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002367
2368 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002370 status = PSA_ERROR_BAD_STATE;
2371 goto exit;
2372 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002373
2374 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 key,
2376 &slot,
2377 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2378 alg);
2379 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002380 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002382
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002383 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 &operation->mac_size);
2385 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002386 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002388
2389 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002390 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 if (is_sign) {
2392 status = psa_driver_wrapper_mac_sign_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002393 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 slot->key.data,
2395 slot->key.bytes,
2396 alg);
2397 } else {
2398 status = psa_driver_wrapper_mac_verify_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002399 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 slot->key.data,
2401 slot->key.bytes,
2402 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002403 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002404
Gilles Peskinefbfac682018-07-08 20:51:54 +02002405exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 if (status != PSA_SUCCESS) {
2407 psa_mac_abort(operation);
2408 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002409
Ryan Everettfb9857f2024-02-14 12:16:41 +00002410 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002411
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002413}
2414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2416 mbedtls_svc_key_id_t key,
2417 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002418{
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002420}
2421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2423 mbedtls_svc_key_id_t key,
2424 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002425{
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002427}
2428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2430 const uint8_t *input,
2431 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432{
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 if (operation->id == 0) {
2434 return PSA_ERROR_BAD_STATE;
2435 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002436
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002437 /* Don't require hash implementations to behave correctly on a
2438 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (input_length == 0) {
2440 return PSA_SUCCESS;
2441 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2444 input, input_length);
2445 if (status != PSA_SUCCESS) {
2446 psa_mac_abort(operation);
2447 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002450}
2451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2453 uint8_t *mac,
2454 size_t mac_size,
2455 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002456{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002457 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2458 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002461 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002462 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002463 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002466 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002467 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002468 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002469
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002470 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2471 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002473 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002474 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002475 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002478 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002479 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002480 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 status = psa_driver_wrapper_mac_sign_finish(operation,
2483 mac, operation->mac_size,
2484 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002485
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002486exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002487 /* In case of success, set the potential excess room in the output buffer
2488 * to an invalid value, to avoid potentially leaking a longer MAC.
2489 * In case of error, set the output length and content to a safe default,
2490 * such that in case the caller misses an error check, the output would be
2491 * an unachievable MAC.
2492 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002494 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002495 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002496 }
mohammad16036df908f2018-04-02 08:34:15 -07002497
Paul Elliottdc42ca82023-02-24 18:11:59 +00002498 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002499
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002501
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002503}
2504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2506 const uint8_t *mac,
2507 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002508{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002509 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2510 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002513 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002514 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002515 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002518 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002519 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002520 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002523 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002524 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002525 }
2526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 status = psa_driver_wrapper_mac_verify_finish(operation,
2528 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002529
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002530exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002534}
2535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2537 psa_algorithm_t alg,
2538 const uint8_t *input,
2539 size_t input_length,
2540 uint8_t *mac,
2541 size_t mac_size,
2542 size_t *mac_length,
2543 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002544{
2545 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002546 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2547 psa_key_slot_t *slot;
2548 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002549
Ronald Cron51131b52021-06-17 17:17:20 +02002550 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 key,
2552 &slot,
2553 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2554 alg);
2555 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002556 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002558
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002559 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 &operation_mac_size);
2561 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002562 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002566 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002567 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002568 }
2569
2570 status = psa_driver_wrapper_mac_compute(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002571 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 slot->key.data, slot->key.bytes,
2573 alg,
2574 input, input_length,
2575 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002576
2577exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002578 /* In case of success, set the potential excess room in the output buffer
2579 * to an invalid value, to avoid potentially leaking a longer MAC.
2580 * In case of error, set the output length and content to a safe default,
2581 * such that in case the caller misses an error check, the output would be
2582 * an unachievable MAC.
2583 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002585 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002586 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002587 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002588
2589 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002590
Ryan Everetta103ec92024-01-31 13:59:57 +00002591 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002594}
2595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002597 psa_algorithm_t alg,
2598 const uint8_t *input,
2599 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 uint8_t *mac,
2601 size_t mac_size,
2602 size_t *mac_length)
2603{
2604 return psa_mac_compute_internal(key, alg,
2605 input, input_length,
2606 mac, mac_size, mac_length, 1);
2607}
2608
2609psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2610 psa_algorithm_t alg,
2611 const uint8_t *input,
2612 size_t input_length,
2613 const uint8_t *mac,
2614 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002615{
2616 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002617 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2618 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 status = psa_mac_compute_internal(key, alg,
2621 input, input_length,
2622 actual_mac, sizeof(actual_mac),
2623 &actual_mac_length, 0);
2624 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002625 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002627
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002629 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002630 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002631 }
Dave Rodgman78701152023-08-29 14:20:18 +01002632 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002633 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002634 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002635 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002636
2637exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002639
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002641}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002642
Gilles Peskinee59236f2018-01-27 23:32:46 +01002643/****************************************************************/
2644/* Asymmetric cryptography */
2645/****************************************************************/
2646
Gilles Peskine449bd832023-01-11 14:50:10 +01002647static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2648 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002649{
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 if (input_is_message) {
2651 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2652 return PSA_ERROR_INVALID_ARGUMENT;
2653 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2656 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2657 return PSA_ERROR_INVALID_ARGUMENT;
2658 }
2659 }
2660 } else {
2661 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2662 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002663 }
2664 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002667}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2670 int input_is_message,
2671 psa_algorithm_t alg,
2672 const uint8_t *input,
2673 size_t input_length,
2674 uint8_t *signature,
2675 size_t signature_size,
2676 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002677{
2678 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2679 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2680 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002681
2682 *signature_length = 0;
2683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 status = psa_sign_verify_check_alg(input_is_message, alg);
2685 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002686 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002688
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002689 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002690 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002691 * buffer can in principle be empty since it doesn't actually have
2692 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 if (signature_size == 0) {
2694 return PSA_ERROR_BUFFER_TOO_SMALL;
2695 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002696
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002697 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 key, &slot,
2699 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2700 PSA_KEY_USAGE_SIGN_HASH,
2701 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002704 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002708 status = PSA_ERROR_INVALID_ARGUMENT;
2709 goto exit;
2710 }
2711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002713 status = psa_driver_wrapper_sign_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002714 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002715 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 signature, signature_size, signature_length);
2717 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002718
2719 status = psa_driver_wrapper_sign_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002720 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002721 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002723 }
2724
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002725
2726exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002727 psa_wipe_tag_output_buffer(signature, status, signature_size,
2728 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002729
Ryan Everetta103ec92024-01-31 13:59:57 +00002730 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002733}
2734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2736 int input_is_message,
2737 psa_algorithm_t alg,
2738 const uint8_t *input,
2739 size_t input_length,
2740 const uint8_t *signature,
2741 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002742{
2743 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2744 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2745 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 status = psa_sign_verify_check_alg(input_is_message, alg);
2748 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002749 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002751
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002752 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 key, &slot,
2754 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2755 PSA_KEY_USAGE_VERIFY_HASH,
2756 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 if (status != PSA_SUCCESS) {
2759 return status;
2760 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002761
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002763 status = psa_driver_wrapper_verify_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002764 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002765 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 signature, signature_length);
2767 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002768 status = psa_driver_wrapper_verify_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002769 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002770 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002772 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002773
Ryan Everetta103ec92024-01-31 13:59:57 +00002774 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002775
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002777
2778}
2779
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002780psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002781 const psa_key_attributes_t *attributes,
2782 const uint8_t *key_buffer,
2783 size_t key_buffer_size,
2784 psa_algorithm_t alg,
2785 const uint8_t *input,
2786 size_t input_length,
2787 uint8_t *signature,
2788 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002790{
2791 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002794 size_t hash_length;
2795 uint8_t hash[PSA_HASH_MAX_SIZE];
2796
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002797 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 PSA_ALG_SIGN_GET_HASH(alg),
2799 input, input_length,
2800 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002803 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002805
2806 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 attributes, key_buffer, key_buffer_size,
2808 alg, hash, hash_length,
2809 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002810 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002811
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002813}
2814
Gilles Peskine449bd832023-01-11 14:50:10 +01002815psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2816 psa_algorithm_t alg,
2817 const uint8_t *input,
2818 size_t input_length,
2819 uint8_t *signature,
2820 size_t signature_size,
2821 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002822{
2823 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002824 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002826}
2827
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002828psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002829 const psa_key_attributes_t *attributes,
2830 const uint8_t *key_buffer,
2831 size_t key_buffer_size,
2832 psa_algorithm_t alg,
2833 const uint8_t *input,
2834 size_t input_length,
2835 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002837{
2838 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002839
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002841 size_t hash_length;
2842 uint8_t hash[PSA_HASH_MAX_SIZE];
2843
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002844 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 PSA_ALG_SIGN_GET_HASH(alg),
2846 input, input_length,
2847 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002848
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002850 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002852
2853 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 attributes, key_buffer, key_buffer_size,
2855 alg, hash, hash_length,
2856 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002857 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002858
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002860}
2861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2863 psa_algorithm_t alg,
2864 const uint8_t *input,
2865 size_t input_length,
2866 const uint8_t *signature,
2867 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002868{
2869 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002870 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002872}
2873
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002874psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002875 const psa_key_attributes_t *attributes,
2876 const uint8_t *key_buffer, size_t key_buffer_size,
2877 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002879{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002880 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2882 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002883#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2885 return mbedtls_psa_rsa_sign_hash(
2886 attributes,
2887 key_buffer, key_buffer_size,
2888 alg, hash, hash_length,
2889 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002890#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2891 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 } else {
2893 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002894 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002895 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002897#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2899 return mbedtls_psa_ecdsa_sign_hash(
2900 attributes,
2901 key_buffer, key_buffer_size,
2902 alg, hash, hash_length,
2903 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002904#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2905 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 } else {
2907 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002908 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002909 }
Ronald Cron4501c982021-03-19 20:09:32 +01002910
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 (void) key_buffer;
2912 (void) key_buffer_size;
2913 (void) hash;
2914 (void) hash_length;
2915 (void) signature;
2916 (void) signature_size;
2917 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002918
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002920}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002921
Gilles Peskine449bd832023-01-11 14:50:10 +01002922psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2923 psa_algorithm_t alg,
2924 const uint8_t *hash,
2925 size_t hash_length,
2926 uint8_t *signature,
2927 size_t signature_size,
2928 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002929{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002930 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002931 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002933}
2934
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002935psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002936 const psa_key_attributes_t *attributes,
2937 const uint8_t *key_buffer, size_t key_buffer_size,
2938 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002940{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002941 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2943 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002944#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2946 return mbedtls_psa_rsa_verify_hash(
2947 attributes,
2948 key_buffer, key_buffer_size,
2949 alg, hash, hash_length,
2950 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002951#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2952 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 } else {
2954 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002955 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002956 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002958#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2960 return mbedtls_psa_ecdsa_verify_hash(
2961 attributes,
2962 key_buffer, key_buffer_size,
2963 alg, hash, hash_length,
2964 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002965#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2966 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 } else {
2968 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002969 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002970 }
Ronald Cron4501c982021-03-19 20:09:32 +01002971
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 (void) key_buffer;
2973 (void) key_buffer_size;
2974 (void) hash;
2975 (void) hash_length;
2976 (void) signature;
2977 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002980}
2981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
2983 psa_algorithm_t alg,
2984 const uint8_t *hash,
2985 size_t hash_length,
2986 const uint8_t *signature,
2987 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002988{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002989 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002990 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002992}
2993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
2995 psa_algorithm_t alg,
2996 const uint8_t *input,
2997 size_t input_length,
2998 const uint8_t *salt,
2999 size_t salt_length,
3000 uint8_t *output,
3001 size_t output_size,
3002 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003003{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003004 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003005 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003006 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003007
Darryl Green5cc689a2018-07-24 15:34:10 +01003008 (void) input;
3009 (void) input_length;
3010 (void) salt;
3011 (void) output;
3012 (void) output_size;
3013
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003014 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3017 return PSA_ERROR_INVALID_ARGUMENT;
3018 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003019
Valerio Setti5bb454a2024-01-15 10:43:16 +01003020 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3022 if (status != PSA_SUCCESS) {
3023 return status;
3024 }
3025 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3026 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003027 status = PSA_ERROR_INVALID_ARGUMENT;
3028 goto exit;
3029 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003030
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003031 status = psa_driver_wrapper_asymmetric_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003032 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003033 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003035exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003036 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003039}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3042 psa_algorithm_t alg,
3043 const uint8_t *input,
3044 size_t input_length,
3045 const uint8_t *salt,
3046 size_t salt_length,
3047 uint8_t *output,
3048 size_t output_size,
3049 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003050{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003052 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003053 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003054
Darryl Green5cc689a2018-07-24 15:34:10 +01003055 (void) input;
3056 (void) input_length;
3057 (void) salt;
3058 (void) output;
3059 (void) output_size;
3060
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003061 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003062
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3064 return PSA_ERROR_INVALID_ARGUMENT;
3065 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003066
Valerio Setti5bb454a2024-01-15 10:43:16 +01003067 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3069 if (status != PSA_SUCCESS) {
3070 return status;
3071 }
3072 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003073 status = PSA_ERROR_INVALID_ARGUMENT;
3074 goto exit;
3075 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003076
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003077 status = psa_driver_wrapper_asymmetric_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003078 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003079 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003081
3082exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003083 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003086}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003087
Paul Elliott9fe12f62022-11-30 19:16:02 +00003088/****************************************************************/
3089/* Asymmetric interruptible cryptography */
3090/****************************************************************/
3091
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003092static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3093
Paul Elliott9fe12f62022-11-30 19:16:02 +00003094void psa_interruptible_set_max_ops(uint32_t max_ops)
3095{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003096 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003097}
3098
3099uint32_t psa_interruptible_get_max_ops(void)
3100{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003101 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003102}
3103
Paul Elliott9fe12f62022-11-30 19:16:02 +00003104uint32_t psa_sign_hash_get_num_ops(
3105 const psa_sign_hash_interruptible_operation_t *operation)
3106{
Paul Elliott296ede92022-12-15 17:00:30 +00003107 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003108}
3109
3110uint32_t psa_verify_hash_get_num_ops(
3111 const psa_verify_hash_interruptible_operation_t *operation)
3112{
Paul Elliott296ede92022-12-15 17:00:30 +00003113 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003114}
3115
Paul Elliott59ad9452022-12-18 15:09:02 +00003116static psa_status_t psa_sign_hash_abort_internal(
3117 psa_sign_hash_interruptible_operation_t *operation)
3118{
3119 if (operation->id == 0) {
3120 /* The object has (apparently) been initialized but it is not (yet)
3121 * in use. It's ok to call abort on such an object, and there's
3122 * nothing to do. */
3123 return PSA_SUCCESS;
3124 }
3125
3126 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3127
3128 status = psa_driver_wrapper_sign_hash_abort(operation);
3129
3130 operation->id = 0;
3131
Paul Elliotte6145dc2023-02-07 12:51:21 +00003132 /* Do not clear either the error_occurred or num_ops elements here as they
3133 * only want to be cleared by the application calling abort, not by abort
3134 * being called at completion of an operation. */
3135
Paul Elliott59ad9452022-12-18 15:09:02 +00003136 return status;
3137}
3138
Paul Elliott9fe12f62022-11-30 19:16:02 +00003139psa_status_t psa_sign_hash_start(
3140 psa_sign_hash_interruptible_operation_t *operation,
3141 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3142 const uint8_t *hash, size_t hash_length)
3143{
3144 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3145 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3146 psa_key_slot_t *slot;
3147
Paul Elliottc9774412023-02-06 15:14:07 +00003148 /* Check that start has not been previously called, or operation has not
3149 * previously errored. */
3150 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003151 return PSA_ERROR_BAD_STATE;
3152 }
3153
Paul Elliott9fe12f62022-11-30 19:16:02 +00003154 status = psa_sign_verify_check_alg(0, alg);
3155 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003156 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003157 return status;
3158 }
3159
3160 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3161 PSA_KEY_USAGE_SIGN_HASH,
3162 alg);
3163
3164 if (status != PSA_SUCCESS) {
3165 goto exit;
3166 }
3167
3168 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3169 status = PSA_ERROR_INVALID_ARGUMENT;
3170 goto exit;
3171 }
3172
Paul Elliott296ede92022-12-15 17:00:30 +00003173 /* Ensure ops count gets reset, in case of operation re-use. */
3174 operation->num_ops = 0;
3175
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003176 status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003177 slot->key.data,
3178 slot->key.bytes, alg,
3179 hash, hash_length);
3180exit:
3181
3182 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003183 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003184 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003185 }
3186
Ryan Everettdcc03d52024-02-14 15:44:13 +00003187 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003188
Paul Elliottc9774412023-02-06 15:14:07 +00003189 if (unlock_status != PSA_SUCCESS) {
3190 operation->error_occurred = 1;
3191 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003192
Paul Elliottc9774412023-02-06 15:14:07 +00003193 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003194}
3195
3196
3197psa_status_t psa_sign_hash_complete(
3198 psa_sign_hash_interruptible_operation_t *operation,
3199 uint8_t *signature, size_t signature_size,
3200 size_t *signature_length)
3201{
3202 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3203
3204 *signature_length = 0;
3205
Paul Elliottc9774412023-02-06 15:14:07 +00003206 /* Check that start has been called first, and that operation has not
3207 * previously errored. */
3208 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003209 status = PSA_ERROR_BAD_STATE;
3210 goto exit;
3211 }
3212
Paul Elliott096abc42023-02-03 18:33:23 +00003213 /* Immediately reject a zero-length signature buffer. This guarantees that
3214 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003215 if (signature_size == 0) {
3216 status = PSA_ERROR_BUFFER_TOO_SMALL;
3217 goto exit;
3218 }
3219
3220 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3221 signature_size,
3222 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003223
Paul Elliott296ede92022-12-15 17:00:30 +00003224 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003225 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003226
Paul Elliottebe225c2023-02-10 14:32:53 +00003227exit:
3228
Paul Elliott59200a22023-02-21 15:07:40 +00003229 psa_wipe_tag_output_buffer(signature, status, signature_size,
3230 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003231
Paul Elliott53bb3122023-02-10 14:22:22 +00003232 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003233 if (status != PSA_SUCCESS) {
3234 operation->error_occurred = 1;
3235 }
3236
Paul Elliott59ad9452022-12-18 15:09:02 +00003237 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003238 }
3239
3240 return status;
3241}
3242
3243psa_status_t psa_sign_hash_abort(
3244 psa_sign_hash_interruptible_operation_t *operation)
3245{
Paul Elliott59ad9452022-12-18 15:09:02 +00003246 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3247
3248 status = psa_sign_hash_abort_internal(operation);
3249
3250 /* We clear the number of ops done here, so that it is not cleared when
3251 * the operation fails or succeeds, only on manual abort. */
3252 operation->num_ops = 0;
3253
Paul Elliottc9774412023-02-06 15:14:07 +00003254 /* Likewise, failure state. */
3255 operation->error_occurred = 0;
3256
Paul Elliott59ad9452022-12-18 15:09:02 +00003257 return status;
3258}
3259
3260static psa_status_t psa_verify_hash_abort_internal(
3261 psa_verify_hash_interruptible_operation_t *operation)
3262{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003263 if (operation->id == 0) {
3264 /* The object has (apparently) been initialized but it is not (yet)
3265 * in use. It's ok to call abort on such an object, and there's
3266 * nothing to do. */
3267 return PSA_SUCCESS;
3268 }
3269
Paul Elliott59ad9452022-12-18 15:09:02 +00003270 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3271
3272 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003273
3274 operation->id = 0;
3275
Paul Elliotte6145dc2023-02-07 12:51:21 +00003276 /* Do not clear either the error_occurred or num_ops elements here as they
3277 * only want to be cleared by the application calling abort, not by abort
3278 * being called at completion of an operation. */
3279
Paul Elliott59ad9452022-12-18 15:09:02 +00003280 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003281}
3282
3283psa_status_t psa_verify_hash_start(
3284 psa_verify_hash_interruptible_operation_t *operation,
3285 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3286 const uint8_t *hash, size_t hash_length,
3287 const uint8_t *signature, size_t signature_length)
3288{
3289 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3290 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3291 psa_key_slot_t *slot;
3292
Paul Elliottc9774412023-02-06 15:14:07 +00003293 /* Check that start has not been previously called, or operation has not
3294 * previously errored. */
3295 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003296 return PSA_ERROR_BAD_STATE;
3297 }
3298
3299 status = psa_sign_verify_check_alg(0, alg);
3300 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003301 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003302 return status;
3303 }
3304
3305 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3306 PSA_KEY_USAGE_VERIFY_HASH,
3307 alg);
3308
3309 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003310 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003311 return status;
3312 }
3313
Paul Elliott296ede92022-12-15 17:00:30 +00003314 /* Ensure ops count gets reset, in case of operation re-use. */
3315 operation->num_ops = 0;
3316
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003317 status = psa_driver_wrapper_verify_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003318 slot->key.data,
3319 slot->key.bytes,
3320 alg, hash, hash_length,
3321 signature, signature_length);
3322
3323 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003324 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003325 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003326 }
3327
Ryan Everett291267f2024-02-14 15:59:15 +00003328 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003329
Paul Elliottc9774412023-02-06 15:14:07 +00003330 if (unlock_status != PSA_SUCCESS) {
3331 operation->error_occurred = 1;
3332 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003333
Paul Elliottc9774412023-02-06 15:14:07 +00003334 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003335}
3336
3337psa_status_t psa_verify_hash_complete(
3338 psa_verify_hash_interruptible_operation_t *operation)
3339{
3340 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3341
Paul Elliottc9774412023-02-06 15:14:07 +00003342 /* Check that start has been called first, and that operation has not
3343 * previously errored. */
3344 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003345 status = PSA_ERROR_BAD_STATE;
3346 goto exit;
3347 }
3348
3349 status = psa_driver_wrapper_verify_hash_complete(operation);
3350
Paul Elliott296ede92022-12-15 17:00:30 +00003351 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003352 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003353 operation);
3354
Paul Elliottebe225c2023-02-10 14:32:53 +00003355exit:
3356
Paul Elliott9fe12f62022-11-30 19:16:02 +00003357 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003358 if (status != PSA_SUCCESS) {
3359 operation->error_occurred = 1;
3360 }
3361
Paul Elliott59ad9452022-12-18 15:09:02 +00003362 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003363 }
3364
3365 return status;
3366}
3367
3368psa_status_t psa_verify_hash_abort(
3369 psa_verify_hash_interruptible_operation_t *operation)
3370{
Paul Elliott59ad9452022-12-18 15:09:02 +00003371 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003372
Paul Elliott59ad9452022-12-18 15:09:02 +00003373 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003374
Paul Elliott59ad9452022-12-18 15:09:02 +00003375 /* We clear the number of ops done here, so that it is not cleared when
3376 * the operation fails or succeeds, only on manual abort. */
3377 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003378
Paul Elliottc9774412023-02-06 15:14:07 +00003379 /* Likewise, failure state. */
3380 operation->error_occurred = 0;
3381
Paul Elliott59ad9452022-12-18 15:09:02 +00003382 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003383}
3384
Paul Elliott588f8ed2022-12-02 18:10:26 +00003385/****************************************************************/
3386/* Asymmetric interruptible cryptography internal */
3387/* implementations */
3388/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003389
Paul Elliott588f8ed2022-12-02 18:10:26 +00003390void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3391{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003392
Paul Elliott588f8ed2022-12-02 18:10:26 +00003393#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3394 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3395 defined(MBEDTLS_ECP_RESTARTABLE)
3396
3397 /* Internal implementation uses zero to indicate infinite number max ops,
3398 * therefore avoid this value, and set to minimum possible. */
3399 if (max_ops == 0) {
3400 max_ops = 1;
3401 }
3402
Paul Elliott588f8ed2022-12-02 18:10:26 +00003403 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003404#else
3405 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003406#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3407 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3408 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3409}
3410
Paul Elliott588f8ed2022-12-02 18:10:26 +00003411uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003412 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003413{
3414#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3415 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3416 defined(MBEDTLS_ECP_RESTARTABLE)
3417
Paul Elliott93d9ca82023-02-15 18:14:21 +00003418 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003419#else
3420 (void) operation;
3421 return 0;
3422#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3423 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3424 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3425}
3426
3427uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003428 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003429{
3430 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3431 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3432 defined(MBEDTLS_ECP_RESTARTABLE)
3433
Paul Elliott93d9ca82023-02-15 18:14:21 +00003434 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003435#else
3436 (void) operation;
3437 return 0;
3438#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3439 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3440 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3441}
3442
3443psa_status_t mbedtls_psa_sign_hash_start(
3444 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3445 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3446 size_t key_buffer_size, psa_algorithm_t alg,
3447 const uint8_t *hash, size_t hash_length)
3448{
3449 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003450 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003451
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003452 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003453 return PSA_ERROR_NOT_SUPPORTED;
3454 }
3455
3456 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003457 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003458 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003459
3460#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003461 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3462 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003463
Paul Elliotta1c94092023-02-15 16:38:04 +00003464 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3465
Paul Elliott93d9ca82023-02-15 18:14:21 +00003466 /* Ensure num_ops is zero'ed in case of context re-use. */
3467 operation->num_ops = 0;
3468
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003469 status = mbedtls_psa_ecp_load_representation(attributes->type,
3470 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003471 key_buffer,
3472 key_buffer_size,
3473 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003474
Paul Elliott068fe072023-01-16 13:59:15 +00003475 if (status != PSA_SUCCESS) {
3476 return status;
3477 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003478
Paul Elliott1bc59df2023-02-05 13:41:57 +00003479 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003480 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003481
Paul Elliott068fe072023-01-16 13:59:15 +00003482 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003483 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003484 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003485
Paul Elliott0290a762023-02-09 14:30:24 +00003486 /* We only need to store the same length of hash as the private key size
3487 * here, it would be truncated by the internal implementation anyway. */
3488 required_hash_length = (hash_length < operation->coordinate_bytes ?
3489 hash_length : operation->coordinate_bytes);
3490
Paul Elliottba70ad42023-02-15 18:23:53 +00003491 if (required_hash_length > sizeof(operation->hash)) {
3492 /* Shouldn't happen, but better safe than sorry. */
3493 return PSA_ERROR_CORRUPTION_DETECTED;
3494 }
3495
Paul Elliott0290a762023-02-09 14:30:24 +00003496 memcpy(operation->hash, hash, required_hash_length);
3497 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003498
3499 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003500
3501#else
Paul Elliott068fe072023-01-16 13:59:15 +00003502 (void) operation;
3503 (void) key_buffer;
3504 (void) key_buffer_size;
3505 (void) alg;
3506 (void) hash;
3507 (void) hash_length;
3508 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003509 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003510
Paul Elliott068fe072023-01-16 13:59:15 +00003511 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003512#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3513 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3514 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003515}
3516
3517psa_status_t mbedtls_psa_sign_hash_complete(
3518 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3519 uint8_t *signature, size_t signature_size,
3520 size_t *signature_length)
3521{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003522#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3523 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3524 defined(MBEDTLS_ECP_RESTARTABLE)
3525
Paul Elliotta1c94092023-02-15 16:38:04 +00003526 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003527 mbedtls_mpi r;
3528 mbedtls_mpi s;
3529
Paul Elliott46845252023-02-03 14:59:11 +00003530 mbedtls_mpi_init(&r);
3531 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003532
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003533 /* Ensure max_ops is set to the current value (or default). */
3534 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3535
Paul Elliotta1c94092023-02-15 16:38:04 +00003536 if (signature_size < 2 * operation->coordinate_bytes) {
3537 status = PSA_ERROR_BUFFER_TOO_SMALL;
3538 goto exit;
3539 }
3540
Paul Elliott588f8ed2022-12-02 18:10:26 +00003541 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003542
Paul Elliott588f8ed2022-12-02 18:10:26 +00003543#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3544 status = mbedtls_to_psa_error(
3545 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003546 &r,
3547 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003548 &operation->ctx->d,
3549 operation->hash,
3550 operation->hash_length,
3551 operation->md_alg,
3552 mbedtls_psa_get_random,
3553 MBEDTLS_PSA_RANDOM_STATE,
3554 &operation->restart_ctx));
3555#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003556 status = PSA_ERROR_NOT_SUPPORTED;
3557 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003558#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3559 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003560 status = mbedtls_to_psa_error(
3561 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003562 &r,
3563 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003564 &operation->ctx->d,
3565 operation->hash,
3566 operation->hash_length,
3567 mbedtls_psa_get_random,
3568 MBEDTLS_PSA_RANDOM_STATE,
3569 mbedtls_psa_get_random,
3570 MBEDTLS_PSA_RANDOM_STATE,
3571 &operation->restart_ctx));
3572 }
3573
Paul Elliottf8e5b562023-02-19 18:43:45 +00003574 /* Hide the fact that the restart context only holds a delta of number of
3575 * ops done during the last operation, not an absolute value. */
3576 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3577
Paul Elliott724bd252023-02-08 12:35:08 +00003578 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003579 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003580 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003581 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003582 operation->coordinate_bytes)
3583 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003584
3585 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003586 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003587 }
3588
3589 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003590 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003591 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003592 operation->coordinate_bytes,
3593 operation->coordinate_bytes)
3594 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003595
3596 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003597 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003598 }
3599
Paul Elliott1bc59df2023-02-05 13:41:57 +00003600 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003601
Paul Elliott724bd252023-02-08 12:35:08 +00003602 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003603 }
Paul Elliott724bd252023-02-08 12:35:08 +00003604
3605exit:
3606
3607 mbedtls_mpi_free(&r);
3608 mbedtls_mpi_free(&s);
3609 return status;
3610
Paul Elliott588f8ed2022-12-02 18:10:26 +00003611 #else
3612
3613 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003614 (void) signature;
3615 (void) signature_size;
3616 (void) signature_length;
3617
3618 return PSA_ERROR_NOT_SUPPORTED;
3619
3620#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3621 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3622 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3623}
3624
3625psa_status_t mbedtls_psa_sign_hash_abort(
3626 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3627{
3628
3629#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3630 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3631 defined(MBEDTLS_ECP_RESTARTABLE)
3632
3633 if (operation->ctx) {
3634 mbedtls_ecdsa_free(operation->ctx);
3635 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003636 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003637 }
3638
3639 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3640
Paul Elliott93d9ca82023-02-15 18:14:21 +00003641 operation->num_ops = 0;
3642
Paul Elliott588f8ed2022-12-02 18:10:26 +00003643 return PSA_SUCCESS;
3644
3645#else
3646
3647 (void) operation;
3648
3649 return PSA_ERROR_NOT_SUPPORTED;
3650
3651#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3652 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3653 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3654}
3655
3656psa_status_t mbedtls_psa_verify_hash_start(
3657 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3658 const psa_key_attributes_t *attributes,
3659 const uint8_t *key_buffer, size_t key_buffer_size,
3660 psa_algorithm_t alg,
3661 const uint8_t *hash, size_t hash_length,
3662 const uint8_t *signature, size_t signature_length)
3663{
3664 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003665 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003666 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003667
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003668 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003669 return PSA_ERROR_NOT_SUPPORTED;
3670 }
3671
3672 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003673 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003674 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003675
3676#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003677 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3678 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003679
Paul Elliotta1c94092023-02-15 16:38:04 +00003680 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3681 mbedtls_mpi_init(&operation->r);
3682 mbedtls_mpi_init(&operation->s);
3683
Paul Elliott93d9ca82023-02-15 18:14:21 +00003684 /* Ensure num_ops is zero'ed in case of context re-use. */
3685 operation->num_ops = 0;
3686
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003687 status = mbedtls_psa_ecp_load_representation(attributes->type,
3688 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003689 key_buffer,
3690 key_buffer_size,
3691 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003692
Paul Elliott068fe072023-01-16 13:59:15 +00003693 if (status != PSA_SUCCESS) {
3694 return status;
3695 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003696
Paul Elliottc569fc22023-02-10 13:02:54 +00003697 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003698
Paul Elliott1bc59df2023-02-05 13:41:57 +00003699 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003700 return PSA_ERROR_INVALID_SIGNATURE;
3701 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003702
Paul Elliott068fe072023-01-16 13:59:15 +00003703 status = mbedtls_to_psa_error(
3704 mbedtls_mpi_read_binary(&operation->r,
3705 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003706 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003707
Paul Elliott068fe072023-01-16 13:59:15 +00003708 if (status != PSA_SUCCESS) {
3709 return status;
3710 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003711
Paul Elliott068fe072023-01-16 13:59:15 +00003712 status = mbedtls_to_psa_error(
3713 mbedtls_mpi_read_binary(&operation->s,
3714 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003715 coordinate_bytes,
3716 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003717
Paul Elliott068fe072023-01-16 13:59:15 +00003718 if (status != PSA_SUCCESS) {
3719 return status;
3720 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003721
Paul Elliott2c9843f2023-02-15 17:32:42 +00003722 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003723
Paul Elliott2c9843f2023-02-15 17:32:42 +00003724 if (status != PSA_SUCCESS) {
3725 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003726 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003727
Paul Elliott0290a762023-02-09 14:30:24 +00003728 /* We only need to store the same length of hash as the private key size
3729 * here, it would be truncated by the internal implementation anyway. */
3730 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3731 coordinate_bytes);
3732
Paul Elliottba70ad42023-02-15 18:23:53 +00003733 if (required_hash_length > sizeof(operation->hash)) {
3734 /* Shouldn't happen, but better safe than sorry. */
3735 return PSA_ERROR_CORRUPTION_DETECTED;
3736 }
3737
Paul Elliott0290a762023-02-09 14:30:24 +00003738 memcpy(operation->hash, hash, required_hash_length);
3739 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003740
3741 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003742#else
Paul Elliott068fe072023-01-16 13:59:15 +00003743 (void) operation;
3744 (void) key_buffer;
3745 (void) key_buffer_size;
3746 (void) alg;
3747 (void) hash;
3748 (void) hash_length;
3749 (void) signature;
3750 (void) signature_length;
3751 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003752 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003753 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003754
Paul Elliott068fe072023-01-16 13:59:15 +00003755 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003756#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3757 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3758 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003759}
3760
3761psa_status_t mbedtls_psa_verify_hash_complete(
3762 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3763{
3764
3765#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3766 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3767 defined(MBEDTLS_ECP_RESTARTABLE)
3768
Paul Elliottf8e5b562023-02-19 18:43:45 +00003769 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3770
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003771 /* Ensure max_ops is set to the current value (or default). */
3772 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3773
Paul Elliottf8e5b562023-02-19 18:43:45 +00003774 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003775 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3776 operation->hash,
3777 operation->hash_length,
3778 &operation->ctx->Q,
3779 &operation->r,
3780 &operation->s,
3781 &operation->restart_ctx));
3782
Paul Elliottf8e5b562023-02-19 18:43:45 +00003783 /* Hide the fact that the restart context only holds a delta of number of
3784 * ops done during the last operation, not an absolute value. */
3785 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3786
3787 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003788#else
3789 (void) operation;
3790
3791 return PSA_ERROR_NOT_SUPPORTED;
3792
3793#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3794 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3795 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3796}
3797
3798psa_status_t mbedtls_psa_verify_hash_abort(
3799 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3800{
3801
3802#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3803 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3804 defined(MBEDTLS_ECP_RESTARTABLE)
3805
3806 if (operation->ctx) {
3807 mbedtls_ecdsa_free(operation->ctx);
3808 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003809 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003810 }
3811
3812 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3813
Paul Elliott93d9ca82023-02-15 18:14:21 +00003814 operation->num_ops = 0;
3815
Paul Elliott588f8ed2022-12-02 18:10:26 +00003816 mbedtls_mpi_free(&operation->r);
3817 mbedtls_mpi_free(&operation->s);
3818
3819 return PSA_SUCCESS;
3820
3821#else
3822 (void) operation;
3823
3824 return PSA_ERROR_NOT_SUPPORTED;
3825
3826#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3827 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3828 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3829}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003830
mohammad1603503973b2018-03-12 15:59:30 +02003831/****************************************************************/
3832/* Symmetric cryptography */
3833/****************************************************************/
3834
Gilles Peskine449bd832023-01-11 14:50:10 +01003835static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3836 mbedtls_svc_key_id_t key,
3837 psa_algorithm_t alg,
3838 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003839{
3840 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3841 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003842 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003843 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3844 PSA_KEY_USAGE_ENCRYPT :
3845 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003846
3847 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003849 status = PSA_ERROR_BAD_STATE;
3850 goto exit;
3851 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003852
Gilles Peskine449bd832023-01-11 14:50:10 +01003853 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003854 status = PSA_ERROR_INVALID_ARGUMENT;
3855 goto exit;
3856 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003857
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3859 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003860 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003861 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003862
Ronald Cron49fafa92021-03-10 08:34:23 +01003863 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003864 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003865 * so we only set it (in the driver wrapper) after resources have been
3866 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003867 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003868 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003869 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003870 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003871 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003872 }
3873 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003874
3875 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003876 if (cipher_operation == MBEDTLS_ENCRYPT) {
3877 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003878 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003879 slot->key.data,
3880 slot->key.bytes,
3881 alg);
3882 } else {
3883 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003884 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003885 slot->key.data,
3886 slot->key.bytes,
3887 alg);
3888 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003889
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003890exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 if (status != PSA_SUCCESS) {
3892 psa_cipher_abort(operation);
3893 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003894
Ryan Everettc0053cc2024-02-14 16:27:13 +00003895 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003896
Gilles Peskine449bd832023-01-11 14:50:10 +01003897 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003898}
3899
Gilles Peskine449bd832023-01-11 14:50:10 +01003900psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3901 mbedtls_svc_key_id_t key,
3902 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003903{
Gilles Peskine449bd832023-01-11 14:50:10 +01003904 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003905}
3906
Gilles Peskine449bd832023-01-11 14:50:10 +01003907psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3908 mbedtls_svc_key_id_t key,
3909 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003910{
Gilles Peskine449bd832023-01-11 14:50:10 +01003911 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003912}
3913
Gilles Peskine449bd832023-01-11 14:50:10 +01003914psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3915 uint8_t *iv,
3916 size_t iv_size,
3917 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003918{
Ronald Cron6d051732020-10-01 14:10:20 +02003919 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003920 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07003921 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003922
Gilles Peskine449bd832023-01-11 14:50:10 +01003923 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003924 status = PSA_ERROR_BAD_STATE;
3925 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003926 }
3927
Gilles Peskine449bd832023-01-11 14:50:10 +01003928 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003929 status = PSA_ERROR_BAD_STATE;
3930 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003931 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003932
Ronald Cron2fb90522021-07-05 12:31:44 +02003933 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003934 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003935 status = PSA_ERROR_BUFFER_TOO_SMALL;
3936 goto exit;
3937 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003938
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02003940 status = PSA_ERROR_GENERIC_ERROR;
3941 goto exit;
3942 }
3943
Gilles Peskine449bd832023-01-11 14:50:10 +01003944 status = psa_generate_random(local_iv, default_iv_length);
3945 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003946 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 }
Ronald Cron5618a392021-03-26 09:52:26 +01003948
Gilles Peskine449bd832023-01-11 14:50:10 +01003949 status = psa_driver_wrapper_cipher_set_iv(operation,
3950 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003951
3952exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003953 if (status == PSA_SUCCESS) {
3954 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02003955 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003956 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003957 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02003958 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003959 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02003960 }
Ronald Cron6d051732020-10-01 14:10:20 +02003961
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003963}
3964
Gilles Peskine449bd832023-01-11 14:50:10 +01003965psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3966 const uint8_t *iv,
3967 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003968{
Ronald Cron6d051732020-10-01 14:10:20 +02003969 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003970
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003972 status = PSA_ERROR_BAD_STATE;
3973 goto exit;
3974 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003975
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003977 status = PSA_ERROR_BAD_STATE;
3978 goto exit;
3979 }
Ronald Crona0d68172021-03-26 10:15:08 +01003980
Gilles Peskine449bd832023-01-11 14:50:10 +01003981 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003982 status = PSA_ERROR_INVALID_ARGUMENT;
3983 goto exit;
3984 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003985
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 status = psa_driver_wrapper_cipher_set_iv(operation,
3987 iv,
3988 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003989
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003990exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003991 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003992 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 } else {
3994 psa_cipher_abort(operation);
3995 }
3996 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003997}
3998
Gilles Peskine449bd832023-01-11 14:50:10 +01003999psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4000 const uint8_t *input,
4001 size_t input_length,
4002 uint8_t *output,
4003 size_t output_size,
4004 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004005{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004006 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004007
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004009 status = PSA_ERROR_BAD_STATE;
4010 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004011 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004012
Gilles Peskine449bd832023-01-11 14:50:10 +01004013 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004014 status = PSA_ERROR_BAD_STATE;
4015 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004016 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004017
Gilles Peskine449bd832023-01-11 14:50:10 +01004018 status = psa_driver_wrapper_cipher_update(operation,
4019 input,
4020 input_length,
4021 output,
4022 output_size,
4023 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004024
4025exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 if (status != PSA_SUCCESS) {
4027 psa_cipher_abort(operation);
4028 }
Ronald Cron6d051732020-10-01 14:10:20 +02004029
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004031}
4032
Gilles Peskine449bd832023-01-11 14:50:10 +01004033psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4034 uint8_t *output,
4035 size_t output_size,
4036 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004037{
David Saadab4ecc272019-02-14 13:48:10 +02004038 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004039
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004041 status = PSA_ERROR_BAD_STATE;
4042 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004043 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004044
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004046 status = PSA_ERROR_BAD_STATE;
4047 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004048 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004049
Gilles Peskine449bd832023-01-11 14:50:10 +01004050 status = psa_driver_wrapper_cipher_finish(operation,
4051 output,
4052 output_size,
4053 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004054
4055exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004056 if (status == PSA_SUCCESS) {
4057 return psa_cipher_abort(operation);
4058 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004059 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004061
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004063 }
mohammad1603503973b2018-03-12 15:59:30 +02004064}
4065
Gilles Peskine449bd832023-01-11 14:50:10 +01004066psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004067{
Gilles Peskine449bd832023-01-11 14:50:10 +01004068 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004069 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004070 * in use. It's ok to call abort on such an object, and there's
4071 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004073 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004074
Gilles Peskine449bd832023-01-11 14:50:10 +01004075 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004076
Ronald Cron49fafa92021-03-10 08:34:23 +01004077 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004078 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004079 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004080
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004082}
4083
Gilles Peskine449bd832023-01-11 14:50:10 +01004084psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4085 psa_algorithm_t alg,
4086 const uint8_t *input,
4087 size_t input_length,
4088 uint8_t *output,
4089 size_t output_size,
4090 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004091{
4092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004093 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004094 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004095 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4096 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004097
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004099 status = PSA_ERROR_INVALID_ARGUMENT;
4100 goto exit;
4101 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004102
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4104 PSA_KEY_USAGE_ENCRYPT,
4105 alg);
4106 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004107 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004109
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4111 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004112 status = PSA_ERROR_GENERIC_ERROR;
4113 goto exit;
4114 }
4115
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 if (default_iv_length > 0) {
4117 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004118 status = PSA_ERROR_BUFFER_TOO_SMALL;
4119 goto exit;
4120 }
4121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 status = psa_generate_random(local_iv, default_iv_length);
4123 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004126 }
4127
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004128 status = psa_driver_wrapper_cipher_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004129 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004130 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004131 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004133
4134exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004135 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004137 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004138 }
Ronald Cron23919522021-07-08 19:00:07 +02004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 if (status == PSA_SUCCESS) {
4141 if (default_iv_length > 0) {
4142 memcpy(output, local_iv, default_iv_length);
4143 }
4144 *output_length += default_iv_length;
4145 } else {
4146 *output_length = 0;
4147 }
4148
4149 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004150}
4151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4153 psa_algorithm_t alg,
4154 const uint8_t *input,
4155 size_t input_length,
4156 uint8_t *output,
4157 size_t output_size,
4158 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004159{
4160 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004161 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004162 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004165 status = PSA_ERROR_INVALID_ARGUMENT;
4166 goto exit;
4167 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004168
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4170 PSA_KEY_USAGE_DECRYPT,
4171 alg);
4172 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004175
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4177 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004178 status = PSA_ERROR_INVALID_ARGUMENT;
4179 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004181 status = PSA_ERROR_INVALID_ARGUMENT;
4182 goto exit;
4183 }
4184
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004185 status = psa_driver_wrapper_cipher_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004186 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004187 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004189
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004190exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004191 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004193 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004195
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004197 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 }
Ronald Cron23919522021-07-08 19:00:07 +02004199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004201}
4202
4203
mohammad16035955c982018-04-26 00:53:03 +03004204/****************************************************************/
4205/* AEAD */
4206/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004207
Paul Elliottbaff51c2021-09-28 17:44:45 +01004208/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004209static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004210{
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004212}
4213
4214/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004215static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4216 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004217{
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004219
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004221#if defined(PSA_WANT_ALG_GCM)
4222 case PSA_ALG_GCM:
4223 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004224 * arbitrarily large nonces. Please note that we do not generally
4225 * recommend the usage of nonces of greater length than
4226 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4227 * size, which can then lead to collisions if you encrypt a very
4228 * large number of messages.*/
4229 if (nonce_length != 0) {
4230 return PSA_SUCCESS;
4231 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004232 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004233#endif /* PSA_WANT_ALG_GCM */
4234#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004235 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 if (nonce_length >= 7 && nonce_length <= 13) {
4237 return PSA_SUCCESS;
4238 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004239 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004240#endif /* PSA_WANT_ALG_CCM */
4241#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004242 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 if (nonce_length == 12) {
4244 return PSA_SUCCESS;
4245 } else if (nonce_length == 8) {
4246 return PSA_ERROR_NOT_SUPPORTED;
4247 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004248 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004249#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004250 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004251 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004253 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004256}
4257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004259{
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4261 return PSA_ERROR_INVALID_ARGUMENT;
4262 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004263
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004265}
4266
Gilles Peskine449bd832023-01-11 14:50:10 +01004267psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4268 psa_algorithm_t alg,
4269 const uint8_t *nonce,
4270 size_t nonce_length,
4271 const uint8_t *additional_data,
4272 size_t additional_data_length,
4273 const uint8_t *plaintext,
4274 size_t plaintext_length,
4275 uint8_t *ciphertext,
4276 size_t ciphertext_size,
4277 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004278{
Ronald Cron215633c2021-03-16 17:15:37 +01004279 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4280 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004281
mohammad1603f08a5502018-06-03 15:05:47 +03004282 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004283
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 status = psa_aead_check_algorithm(alg);
4285 if (status != PSA_SUCCESS) {
4286 return status;
4287 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004288
Ronald Cron9a986162021-03-26 12:40:07 +01004289 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4291 if (status != PSA_SUCCESS) {
4292 return status;
4293 }
mohammad16035955c982018-04-26 00:53:03 +03004294
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 status = psa_aead_check_nonce_length(alg, nonce_length);
4296 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004297 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004299
Ronald Cronde822812021-03-17 16:08:20 +01004300 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004301 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004302 alg,
4303 nonce, nonce_length,
4304 additional_data, additional_data_length,
4305 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004307
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4309 memset(ciphertext, 0, ciphertext_size);
4310 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004311
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004312exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004313 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004314
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004316}
4317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4319 psa_algorithm_t alg,
4320 const uint8_t *nonce,
4321 size_t nonce_length,
4322 const uint8_t *additional_data,
4323 size_t additional_data_length,
4324 const uint8_t *ciphertext,
4325 size_t ciphertext_length,
4326 uint8_t *plaintext,
4327 size_t plaintext_size,
4328 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004329{
Ronald Cron215633c2021-03-16 17:15:37 +01004330 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4331 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004332
Gilles Peskineee652a32018-06-01 19:23:52 +02004333 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004334
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 status = psa_aead_check_algorithm(alg);
4336 if (status != PSA_SUCCESS) {
4337 return status;
4338 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004339
Ronald Cron9a986162021-03-26 12:40:07 +01004340 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4342 if (status != PSA_SUCCESS) {
4343 return status;
4344 }
mohammad16035955c982018-04-26 00:53:03 +03004345
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 status = psa_aead_check_nonce_length(alg, nonce_length);
4347 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004348 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004350
Ronald Cronde822812021-03-17 16:08:20 +01004351 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004352 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004353 alg,
4354 nonce, nonce_length,
4355 additional_data, additional_data_length,
4356 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 if (status != PSA_SUCCESS && plaintext_size != 0) {
4360 memset(plaintext, 0, plaintext_size);
4361 }
mohammad160360a64d02018-06-03 17:20:42 +03004362
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004363exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004364 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 return status;
mohammad16035955c982018-04-26 00:53:03 +03004367}
4368
Gilles Peskine449bd832023-01-11 14:50:10 +01004369static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4370{
4371 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004372
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004374#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004376 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4378 return PSA_ERROR_INVALID_ARGUMENT;
4379 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004380 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004381#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004382
Przemek Stekiel86679c72022-10-06 17:06:56 +02004383#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004385 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4387 return PSA_ERROR_INVALID_ARGUMENT;
4388 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004389 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004390#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004391
Przemek Stekiel86679c72022-10-06 17:06:56 +02004392#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004394 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 if (tag_len != 16) {
4396 return PSA_ERROR_INVALID_ARGUMENT;
4397 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004398 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004399#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004400
4401 default:
4402 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004404 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004406}
4407
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004408/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004409static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4410 int is_encrypt,
4411 mbedtls_svc_key_id_t key,
4412 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004413{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004414 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4415 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4416 psa_key_slot_t *slot = NULL;
4417 psa_key_usage_t key_usage = 0;
4418
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 status = psa_aead_check_algorithm(alg);
4420 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004421 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004423
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004425 status = PSA_ERROR_BAD_STATE;
4426 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004427 }
4428
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 if (operation->nonce_set || operation->lengths_set ||
4430 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004431 status = PSA_ERROR_BAD_STATE;
4432 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004433 }
4434
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004436 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004438 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4442 alg);
4443 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004444 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004446
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004448 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004450
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 if (is_encrypt) {
4452 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004453 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 slot->key.data,
4455 slot->key.bytes,
4456 alg);
4457 } else {
4458 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004459 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 slot->key.data,
4461 slot->key.bytes,
4462 alg);
4463 }
4464 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004465 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004467
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004468 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004469
4470exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00004471 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004472
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004474 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004476 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 } else {
4478 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004479 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004480
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004482}
4483
Paul Elliott302ff6b2021-04-20 18:10:30 +01004484/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004485psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4486 mbedtls_svc_key_id_t key,
4487 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004488{
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004490}
4491
4492/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004493psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4494 mbedtls_svc_key_id_t key,
4495 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004496{
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004498}
4499
4500/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004501psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4502 uint8_t *nonce,
4503 size_t nonce_size,
4504 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004505{
4506 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004507 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004508 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004509
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004510 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004511
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004513 status = PSA_ERROR_BAD_STATE;
4514 goto exit;
4515 }
4516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004518 status = PSA_ERROR_BAD_STATE;
4519 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004520 }
4521
Paul Elliotte193ea82021-10-01 13:00:16 +01004522 /* For CCM, this size may not be correct according to the PSA
4523 * specification. The PSA Crypto 1.0.1 specification states:
4524 *
4525 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4526 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4527 *
4528 * However this restriction that L has to be the smallest integer is not
4529 * applied in practice, and it is not implementable here since the
4530 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4532 operation->alg);
4533 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004534 status = PSA_ERROR_BUFFER_TOO_SMALL;
4535 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004536 }
4537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 status = psa_generate_random(local_nonce, required_nonce_size);
4539 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004540 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004542
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004544
Paul Elliott39dc6b82021-05-11 19:16:09 +01004545exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 if (status == PSA_SUCCESS) {
4547 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004548 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 } else {
4550 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004551 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004552
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004554}
4555
4556/* Set the nonce for a multipart authenticated encryption or decryption
4557 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004558psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4559 const uint8_t *nonce,
4560 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004561{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4563
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004565 status = PSA_ERROR_BAD_STATE;
4566 goto exit;
4567 }
4568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004570 status = PSA_ERROR_BAD_STATE;
4571 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004572 }
4573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4575 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004576 status = PSA_ERROR_INVALID_ARGUMENT;
4577 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004578 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004579
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4581 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004582
Paul Elliott39dc6b82021-05-11 19:16:09 +01004583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004585 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 } else {
4587 psa_aead_abort(operation);
4588 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004589
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004591}
4592
4593/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004594psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4595 size_t ad_length,
4596 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004597{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004598 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4599
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004601 status = PSA_ERROR_BAD_STATE;
4602 goto exit;
4603 }
4604
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 if (operation->lengths_set || operation->ad_started ||
4606 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004607 status = PSA_ERROR_BAD_STATE;
4608 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004609 }
4610
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004612#if defined(PSA_WANT_ALG_GCM)
4613 case PSA_ALG_GCM:
4614 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 * bits. Without the guard this code will generate warnings on 32bit
4616 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004617#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 if (((uint64_t) ad_length) >> 61 != 0 ||
4619 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004620 status = PSA_ERROR_INVALID_ARGUMENT;
4621 goto exit;
4622 }
Paul Elliott325d3742021-09-27 17:56:28 +01004623#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004624 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004625#endif /* PSA_WANT_ALG_GCM */
4626#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004627 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004629 status = PSA_ERROR_INVALID_ARGUMENT;
4630 goto exit;
4631 }
4632 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004633#endif /* PSA_WANT_ALG_CCM */
4634#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004635 case PSA_ALG_CHACHA20_POLY1305:
4636 /* No length restrictions for ChaChaPoly. */
4637 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004638#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004639 default:
4640 break;
4641 }
Paul Elliott325d3742021-09-27 17:56:28 +01004642
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4644 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004645
Paul Elliott39dc6b82021-05-11 19:16:09 +01004646exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004648 operation->ad_remaining = ad_length;
4649 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004650 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 } else {
4652 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004653 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004654
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004656}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004657
4658/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004659psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4660 const uint8_t *input,
4661 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004662{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004663 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4664
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004666 status = PSA_ERROR_BAD_STATE;
4667 goto exit;
4668 }
4669
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004671 status = PSA_ERROR_BAD_STATE;
4672 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004673 }
4674
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 if (operation->lengths_set) {
4676 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004677 status = PSA_ERROR_INVALID_ARGUMENT;
4678 goto exit;
4679 }
4680
4681 operation->ad_remaining -= input_length;
4682 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004683#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004685 status = PSA_ERROR_BAD_STATE;
4686 goto exit;
4687 }
4688#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004689
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 status = psa_driver_wrapper_aead_update_ad(operation, input,
4691 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004692
Paul Elliott39dc6b82021-05-11 19:16:09 +01004693exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004695 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 } else {
4697 psa_aead_abort(operation);
4698 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004699
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004701}
4702
4703/* Encrypt or decrypt a message fragment in an active multipart AEAD
4704 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004705psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4706 const uint8_t *input,
4707 size_t input_length,
4708 uint8_t *output,
4709 size_t output_size,
4710 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004711{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004712 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004713
4714 *output_length = 0;
4715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004717 status = PSA_ERROR_BAD_STATE;
4718 goto exit;
4719 }
4720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004722 status = PSA_ERROR_BAD_STATE;
4723 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004724 }
4725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004727 /* Additional data length was supplied, but not all the additional
4728 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004730 status = PSA_ERROR_INVALID_ARGUMENT;
4731 goto exit;
4732 }
4733
4734 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004736 status = PSA_ERROR_INVALID_ARGUMENT;
4737 goto exit;
4738 }
4739
4740 operation->body_remaining -= input_length;
4741 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004742#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004744 status = PSA_ERROR_BAD_STATE;
4745 goto exit;
4746 }
4747#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004748
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4750 output, output_size,
4751 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004752
Paul Elliott39dc6b82021-05-11 19:16:09 +01004753exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004755 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 } else {
4757 psa_aead_abort(operation);
4758 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004759
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004761}
4762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004764{
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 if (operation->id == 0 || !operation->nonce_set) {
4766 return PSA_ERROR_BAD_STATE;
4767 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004768
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4770 operation->body_remaining != 0)) {
4771 return PSA_ERROR_INVALID_ARGUMENT;
4772 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004773
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004775}
4776
Paul Elliott302ff6b2021-04-20 18:10:30 +01004777/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004778psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4779 uint8_t *ciphertext,
4780 size_t ciphertext_size,
4781 size_t *ciphertext_length,
4782 uint8_t *tag,
4783 size_t tag_size,
4784 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004785{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004786 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4787
Paul Elliott302ff6b2021-04-20 18:10:30 +01004788 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004789 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004790
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 status = psa_aead_final_checks(operation);
4792 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004793 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004795
Gilles Peskine449bd832023-01-11 14:50:10 +01004796 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004797 status = PSA_ERROR_BAD_STATE;
4798 goto exit;
4799 }
4800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4802 ciphertext_size,
4803 ciphertext_length,
4804 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004805
Paul Elliott39dc6b82021-05-11 19:16:09 +01004806exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004807
4808
Paul Elliottf47b0952021-05-21 18:02:33 +01004809 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004810 * the zero tag size, make sure the tag is set to something implausible.
4811 * Even if the operation succeeds, make sure we clear the rest of the
4812 * buffer to prevent potential leakage of anything previously placed in
4813 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004814 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004815
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004819}
4820
4821/* Finish authenticating and decrypting a message in a multipart AEAD
4822 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004823psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4824 uint8_t *plaintext,
4825 size_t plaintext_size,
4826 size_t *plaintext_length,
4827 const uint8_t *tag,
4828 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004829{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004830 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4831
Paul Elliott302ff6b2021-04-20 18:10:30 +01004832 *plaintext_length = 0;
4833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 status = psa_aead_final_checks(operation);
4835 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004836 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004838
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004840 status = PSA_ERROR_BAD_STATE;
4841 goto exit;
4842 }
4843
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4845 plaintext_size,
4846 plaintext_length,
4847 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004848
4849exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004853}
4854
4855/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004856psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004857{
4858 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4859
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004861 /* The object has (apparently) been initialized but it is not (yet)
4862 * in use. It's ok to call abort on such an object, and there's
4863 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004865 }
4866
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004872}
4873
Gilles Peskinee59236f2018-01-27 23:32:46 +01004874/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004875/* Generators */
4876/****************************************************************/
4877
Przemek Stekiel69c46792022-06-10 12:59:51 +02004878#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004879 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004880 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05304881 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304882 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08004883#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004884#endif /* At least one builtin KDF */
4885
Przemek Stekiel69c46792022-06-10 12:59:51 +02004886#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004887 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4888 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4889static psa_status_t psa_key_derivation_start_hmac(
4890 psa_mac_operation_t *operation,
4891 psa_algorithm_t hash_alg,
4892 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02004894{
4895 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4898 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4899 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02004900
Steven Cooreman72f736a2021-05-07 14:14:37 +02004901 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02004903
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 status = psa_driver_wrapper_mac_sign_setup(operation,
4905 &attributes,
4906 hmac_key, hmac_key_length,
4907 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02004908
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 psa_reset_key_attributes(&attributes);
4910 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02004911}
4912#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004913
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004914#define HKDF_STATE_INIT 0 /* no input yet */
4915#define HKDF_STATE_STARTED 1 /* got salt */
4916#define HKDF_STATE_KEYED 2 /* got key */
4917#define HKDF_STATE_OUTPUT 3 /* output started */
4918
Gilles Peskinecbe66502019-05-16 16:59:18 +02004919static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01004920 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004921{
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4923 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4924 } else {
4925 return operation->alg;
4926 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004927}
4928
Gilles Peskine449bd832023-01-11 14:50:10 +01004929psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004930{
4931 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4933 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004934 /* The object has (apparently) been initialized but it is not
4935 * in use. It's ok to call abort on such an object, and there's
4936 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004938#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
4940 mbedtls_free(operation->ctx.hkdf.info);
4941 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4942 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004943#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004944#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4945 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4947 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4948 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4949 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004950 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02004952 }
4953
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004955 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01004957 }
4958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004960 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01004962 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01004963#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004965 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02004967 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01004968#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02004969 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004970
4971 /* We leave the fields Ai and output_block to be erased safely by the
4972 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004974#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4975 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004976#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
4978 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
4979 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
4980 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004981#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304982#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05304983 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304984 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004985 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304986 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304987 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304988
4989 status = PSA_SUCCESS;
4990 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304991#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004992 {
4993 status = PSA_ERROR_BAD_STATE;
4994 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 mbedtls_platform_zeroize(operation, sizeof(*operation));
4996 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004997}
4998
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004999psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005001{
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005003 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005005 }
5006
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005007 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005009}
5010
Gilles Peskine449bd832023-01-11 14:50:10 +01005011psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5012 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005013{
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 if (operation->alg == 0) {
5015 return PSA_ERROR_BAD_STATE;
5016 }
5017 if (capacity > operation->capacity) {
5018 return PSA_ERROR_INVALID_ARGUMENT;
5019 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005020 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005022}
5023
Przemek Stekiel69c46792022-06-10 12:59:51 +02005024#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005025/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005026static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5027 psa_algorithm_t kdf_alg,
5028 uint8_t *output,
5029 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005030{
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5032 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005033 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005034 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005035#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005037#else
5038 const uint8_t last_block = 0xff;
5039#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005040
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 if (hkdf->state < HKDF_STATE_KEYED ||
5042 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005043#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005045#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 )) {
5047 return PSA_ERROR_BAD_STATE;
5048 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005049 hkdf->state = HKDF_STATE_OUTPUT;
5050
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005052 /* Copy what remains of the current block */
5053 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005055 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 }
5057 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005058 output += n;
5059 output_length -= n;
5060 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005062 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005064 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005065 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005066 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005067 * object was corrupted or if this function is called directly
5068 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 if (hkdf->block_number == last_block) {
5070 return PSA_ERROR_BAD_STATE;
5071 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005072
5073 /* We need a new block */
5074 ++hkdf->block_number;
5075 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005076
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5078 hash_alg,
5079 hkdf->prk,
5080 hash_length);
5081 if (status != PSA_SUCCESS) {
5082 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005083 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005084
5085 if (hkdf->block_number != 1) {
5086 status = psa_mac_update(&hkdf->hmac,
5087 hkdf->output_block,
5088 hash_length);
5089 if (status != PSA_SUCCESS) {
5090 return status;
5091 }
5092 }
5093 status = psa_mac_update(&hkdf->hmac,
5094 hkdf->info,
5095 hkdf->info_length);
5096 if (status != PSA_SUCCESS) {
5097 return status;
5098 }
5099 status = psa_mac_update(&hkdf->hmac,
5100 &hkdf->block_number, 1);
5101 if (status != PSA_SUCCESS) {
5102 return status;
5103 }
5104 status = psa_mac_sign_finish(&hkdf->hmac,
5105 hkdf->output_block,
5106 sizeof(hkdf->output_block),
5107 &hmac_output_length);
5108 if (status != PSA_SUCCESS) {
5109 return status;
5110 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005111 }
5112
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005114}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005115#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005116
John Durkop07cc04a2020-11-16 22:08:34 -08005117#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5118 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005119static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5120 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005122{
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5124 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005125 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5126 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005127 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005128
Janos Follath7742fee2019-06-17 12:58:10 +01005129 /* We can't be wanting more output after block 0xff, otherwise
5130 * the capacity check in psa_key_derivation_output_bytes() would have
5131 * prevented this call. It could happen only if the operation
5132 * object was corrupted or if this function is called directly
5133 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 if (tls12_prf->block_number == 0xff) {
5135 return PSA_ERROR_CORRUPTION_DETECTED;
5136 }
Janos Follath7742fee2019-06-17 12:58:10 +01005137
5138 /* We need a new block */
5139 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005140 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005141
5142 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5143 *
5144 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5145 *
5146 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5147 * HMAC_hash(secret, A(2) + seed) +
5148 * HMAC_hash(secret, A(3) + seed) + ...
5149 *
5150 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005151 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005152 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005153 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005154 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005155 * is currently extracted as `output_block` and where i is
5156 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005157 */
5158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 status = psa_key_derivation_start_hmac(&hmac,
5160 hash_alg,
5161 tls12_prf->secret,
5162 tls12_prf->secret_length);
5163 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005164 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005166
5167 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005169 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5170 * the variable seed and in this instance means it in the context of the
5171 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 status = psa_mac_update(&hmac,
5173 tls12_prf->label,
5174 tls12_prf->label_length);
5175 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005176 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 }
5178 status = psa_mac_update(&hmac,
5179 tls12_prf->seed,
5180 tls12_prf->seed_length);
5181 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005182 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 }
5184 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005185 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5187 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005188 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005190 }
5191
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 status = psa_mac_sign_finish(&hmac,
5193 tls12_prf->Ai, hash_length,
5194 &hmac_output_length);
5195 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005196 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 }
5198 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005199 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005201
5202 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 status = psa_key_derivation_start_hmac(&hmac,
5204 hash_alg,
5205 tls12_prf->secret,
5206 tls12_prf->secret_length);
5207 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005208 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 }
5210 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5211 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005212 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 }
5214 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5215 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005216 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 }
5218 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5219 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005220 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 }
5222 status = psa_mac_sign_finish(&hmac,
5223 tls12_prf->output_block, hash_length,
5224 &hmac_output_length);
5225 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005226 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005228
Janos Follath7742fee2019-06-17 12:58:10 +01005229
5230cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 cleanup_status = psa_mac_abort(&hmac);
5232 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005233 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005235
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005237}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005238
Janos Follath844eb0e2019-06-19 12:10:49 +01005239static psa_status_t psa_key_derivation_tls12_prf_read(
5240 psa_tls12_prf_key_derivation_t *tls12_prf,
5241 psa_algorithm_t alg,
5242 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005244{
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5246 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005247 psa_status_t status;
5248 uint8_t offset, length;
5249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005251 case PSA_TLS12_PRF_STATE_LABEL_SET:
5252 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5253 break;
5254 case PSA_TLS12_PRF_STATE_OUTPUT:
5255 break;
5256 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005258 }
5259
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005261 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 if (tls12_prf->left_in_block == 0) {
5263 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5264 alg);
5265 if (status != PSA_SUCCESS) {
5266 return status;
5267 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005268
5269 continue;
5270 }
5271
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005273 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005275 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005277
5278 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005280 output += length;
5281 output_length -= length;
5282 tls12_prf->left_in_block -= length;
5283 }
5284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005286}
John Durkop07cc04a2020-11-16 22:08:34 -08005287#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5288 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005289
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005290#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5291static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5292 psa_tls12_ecjpake_to_pms_t *ecjpake,
5293 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005295{
5296 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005297 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005298
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 if (output_length != 32) {
5300 return PSA_ERROR_INVALID_ARGUMENT;
5301 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005302
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5304 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5305 &output_size);
5306 if (status != PSA_SUCCESS) {
5307 return status;
5308 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005309
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 if (output_size != output_length) {
5311 return PSA_ERROR_GENERIC_ERROR;
5312 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005313
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005315}
5316#endif
5317
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305318#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305319static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5320 psa_pbkdf2_key_derivation_t *pbkdf2,
5321 psa_algorithm_t prf_alg,
5322 uint8_t prf_output_length,
5323 psa_key_attributes_t *attributes)
5324{
5325 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305326 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305327 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305328 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305329 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305330 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305331 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305332
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305333 mac_operation.is_sign = 1;
5334 mac_operation.mac_size = prf_output_length;
5335 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305336
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305337 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5338 attributes,
5339 pbkdf2->password,
5340 pbkdf2->password_length,
5341 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305342 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305343 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305344 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305345 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5346 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305347 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305348 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305349 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305350 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305351 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305352 }
5353 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5354 &mac_output_length);
5355 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305356 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305357 }
5358
5359 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305360 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305361 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305362 }
5363
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305364 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305365
5366 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305367 /* We are passing prf_output_length as mac_size because the driver
5368 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305369 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305370 status = psa_driver_wrapper_mac_compute(attributes,
5371 pbkdf2->password,
5372 pbkdf2->password_length,
5373 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305374 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305375 &mac_output_length);
5376 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305377 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305378 }
5379
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305380 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305381 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305382
5383cleanup:
5384 /* Zeroise buffers to clear sensitive data from memory. */
5385 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5386 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305387}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305388
5389static psa_status_t psa_key_derivation_pbkdf2_read(
5390 psa_pbkdf2_key_derivation_t *pbkdf2,
5391 psa_algorithm_t kdf_alg,
5392 uint8_t *output,
5393 size_t output_length)
5394{
5395 psa_status_t status;
5396 psa_algorithm_t prf_alg;
5397 uint8_t prf_output_length;
5398 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5399 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5400 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5401
5402 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5403 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5404 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5405 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305406 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5407 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305408 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305409 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305410 } else {
5411 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305412 }
5413
5414 switch (pbkdf2->state) {
5415 case PSA_PBKDF2_STATE_PASSWORD_SET:
5416 /* Initially we need a new block so bytes_used is equal to block size*/
5417 pbkdf2->bytes_used = prf_output_length;
5418 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5419 break;
5420 case PSA_PBKDF2_STATE_OUTPUT:
5421 break;
5422 default:
5423 return PSA_ERROR_BAD_STATE;
5424 }
5425
5426 while (output_length != 0) {
5427 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5428 if (n > output_length) {
5429 n = (uint8_t) output_length;
5430 }
5431 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5432 output += n;
5433 output_length -= n;
5434 pbkdf2->bytes_used += n;
5435
5436 if (output_length == 0) {
5437 break;
5438 }
5439
5440 /* We need a new block */
5441 pbkdf2->bytes_used = 0;
5442 pbkdf2->block_number++;
5443
5444 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5445 prf_output_length,
5446 &attributes);
5447 if (status != PSA_SUCCESS) {
5448 return status;
5449 }
5450 }
5451
5452 return PSA_SUCCESS;
5453}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305454#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305455
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005456psa_status_t psa_key_derivation_output_bytes(
5457 psa_key_derivation_operation_t *operation,
5458 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005460{
5461 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005465 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005467 }
5468
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005470 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005471 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005472 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005473 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005474 goto exit;
5475 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005477 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005478 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005479 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5480 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005481 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005482 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005484 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005485 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005486
Przemek Stekiel69c46792022-06-10 12:59:51 +02005487#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5489 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5490 output, output_length);
5491 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005492#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005493#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5494 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5496 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5497 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5498 kdf_alg, output,
5499 output_length);
5500 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005501#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5502 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005503#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005505 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5507 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005508#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305509#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305510 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305511 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5512 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305513 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305514#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005515
Gilles Peskineeab56e42018-07-12 17:12:33 +02005516 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005517 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005519 }
5520
5521exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005523 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005524 * This allows us to differentiate between exhausted operations and
5525 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5526 * operations. */
5527 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005529 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005531 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005533}
5534
David Brown7807bf72021-02-09 16:28:23 -07005535#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005536static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005537{
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 if (data_size >= 8) {
5539 mbedtls_des_key_set_parity(data);
5540 }
5541 if (data_size >= 16) {
5542 mbedtls_des_key_set_parity(data + 8);
5543 }
5544 if (data_size >= 24) {
5545 mbedtls_des_key_set_parity(data + 16);
5546 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005547}
David Brown7807bf72021-02-09 16:28:23 -07005548#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005549
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005550/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 * ECC keys on a Weierstrass elliptic curve require the generation
5552 * of a private key which is an integer
5553 * in the range [1, N - 1], where N is the boundary of the private key domain:
5554 * N is the prime p for Diffie-Hellman, or the order of the
5555 * curve’s base point for ECC.
5556 *
5557 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5558 * This function generates the private key using the following process:
5559 *
5560 * 1. Draw a byte string of length ceiling(m/8) bytes.
5561 * 2. If m is not a multiple of 8, set the most significant
5562 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5563 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5564 * 4. If k > N - 2, discard the result and return to step 1.
5565 * 5. Output k + 1 as the private key.
5566 *
5567 * This method allows compliance to NIST standards, specifically the methods titled
5568 * Key-Pair Generation by Testing Candidates in the following publications:
5569 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5570 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5571 * Diffie-Hellman keys.
5572 *
5573 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5574 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5575 *
5576 * Note: Function allocates memory for *data buffer, so given *data should be
5577 * always NULL.
5578 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005579#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5580#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005581static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005582 psa_key_slot_t *slot,
5583 size_t bits,
5584 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005585 uint8_t **data
5586 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005587{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005588 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005589 mbedtls_mpi k;
5590 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005592 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005593 size_t m;
5594 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005595
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 mbedtls_mpi_init(&k);
5597 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005598
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005599 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005601 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01005602 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005603
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005605 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005606 goto cleanup;
5607 }
5608
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005609 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005611
Gilles Peskine449bd832023-01-11 14:50:10 +01005612 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005613
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005614 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005615 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005616 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005617
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005618 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005619
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005620 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005622
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005623 /* Note: This function is always called with *data == NULL and it
5624 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 *data = mbedtls_calloc(1, m_bytes);
5626 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005627 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005628 goto cleanup;
5629 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005630
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005632 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005634 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005636
5637 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005639 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005640 * (8 * ceiling(m/8) - m) bits of the first byte in
5641 * the string to zero.
5642 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005644 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005645 }
5646
5647 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005648 * big-endian byte string.
5649 */
5650 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005651
5652 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 * Result of comparison is returned. When it indicates error
5654 * then this function is called again.
5655 */
5656 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005657 }
5658
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005659 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5661 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005662cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 if (ret != 0) {
5664 status = mbedtls_to_psa_error(ret);
5665 }
5666 if (status != PSA_SUCCESS) {
5667 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005668 *data = NULL;
5669 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 mbedtls_mpi_free(&k);
5671 mbedtls_mpi_free(&diff_N_2);
5672 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005673}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005674
5675/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5676 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5677 *
5678 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5679 * draw a 32-byte string and process it as specified in
5680 * Elliptic Curves for Security [RFC7748] §5.
5681 *
5682 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5683 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5684 *
5685 * Note: Function allocates memory for *data buffer, so given *data should be
5686 * always NULL.
5687 */
5688
5689static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5690 size_t bits,
5691 psa_key_derivation_operation_t *operation,
5692 uint8_t **data
5693 )
5694{
5695 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005697
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005699 case 255:
5700 output_length = 32;
5701 break;
5702 case 448:
5703 output_length = 56;
5704 break;
5705 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005707 break;
5708 }
5709
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005711
Gilles Peskine449bd832023-01-11 14:50:10 +01005712 if (*data == NULL) {
5713 return PSA_ERROR_INSUFFICIENT_MEMORY;
5714 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005715
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005717
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005719 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005723 case 255:
5724 (*data)[0] &= 248;
5725 (*data)[31] &= 127;
5726 (*data)[31] |= 64;
5727 break;
5728 case 448:
5729 (*data)[0] &= 252;
5730 (*data)[55] |= 128;
5731 break;
5732 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005734 break;
5735 }
5736
5737 return status;
5738}
Valerio Setti86587ab2023-06-27 13:09:30 +02005739#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5740static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5741 psa_key_slot_t *slot, size_t bits,
5742 psa_key_derivation_operation_t *operation, uint8_t **data)
5743{
5744 (void) slot;
5745 (void) bits;
5746 (void) operation;
5747 (void) data;
5748 return PSA_ERROR_NOT_SUPPORTED;
5749}
5750
5751static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5752 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5753{
5754 (void) bits;
5755 (void) operation;
5756 (void) data;
5757 return PSA_ERROR_NOT_SUPPORTED;
5758}
5759#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5760#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005761
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005762static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005763 psa_key_slot_t *slot,
5764 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005766{
5767 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305769 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005770 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005771
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5773 return PSA_ERROR_INVALID_ARGUMENT;
5774 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005775
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005776#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005777 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5779 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5780 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005781 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5783 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005784 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 }
5786 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005787 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5789 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005790 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005792 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005793 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005794#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005795 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 if (key_type_is_raw_bytes(slot->attr.type)) {
5797 if (bits % 8 != 0) {
5798 return PSA_ERROR_INVALID_ARGUMENT;
5799 }
5800 data = mbedtls_calloc(1, bytes);
5801 if (data == NULL) {
5802 return PSA_ERROR_INSUFFICIENT_MEMORY;
5803 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 status = psa_key_derivation_output_bytes(operation, data, bytes);
5806 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005807 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 }
David Brown12ca5032021-02-11 11:02:00 -07005809#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5811 psa_des_set_key_parity(data, bytes);
5812 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005813#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 } else {
5815 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005816 }
Ronald Crondd04d422020-11-28 15:14:42 +01005817
Ronald Cronbf33c932020-11-28 18:06:53 +01005818 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005819
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005820 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005821 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 &storage_size);
5823 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305824 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 }
Archanad8a83dc2021-06-14 10:04:16 +05305826 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 status = psa_allocate_buffer_to_slot(slot, storage_size);
5828 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305829 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 }
Archanad8a83dc2021-06-14 10:04:16 +05305831
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005832 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 data, bytes,
5834 slot->key.data,
5835 slot->key.bytes,
5836 &slot->key.bytes, &bits);
5837 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005838 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005840
5841exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 mbedtls_free(data);
5843 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005844}
5845
Gilles Peskine092ce512024-02-20 12:31:24 +01005846static const psa_key_production_parameters_t default_production_parameters =
5847 PSA_KEY_PRODUCTION_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005848
Gilles Peskine092ce512024-02-20 12:31:24 +01005849int psa_key_production_parameters_are_default(
5850 const psa_key_production_parameters_t *params,
5851 size_t params_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005852{
Gilles Peskine092ce512024-02-20 12:31:24 +01005853 if (params->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005854 return 0;
5855 }
Gilles Peskine092ce512024-02-20 12:31:24 +01005856 if (params_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005857 return 0;
5858 }
5859 return 1;
5860}
5861
5862psa_status_t psa_key_derivation_output_key_ext(
5863 const psa_key_attributes_t *attributes,
5864 psa_key_derivation_operation_t *operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005865 const psa_key_production_parameters_t *params,
5866 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005867 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005868{
5869 psa_status_t status;
5870 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005871 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005872
Ronald Cron81709fc2020-11-14 12:10:32 +01005873 *key = MBEDTLS_SVC_KEY_ID_INIT;
5874
Gilles Peskine0f84d622019-09-12 19:03:13 +02005875 /* Reject any attempt to create a zero-length key so that we don't
5876 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 if (psa_get_key_bits(attributes) == 0) {
5878 return PSA_ERROR_INVALID_ARGUMENT;
5879 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005880
Gilles Peskine092ce512024-02-20 12:31:24 +01005881 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005882 return PSA_ERROR_INVALID_ARGUMENT;
5883 }
5884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 if (operation->alg == PSA_ALG_NONE) {
5886 return PSA_ERROR_BAD_STATE;
5887 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005888
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 if (!operation->can_output_key) {
5890 return PSA_ERROR_NOT_PERMITTED;
5891 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5894 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005895#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005897 /* Deriving a key in a secure element is not implemented yet. */
5898 status = PSA_ERROR_NOT_SUPPORTED;
5899 }
5900#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 if (status == PSA_SUCCESS) {
5902 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005903 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005905 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 if (status == PSA_SUCCESS) {
5907 status = psa_finish_key_creation(slot, driver, key);
5908 }
5909 if (status != PSA_SUCCESS) {
5910 psa_fail_key_creation(slot, driver);
5911 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005912
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005914}
5915
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005916psa_status_t psa_key_derivation_output_key(
5917 const psa_key_attributes_t *attributes,
5918 psa_key_derivation_operation_t *operation,
5919 mbedtls_svc_key_id_t *key)
5920{
Gilles Peskinec81393b2024-02-14 20:51:28 +01005921 return psa_key_derivation_output_key_ext(attributes, operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005922 &default_production_parameters, 0,
Gilles Peskinec81393b2024-02-14 20:51:28 +01005923 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005924}
Gilles Peskineeab56e42018-07-12 17:12:33 +02005925
5926
5927/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005928/* Key derivation */
5929/****************************************************************/
5930
Steven Cooreman932ffb72021-02-15 12:14:32 +01005931#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005932static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005933{
5934#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5936 return 1;
5937 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005938#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005939#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5941 return 1;
5942 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005943#endif
5944#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5946 return 1;
5947 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005948#endif
5949#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5951 return 1;
5952 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005953#endif
5954#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5956 return 1;
5957 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005958#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005959#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5961 return 1;
5962 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005963#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05305964#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5965 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5966 return 1;
5967 }
5968#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05305969#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
5970 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5971 return 1;
5972 }
5973#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005975}
5976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005978{
5979 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 psa_status_t status = psa_hash_setup(&operation, alg);
5981 psa_hash_abort(&operation);
5982 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005983}
5984
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05305985static psa_status_t psa_key_derivation_set_maximum_capacity(
5986 psa_key_derivation_operation_t *operation,
5987 psa_algorithm_t kdf_alg)
5988{
5989#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5990 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5991 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
5992 return PSA_SUCCESS;
5993 }
5994#endif
5995#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
5996 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5997#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05305998 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05305999 PSA_KEY_TYPE_AES,
6000 128U,
6001 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306002#else
6003 operation->capacity = SIZE_MAX;
6004#endif
6005 return PSA_SUCCESS;
6006 }
6007#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6008
6009 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6010 * invalid or meaningless but it does not affect this function */
6011 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6012 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306013 if (hash_size == 0) {
6014 return PSA_ERROR_NOT_SUPPORTED;
6015 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306016
6017 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6018 * we might fail later, which is somewhat unfriendly and potentially
6019 * risk-prone. */
6020 psa_status_t status = psa_hash_try_support(hash_alg);
6021 if (status != PSA_SUCCESS) {
6022 return status;
6023 }
6024
6025#if defined(PSA_WANT_ALG_HKDF)
6026 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6027 operation->capacity = 255 * hash_size;
6028 } else
6029#endif
6030#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6031 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6032 operation->capacity = hash_size;
6033 } else
6034#endif
6035#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6036 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6037 operation->capacity = 255 * hash_size;
6038 } else
6039#endif
6040#if defined(PSA_WANT_ALG_TLS12_PRF)
6041 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6042 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6043 operation->capacity = SIZE_MAX;
6044 } else
6045#endif
6046#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6047 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6048 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6049 /* Master Secret is always 48 bytes
6050 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6051 operation->capacity = 48U;
6052 } else
6053#endif
6054#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6055 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6056#if (SIZE_MAX > UINT32_MAX)
6057 operation->capacity = UINT32_MAX * hash_size;
6058#else
6059 operation->capacity = SIZE_MAX;
6060#endif
6061 } else
6062#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6063 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306064 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306065 status = PSA_ERROR_NOT_SUPPORTED;
6066 }
6067 return status;
6068}
6069
Gilles Peskine969c5d62019-01-16 15:53:06 +01006070static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006071 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006073{
Janos Follath5fe19732019-06-20 15:09:30 +01006074 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6075 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006077
Gilles Peskine969c5d62019-01-16 15:53:06 +01006078 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 if (!is_kdf_alg_supported(kdf_alg)) {
6080 return PSA_ERROR_NOT_SUPPORTED;
6081 }
John Durkop07cc04a2020-11-16 22:08:34 -08006082
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306083 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6084 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306085 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006086}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006089{
6090#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 if (alg == PSA_ALG_ECDH) {
6092 return PSA_SUCCESS;
6093 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006094#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006095#if defined(PSA_WANT_ALG_FFDH)
6096 if (alg == PSA_ALG_FFDH) {
6097 return PSA_SUCCESS;
6098 }
6099#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006100 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006102}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006103
6104static int psa_key_derivation_allows_free_form_secret_input(
6105 psa_algorithm_t kdf_alg)
6106{
6107#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6108 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6109 return 0;
6110 }
6111#endif
6112 (void) kdf_alg;
6113 return 1;
6114}
John Durkop07cc04a2020-11-16 22:08:34 -08006115#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006116
Gilles Peskine449bd832023-01-11 14:50:10 +01006117psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6118 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006119{
6120 psa_status_t status;
6121
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 if (operation->alg != 0) {
6123 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006124 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6127 return PSA_ERROR_INVALID_ARGUMENT;
6128 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6129#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6130 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6131 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6132 status = psa_key_agreement_try_support(ka_alg);
6133 if (status != PSA_SUCCESS) {
6134 return status;
6135 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006136 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6137 return PSA_ERROR_INVALID_ARGUMENT;
6138 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6140#else
6141 return PSA_ERROR_NOT_SUPPORTED;
6142#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6143 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6144#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6145 status = psa_key_derivation_setup_kdf(operation, alg);
6146#else
6147 return PSA_ERROR_NOT_SUPPORTED;
6148#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6149 } else {
6150 return PSA_ERROR_INVALID_ARGUMENT;
6151 }
6152
6153 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006154 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 }
6156 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006157}
6158
Przemek Stekiel69c46792022-06-10 12:59:51 +02006159#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006160static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6161 psa_algorithm_t kdf_alg,
6162 psa_key_derivation_step_t step,
6163 const uint8_t *data,
6164 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006165{
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006167 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006169 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006170#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6172 return PSA_ERROR_INVALID_ARGUMENT;
6173 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006174#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 if (hkdf->state != HKDF_STATE_INIT) {
6176 return PSA_ERROR_BAD_STATE;
6177 } else {
6178 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6179 hash_alg,
6180 data, data_length);
6181 if (status != PSA_SUCCESS) {
6182 return status;
6183 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006184 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006186 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006187 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006188#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006190 /* We shouldn't be in different state as HKDF_EXPAND only allows
6191 * two inputs: SECRET (this case) and INFO which does not modify
6192 * the state. It could happen only if the hkdf
6193 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 if (hkdf->state != HKDF_STATE_INIT) {
6195 return PSA_ERROR_BAD_STATE;
6196 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006197
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006198 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6200 return PSA_ERROR_INVALID_ARGUMENT;
6201 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 memcpy(hkdf->prk, data, data_length);
6204 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006205#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006206 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006207 /* HKDF: If no salt was provided, use an empty salt.
6208 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006210#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6212 return PSA_ERROR_BAD_STATE;
6213 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006214#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6216 hash_alg,
6217 NULL, 0);
6218 if (status != PSA_SUCCESS) {
6219 return status;
6220 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006221 hkdf->state = HKDF_STATE_STARTED;
6222 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 if (hkdf->state != HKDF_STATE_STARTED) {
6224 return PSA_ERROR_BAD_STATE;
6225 }
6226 status = psa_mac_update(&hkdf->hmac,
6227 data, data_length);
6228 if (status != PSA_SUCCESS) {
6229 return status;
6230 }
6231 status = psa_mac_sign_finish(&hkdf->hmac,
6232 hkdf->prk,
6233 sizeof(hkdf->prk),
6234 &data_length);
6235 if (status != PSA_SUCCESS) {
6236 return status;
6237 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006238 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006239
Gilles Peskine2b522db2019-04-12 15:11:49 +02006240 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006241 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006242#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006244 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006246 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006248#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006249 {
6250 /* Block 0 is empty, and the next block will be
6251 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006253 }
6254
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006256 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006257#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6259 return PSA_ERROR_INVALID_ARGUMENT;
6260 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006261#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006262#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6264 hkdf->state == HKDF_STATE_INIT) {
6265 return PSA_ERROR_BAD_STATE;
6266 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006267#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 if (hkdf->state == HKDF_STATE_OUTPUT) {
6269 return PSA_ERROR_BAD_STATE;
6270 }
6271 if (hkdf->info_set) {
6272 return PSA_ERROR_BAD_STATE;
6273 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006274 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 if (data_length != 0) {
6276 hkdf->info = mbedtls_calloc(1, data_length);
6277 if (hkdf->info == NULL) {
6278 return PSA_ERROR_INSUFFICIENT_MEMORY;
6279 }
6280 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006281 }
6282 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006284 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006286 }
6287}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006288#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006289
John Durkop07cc04a2020-11-16 22:08:34 -08006290#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6291 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006292static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6293 const uint8_t *data,
6294 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006295{
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6297 return PSA_ERROR_BAD_STATE;
6298 }
Janos Follathf08e2652019-06-13 09:05:41 +01006299
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 if (data_length != 0) {
6301 prf->seed = mbedtls_calloc(1, data_length);
6302 if (prf->seed == NULL) {
6303 return PSA_ERROR_INSUFFICIENT_MEMORY;
6304 }
Janos Follathf08e2652019-06-13 09:05:41 +01006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006307 prf->seed_length = data_length;
6308 }
Janos Follathf08e2652019-06-13 09:05:41 +01006309
Gilles Peskine43f958b2020-12-13 14:55:14 +01006310 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006313}
6314
Gilles Peskine449bd832023-01-11 14:50:10 +01006315static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6316 const uint8_t *data,
6317 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006318{
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6320 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6321 return PSA_ERROR_BAD_STATE;
6322 }
Janos Follath81550542019-06-13 14:26:34 +01006323
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 if (data_length != 0) {
6325 prf->secret = mbedtls_calloc(1, data_length);
6326 if (prf->secret == NULL) {
6327 return PSA_ERROR_INSUFFICIENT_MEMORY;
6328 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006331 prf->secret_length = data_length;
6332 }
Janos Follath81550542019-06-13 14:26:34 +01006333
Gilles Peskine43f958b2020-12-13 14:55:14 +01006334 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006335
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006337}
6338
Gilles Peskine449bd832023-01-11 14:50:10 +01006339static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6340 const uint8_t *data,
6341 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006342{
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6344 return PSA_ERROR_BAD_STATE;
6345 }
Janos Follath63028dd2019-06-13 09:15:47 +01006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 if (data_length != 0) {
6348 prf->label = mbedtls_calloc(1, data_length);
6349 if (prf->label == NULL) {
6350 return PSA_ERROR_INSUFFICIENT_MEMORY;
6351 }
Janos Follath63028dd2019-06-13 09:15:47 +01006352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006354 prf->label_length = data_length;
6355 }
Janos Follath63028dd2019-06-13 09:15:47 +01006356
Gilles Peskine43f958b2020-12-13 14:55:14 +01006357 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006358
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006360}
6361
Gilles Peskine449bd832023-01-11 14:50:10 +01006362static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6363 psa_key_derivation_step_t step,
6364 const uint8_t *data,
6365 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006366{
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006368 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006370 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006372 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006374 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006376 }
6377}
John Durkop07cc04a2020-11-16 22:08:34 -08006378#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6379 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6380
6381#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6382static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6383 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006384 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006386{
6387 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6389 4 + data_length + prf->other_secret_length :
6390 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6393 return PSA_ERROR_INVALID_ARGUMENT;
6394 }
John Durkop07cc04a2020-11-16 22:08:34 -08006395
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 uint8_t *pms = mbedtls_calloc(1, pms_len);
6397 if (pms == NULL) {
6398 return PSA_ERROR_INSUFFICIENT_MEMORY;
6399 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006400 uint8_t *cur = pms;
6401
6402 /* pure-PSK:
6403 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006404 *
6405 * The premaster secret is formed as follows: if the PSK is N octets
6406 * long, concatenate a uint16 with the value N, N zero octets, a second
6407 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006408 *
6409 * mixed-PSK:
6410 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6411 * follows: concatenate a uint16 with the length of the other secret,
6412 * the other secret itself, uint16 with the length of PSK, and the
6413 * PSK itself.
6414 * For details please check:
6415 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6416 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6417 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006418 */
6419
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6421 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6422 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6423 if (prf->other_secret_length != 0) {
6424 memcpy(cur, prf->other_secret, prf->other_secret_length);
6425 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006426 cur += prf->other_secret_length;
6427 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 } else {
6429 *cur++ = MBEDTLS_BYTE_1(data_length);
6430 *cur++ = MBEDTLS_BYTE_0(data_length);
6431 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006432 cur += data_length;
6433 }
6434
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 *cur++ = MBEDTLS_BYTE_1(data_length);
6436 *cur++ = MBEDTLS_BYTE_0(data_length);
6437 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006438 cur += data_length;
6439
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006440 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006441
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006442 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006444}
Janos Follath6660f0e2019-06-17 08:44:03 +01006445
Przemek Stekiele47201b2022-04-19 13:53:28 +02006446static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6447 psa_tls12_prf_key_derivation_t *prf,
6448 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006450{
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6452 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006453 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006454
6455 if (data_length != 0) {
6456 prf->other_secret = mbedtls_calloc(1, data_length);
6457 if (prf->other_secret == NULL) {
6458 return PSA_ERROR_INSUFFICIENT_MEMORY;
6459 }
6460
6461 memcpy(prf->other_secret, data, data_length);
6462 prf->other_secret_length = data_length;
6463 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006464 prf->other_secret_length = 0;
6465 }
6466
6467 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006470}
6471
Janos Follath6660f0e2019-06-17 08:44:03 +01006472static psa_status_t psa_tls12_prf_psk_to_ms_input(
6473 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006474 psa_key_derivation_step_t step,
6475 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006477{
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006479 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 return psa_tls12_prf_psk_to_ms_set_key(prf,
6481 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006482 break;
6483 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6485 data,
6486 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006487 break;
6488 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006490 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006491
Przemek Stekiele47201b2022-04-19 13:53:28 +02006492 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006493}
John Durkop07cc04a2020-11-16 22:08:34 -08006494#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006495
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006496#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6497static psa_status_t psa_tls12_ecjpake_to_pms_input(
6498 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006499 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006500 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006502{
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6504 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6505 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006506 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006507
6508 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 if (data[0] != 0x04) {
6510 return PSA_ERROR_INVALID_ARGUMENT;
6511 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006512
6513 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006515
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006517}
6518#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306519
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306520#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306521static psa_status_t psa_pbkdf2_set_input_cost(
6522 psa_pbkdf2_key_derivation_t *pbkdf2,
6523 psa_key_derivation_step_t step,
6524 uint64_t data)
6525{
6526 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6527 return PSA_ERROR_INVALID_ARGUMENT;
6528 }
6529
6530 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6531 return PSA_ERROR_BAD_STATE;
6532 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306533
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306534 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306535 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306536 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306537
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306538 if (data == 0) {
6539 return PSA_ERROR_INVALID_ARGUMENT;
6540 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306541
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306542 pbkdf2->input_cost = data;
6543 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6544
6545 return PSA_SUCCESS;
6546}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306547
6548static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6549 const uint8_t *data,
6550 size_t data_length)
6551{
Gilles Peskineead17662023-08-20 22:02:51 +02006552 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6553 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6554 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6555 /* Appending to existing salt. No state change. */
6556 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306557 return PSA_ERROR_BAD_STATE;
6558 }
6559
Gilles Peskineca57d782023-07-20 19:08:06 +02006560 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006561 /* Appending an empty string, nothing to do. */
6562 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306563 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306564
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306565 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6566 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306567 return PSA_ERROR_INSUFFICIENT_MEMORY;
6568 }
6569
Gilles Peskineead17662023-08-20 22:02:51 +02006570 if (pbkdf2->salt_length != 0) {
6571 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6572 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306573 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306574 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306575 mbedtls_free(pbkdf2->salt);
6576 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306577 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306578 return PSA_SUCCESS;
6579}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306580
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306581#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306582static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306583 const uint8_t *input,
6584 size_t input_len,
6585 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306586 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306587{
6588 psa_status_t status = PSA_SUCCESS;
6589 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6590 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306591 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306592 } else {
6593 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306594 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306595 }
6596 return status;
6597}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306598#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306599
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306600#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306601static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6602 size_t input_len,
6603 uint8_t *output,
6604 size_t *output_len)
6605{
6606 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306607 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306609 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306610 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6611 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6612 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306613 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306614 * mac_size as the driver function sets mac_output_length = mac_size
6615 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306616 status = psa_driver_wrapper_mac_compute(&attributes,
6617 zeros, sizeof(zeros),
6618 PSA_ALG_CMAC, input, input_len,
6619 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306620 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6621 128U,
6622 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306623 output_len);
6624 } else {
6625 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306626 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306627 }
6628 return status;
6629}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306630#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306631
6632static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306633 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306634 const uint8_t *data,
6635 size_t data_length)
6636{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306637 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306638 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6639 return PSA_ERROR_BAD_STATE;
6640 }
6641
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306642#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306643 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6644 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6645 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6646 pbkdf2->password,
6647 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306648 } else
6649#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6650#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6651 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306652 status = psa_pbkdf2_cmac_set_password(data, data_length,
6653 pbkdf2->password,
6654 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306655 } else
6656#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6657 {
6658 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306659 }
6660
6661 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6662
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306663 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306664}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306665
6666static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306667 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306668 psa_key_derivation_step_t step,
6669 const uint8_t *data,
6670 size_t data_length)
6671{
6672 switch (step) {
6673 case PSA_KEY_DERIVATION_INPUT_SALT:
6674 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6675 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306676 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306677 default:
6678 return PSA_ERROR_INVALID_ARGUMENT;
6679 }
6680}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306681#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306682
Gilles Peskineb8965192019-09-24 16:21:10 +02006683/** Check whether the given key type is acceptable for the given
6684 * input step of a key derivation.
6685 *
6686 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6687 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6688 * Both secret and non-secret inputs can alternatively have the type
6689 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6690 * that the input was passed as a buffer rather than via a key object.
6691 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006692static int psa_key_derivation_check_input_type(
6693 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006695{
Gilles Peskine449bd832023-01-11 14:50:10 +01006696 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006697 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006698 if (key_type == PSA_KEY_TYPE_DERIVE) {
6699 return PSA_SUCCESS;
6700 }
6701 if (key_type == PSA_KEY_TYPE_NONE) {
6702 return PSA_SUCCESS;
6703 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006704 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006705 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006706 if (key_type == PSA_KEY_TYPE_DERIVE) {
6707 return PSA_SUCCESS;
6708 }
6709 if (key_type == PSA_KEY_TYPE_NONE) {
6710 return PSA_SUCCESS;
6711 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006712 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006713 case PSA_KEY_DERIVATION_INPUT_LABEL:
6714 case PSA_KEY_DERIVATION_INPUT_SALT:
6715 case PSA_KEY_DERIVATION_INPUT_INFO:
6716 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006717 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6718 return PSA_SUCCESS;
6719 }
6720 if (key_type == PSA_KEY_TYPE_NONE) {
6721 return PSA_SUCCESS;
6722 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006723 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306724 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6725 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6726 return PSA_SUCCESS;
6727 }
6728 if (key_type == PSA_KEY_TYPE_DERIVE) {
6729 return PSA_SUCCESS;
6730 }
6731 if (key_type == PSA_KEY_TYPE_NONE) {
6732 return PSA_SUCCESS;
6733 }
6734 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006735 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006736 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006737}
6738
Janos Follathb80a94e2019-06-12 15:54:46 +01006739static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006740 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006741 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006742 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006743 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006744 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006745{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006746 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006747 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006748
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 status = psa_key_derivation_check_input_type(step, key_type);
6750 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006751 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006753
Przemek Stekiel69c46792022-06-10 12:59:51 +02006754#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006755 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6756 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6757 step, data, data_length);
6758 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006759#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006760#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6762 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6763 step, data, data_length);
6764 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006765#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6766#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6768 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6769 step, data, data_length);
6770 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006771#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006772#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006774 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6776 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006777#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306778#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306779 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306780 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6781 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306782 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306783#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006784 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006785 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006786 (void) data;
6787 (void) data_length;
6788 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006789 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006790 }
6791
Gilles Peskine224b0d62019-09-23 18:13:17 +02006792exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006793 if (status != PSA_SUCCESS) {
6794 psa_key_derivation_abort(operation);
6795 }
6796 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006797}
6798
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306799static psa_status_t psa_key_derivation_input_integer_internal(
6800 psa_key_derivation_operation_t *operation,
6801 psa_key_derivation_step_t step,
6802 uint64_t value)
6803{
6804 psa_status_t status;
6805 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6806
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306807#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306808 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306809 status = psa_pbkdf2_set_input_cost(
6810 &operation->ctx.pbkdf2, step, value);
6811 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306812#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306813 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306814 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306815 (void) value;
6816 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306817 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306818 }
6819
6820 if (status != PSA_SUCCESS) {
6821 psa_key_derivation_abort(operation);
6822 }
6823 return status;
6824}
6825
Janos Follath51f4a0f2019-06-14 11:35:55 +01006826psa_status_t psa_key_derivation_input_bytes(
6827 psa_key_derivation_operation_t *operation,
6828 psa_key_derivation_step_t step,
6829 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006831{
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 return psa_key_derivation_input_internal(operation, step,
6833 PSA_KEY_TYPE_NONE,
6834 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006835}
6836
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306837psa_status_t psa_key_derivation_input_integer(
6838 psa_key_derivation_operation_t *operation,
6839 psa_key_derivation_step_t step,
6840 uint64_t value)
6841{
6842 return psa_key_derivation_input_integer_internal(operation, step, value);
6843}
6844
Janos Follath51f4a0f2019-06-14 11:35:55 +01006845psa_status_t psa_key_derivation_input_key(
6846 psa_key_derivation_operation_t *operation,
6847 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006849{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006850 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006851 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006852 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006853
Ronald Cron5c522922020-11-14 16:35:34 +01006854 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6856 if (status != PSA_SUCCESS) {
6857 psa_key_derivation_abort(operation);
6858 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006859 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006860
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306861 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6862 * permission to output to a key object. */
6863 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6864 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006865 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006867
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 status = psa_key_derivation_input_internal(operation,
6869 step, slot->attr.type,
6870 slot->key.data,
6871 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006872
Ryan Everett5ac6fa72024-02-14 17:11:36 +00006873 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006874
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006876}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006877
6878
6879
6880/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006881/* Key agreement */
6882/****************************************************************/
6883
Gilles Peskine449bd832023-01-11 14:50:10 +01006884psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6885 const uint8_t *key_buffer,
6886 size_t key_buffer_size,
6887 psa_algorithm_t alg,
6888 const uint8_t *peer_key,
6889 size_t peer_key_length,
6890 uint8_t *shared_secret,
6891 size_t shared_secret_size,
6892 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006893{
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006895#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006896 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6898 key_buffer_size, alg,
6899 peer_key, peer_key_length,
6900 shared_secret,
6901 shared_secret_size,
6902 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006903#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006904
6905#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6906 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006907 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006908 peer_key,
6909 peer_key_length,
6910 key_buffer,
6911 key_buffer_size,
6912 shared_secret,
6913 shared_secret_size,
6914 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006915#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6916
Gilles Peskine01d718c2018-09-18 12:01:02 +02006917 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006918 (void) attributes;
6919 (void) key_buffer;
6920 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006921 (void) peer_key;
6922 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006923 (void) shared_secret;
6924 (void) shared_secret_size;
6925 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006926 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006927 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006928}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006929
Aditya Deshpande17845b82022-10-13 17:21:01 +01006930/** Internal function for raw key agreement
6931 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006932 * to the driver's implementation if a driver is present.
6933 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006934 * (psa_key_agreement_raw_builtin).
6935 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006936static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6937 psa_key_slot_t *private_key,
6938 const uint8_t *peer_key,
6939 size_t peer_key_length,
6940 uint8_t *shared_secret,
6941 size_t shared_secret_size,
6942 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006943{
Gilles Peskine449bd832023-01-11 14:50:10 +01006944 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6945 return PSA_ERROR_NOT_SUPPORTED;
6946 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006947
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006948 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 private_key->key.data,
6950 private_key->key.bytes, alg,
6951 peer_key, peer_key_length,
6952 shared_secret,
6953 shared_secret_size,
6954 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006955}
6956
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006957/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006958 * to potentially free embedded data structures and wipe confidential data.
6959 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006960static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6961 psa_key_derivation_step_t step,
6962 psa_key_slot_t *private_key,
6963 const uint8_t *peer_key,
6964 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006965{
6966 psa_status_t status;
Moritz Fischer967f8cd2024-03-02 00:55:06 +00006967 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
Gilles Peskine01d718c2018-09-18 12:01:02 +02006968 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006970
6971 /* Step 1: run the secret agreement algorithm to generate the shared
6972 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 status = psa_key_agreement_raw_internal(ka_alg,
6974 private_key,
6975 peer_key, peer_key_length,
6976 shared_secret,
6977 sizeof(shared_secret),
6978 &shared_secret_length);
6979 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006980 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006981 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006982
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006983 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006984 * the shared secret. A shared secret is permitted wherever a key
6985 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 status = psa_key_derivation_input_internal(operation, step,
6987 PSA_KEY_TYPE_DERIVE,
6988 shared_secret,
6989 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006990exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006991 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6992 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006993}
6994
Gilles Peskine449bd832023-01-11 14:50:10 +01006995psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6996 psa_key_derivation_step_t step,
6997 mbedtls_svc_key_id_t private_key,
6998 const uint8_t *peer_key,
6999 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007000{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007001 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007002 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007003 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007004
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7006 return PSA_ERROR_INVALID_ARGUMENT;
7007 }
Ronald Cron5c522922020-11-14 16:35:34 +01007008 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7010 if (status != PSA_SUCCESS) {
7011 return status;
7012 }
7013 status = psa_key_agreement_internal(operation, step,
7014 slot,
7015 peer_key, peer_key_length);
7016 if (status != PSA_SUCCESS) {
7017 psa_key_derivation_abort(operation);
7018 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007019 /* If a private key has been added as SECRET, we allow the derived
7020 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007022 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007024 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007025
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007026 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007027
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007029}
7030
Gilles Peskine449bd832023-01-11 14:50:10 +01007031psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7032 mbedtls_svc_key_id_t private_key,
7033 const uint8_t *peer_key,
7034 size_t peer_key_length,
7035 uint8_t *output,
7036 size_t output_size,
7037 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007038{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007039 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007040 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007041 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007042 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007043
Gilles Peskine449bd832023-01-11 14:50:10 +01007044 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007045 status = PSA_ERROR_INVALID_ARGUMENT;
7046 goto exit;
7047 }
Ronald Cron5c522922020-11-14 16:35:34 +01007048 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007049 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7050 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007051 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007053
Gilles Peskine3e561302022-04-14 00:17:15 +02007054 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7055 * for the output size. The PSA specification only guarantees that this
7056 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7057 * but it might be nice to allow smaller buffers if the output fits.
7058 * At the time of writing this comment, with only ECDH implemented,
7059 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7060 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7061 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007062 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7064 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007065 status = PSA_ERROR_BUFFER_TOO_SMALL;
7066 goto exit;
7067 }
7068
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 status = psa_key_agreement_raw_internal(alg, slot,
7070 peer_key, peer_key_length,
7071 output, output_size,
7072 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007073
7074exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007076 /* If an error happens and is not handled properly, the output
7077 * may be used as a key to protect sensitive data. Arrange for such
7078 * a key to be random, which is likely to result in decryption or
7079 * verification errors. This is better than filling the buffer with
7080 * some constant data such as zeros, which would result in the data
7081 * being protected with a reproducible, easily knowable key.
7082 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007084 *output_length = output_size;
7085 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007086
Ryan Everetta103ec92024-01-31 13:59:57 +00007087 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007088
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007090}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007091
7092
Gilles Peskine30524eb2020-11-13 17:02:26 +01007093
Gilles Peskine86a440b2018-11-12 18:39:40 +01007094/****************************************************************/
7095/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007096/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007097
Gilles Peskine672a7712023-04-28 21:00:28 +02007098#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7099#include "entropy_poll.h"
7100#endif
7101
Gilles Peskine30524eb2020-11-13 17:02:26 +01007102/** Initialize the PSA random generator.
7103 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007104static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007105{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007106#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007108#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7109
Gilles Peskine30524eb2020-11-13 17:02:26 +01007110 /* Set default configuration if
7111 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007112 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007113 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 }
7115 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007116 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007118
Gilles Peskine449bd832023-01-11 14:50:10 +01007119 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007120#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7121 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7122 /* The PSA entropy injection feature depends on using NV seed as an entropy
7123 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007124 mbedtls_entropy_add_source(&rng->entropy,
7125 mbedtls_nv_seed_poll, NULL,
7126 MBEDTLS_ENTROPY_BLOCK_SIZE,
7127 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007128#endif
7129
Valerio Setti061d4e42024-02-26 12:52:44 +01007130 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007131#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007132}
7133
7134/** Deinitialize the PSA random generator.
7135 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007136static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007137{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007138#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007139 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007140#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01007141 mbedtls_psa_drbg_free(&rng->drbg);
Valerio Setti83e0de82023-11-24 12:13:05 +01007142 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007143#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007144}
7145
7146/** Seed the PSA random generator.
7147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007148static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007149{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007150#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7151 /* Do nothing: the external RNG seeds itself. */
7152 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007154#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007155 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01007156 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 drbg_seed, sizeof(drbg_seed) - 1);
7158 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007159#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007160}
7161
Gilles Peskine449bd832023-01-11 14:50:10 +01007162psa_status_t psa_generate_random(uint8_t *output,
7163 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007164{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007165 GUARD_MODULE_INITIALIZED;
7166
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007167#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7168
7169 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7171 output, output_size,
7172 &output_length);
7173 if (status != PSA_SUCCESS) {
7174 return status;
7175 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007176 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007177 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 if (output_length != output_size) {
7179 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7180 }
7181 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007182
7183#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7184
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 while (output_size > 0) {
Valerio Setti718180c2024-02-27 11:58:39 +01007186 int ret = MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
Gilles Peskine71ddab92021-01-04 21:01:07 +01007187 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7189 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7190 output_size);
Valerio Setti718180c2024-02-27 11:58:39 +01007191#if defined(MBEDTLS_CTR_DRBG_C)
7192 ret = mbedtls_ctr_drbg_random(&global_data.rng.drbg, output, request_size);
7193#elif defined(MBEDTLS_HMAC_DRBG_C)
7194 ret = mbedtls_hmac_drbg_random(&global_data.rng.drbg, output, request_size);
7195#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 if (ret != 0) {
7197 return mbedtls_to_psa_error(ret);
7198 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007199 output_size -= request_size;
7200 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007201 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007202 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007203#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007204}
7205
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007206#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007207psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7208 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007209{
Paul Elliott600472b2024-02-23 17:26:58 +00007210 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007211 return PSA_ERROR_NOT_PERMITTED;
7212 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007213
Gilles Peskine449bd832023-01-11 14:50:10 +01007214 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7215 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7216 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7217 return PSA_ERROR_INVALID_ARGUMENT;
7218 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007219
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007221}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007222#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007223
Ronald Cron3772afe2021-02-08 16:10:05 +01007224/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007225 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007226 * \param type The key type
7227 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007228 *
7229 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007230 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007231 * \retval #PSA_ERROR_INVALID_ARGUMENT
7232 * The size in bits of the key is not valid.
7233 * \retval #PSA_ERROR_NOT_SUPPORTED
7234 * The type and/or the size in bits of the key or the combination of
7235 * the two is not supported.
7236 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007237static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007238 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007239{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007240 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007241
Gilles Peskine449bd832023-01-11 14:50:10 +01007242 if (key_type_is_raw_bytes(type)) {
7243 status = psa_validate_unstructured_key_bit_size(type, bits);
7244 if (status != PSA_SUCCESS) {
7245 return status;
7246 }
7247 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007248#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7250 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7251 return PSA_ERROR_NOT_SUPPORTED;
7252 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007253 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007254 return PSA_ERROR_NOT_SUPPORTED;
7255 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007256
Ronald Cron01b2aba2020-10-05 09:42:02 +02007257 /* Accept only byte-aligned keys, for the same reasons as
7258 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 if (bits % 8 != 0) {
7260 return PSA_ERROR_NOT_SUPPORTED;
7261 }
7262 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007263#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007264
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007265#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007267 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 return PSA_SUCCESS;
7269 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007270#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007271
Valerio Settia55f0422023-07-10 15:34:41 +02007272#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007273 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007274 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007275 return PSA_ERROR_NOT_SUPPORTED;
7276 }
7277 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007278#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007279 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007281 }
7282
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007284}
7285
Ronald Cron55ed0592020-10-05 10:30:40 +02007286psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007287 const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007288 const psa_key_production_parameters_t *params, size_t params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007290{
7291 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007292 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007293
Gilles Peskine7a18f962024-02-12 16:48:11 +01007294 /* Only used for RSA */
Gilles Peskine092ce512024-02-20 12:31:24 +01007295 (void) params;
7296 (void) params_data_length;
Gilles Peskine7a18f962024-02-12 16:48:11 +01007297
Gilles Peskine449bd832023-01-11 14:50:10 +01007298 if (key_type_is_raw_bytes(type)) {
7299 status = psa_generate_random(key_buffer, key_buffer_size);
7300 if (status != PSA_SUCCESS) {
7301 return status;
7302 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007303
David Brown12ca5032021-02-11 11:02:00 -07007304#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 if (type == PSA_KEY_TYPE_DES) {
7306 psa_des_set_key_parity(key_buffer, key_buffer_size);
7307 }
David Brown8107e312021-02-12 08:08:46 -07007308#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007310
Valerio Setti76df8c12023-07-11 14:11:28 +02007311#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007312 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskinee22f6a92024-02-26 15:45:33 +01007313 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskine4c32b692024-02-16 00:11:09 +01007314 params, params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007315 key_buffer,
7316 key_buffer_size,
7317 key_buffer_length);
7318 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007319#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007320
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007321#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7323 return mbedtls_psa_ecp_generate_key(attributes,
7324 key_buffer,
7325 key_buffer_size,
7326 key_buffer_length);
7327 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007328#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007329
Valerio Settia55f0422023-07-10 15:34:41 +02007330#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007331 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7332 return mbedtls_psa_ffdh_generate_key(attributes,
7333 key_buffer,
7334 key_buffer_size,
7335 key_buffer_length);
7336 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007337#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007338 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007339 (void) key_buffer_length;
7340 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007341 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007342
Gilles Peskine449bd832023-01-11 14:50:10 +01007343 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007344}
Darryl Green0c6575a2018-11-07 16:05:30 +00007345
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007346psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007347 const psa_key_production_parameters_t *params,
7348 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007349 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007350{
7351 psa_status_t status;
7352 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007353 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007354 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007355
Ronald Cron81709fc2020-11-14 12:10:32 +01007356 *key = MBEDTLS_SVC_KEY_ID_INIT;
7357
Gilles Peskine0f84d622019-09-12 19:03:13 +02007358 /* Reject any attempt to create a zero-length key so that we don't
7359 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 if (psa_get_key_bits(attributes) == 0) {
7361 return PSA_ERROR_INVALID_ARGUMENT;
7362 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007363
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007364 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007365 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007366 return PSA_ERROR_INVALID_ARGUMENT;
7367 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007368
Gilles Peskine7a18f962024-02-12 16:48:11 +01007369#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007370 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +01007371 if (params->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01007372 return PSA_ERROR_INVALID_ARGUMENT;
7373 }
7374 } else
7375#endif
Gilles Peskine092ce512024-02-20 12:31:24 +01007376 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007377 return PSA_ERROR_INVALID_ARGUMENT;
7378 }
7379
Gilles Peskine449bd832023-01-11 14:50:10 +01007380 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7381 &slot, &driver);
7382 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007383 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007384 }
Gilles Peskine11792082019-08-06 18:36:36 +02007385
Ronald Cron977c2472020-10-13 08:32:21 +02007386 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307387 * storage ( thus not in the case of generating a key in a secure element
7388 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7389 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007390 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007391 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007393 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007394 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007396 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007397 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007398
7399 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007400 attributes->type,
7401 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007402 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007403 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 attributes, &key_buffer_size);
7405 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007406 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007407 }
Ronald Cron977c2472020-10-13 08:32:21 +02007408 }
Ronald Cron977c2472020-10-13 08:32:21 +02007409
Gilles Peskine449bd832023-01-11 14:50:10 +01007410 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7411 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007412 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007413 }
Ronald Cron977c2472020-10-13 08:32:21 +02007414 }
7415
Gilles Peskine449bd832023-01-11 14:50:10 +01007416 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007417 params, params_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01007418 slot->key.data, slot->key.bytes,
7419 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 if (status != PSA_SUCCESS) {
7421 psa_remove_key_data_from_memory(slot);
7422 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007423
Gilles Peskine11792082019-08-06 18:36:36 +02007424exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007425 if (status == PSA_SUCCESS) {
7426 status = psa_finish_key_creation(slot, driver, key);
7427 }
7428 if (status != PSA_SUCCESS) {
7429 psa_fail_key_creation(slot, driver);
7430 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007431
Gilles Peskine449bd832023-01-11 14:50:10 +01007432 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007433}
7434
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007435psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7436 mbedtls_svc_key_id_t *key)
7437{
7438 return psa_generate_key_ext(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007439 &default_production_parameters, 0,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007440 key);
7441}
7442
Gilles Peskinee59236f2018-01-27 23:32:46 +01007443/****************************************************************/
7444/* Module setup */
7445/****************************************************************/
7446
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007447#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007448psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 void (* entropy_init)(mbedtls_entropy_context *ctx),
7450 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007451{
Gilles Peskine449bd832023-01-11 14:50:10 +01007452 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7453 return PSA_ERROR_BAD_STATE;
7454 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007455 global_data.rng.entropy_init = entropy_init;
7456 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007458}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007459#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007460
Gilles Peskine449bd832023-01-11 14:50:10 +01007461void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007462{
Paul Elliott600472b2024-02-23 17:26:58 +00007463 /* Need to hold the mutex here to prevent this going ahead before
7464 * psa_crypto_init() has completed, and to ensure integrity of
7465 * global_data. */
7466#if defined(MBEDTLS_THREADING_C)
7467 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
7468#endif /* defined(MBEDTLS_THREADING_C) */
7469
Gilles Peskine449bd832023-01-11 14:50:10 +01007470 psa_wipe_all_key_slots();
Valerio Setti83e0de82023-11-24 12:13:05 +01007471 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7472 mbedtls_psa_random_free(&global_data.rng);
7473 }
Paul Elliott600472b2024-02-23 17:26:58 +00007474
Gilles Peskinec6b69072018-11-20 21:42:52 +01007475 /* Wipe all remaining data, including configuration.
7476 * In particular, this sets all state indicator to the value
7477 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007479
Paul Elliott600472b2024-02-23 17:26:58 +00007480#if defined(MBEDTLS_THREADING_C)
7481 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
7482#endif /* defined(MBEDTLS_THREADING_C) */
7483
Ronald Cron9ba76912021-04-10 16:57:30 +02007484 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007485 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007486}
7487
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007488#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7489/** Recover a transaction that was interrupted by a power failure.
7490 *
7491 * This function is called during initialization, before psa_crypto_init()
7492 * returns. If this function returns a failure status, the initialization
7493 * fails.
7494 */
7495static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007497{
Gilles Peskine449bd832023-01-11 14:50:10 +01007498 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007499 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7500 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 /* TODO - fall through to the failure case until this
7502 * is implemented.
7503 * https://github.com/ARMmbed/mbed-crypto/issues/218
7504 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007505 default:
7506 /* We found an unsupported transaction in the storage.
7507 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007509 }
7510}
7511#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7512
Gilles Peskine449bd832023-01-11 14:50:10 +01007513psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007514{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007515 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007516
Paul Elliott600472b2024-02-23 17:26:58 +00007517 /* Need to hold the mutex for the entire function to prevent incomplete
7518 * initialisation before someone calls psa_crypto_free() or calls this
7519 * function again before we set global_data.initialised to 1. */
7520 #if defined(MBEDTLS_THREADING_C)
7521 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
7522#endif /* defined(MBEDTLS_THREADING_C) */
7523
7524 /* We cannot use psa_get_initialized() here as we already hold the mutex. */
7525 if (global_data.initialized == 1) {
7526#if defined(MBEDTLS_THREADING_C)
7527 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
7528#endif /* defined(MBEDTLS_THREADING_C) */
7529
7530 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 return PSA_SUCCESS;
7532 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007533
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007534 /* Init drivers */
7535 status = psa_driver_wrapper_init();
7536 if (status != PSA_SUCCESS) {
7537 goto exit;
7538 }
7539 global_data.drivers_initialized = 1;
7540
Valerio Setti402cfba2023-11-13 10:24:32 +01007541 status = psa_initialize_key_slots();
7542 if (status != PSA_SUCCESS) {
7543 goto exit;
7544 }
7545
Gilles Peskine30524eb2020-11-13 17:02:26 +01007546 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007547 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007548 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 status = mbedtls_psa_random_seed(&global_data.rng);
7550 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007551 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007552 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007553 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007554
Gilles Peskine4b734222019-07-24 15:56:31 +02007555#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007556 status = psa_crypto_load_transaction();
7557 if (status == PSA_SUCCESS) {
7558 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7559 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007560 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007561 }
7562 status = psa_crypto_stop_transaction();
7563 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007564 /* There's no transaction to complete. It's all good. */
7565 status = PSA_SUCCESS;
7566 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007567#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007568
Gilles Peskinec6b69072018-11-20 21:42:52 +01007569 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007570 global_data.initialized = 1;
7571
7572exit:
Paul Elliott600472b2024-02-23 17:26:58 +00007573
7574#if defined(MBEDTLS_THREADING_C)
7575 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
7576#endif /* defined(MBEDTLS_THREADING_C) */
7577
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 if (status != PSA_SUCCESS) {
7579 mbedtls_psa_crypto_free();
7580 }
7581 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007582}
7583
Yanray Wangffc3c482023-07-11 12:01:04 +08007584#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007585psa_status_t psa_crypto_driver_pake_get_password_len(
7586 const psa_crypto_driver_pake_inputs_t *inputs,
7587 size_t *password_len)
7588{
7589 if (inputs->password_len == 0) {
7590 return PSA_ERROR_BAD_STATE;
7591 }
7592
7593 *password_len = inputs->password_len;
7594
7595 return PSA_SUCCESS;
7596}
7597
7598psa_status_t psa_crypto_driver_pake_get_password(
7599 const psa_crypto_driver_pake_inputs_t *inputs,
7600 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7601{
7602 if (inputs->password_len == 0) {
7603 return PSA_ERROR_BAD_STATE;
7604 }
7605
7606 if (buffer_size < inputs->password_len) {
7607 return PSA_ERROR_BUFFER_TOO_SMALL;
7608 }
7609
7610 memcpy(buffer, inputs->password, inputs->password_len);
7611 *buffer_length = inputs->password_len;
7612
7613 return PSA_SUCCESS;
7614}
7615
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007616psa_status_t psa_crypto_driver_pake_get_user_len(
7617 const psa_crypto_driver_pake_inputs_t *inputs,
7618 size_t *user_len)
7619{
7620 if (inputs->user_len == 0) {
7621 return PSA_ERROR_BAD_STATE;
7622 }
7623
7624 *user_len = inputs->user_len;
7625
7626 return PSA_SUCCESS;
7627}
7628
7629psa_status_t psa_crypto_driver_pake_get_user(
7630 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007631 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007632{
7633 if (inputs->user_len == 0) {
7634 return PSA_ERROR_BAD_STATE;
7635 }
7636
Przemek Stekielc0e62502023-03-14 11:49:36 +01007637 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007638 return PSA_ERROR_BUFFER_TOO_SMALL;
7639 }
7640
Przemek Stekielc0e62502023-03-14 11:49:36 +01007641 memcpy(user_id, inputs->user, inputs->user_len);
7642 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007643
7644 return PSA_SUCCESS;
7645}
7646
7647psa_status_t psa_crypto_driver_pake_get_peer_len(
7648 const psa_crypto_driver_pake_inputs_t *inputs,
7649 size_t *peer_len)
7650{
7651 if (inputs->peer_len == 0) {
7652 return PSA_ERROR_BAD_STATE;
7653 }
7654
7655 *peer_len = inputs->peer_len;
7656
7657 return PSA_SUCCESS;
7658}
7659
7660psa_status_t psa_crypto_driver_pake_get_peer(
7661 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007662 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007663{
7664 if (inputs->peer_len == 0) {
7665 return PSA_ERROR_BAD_STATE;
7666 }
7667
Przemek Stekielc0e62502023-03-14 11:49:36 +01007668 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007669 return PSA_ERROR_BUFFER_TOO_SMALL;
7670 }
7671
Przemek Stekielc0e62502023-03-14 11:49:36 +01007672 memcpy(peer_id, inputs->peer, inputs->peer_len);
7673 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007674
7675 return PSA_SUCCESS;
7676}
7677
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007678psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7679 const psa_crypto_driver_pake_inputs_t *inputs,
7680 psa_pake_cipher_suite_t *cipher_suite)
7681{
7682 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7683 return PSA_ERROR_BAD_STATE;
7684 }
7685
7686 *cipher_suite = inputs->cipher_suite;
7687
7688 return PSA_SUCCESS;
7689}
7690
Neil Armstronga7d08c32022-06-01 18:21:20 +02007691psa_status_t psa_pake_setup(
7692 psa_pake_operation_t *operation,
7693 const psa_pake_cipher_suite_t *cipher_suite)
7694{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007695 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007696
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007697 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007698 status = PSA_ERROR_BAD_STATE;
7699 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007700 }
7701
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007702 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007703 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007704 status = PSA_ERROR_INVALID_ARGUMENT;
7705 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007706 }
7707
Przemek Stekiel51eac532022-12-07 11:04:51 +01007708 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7709
Przemek Stekiele12ed362022-12-21 12:54:46 +01007710 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007711 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7712 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007713 operation->data.inputs.cipher_suite = *cipher_suite;
7714
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007715#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007716 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007717 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007718 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007719
David Horstmann16f01512023-06-14 17:21:07 +01007720 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007721 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007722 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007723#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007724 {
7725 status = PSA_ERROR_NOT_SUPPORTED;
7726 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007727 }
7728
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007729 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7730
Przemek Stekiel51eac532022-12-07 11:04:51 +01007731 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007732exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007733 psa_pake_abort(operation);
7734 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007735}
7736
7737psa_status_t psa_pake_set_password_key(
7738 psa_pake_operation_t *operation,
7739 mbedtls_svc_key_id_t password)
7740{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007741 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007742 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7743 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007744 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007745
Przemek Stekiel51eac532022-12-07 11:04:51 +01007746 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007747 status = PSA_ERROR_BAD_STATE;
7748 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007749 }
7750
Przemek Stekiel6c764412022-11-22 14:05:12 +01007751 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7752 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007753 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007754 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007755 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007756 }
7757
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007758 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007759
Przemek Stekiel51eac532022-12-07 11:04:51 +01007760 if (type != PSA_KEY_TYPE_PASSWORD &&
7761 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7762 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007763 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007764 }
7765
Przemek Stekiel51eac532022-12-07 11:04:51 +01007766 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7767 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007768 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007769 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007770 }
7771
7772 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7773 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01007774 operation->data.inputs.attributes = slot->attr;
7775
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007776exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007777 if (status != PSA_SUCCESS) {
7778 psa_pake_abort(operation);
7779 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00007780 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007781 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007782}
7783
7784psa_status_t psa_pake_set_user(
7785 psa_pake_operation_t *operation,
7786 const uint8_t *user_id,
7787 size_t user_id_len)
7788{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007789 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007790
7791 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007792 status = PSA_ERROR_BAD_STATE;
7793 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007794 }
7795
Przemek Stekiel51eac532022-12-07 11:04:51 +01007796 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007797 status = PSA_ERROR_INVALID_ARGUMENT;
7798 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007799 }
7800
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007801 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007802 status = PSA_ERROR_BAD_STATE;
7803 goto exit;
7804 }
7805
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007806 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7807 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007808 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7809 goto exit;
7810 }
7811
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007812 memcpy(operation->data.inputs.user, user_id, user_id_len);
7813 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007814
7815 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007816exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007817 psa_pake_abort(operation);
7818 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007819}
7820
7821psa_status_t psa_pake_set_peer(
7822 psa_pake_operation_t *operation,
7823 const uint8_t *peer_id,
7824 size_t peer_id_len)
7825{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007826 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007827
7828 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007829 status = PSA_ERROR_BAD_STATE;
7830 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007831 }
7832
Przemek Stekiel51eac532022-12-07 11:04:51 +01007833 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007834 status = PSA_ERROR_INVALID_ARGUMENT;
7835 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007836 }
7837
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007838 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007839 status = PSA_ERROR_BAD_STATE;
7840 goto exit;
7841 }
7842
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007843 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7844 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007845 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7846 goto exit;
7847 }
7848
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007849 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7850 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007851
7852 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007853exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007854 psa_pake_abort(operation);
7855 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007856}
7857
7858psa_status_t psa_pake_set_role(
7859 psa_pake_operation_t *operation,
7860 psa_pake_role_t role)
7861{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007862 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007863
Przemek Stekiel51eac532022-12-07 11:04:51 +01007864 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007865 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007866 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007867 }
7868
Przemek Stekiel09104b82023-03-06 14:11:51 +01007869 switch (operation->alg) {
7870#if defined(PSA_WANT_ALG_JPAKE)
7871 case PSA_ALG_JPAKE:
7872 if (role == PSA_PAKE_ROLE_NONE) {
7873 return PSA_SUCCESS;
7874 }
7875 status = PSA_ERROR_INVALID_ARGUMENT;
7876 break;
7877#endif
7878 default:
7879 (void) role;
7880 status = PSA_ERROR_NOT_SUPPORTED;
7881 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007882 }
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
David Horstmanne7f21e62023-05-12 18:17:21 +01007888/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007889#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007890static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007891 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007892{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007893 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007894 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007895 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007896
David Horstmann024e5c52023-06-14 15:48:21 +01007897 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007898 is_x1 = (stage->outputs < 1);
7899 } else {
7900 is_x1 = (stage->inputs < 1);
7901 }
7902
David Horstmann74a3d8c2023-06-14 18:28:19 +01007903 key_share_step = is_x1 ?
7904 PSA_JPAKE_X1_STEP_KEY_SHARE :
7905 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007906 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007907 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7908 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7909 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7910 } else {
7911 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007912 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01007913 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01007914}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007915#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007916
Przemek Stekiel2797d372022-12-22 11:19:22 +01007917static psa_status_t psa_pake_complete_inputs(
7918 psa_pake_operation_t *operation)
7919{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007920 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007921 /* Create copy of the inputs on stack as inputs share memory
7922 with the driver context which will be setup by the driver. */
7923 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007924
Przemek Stekielfde11282023-03-13 16:06:09 +01007925 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007926 return PSA_ERROR_BAD_STATE;
7927 }
7928
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007929 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007930 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7931 return PSA_ERROR_BAD_STATE;
7932 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007933 }
7934
Przemek Stekiel18620a32023-01-17 16:34:52 +01007935 /* Clear driver context */
7936 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7937
7938 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007939
7940 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01007941 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007942
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007943 /* User and peer are translated to role. */
7944 mbedtls_free(inputs.user);
7945 mbedtls_free(inputs.peer);
7946
Przemek Stekiel2797d372022-12-22 11:19:22 +01007947 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007948#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007949 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007950 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007951 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007952#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007953 {
7954 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007955 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007956 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007957 return status;
7958}
7959
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007960#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01007961static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01007962 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01007963 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01007964 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007965{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007966 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7967 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7968 step != PSA_PAKE_STEP_ZK_PROOF) {
7969 return PSA_ERROR_INVALID_ARGUMENT;
7970 }
7971
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007972 psa_jpake_computation_stage_t *computation_stage =
7973 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007974
David Horstmann5da95602023-06-08 15:37:12 +01007975 if (computation_stage->round != PSA_JPAKE_FIRST &&
7976 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007977 return PSA_ERROR_BAD_STATE;
7978 }
7979
David Horstmanne7f21e62023-05-12 18:17:21 +01007980 /* Check that the step we are given is the one we were expecting */
7981 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007982 return PSA_ERROR_BAD_STATE;
7983 }
7984
David Horstmanne7f21e62023-05-12 18:17:21 +01007985 if (step == PSA_PAKE_STEP_KEY_SHARE &&
7986 computation_stage->inputs == 0 &&
7987 computation_stage->outputs == 0) {
7988 /* Start of the round, so function decides whether we are inputting
7989 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01007990 computation_stage->io_mode = io_mode;
7991 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007992 /* Middle of the round so the mode we are in must match the function
7993 * called by the user */
7994 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007995 }
7996
7997 return PSA_SUCCESS;
7998}
7999
David Horstmanne7f21e62023-05-12 18:17:21 +01008000static psa_status_t psa_jpake_epilogue(
8001 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008002 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008003{
David Horstmanne7f21e62023-05-12 18:17:21 +01008004 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008005 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008006
David Horstmanne7f21e62023-05-12 18:17:21 +01008007 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8008 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008009 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008010 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008011 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008012 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008013 }
8014 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008015 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008016 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008017 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008018 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008019 }
8020 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008021 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8022 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008023 /* End of a round, move to the next round */
8024 stage->inputs = 0;
8025 stage->outputs = 0;
8026 stage->round++;
8027 }
8028 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008029 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008030 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008031 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008032 return PSA_SUCCESS;
8033}
David Horstmanne7f21e62023-05-12 18:17:21 +01008034
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008035#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008036
Neil Armstronga7d08c32022-06-01 18:21:20 +02008037psa_status_t psa_pake_output(
8038 psa_pake_operation_t *operation,
8039 psa_pake_step_t step,
8040 uint8_t *output,
8041 size_t output_size,
8042 size_t *output_length)
8043{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008044 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008045 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008046 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008047
8048 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008049 status = psa_pake_complete_inputs(operation);
8050 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008051 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008052 }
8053 }
8054
8055 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008056 status = PSA_ERROR_BAD_STATE;
8057 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008058 }
8059
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008060 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008061 status = PSA_ERROR_INVALID_ARGUMENT;
8062 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008063 }
8064
Przemek Stekiele12ed362022-12-21 12:54:46 +01008065 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008066#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008067 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008068 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008069 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008070 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008071 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008072 driver_step = convert_jpake_computation_stage_to_driver_step(
8073 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008074 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008075#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008076 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008077 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008078 status = PSA_ERROR_NOT_SUPPORTED;
8079 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008080 }
8081
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008082 status = psa_driver_wrapper_pake_output(operation, driver_step,
8083 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008084
8085 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008086 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008087 }
8088
8089 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008090#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008091 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008092 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008093 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008094 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008095 }
8096 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008097#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008098 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008099 status = PSA_ERROR_NOT_SUPPORTED;
8100 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008101 }
8102
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008103 return PSA_SUCCESS;
8104exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008105 psa_pake_abort(operation);
8106 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008107}
8108
8109psa_status_t psa_pake_input(
8110 psa_pake_operation_t *operation,
8111 psa_pake_step_t step,
8112 const uint8_t *input,
8113 size_t input_length)
8114{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008115 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008116 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008117 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8118 operation->primitive,
8119 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008120
8121 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008122 status = psa_pake_complete_inputs(operation);
8123 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008124 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008125 }
8126 }
8127
8128 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008129 status = PSA_ERROR_BAD_STATE;
8130 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008131 }
8132
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008133 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008134 status = PSA_ERROR_INVALID_ARGUMENT;
8135 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008136 }
8137
Przemek Stekiele12ed362022-12-21 12:54:46 +01008138 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008139#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008140 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008141 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008142 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008143 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008144 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008145 driver_step = convert_jpake_computation_stage_to_driver_step(
8146 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008147 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008148#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008149 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008150 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008151 status = PSA_ERROR_NOT_SUPPORTED;
8152 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008153 }
8154
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008155 status = psa_driver_wrapper_pake_input(operation, driver_step,
8156 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008157
8158 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008159 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008160 }
8161
8162 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008163#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008164 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008165 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008166 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008167 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008168 }
8169 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008170#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008171 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008172 status = PSA_ERROR_NOT_SUPPORTED;
8173 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008174 }
8175
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008176 return PSA_SUCCESS;
8177exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008178 psa_pake_abort(operation);
8179 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008180}
8181
8182psa_status_t psa_pake_get_implicit_key(
8183 psa_pake_operation_t *operation,
8184 psa_key_derivation_operation_t *output)
8185{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008186 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008187 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008188 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008189 size_t shared_key_len = 0;
8190
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008191 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008192 status = PSA_ERROR_BAD_STATE;
8193 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008194 }
8195
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008196#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008197 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008198 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008199 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008200 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008201 status = PSA_ERROR_BAD_STATE;
8202 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008203 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008204 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008205#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008206 {
8207 status = PSA_ERROR_NOT_SUPPORTED;
8208 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008209 }
8210
Przemek Stekiel0c781802022-11-29 14:53:13 +01008211 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8212 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008213 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008214 &shared_key_len);
8215
8216 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008217 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008218 }
8219
8220 status = psa_key_derivation_input_bytes(output,
8221 PSA_KEY_DERIVATION_INPUT_SECRET,
8222 shared_key,
8223 shared_key_len);
8224
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008225 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008226exit:
8227 abort_status = psa_pake_abort(operation);
8228 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008229}
8230
8231psa_status_t psa_pake_abort(
8232 psa_pake_operation_t *operation)
8233{
Przemek Stekield69dca92023-01-31 19:59:20 +01008234 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008235
Przemek Stekield69dca92023-01-31 19:59:20 +01008236 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008237 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008238 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008239
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008240 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8241 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008242 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008243 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008244 }
8245 if (operation->data.inputs.user != NULL) {
8246 mbedtls_free(operation->data.inputs.user);
8247 }
8248 if (operation->data.inputs.peer != NULL) {
8249 mbedtls_free(operation->data.inputs.peer);
8250 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008251 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008252 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008253
Przemek Stekield69dca92023-01-31 19:59:20 +01008254 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008255}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008256#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008257
Gilles Peskinee59236f2018-01-27 23:32:46 +01008258#endif /* MBEDTLS_PSA_CRYPTO_C */