blob: 9d7b72f87c590ee5c5d35df8e0242a3f07bc705a [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"
Gilles Peskinea5905292018-02-07 20:59:33 +010074
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020075#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
76 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
77 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020078#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020079#endif
80
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010081/****************************************************************/
82/* Global data, support functions and library management */
83/****************************************************************/
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020086{
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020088}
89
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010090/* Values for psa_global_data_t::rng_state */
91#define RNG_NOT_INITIALIZED 0
92#define RNG_INITIALIZED 1
93#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010094
Gilles Peskine449bd832023-01-11 14:50:10 +010095typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +010096 uint8_t initialized;
97 uint8_t rng_state;
98 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +010099 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100100} psa_global_data_t;
101
102static psa_global_data_t global_data;
103
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100104#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
105mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
106 &global_data.rng.drbg;
107#endif
108
itayzafrir0adf0fc2018-09-06 16:24:41 +0300109#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (global_data.initialized == 0) \
111 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300112
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100113int psa_can_do_hash(psa_algorithm_t hash_alg)
114{
115 (void) hash_alg;
116 return global_data.drivers_initialized;
117}
Valerio Settic6f004f2023-12-12 11:27:36 +0100118
Valerio Setti1fff4f22023-12-28 14:19:34 +0100119int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
Valerio Settic6f004f2023-12-12 11:27:36 +0100120{
Valerio Setti1fff4f22023-12-28 14:19:34 +0100121 (void) key_type;
Valerio Settic6f004f2023-12-12 11:27:36 +0100122 (void) cipher_alg;
123 return global_data.drivers_initialized;
124}
125
126
Valerio Settia55f0422023-07-10 15:34:41 +0200127#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200128 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200129 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200130static int psa_is_dh_key_size_valid(size_t bits)
131{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200132 if (bits != 2048 && bits != 3072 && bits != 4096 &&
133 bits != 6144 && bits != 8192) {
134 return 0;
135 }
136
137 return 1;
138}
Valerio Settia55f0422023-07-10 15:34:41 +0200139#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200140 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200141 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100144{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100145 /* Mbed TLS error codes can combine a high-level error code and a
146 * low-level error code. The low-level error usually reflects the
147 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 int low_level_ret = -(-ret & 0x007f);
149 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100150 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100152
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100153#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100154 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
155 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100157 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
158 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100159#endif
160
161#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200162 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
163 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
164 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
165 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
166 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200168 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200170 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100172#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200173
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100174#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000175 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100178#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100179
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100180#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100185#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100186
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100187#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100190#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200191
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100192#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200193 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200195 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100197#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200198
Dave Rodgman509b5672023-08-16 19:26:23 +0100199#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100200 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100204 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100212 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100214#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
217 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100218 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
219 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100222 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
223 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100227#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100228
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100229#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100232#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
Gilles Peskinee59236f2018-01-27 23:32:46 +0100234 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
235 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
236 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100238
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100239#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100240 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200242 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100244 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100246#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100247
Gilles Peskine82e57d12020-11-13 21:31:17 +0100248#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100250 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
251 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100252 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100254 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
255 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100257 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100259#endif
260
Dave Rodgmandea266f2023-08-31 11:52:43 +0100261#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100262 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100264 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100268#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100271#endif
272#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100273
Dave Rodgman509b5672023-08-16 19:26:23 +0100274#if defined(MBEDTLS_BIGNUM_C)
275#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100276 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100278#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100279 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100281 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100283 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100285 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100287 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100289 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100291 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100293#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100294
Dave Rodgman509b5672023-08-16 19:26:23 +0100295#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100296 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100298 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
299 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100301#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
302 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100303 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100305#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
307 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100311 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
312 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100314 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100316 case MBEDTLS_ERR_PK_INVALID_ALG:
317 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
318 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100320 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200322 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100324#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100325
Gilles Peskineff2d2002019-05-06 15:26:23 +0200326 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200328 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200330
Dave Rodgman509b5672023-08-16 19:26:23 +0100331#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100332 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100334 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100336 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100338 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100340 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
341 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100343 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100345 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100349#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200350
Dave Rodgman1dab4452023-09-01 09:59:51 +0100351#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300352 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300353 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300355 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300357 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300359 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
360 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300362 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100364 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100366
Dave Rodgman509b5672023-08-16 19:26:23 +0100367#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000368 case MBEDTLS_ERR_ECP_IN_PROGRESS:
369 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100370#endif
371#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000372
Janos Follatha13b9052019-11-22 12:48:59 +0000373 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300375
Gilles Peskinee59236f2018-01-27 23:32:46 +0100376 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100378 }
379}
380
Paul Elliottdc42ca82023-02-24 18:11:59 +0000381/**
382 * \brief For output buffers which contain "tags"
383 * (outputs that may be checked for validity like
384 * hashes, MACs and signatures), fill the unused
385 * part of the output buffer (the whole buffer on
386 * error, the trailing part on success) with
387 * something that isn't a valid tag (barring an
388 * attack on the tag and deliberately-crafted
389 * input), in case the caller doesn't check the
390 * return status properly.
391 *
392 * \param output_buffer Pointer to buffer to wipe. May not be NULL
393 * unless \p output_buffer_size is zero.
394 * \param status Status of function called to generate
395 * output_buffer originally
396 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
397 * could be NULL.
398 * \param output_buffer_length Length of data written to output_buffer, must be
399 * less than \p output_buffer_size
400 */
401static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000402 size_t output_buffer_size, size_t output_buffer_length)
403{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000404 size_t offset = 0;
405
406 if (output_buffer_size == 0) {
407 /* If output_buffer_size is 0 then we have nothing to do. We must not
408 call memset because output_buffer may be NULL in this case */
409 return;
410 }
411
412 if (status == PSA_SUCCESS) {
413 offset = output_buffer_length;
414 }
415
416 memset(output_buffer + offset, '!', output_buffer_size - offset);
417}
418
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
421 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200422{
423 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200425 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200426 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200427 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200428 case PSA_KEY_TYPE_PASSWORD:
429 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200430 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100431#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200432 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (bits != 128 && bits != 192 && bits != 256) {
434 return PSA_ERROR_INVALID_ARGUMENT;
435 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200436 break;
437#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200438#if defined(PSA_WANT_KEY_TYPE_ARIA)
439 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (bits != 128 && bits != 192 && bits != 256) {
441 return PSA_ERROR_INVALID_ARGUMENT;
442 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200443 break;
444#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100445#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (bits != 128 && bits != 192 && bits != 256) {
448 return PSA_ERROR_INVALID_ARGUMENT;
449 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200450 break;
451#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100452#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200453 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (bits != 64 && bits != 128 && bits != 192) {
455 return PSA_ERROR_INVALID_ARGUMENT;
456 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200457 break;
458#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100459#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200460 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (bits != 256) {
462 return PSA_ERROR_INVALID_ARGUMENT;
463 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200464 break;
465#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200468 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (bits % 8 != 0) {
470 return PSA_ERROR_INVALID_ARGUMENT;
471 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200474}
475
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100476/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100477 *
Steven Cooremancd640932021-03-08 12:00:27 +0100478 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
479 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100480 *
481 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100482 * \param[in] key_type The key type of the key to be used with the
483 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100484 *
485 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100486 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100487 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100488 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100489 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100490MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100491 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100493{
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (PSA_ALG_IS_HMAC(algorithm)) {
495 if (key_type == PSA_KEY_TYPE_HMAC) {
496 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100497 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100498 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
501 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
502 * key. */
503 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
504 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
505 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
506 * the block length (larger than 1) for block ciphers. */
507 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
508 return PSA_SUCCESS;
509 }
510 }
511 }
512
513 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100514}
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
517 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200518{
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (slot->key.data != NULL) {
520 return PSA_ERROR_ALREADY_EXISTS;
521 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 slot->key.data = mbedtls_calloc(1, buffer_length);
524 if (slot->key.data == NULL) {
525 return PSA_ERROR_INSUFFICIENT_MEMORY;
526 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200527
Ronald Cronea0f8a62020-11-25 17:52:23 +0100528 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200530}
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
533 const uint8_t *data,
534 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200535{
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 psa_status_t status = psa_allocate_buffer_to_slot(slot,
537 data_length);
538 if (status != PSA_SUCCESS) {
539 return status;
540 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 memcpy(slot->key.data, data, data_length);
543 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200544}
545
Ronald Crona0fe59f2020-11-29 11:14:53 +0100546psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100547 const psa_key_attributes_t *attributes,
548 const uint8_t *data, size_t data_length,
549 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100552 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
553 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200555 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (data_length == 0) {
557 return PSA_ERROR_NOT_SUPPORTED;
558 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (key_type_is_raw_bytes(type)) {
561 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
564 *bits);
565 if (status != PSA_SUCCESS) {
566 return status;
567 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200568
Ronald Crondd04d422020-11-28 15:14:42 +0100569 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100571 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 return PSA_SUCCESS;
575 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200576#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200577 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100578 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200579 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100580 return PSA_ERROR_INVALID_ARGUMENT;
581 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200582 return mbedtls_psa_ffdh_import_key(attributes,
583 data, data_length,
584 key_buffer, key_buffer_size,
585 key_buffer_length,
586 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100587 }
Valerio Settia55f0422023-07-10 15:34:41 +0200588#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200589 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200590#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
592 if (PSA_KEY_TYPE_IS_ECC(type)) {
593 return mbedtls_psa_ecp_import_key(attributes,
594 data, data_length,
595 key_buffer, key_buffer_size,
596 key_buffer_length,
597 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100598 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200599#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800600 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200601#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
602 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
604 if (PSA_KEY_TYPE_IS_RSA(type)) {
605 return mbedtls_psa_rsa_import_key(attributes,
606 data, data_length,
607 key_buffer, key_buffer_size,
608 key_buffer_length,
609 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200610 }
Valerio Settife478902023-07-25 12:27:19 +0200611#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
612 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800613 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100614 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100617}
618
Gilles Peskinef603c712019-01-19 13:40:11 +0100619/** Calculate the intersection of two algorithm usage policies.
620 *
621 * Return 0 (which allows no operation) on incompatibility.
622 */
623static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100624 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100625 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100627{
Gilles Peskine549ea862019-05-22 11:45:59 +0200628 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (alg1 == alg2) {
630 return alg1;
631 }
Steven Cooreman03488022021-02-18 12:07:20 +0100632 /* If the policies are from the same hash-and-sign family, check
633 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
635 PSA_ALG_IS_SIGN_HASH(alg2) &&
636 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
637 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
638 return alg2;
639 }
640 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
641 return alg1;
642 }
Steven Cooreman03488022021-02-18 12:07:20 +0100643 }
644 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100645 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100646 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
648 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
649 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
650 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
651 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100652 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100653
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100654 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
656 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
657 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
658 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100659 }
660 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
662 (alg1_len <= alg2_len)) {
663 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100664 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
666 (alg2_len <= alg1_len)) {
667 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100668 }
669 }
670 /* If the policies are from the same MAC family, check whether one
671 * of them is a minimum-MAC-length policy. Calculate the most
672 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
674 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
675 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100676 /* Validate the combination of key type and algorithm. Since the base
677 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
679 return 0;
680 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100681
Steven Cooreman31a876d2021-03-03 20:47:40 +0100682 /* Get the (exact or at-least) output lengths for both sides of the
683 * requested intersection. None of the currently supported algorithms
684 * have an output length dependent on the actual key size, so setting it
685 * to a bogus value of 0 is currently OK.
686 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100687 * Note that for at-least-this-length wildcard algorithms, the output
688 * length is set to the shortest allowed length, which allows us to
689 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
691 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100692 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100693
Steven Cooremana1d83222021-02-25 10:20:29 +0100694 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
696 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
697 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100698 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100699
700 /* If only one is an at-least-this-length policy, the intersection would
701 * be the other (fixed-length) policy as long as said fixed length is
702 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
704 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100705 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
707 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100708 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100709
Steven Cooremancd640932021-03-08 12:00:27 +0100710 /* If none of them are wildcards, check whether they define the same tag
711 * length. This is still possible here when one is default-length and
712 * the other specific-length. Ensure to always return the
713 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (alg1_len == alg2_len) {
715 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
716 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100717 }
718 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100720}
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722static int psa_key_algorithm_permits(psa_key_type_t key_type,
723 psa_algorithm_t policy_alg,
724 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200725{
Gilles Peskine549ea862019-05-22 11:45:59 +0200726 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (requested_alg == policy_alg) {
728 return 1;
729 }
Steven Cooreman03488022021-02-18 12:07:20 +0100730 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
731 * and requested_alg is the same hash-and-sign family with any hash,
732 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
734 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
735 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
736 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100737 }
738 /* If policy_alg is a wildcard AEAD algorithm of the same base as
739 * the requested algorithm, check the requested tag length to be
740 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (PSA_ALG_IS_AEAD(policy_alg) &&
742 PSA_ALG_IS_AEAD(requested_alg) &&
743 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
744 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
745 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
746 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
747 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100748 }
Steven Cooremancd640932021-03-08 12:00:27 +0100749 /* If policy_alg is a MAC algorithm of the same base as the requested
750 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (PSA_ALG_IS_MAC(policy_alg) &&
752 PSA_ALG_IS_MAC(requested_alg) &&
753 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
754 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100755 /* Validate the combination of key type and algorithm. Since the policy
756 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
758 return 0;
759 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100760
Steven Cooreman31a876d2021-03-03 20:47:40 +0100761 /* Get both the requested output length for the algorithm which is to be
762 * verified, and the default output length for the base algorithm.
763 * Note that none of the currently supported algorithms have an output
764 * length dependent on actual key size, so setting it to a bogus value
765 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100766 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100768 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 key_type, 0,
770 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100771
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100772 /* If the policy is default-length, only allow an algorithm with
773 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
775 return requested_output_length == default_output_length;
776 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100777
778 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100779 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
781 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
782 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100783 }
784
Steven Cooremancd640932021-03-08 12:00:27 +0100785 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
786 * check for the requested MAC length to be equal to or longer than the
787 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
789 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
790 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100791 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200792 }
Steven Cooremance48e852020-10-05 16:02:45 +0200793 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200794 * a key derivation with that key agreement should also be allowed. This
795 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
797 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
798 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
799 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200800 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100801 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200803}
804
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100805/** Test whether a policy permits an algorithm.
806 *
807 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100808 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100809 * \note This function requires providing the key type for which the policy is
810 * being validated, since some algorithm policy definitions (e.g. MAC)
811 * have different properties depending on what kind of cipher it is
812 * combined with.
813 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100814 * \retval PSA_SUCCESS When \p alg is a specific algorithm
815 * allowed by the \p policy.
816 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
817 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
818 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100819 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
821 psa_key_type_t key_type,
822 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100823{
Steven Cooremand788fab2021-02-25 11:29:17 +0100824 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (alg == 0) {
826 return PSA_ERROR_INVALID_ARGUMENT;
827 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100828
Steven Cooreman7e39f052021-02-23 14:18:32 +0100829 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if (PSA_ALG_IS_WILDCARD(alg)) {
831 return PSA_ERROR_INVALID_ARGUMENT;
832 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
835 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
836 return PSA_SUCCESS;
837 } else {
838 return PSA_ERROR_NOT_PERMITTED;
839 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100840}
841
Gilles Peskinef603c712019-01-19 13:40:11 +0100842/** Restrict a key policy based on a constraint.
843 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100844 * \note This function requires providing the key type for which the policy is
845 * being restricted, since some algorithm policy definitions (e.g. MAC)
846 * have different properties depending on what kind of cipher it is
847 * combined with.
848 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100849 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100850 * \param[in,out] policy The policy to restrict.
851 * \param[in] constraint The policy constraint to apply.
852 *
853 * \retval #PSA_SUCCESS
854 * \c *policy contains the intersection of the original value of
855 * \c *policy and \c *constraint.
856 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100857 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100858 * \c *policy is unchanged.
859 */
860static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100861 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100862 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100864{
865 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 psa_key_policy_algorithm_intersection(key_type, policy->alg,
867 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200868 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
870 constraint->alg2);
871 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
872 return PSA_ERROR_INVALID_ARGUMENT;
873 }
874 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
875 return PSA_ERROR_INVALID_ARGUMENT;
876 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100877 policy->usage &= constraint->usage;
878 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200879 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100881}
882
Przemek Stekiel061f6942022-11-30 10:51:35 +0100883/** Get the description of a key given its identifier and policy constraints
884 * and lock it.
885 *
886 * The key must have allow all the usage flags set in \p usage. If \p alg is
887 * nonzero, the key must allow operations with this algorithm. If \p alg is
888 * zero, the algorithm is not checked.
889 *
890 * In case of a persistent key, the function loads the description of the key
891 * into a key slot if not already done.
892 *
Ryan Everett098c6652024-01-03 13:03:36 +0000893 * On success, the returned key slot has been registered for reading.
894 * It is the responsibility of the caller to call psa_unregister_read(slot)
895 * when they have finished reading the contents of the slot.
Przemek Stekiel061f6942022-11-30 10:51:35 +0100896 */
897static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100898 mbedtls_svc_key_id_t key,
899 psa_key_slot_t **p_slot,
900 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100902{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100904 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 status = psa_get_and_lock_key_slot(key, p_slot);
907 if (status != PSA_SUCCESS) {
908 return status;
909 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200910 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100911
912 /* Enforce that usage policy for the key slot contains all the flags
913 * required by the usage parameter. There is one exception: public
914 * keys can always be exported, so we treat public key objects as
915 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100921 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200922 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100923 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100924
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800925 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if (alg != 0) {
927 status = psa_key_policy_permits(&slot->attr.policy,
928 slot->attr.type,
929 alg);
930 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100931 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100933 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200936
937error:
938 *p_slot = NULL;
Ryan Everett098c6652024-01-03 13:03:36 +0000939 psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100942}
Darryl Green940d72c2018-07-13 13:18:51 +0100943
Ronald Cron5c522922020-11-14 16:35:34 +0100944/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200945 *
946 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200947 * available, as opposed to a key in a secure element and/or to be used
948 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200949 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200950 * This is a temporary function that may be used instead of
951 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
952 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200953 *
Ryan Everett098c6652024-01-03 13:03:36 +0000954 * On success, the returned key slot has been registered for reading.
955 * It is the responsibility of the caller to call psa_unregister_read(slot)
956 * when they have finished reading the contents of the slot.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200957 */
Ronald Cron5c522922020-11-14 16:35:34 +0100958static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
959 mbedtls_svc_key_id_t key,
960 psa_key_slot_t **p_slot,
961 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200963{
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
965 usage, alg);
966 if (status != PSA_SUCCESS) {
967 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200968 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everett098c6652024-01-03 13:03:36 +0000971 psa_unregister_read(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 *p_slot = NULL;
973 return PSA_ERROR_NOT_SUPPORTED;
974 }
975
976 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200977}
Gilles Peskine28f8f302019-07-24 13:30:31 +0200978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +0000980{
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +0100982 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 }
Ronald Cronea0f8a62020-11-25 17:52:23 +0100984
Ronald Cronea0f8a62020-11-25 17:52:23 +0100985 slot->key.data = NULL;
986 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +0000989}
990
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100991/** Completely wipe a slot in memory, including its policy.
992 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100993psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100994{
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +0100996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 /*
998 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +0000999 * do our best to report an unexpected amount of registered readers or
1000 * an unexpected state.
1001 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1002 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1004 * function is called as part of the execution of a test suite, the
1005 * execution of the test suite is stopped in error if the assertion fails.
1006 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001007 switch (slot->state) {
1008 case PSA_SLOT_FULL:
1009 /* In this state psa_wipe_key_slot() must only be called if the
1010 * caller is the last reader. */
1011 case PSA_SLOT_PENDING_DELETION:
1012 /* In this state psa_wipe_key_slot() must only be called if the
1013 * caller is the last reader. */
1014 if (slot->registered_readers != 1) {
1015 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1);
1016 status = PSA_ERROR_CORRUPTION_DETECTED;
1017 }
1018 break;
1019 case PSA_SLOT_FILLING:
1020 /* In this state registered_readers must be 0. */
1021 if (slot->registered_readers != 0) {
1022 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0);
1023 status = PSA_ERROR_CORRUPTION_DETECTED;
1024 }
1025 break;
1026 case PSA_SLOT_EMPTY:
1027 /* The slot is already empty, it cannot be wiped. */
1028 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1029 status = PSA_ERROR_CORRUPTION_DETECTED;
1030 break;
1031 default:
1032 /* The slot's state is invalid. */
1033 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001034 }
1035
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001036 /* Multipart operations may still be using the key. This is safe
1037 * because all multipart operation objects are independent from
1038 * the key slot: if they need to access the key after the setup
1039 * phase, they have a copy of the key. Note that this means that
1040 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001041 /* At this point, key material and other type-specific content has
1042 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001043 * zeroize because the metadata is not particularly sensitive.
1044 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 memset(slot, 0, sizeof(*slot));
1046 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001047}
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001050{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001051 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001052 psa_status_t status; /* status of the last operation */
1053 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001054#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1055 psa_se_drv_table_entry_t *driver;
1056#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (mbedtls_svc_key_id_is_null(key)) {
1059 return PSA_SUCCESS;
1060 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001061
Ronald Cronf2911112020-10-29 17:51:10 +01001062 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001063 * Get the description of the key in a key slot, and register to read it.
1064 * In the case of a persistent key, this will load the key description
1065 * from persistent memory if not done yet.
1066 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001067 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001068 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 status = psa_get_and_lock_key_slot(key, &slot);
1070 if (status != PSA_SUCCESS) {
1071 return status;
1072 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001073
Ryan Everettc053d962024-01-25 17:56:32 +00001074#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001075 /* We cannot unlock between setting the state to PENDING_DELETION
1076 * and destroying the key in storage, as otherwise another thread
1077 * could load the key into a new slot and the key will not be
1078 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001079 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1080 &mbedtls_threading_key_slot_mutex));
1081#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001082 /* Set the key slot containing the key description's state to
1083 * PENDING_DELETION. This stops new operations from registering
1084 * to read the slot. Current readers can safely continue to access
1085 * the key within the slot; the last registered reader will
1086 * automatically wipe the slot when they call psa_unregister_read().
1087 * If the key is persistent, we can now delete the copy of the key
1088 * from memory. If the key is opaque, we require the driver to
1089 * deal with the deletion. */
Ryan Everettc053d962024-01-25 17:56:32 +00001090 status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1091 PSA_SLOT_PENDING_DELETION);
1092
1093 if (status != PSA_SUCCESS) {
1094 goto exit;
1095 }
Ronald Cronf2911112020-10-29 17:51:10 +01001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001098 /* Refuse the destruction of a read-only key (which may or may not work
1099 * if we attempt it, depending on whether the key is merely read-only
1100 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001101 * Just do the best we can, which is to wipe the copy in memory
1102 * (done in this function's cleanup code). */
1103 overall_status = PSA_ERROR_NOT_PERMITTED;
1104 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001105 }
1106
Gilles Peskine354f7672019-07-12 23:46:38 +02001107#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1109 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001110 /* For a key in a secure element, we need to do three things:
1111 * remove the key file in internal storage, destroy the
1112 * key inside the secure element, and update the driver's
1113 * persistent data. Start a transaction that will encompass these
1114 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001116 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001118 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 status = psa_crypto_save_transaction();
1120 if (status != PSA_SUCCESS) {
1121 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001122 /* We should still try to destroy the key in the secure
1123 * element and the key metadata in storage. This is especially
1124 * important if the error is that the storage is full.
1125 * But how to do it exactly without risking an inconsistent
1126 * state after a reset?
1127 * https://github.com/ARMmbed/mbed-crypto/issues/215
1128 */
1129 overall_status = status;
1130 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001131 }
1132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 status = psa_destroy_se_key(driver,
1134 psa_key_slot_get_slot_number(slot));
1135 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001136 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001138 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001139#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1140
Darryl Greend49a4992018-06-18 17:27:26 +01001141#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001143 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001144 * The slot will still hold a copy of the key until the last reader
1145 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 status = psa_destroy_persistent_key(slot->attr.id);
1147 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001148 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 }
Darryl Greend49a4992018-06-18 17:27:26 +01001150 }
1151#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001152
Gilles Peskinefc762652019-07-22 19:30:34 +02001153#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if (driver != NULL) {
1155 status = psa_save_se_persistent_data(driver);
1156 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001157 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 }
1159 status = psa_crypto_stop_transaction();
1160 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001161 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001163 }
1164#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1165
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001166exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001167 /* Unregister from reading the slot. If we are the last active reader
1168 * then this will wipe the slot. */
1169 status = psa_unregister_read(slot);
Ryan Everettc053d962024-01-25 17:56:32 +00001170
1171#if defined(MBEDTLS_THREADING_C)
1172 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1173 &mbedtls_threading_key_slot_mutex));
1174#endif
1175
1176 /* Prioritize CORRUPTION_DETECTED from unregistering or
1177 * SERVICE_FAILURE from unlocking over a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001179 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 }
1181 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001182}
1183
Valerio Settib2bcedb2023-07-10 11:24:00 +02001184#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001185 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001186static psa_status_t psa_get_rsa_public_exponent(
1187 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001189{
1190 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001191 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001192 uint8_t *buffer = NULL;
1193 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1197 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001198 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 }
1200 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001201 /* It's the default value, which is reported as an empty string,
1202 * so there's nothing to do. */
1203 goto exit;
1204 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 buflen = mbedtls_mpi_size(&mpi);
1207 buffer = mbedtls_calloc(1, buflen);
1208 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001209 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1210 goto exit;
1211 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1213 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001214 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001216 attributes->domain_parameters = buffer;
1217 attributes->domain_parameters_size = buflen;
1218
1219exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 mbedtls_mpi_free(&mpi);
1221 if (ret != 0) {
1222 mbedtls_free(buffer);
1223 }
1224 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001225}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001226#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001227 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001228
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001229/** Retrieve all the publicly-accessible attributes of a key.
1230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001231psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1232 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001233{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001234 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001235 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001236 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1241 if (status != PSA_SUCCESS) {
1242 return status;
1243 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001244
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001245 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1247 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001248
1249#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1251 psa_set_key_slot_number(attributes,
1252 psa_key_slot_get_slot_number(slot));
1253 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001254#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001257#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1258 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001259 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001260 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001261 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Pengyu Lvf75893b2023-12-08 17:21:39 +08001262 /* TODO: This is a temporary situation where domain parameters are deprecated,
1263 * but we need it for namely generating an RSA key with a non-default exponent.
1264 * This would be improved after https://github.com/Mbed-TLS/mbedtls/issues/6494.
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001267 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001268
Ronald Cron90857082020-11-25 14:58:33 +01001269 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 slot->attr.type,
1271 slot->key.data,
1272 slot->key.bytes,
1273 &rsa);
1274 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001275 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 status = psa_get_rsa_public_exponent(rsa,
1279 attributes);
1280 mbedtls_rsa_free(rsa);
1281 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001282 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001283 break;
Pengyu Lve9efbc22023-12-08 16:59:08 +08001284#else
1285 case PSA_KEY_TYPE_RSA_KEY_PAIR:
1286 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1287 attributes->domain_parameters = NULL;
1288 attributes->domain_parameters_size = SIZE_MAX;
1289 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001290#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1291 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001292 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001293 default:
1294 /* Nothing else to do. */
1295 break;
1296 }
1297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (status != PSA_SUCCESS) {
1299 psa_reset_key_attributes(attributes);
1300 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001301
Ryan Everett1b70a072024-01-04 10:32:49 +00001302 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001305}
1306
Gilles Peskinec8000c02019-08-02 20:15:51 +02001307#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1308psa_status_t psa_get_key_slot_number(
1309 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001311{
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001313 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 return PSA_SUCCESS;
1315 } else {
1316 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001317 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001318}
1319#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1322 size_t key_buffer_size,
1323 uint8_t *data,
1324 size_t data_size,
1325 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001326{
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 if (key_buffer_size > data_size) {
1328 return PSA_ERROR_BUFFER_TOO_SMALL;
1329 }
1330 memcpy(data, key_buffer, key_buffer_size);
1331 memset(data + key_buffer_size, 0,
1332 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001333 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001335}
1336
Ronald Cron7285cda2020-11-26 14:46:37 +01001337psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001338 const psa_key_attributes_t *attributes,
1339 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001341{
Ronald Crond18b5f82020-11-25 16:46:05 +01001342 psa_key_type_t type = attributes->core.type;
1343
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 if (key_type_is_raw_bytes(type) ||
1345 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001346 PSA_KEY_TYPE_IS_ECC(type) ||
1347 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 return psa_export_key_buffer_internal(
1349 key_buffer, key_buffer_size,
1350 data, data_size, data_length);
1351 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001352 /* This shouldn't happen in the reference implementation, but
1353 it is valid for a special-purpose implementation to omit
1354 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001356 }
1357}
1358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1360 uint8_t *data,
1361 size_t data_size,
1362 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001363{
1364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1365 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1366 psa_key_slot_t *slot;
1367
Ronald Crone907e552021-01-18 13:22:38 +01001368 /* Reject a zero-length output buffer now, since this can never be a
1369 * valid key representation. This way we know that data must be a valid
1370 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 if (data_size == 0) {
1372 return PSA_ERROR_BUFFER_TOO_SMALL;
1373 }
Ronald Crone907e552021-01-18 13:22:38 +01001374
Ronald Cron9486f9d2020-11-26 13:36:23 +01001375 /* Set the key to empty now, so that even when there are errors, we always
1376 * set data_length to a value between 0 and data_size. On error, setting
1377 * the key to empty is a good choice because an empty key representation is
1378 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001379 *data_length = 0;
1380
Ronald Cron9486f9d2020-11-26 13:36:23 +01001381 /* Export requires the EXPORT flag. There is an exception for public keys,
1382 * which don't require any flag, but
1383 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1384 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1386 PSA_KEY_USAGE_EXPORT, 0);
1387 if (status != PSA_SUCCESS) {
1388 return status;
1389 }
mohammad160306e79202018-03-28 13:17:44 +03001390
Ronald Crond18b5f82020-11-25 16:46:05 +01001391 psa_key_attributes_t attributes = {
1392 .core = slot->attr
1393 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 status = psa_driver_wrapper_export_key(&attributes,
1395 slot->key.data, slot->key.bytes,
1396 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001397
Ryan Everett1b70a072024-01-04 10:32:49 +00001398 unlock_status = psa_unregister_read(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001401}
1402
Ronald Cron7285cda2020-11-26 14:46:37 +01001403psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001404 const psa_key_attributes_t *attributes,
1405 const uint8_t *key_buffer,
1406 size_t key_buffer_size,
1407 uint8_t *data,
1408 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001410{
Ronald Crond18b5f82020-11-25 16:46:05 +01001411 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001412
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001413 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001414 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1415 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001416 /* Exporting public -> public */
1417 return psa_export_key_buffer_internal(
1418 key_buffer, key_buffer_size,
1419 data, data_size, data_length);
1420 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001421#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001422 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1423 return mbedtls_psa_rsa_export_public_key(attributes,
1424 key_buffer,
1425 key_buffer_size,
1426 data,
1427 data_size,
1428 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001429#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001430 /* We don't know how to convert a private RSA key to public. */
1431 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001432#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001433 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001434 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001435#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001436 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1437 return mbedtls_psa_ecp_export_public_key(attributes,
1438 key_buffer,
1439 key_buffer_size,
1440 data,
1441 data_size,
1442 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001443#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001444 /* We don't know how to convert a private ECC key to public */
1445 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001446#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001447 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001448 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001449#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001450 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001451 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001452 key_buffer,
1453 key_buffer_size,
1454 data, data_size,
1455 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001456#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001457 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001458#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001459 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001461 (void) key_buffer;
1462 (void) key_buffer_size;
1463 (void) data;
1464 (void) data_size;
1465 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001467 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001468}
Ronald Crond18b5f82020-11-25 16:46:05 +01001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1471 uint8_t *data,
1472 size_t data_size,
1473 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001474{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001475 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001476 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001477 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001478 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001479
Ronald Crone907e552021-01-18 13:22:38 +01001480 /* Reject a zero-length output buffer now, since this can never be a
1481 * valid key representation. This way we know that data must be a valid
1482 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (data_size == 0) {
1484 return PSA_ERROR_BUFFER_TOO_SMALL;
1485 }
Ronald Crone907e552021-01-18 13:22:38 +01001486
Darryl Greendd8fb772018-11-07 16:00:44 +00001487 /* Set the key to empty now, so that even when there are errors, we always
1488 * set data_length to a value between 0 and data_size. On error, setting
1489 * the key to empty is a good choice because an empty key representation is
1490 * unlikely to be accepted anywhere. */
1491 *data_length = 0;
1492
1493 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1495 if (status != PSA_SUCCESS) {
1496 return status;
1497 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1500 status = PSA_ERROR_INVALID_ARGUMENT;
1501 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001502 }
1503
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001504 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001505 .core = slot->attr
1506 };
Ronald Cron67227982020-11-26 15:16:05 +01001507 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001508 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001510
1511exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00001512 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001515}
1516
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001517MBEDTLS_STATIC_ASSERT(
1518 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1519 "One or more key attribute flag is listed as both external-only and dual-use")
1520MBEDTLS_STATIC_ASSERT(
1521 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1522 "One or more key attribute flag is listed as both internal-only and dual-use")
1523MBEDTLS_STATIC_ASSERT(
1524 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1525 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001526
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001527/** Validate that a key policy is internally well-formed.
1528 *
1529 * This function only rejects invalid policies. It does not validate the
1530 * consistency of the policy with respect to other attributes of the key
1531 * such as the key type.
1532 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001533static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001534{
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1536 PSA_KEY_USAGE_COPY |
1537 PSA_KEY_USAGE_ENCRYPT |
1538 PSA_KEY_USAGE_DECRYPT |
1539 PSA_KEY_USAGE_SIGN_MESSAGE |
1540 PSA_KEY_USAGE_VERIFY_MESSAGE |
1541 PSA_KEY_USAGE_SIGN_HASH |
1542 PSA_KEY_USAGE_VERIFY_HASH |
1543 PSA_KEY_USAGE_VERIFY_DERIVATION |
1544 PSA_KEY_USAGE_DERIVE)) != 0) {
1545 return PSA_ERROR_INVALID_ARGUMENT;
1546 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001549}
1550
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001551/** Validate the internal consistency of key attributes.
1552 *
1553 * This function only rejects invalid attribute values. If does not
1554 * validate the consistency of the attributes with any key data that may
1555 * be involved in the creation of the key.
1556 *
1557 * Call this function early in the key creation process.
1558 *
1559 * \param[in] attributes Key attributes for the new key.
1560 * \param[out] p_drv On any return, the driver for the key, if any.
1561 * NULL for a transparent key.
1562 *
1563 */
1564static psa_status_t psa_validate_key_attributes(
1565 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001567{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001568 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1570 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 status = psa_validate_key_location(lifetime, p_drv);
1573 if (status != PSA_SUCCESS) {
1574 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001575 }
1576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 status = psa_validate_key_persistence(lifetime);
1578 if (status != PSA_SUCCESS) {
1579 return status;
1580 }
1581
1582 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1583 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1584 return PSA_ERROR_INVALID_ARGUMENT;
1585 }
1586 } else {
1587 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1588 return PSA_ERROR_INVALID_ARGUMENT;
1589 }
1590 }
1591
1592 status = psa_validate_key_policy(&attributes->core.policy);
1593 if (status != PSA_SUCCESS) {
1594 return status;
1595 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001596
1597 /* Refuse to create overly large keys.
1598 * Note that this doesn't trigger on import if the attributes don't
1599 * explicitly specify a size (so psa_get_key_bits returns 0), so
1600 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1602 return PSA_ERROR_NOT_SUPPORTED;
1603 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001604
Gilles Peskine91e8c332019-08-02 19:19:39 +02001605 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1607 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1608 return PSA_ERROR_INVALID_ARGUMENT;
1609 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001612}
1613
Gilles Peskine4747d192019-04-17 15:05:45 +02001614/** Prepare a key slot to receive key material.
1615 *
1616 * This function allocates a key slot and sets its metadata.
1617 *
1618 * If this function fails, call psa_fail_key_creation().
1619 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001620 * This function is intended to be used as follows:
1621 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001622 * it with the specified attributes, and in case of a volatile key assign it
1623 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001624 * -# Populate the slot with the key material.
1625 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1626 * In case of failure at any step, stop the sequence and call
1627 * psa_fail_key_creation().
1628 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001629 * On success, the key slot's state is PSA_SLOT_FILLING.
1630 * It is the responsibility of the caller to change the slot's state to
1631 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001632 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001633 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001634 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001635 * \param[out] p_slot On success, a pointer to the prepared slot.
1636 * \param[out] p_drv On any return, the driver for the key, if any.
1637 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001638 *
1639 * \retval #PSA_SUCCESS
1640 * The key slot is ready to receive key material.
1641 * \return If this function fails, the key slot is an invalid state.
1642 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001643 */
1644static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001645 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001646 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001647 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001649{
1650 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001651 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 psa_key_slot_t *slot;
1653
Gilles Peskinedf179142019-07-15 22:02:14 +02001654 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001655 *p_drv = NULL;
1656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 status = psa_validate_key_attributes(attributes, p_drv);
1658 if (status != PSA_SUCCESS) {
1659 return status;
1660 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001661
Ryan Everettb69118e2024-01-02 15:54:32 +00001662 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (status != PSA_SUCCESS) {
1664 return status;
1665 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001666 slot = *p_slot;
1667
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001668 /* We're storing the declared bit-size of the key. It's up to each
1669 * creation mechanism to verify that this information is correct.
1670 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001671 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001672 * is optional (import, copy). In case of a volatile key, assign it the
1673 * volatile key identifier associated to the slot returned to contain its
1674 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001675
1676 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001678#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1679 slot->attr.id = volatile_key_id;
1680#else
1681 slot->attr.id.key_id = volatile_key_id;
1682#endif
1683 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001684
Gilles Peskine91e8c332019-08-02 19:19:39 +02001685 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001686 * external-only flags, query `attributes`. Thanks to the check
1687 * in psa_validate_key_attributes(), this leaves the dual-use
Ryan Everettb69118e2024-01-02 15:54:32 +00001688 * flags and any internal flag that psa_reserve_free_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001689 * may have set. */
1690 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001691
Gilles Peskinecbaff462019-07-12 23:46:04 +02001692#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001693 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001694 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001695 * create the key file in internal storage, create the
1696 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001697 * persistent data. This is done by starting a transaction that will
1698 * encompass these three actions.
1699 * For registering a volatile key, we just need to find an appropriate
1700 * slot number inside the SE. Since the key is designated volatile, creating
1701 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001702 /* The first thing to do is to find a slot number for the new key.
1703 * We save the slot number in persistent storage as part of the
1704 * transaction data. It will be needed to recover if the power
1705 * fails during the key creation process, to clean up on the secure
1706 * element side after restarting. Obtaining a slot number from the
1707 * secure element driver updates its persistent state, but we do not yet
1708 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001709 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001711 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1713 &slot_number);
1714 if (status != PSA_SUCCESS) {
1715 return status;
1716 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1719 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001720 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001721 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001722 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 status = psa_crypto_save_transaction();
1724 if (status != PSA_SUCCESS) {
1725 (void) psa_crypto_stop_transaction();
1726 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001727 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001728 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001729
1730 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001732 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001733
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001735 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001737 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001738#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001741}
Gilles Peskine4747d192019-04-17 15:05:45 +02001742
1743/** Finalize the creation of a key once its key material has been set.
1744 *
1745 * This entails writing the key to persistent storage.
1746 *
1747 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001748 * See the documentation of psa_start_key_creation() for the intended use
1749 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001750 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001751 * If the finalization succeeds, the function sets the key slot's state to
1752 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1753 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001754 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001755 * \param[in,out] slot Pointer to the slot with key material.
1756 * \param[in] driver The secure element driver for the key,
1757 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001758 * \param[out] key On success, identifier of the key. Note that the
1759 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001760 *
1761 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001762 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001763 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1764 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1765 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1766 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1767 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1768 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001769 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001770 * \return If this function fails, the key slot is an invalid state.
1771 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001772 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001773static psa_status_t psa_finish_key_creation(
1774 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001775 psa_se_drv_table_entry_t *driver,
1776 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001777{
1778 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001779 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001780 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001781
1782#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001784#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001786 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001787 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001789
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001790 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1791 sizeof(data.slot_number),
1792 "Slot number size does not match psa_se_key_data_storage_t");
1793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1795 status = psa_save_persistent_key(&slot->attr,
1796 (uint8_t *) &data,
1797 sizeof(data));
1798 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001799#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1800 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001801 /* Key material is saved in export representation in the slot, so
1802 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 status = psa_save_persistent_key(&slot->attr,
1804 slot->key.data,
1805 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001806 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001807 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001808#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1809
Gilles Peskinecbaff462019-07-12 23:46:04 +02001810#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001811 /* Finish the transaction for a key creation. This does not
1812 * happen when registering an existing key. Detect this case
1813 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001814 * creation of a persistent key in a secure element requires a transaction,
1815 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 if (driver != NULL &&
1817 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1818 status = psa_save_se_persistent_data(driver);
1819 if (status != PSA_SUCCESS) {
1820 psa_destroy_persistent_key(slot->attr.id);
1821 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001822 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001824 }
1825#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001828 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001829 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1830 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001832 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001834 }
Ronald Cron50972942020-11-14 11:28:25 +01001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001837}
1838
1839/** Abort the creation of a key.
1840 *
1841 * You may call this function after calling psa_start_key_creation(),
1842 * or after psa_finish_key_creation() fails. In other circumstances, this
1843 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001844 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001845 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001846 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001847 * \param[in,out] slot Pointer to the slot with key material.
1848 * \param[in] driver The secure element driver for the key,
1849 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001850 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001851static void psa_fail_key_creation(psa_key_slot_t *slot,
1852 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001853{
Gilles Peskine011e4282019-06-26 18:34:38 +02001854 (void) driver;
1855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001857 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001859
1860#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001861 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001862 * element, and the failure happened later (when saving metadata
1863 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001864 * element.
1865 * https://github.com/ARMmbed/mbed-crypto/issues/217
1866 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001867
Gilles Peskined7729582019-08-05 15:55:54 +02001868 /* Abort the ongoing transaction if any (there may not be one if
1869 * the creation process failed before starting one, or if the
1870 * key creation is a registration of a key in a secure element).
1871 * Earlier functions must already have done what it takes to undo any
1872 * partial creation. All that's left is to update the transaction data
1873 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001875#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001878}
1879
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001880/** Validate optional attributes during key creation.
1881 *
1882 * Some key attributes are optional during key creation. If they are
1883 * specified in the attributes structure, check that they are consistent
1884 * with the data in the slot.
1885 *
1886 * This function should be called near the end of key creation, after
1887 * the slot in memory is fully populated but before saving persistent data.
1888 */
1889static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001890 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001892{
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 if (attributes->core.type != 0) {
1894 if (attributes->core.type != slot->attr.type) {
1895 return PSA_ERROR_INVALID_ARGUMENT;
1896 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001897 }
1898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001900#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1901 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1903 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001904 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001905 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001906 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001907
Ronald Cron90857082020-11-25 14:58:33 +01001908 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 slot->attr.type,
1910 slot->key.data,
1911 slot->key.bytes,
1912 &rsa);
1913 if (status != PSA_SUCCESS) {
1914 return status;
1915 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 mbedtls_mpi_init(&actual);
1918 mbedtls_mpi_init(&required);
1919 ret = mbedtls_rsa_export(rsa,
1920 NULL, NULL, NULL, NULL, &actual);
1921 mbedtls_rsa_free(rsa);
1922 mbedtls_free(rsa);
1923 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001924 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 }
1926 ret = mbedtls_mpi_read_binary(&required,
1927 attributes->domain_parameters,
1928 attributes->domain_parameters_size);
1929 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001930 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 }
1932 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001933 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 }
1935rsa_exit:
1936 mbedtls_mpi_free(&actual);
1937 mbedtls_mpi_free(&required);
1938 if (ret != 0) {
1939 return mbedtls_to_psa_error(ret);
1940 }
1941 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02001942#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
1943 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001944 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001945 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001947 }
1948 }
1949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 if (attributes->core.bits != 0) {
1951 if (attributes->core.bits != slot->attr.bits) {
1952 return PSA_ERROR_INVALID_ARGUMENT;
1953 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001954 }
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001957}
1958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1960 const uint8_t *data,
1961 size_t data_length,
1962 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001963{
1964 psa_status_t status;
1965 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001966 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001967 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301968 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001969
Ronald Cron81709fc2020-11-14 12:10:32 +01001970 *key = MBEDTLS_SVC_KEY_ID_INIT;
1971
Gilles Peskine0f84d622019-09-12 19:03:13 +02001972 /* Reject zero-length symmetric keys (including raw data key objects).
1973 * This also rejects any key which might be encoded as an empty string,
1974 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 if (data_length == 0) {
1976 return PSA_ERROR_INVALID_ARGUMENT;
1977 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001978
Archana449608b2021-09-08 15:36:05 +05301979 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if (data_length > SIZE_MAX / 8) {
1981 return PSA_ERROR_NOT_SUPPORTED;
1982 }
Archana6ed4bda2021-08-04 10:47:15 +05301983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1985 &slot, &driver);
1986 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001987 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001989
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001990 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301991 * storage ( thus not in the case of importing a key in a secure element
1992 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1993 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 if (slot->key.data == NULL) {
1995 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301996 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 attributes, data, data_length, &storage_size);
1998 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301999 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 }
Archanad8a83dc2021-06-14 10:04:16 +05302001 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 status = psa_allocate_buffer_to_slot(slot, storage_size);
2003 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002004 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002006 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002007
2008 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 status = psa_driver_wrapper_import_key(attributes,
2010 data, data_length,
2011 slot->key.data,
2012 slot->key.bytes,
2013 &slot->key.bytes, &bits);
2014 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002015 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002019 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002021 status = PSA_ERROR_INVALID_ARGUMENT;
2022 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002023 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002024
Archana6ed4bda2021-08-04 10:47:15 +05302025 /* Enforce a size limit, and in particular ensure that the bit
2026 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302028 status = PSA_ERROR_NOT_SUPPORTED;
2029 goto exit;
2030 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 status = psa_validate_optional_attributes(slot, attributes);
2032 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002033 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002037exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 if (status != PSA_SUCCESS) {
2039 psa_fail_key_creation(slot, driver);
2040 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002043}
2044
Gilles Peskined7729582019-08-05 15:55:54 +02002045#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2046psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002048{
2049 psa_status_t status;
2050 psa_key_slot_t *slot = NULL;
2051 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002052 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002053
2054 /* Leaving attributes unspecified is not currently supported.
2055 * It could make sense to query the key type and size from the
2056 * secure element, but not all secure elements support this
2057 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2059 return PSA_ERROR_NOT_SUPPORTED;
2060 }
2061 if (psa_get_key_bits(attributes) == 0) {
2062 return PSA_ERROR_NOT_SUPPORTED;
2063 }
Gilles Peskined7729582019-08-05 15:55:54 +02002064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2066 &slot, &driver);
2067 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002068 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 }
Gilles Peskined7729582019-08-05 15:55:54 +02002070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002072
2073exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 if (status != PSA_SUCCESS) {
2075 psa_fail_key_creation(slot, driver);
2076 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002077
Gilles Peskined7729582019-08-05 15:55:54 +02002078 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 psa_close_key(key);
2080 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002081}
2082#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2085 const psa_key_attributes_t *specified_attributes,
2086 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002087{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002088 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002089 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002090 psa_key_slot_t *source_slot = NULL;
2091 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002092 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002093 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302094 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002095
Ronald Cron81709fc2020-11-14 12:10:32 +01002096 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2097
Archana8a180362021-07-05 02:18:48 +05302098 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2100 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002101 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 status = psa_validate_optional_attributes(source_slot,
2105 specified_attributes);
2106 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002107 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002109
Archana9d17bf42021-09-10 06:22:44 +05302110 /* The target key type and number of bits have been validated by
2111 * psa_validate_optional_attributes() to be either equal to zero or
2112 * equal to the ones of the source key. So it is safe to inherit
2113 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302114 * */
Archana9d17bf42021-09-10 06:22:44 +05302115 actual_attributes.core.bits = source_slot->attr.bits;
2116 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302117
2118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 status = psa_restrict_key_policy(source_slot->attr.type,
2120 &actual_attributes.core.policy,
2121 &source_slot->attr.policy);
2122 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2127 &target_slot, &driver);
2128 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 }
2131 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2132 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302133 /*
Archana9d17bf42021-09-10 06:22:44 +05302134 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302135 * the source key would need to be exported as plaintext and re-imported
2136 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302137 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302138 * appropriate API invocations from the application, if needed.
2139 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002140 status = PSA_ERROR_NOT_SUPPORTED;
2141 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002142 }
Archana8a180362021-07-05 02:18:48 +05302143 /*
2144 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302145 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302146 * - For opaque keys this translates to an invocation of the drivers'
2147 * copy_key entry point through the dispatch layer.
2148 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2150 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2151 &storage_size);
2152 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302153 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 }
Archana374fe5b2021-09-08 15:50:28 +05302155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2157 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302158 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 }
Archana374fe5b2021-09-08 15:50:28 +05302160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 status = psa_driver_wrapper_copy_key(&actual_attributes,
2162 source_slot->key.data,
2163 source_slot->key.bytes,
2164 target_slot->key.data,
2165 target_slot->key.bytes,
2166 &target_slot->key.bytes);
2167 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302168 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 }
2170 } else {
2171 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302172 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 source_slot->key.bytes);
2174 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302175 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 }
Archana8a180362021-07-05 02:18:48 +05302177 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002179exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 if (status != PSA_SUCCESS) {
2181 psa_fail_key_creation(target_slot, driver);
2182 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002183
Ryan Everett1b70a072024-01-04 10:32:49 +00002184 unlock_status = psa_unregister_read(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002185
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002187}
2188
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002189
2190
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002191/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002192/* Message digests */
2193/****************************************************************/
2194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002196{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002197 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if (operation->id == 0) {
2199 return PSA_SUCCESS;
2200 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002203 operation->id = 0;
2204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002206}
2207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2209 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002210{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002211 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002212
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002213 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002215 status = PSA_ERROR_BAD_STATE;
2216 goto exit;
2217 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002220 status = PSA_ERROR_INVALID_ARGUMENT;
2221 goto exit;
2222 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002223
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002224 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2225 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002229
2230exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 if (status != PSA_SUCCESS) {
2232 psa_hash_abort(operation);
2233 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002234
2235 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002236}
2237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2239 const uint8_t *input,
2240 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002241{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002242 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002245 status = PSA_ERROR_BAD_STATE;
2246 goto exit;
2247 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002248
Steven Cooremanc8288352021-03-04 14:02:19 +01002249 /* Don't require hash implementations to behave correctly on a
2250 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 if (input_length == 0) {
2252 return PSA_SUCCESS;
2253 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002254
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002256
2257exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 if (status != PSA_SUCCESS) {
2259 psa_hash_abort(operation);
2260 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002263}
2264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2266 uint8_t *hash,
2267 size_t hash_size,
2268 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002270 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 if (operation->id == 0) {
2272 return PSA_ERROR_BAD_STATE;
2273 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002274
Steven Cooreman1e582352021-02-18 17:24:37 +01002275 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 operation, hash, hash_size, hash_length);
2277 psa_hash_abort(operation);
2278 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002279}
2280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2282 const uint8_t *hash,
2283 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002284{
Ronald Cron69a63422021-10-18 09:47:58 +02002285 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002287 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 operation,
2289 actual_hash, sizeof(actual_hash),
2290 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002293 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002297 status = PSA_ERROR_INVALID_SIGNATURE;
2298 goto exit;
2299 }
2300
Dave Rodgman78701152023-08-29 14:20:18 +01002301 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002302 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002304
2305exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2307 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002308 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002312}
2313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314psa_status_t psa_hash_compute(psa_algorithm_t alg,
2315 const uint8_t *input, size_t input_length,
2316 uint8_t *hash, size_t hash_size,
2317 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002318{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002319 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 if (!PSA_ALG_IS_HASH(alg)) {
2321 return PSA_ERROR_INVALID_ARGUMENT;
2322 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2325 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002326}
2327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328psa_status_t psa_hash_compare(psa_algorithm_t alg,
2329 const uint8_t *input, size_t input_length,
2330 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002331{
Ronald Cron69a63422021-10-18 09:47:58 +02002332 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002333 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if (!PSA_ALG_IS_HASH(alg)) {
2336 return PSA_ERROR_INVALID_ARGUMENT;
2337 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002338
Steven Cooreman1e582352021-02-18 17:24:37 +01002339 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 alg, input, input_length,
2341 actual_hash, sizeof(actual_hash),
2342 &actual_hash_length);
2343 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002344 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 }
2346 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002347 status = PSA_ERROR_INVALID_SIGNATURE;
2348 goto exit;
2349 }
Dave Rodgman78701152023-08-29 14:20:18 +01002350 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002351 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002353
2354exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2356 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002357}
2358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2360 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002361{
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (source_operation->id == 0 ||
2363 target_operation->id != 0) {
2364 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002365 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2368 target_operation);
2369 if (status != PSA_SUCCESS) {
2370 psa_hash_abort(target_operation);
2371 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002374}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002375
2376
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002377/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002378/* MAC */
2379/****************************************************************/
2380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002382{
Steven Cooremane6804192021-03-19 18:28:56 +01002383 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 if (operation->id == 0) {
2385 return PSA_SUCCESS;
2386 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002389 operation->mac_size = 0;
2390 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002391 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002394}
2395
Ronald Cron2dff3b22021-06-17 16:33:22 +02002396static psa_status_t psa_mac_finalize_alg_and_key_validation(
2397 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002398 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002400{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002401 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 psa_key_type_t key_type = psa_get_key_type(attributes);
2403 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002404
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 if (!PSA_ALG_IS_MAC(alg)) {
2406 return PSA_ERROR_INVALID_ARGUMENT;
2407 }
Steven Cooremane6804192021-03-19 18:28:56 +01002408
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002409 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 status = psa_mac_key_can_do(alg, key_type);
2411 if (status != PSA_SUCCESS) {
2412 return status;
2413 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002414
Steven Cooremand1a68f12021-05-10 11:13:41 +02002415 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002419 /* A very short MAC is too short for security since it can be
2420 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2421 * so we make this our minimum, even though 32 bits is still
2422 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002424 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002425
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2427 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002428 /* It's impossible to "truncate" to a larger length than the full length
2429 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002431 }
2432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002434 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2435 * that is disabled in the compile-time configuration. The result can
2436 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2437 * configuration into account. In this case, force a return of
2438 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2439 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2440 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2441 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2442 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002444 }
2445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002447}
2448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2450 mbedtls_svc_key_id_t key,
2451 psa_algorithm_t alg,
2452 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002453{
2454 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2455 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002456 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002457 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002458
2459 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002461 status = PSA_ERROR_BAD_STATE;
2462 goto exit;
2463 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002464
2465 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 key,
2467 &slot,
2468 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2469 alg);
2470 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002471 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002473
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002474 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002475 .core = slot->attr
2476 };
2477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2479 &operation->mac_size);
2480 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002481 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483
2484 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002485 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (is_sign) {
2487 status = psa_driver_wrapper_mac_sign_setup(operation,
2488 &attributes,
2489 slot->key.data,
2490 slot->key.bytes,
2491 alg);
2492 } else {
2493 status = psa_driver_wrapper_mac_verify_setup(operation,
2494 &attributes,
2495 slot->key.data,
2496 slot->key.bytes,
2497 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002498 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002499
Gilles Peskinefbfac682018-07-08 20:51:54 +02002500exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 if (status != PSA_SUCCESS) {
2502 psa_mac_abort(operation);
2503 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002504
Ryan Everett1b70a072024-01-04 10:32:49 +00002505 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002508}
2509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2511 mbedtls_svc_key_id_t key,
2512 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002513{
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002515}
2516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2518 mbedtls_svc_key_id_t key,
2519 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002520{
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002522}
2523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2525 const uint8_t *input,
2526 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002527{
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 if (operation->id == 0) {
2529 return PSA_ERROR_BAD_STATE;
2530 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002532 /* Don't require hash implementations to behave correctly on a
2533 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 if (input_length == 0) {
2535 return PSA_SUCCESS;
2536 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002537
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2539 input, input_length);
2540 if (status != PSA_SUCCESS) {
2541 psa_mac_abort(operation);
2542 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545}
2546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2548 uint8_t *mac,
2549 size_t mac_size,
2550 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002551{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002552 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2553 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002556 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002557 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002558 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002561 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002562 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002563 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002564
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002565 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2566 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002568 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002569 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002570 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002573 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002574 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002575 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 status = psa_driver_wrapper_mac_sign_finish(operation,
2578 mac, operation->mac_size,
2579 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002580
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002581exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002582 /* In case of success, set the potential excess room in the output buffer
2583 * to an invalid value, to avoid potentially leaking a longer MAC.
2584 * In case of error, set the output length and content to a safe default,
2585 * such that in case the caller misses an error check, the output would be
2586 * an unachievable MAC.
2587 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002589 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002590 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002591 }
mohammad16036df908f2018-04-02 08:34:15 -07002592
Paul Elliottdc42ca82023-02-24 18:11:59 +00002593 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002594
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002598}
2599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2601 const uint8_t *mac,
2602 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002604 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2605 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002608 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002609 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002610 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002613 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002614 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002615 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002618 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002619 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002620 }
2621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 status = psa_driver_wrapper_mac_verify_finish(operation,
2623 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002624
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002625exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002627
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002629}
2630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2632 psa_algorithm_t alg,
2633 const uint8_t *input,
2634 size_t input_length,
2635 uint8_t *mac,
2636 size_t mac_size,
2637 size_t *mac_length,
2638 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002639{
2640 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002641 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2642 psa_key_slot_t *slot;
2643 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002644 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002645
Ronald Cron51131b52021-06-17 17:17:20 +02002646 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 key,
2648 &slot,
2649 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2650 alg);
2651 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002652 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002654
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002655 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002656 .core = slot->attr
2657 };
2658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2660 &operation_mac_size);
2661 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002662 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002666 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002667 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002668 }
2669
2670 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 &attributes,
2672 slot->key.data, slot->key.bytes,
2673 alg,
2674 input, input_length,
2675 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002676
2677exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002678 /* In case of success, set the potential excess room in the output buffer
2679 * to an invalid value, to avoid potentially leaking a longer MAC.
2680 * In case of error, set the output length and content to a safe default,
2681 * such that in case the caller misses an error check, the output would be
2682 * an unachievable MAC.
2683 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002685 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002686 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002687 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002688
2689 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002690
Ryan Everett1b70a072024-01-04 10:32:49 +00002691 unlock_status = psa_unregister_read(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002694}
2695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002697 psa_algorithm_t alg,
2698 const uint8_t *input,
2699 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 uint8_t *mac,
2701 size_t mac_size,
2702 size_t *mac_length)
2703{
2704 return psa_mac_compute_internal(key, alg,
2705 input, input_length,
2706 mac, mac_size, mac_length, 1);
2707}
2708
2709psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2710 psa_algorithm_t alg,
2711 const uint8_t *input,
2712 size_t input_length,
2713 const uint8_t *mac,
2714 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002715{
2716 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002717 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2718 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 status = psa_mac_compute_internal(key, alg,
2721 input, input_length,
2722 actual_mac, sizeof(actual_mac),
2723 &actual_mac_length, 0);
2724 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002725 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002729 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002730 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002731 }
Dave Rodgman78701152023-08-29 14:20:18 +01002732 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002733 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002734 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002735 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002736
2737exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002741}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002742
Gilles Peskinee59236f2018-01-27 23:32:46 +01002743/****************************************************************/
2744/* Asymmetric cryptography */
2745/****************************************************************/
2746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2748 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002749{
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 if (input_is_message) {
2751 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2752 return PSA_ERROR_INVALID_ARGUMENT;
2753 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2756 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2757 return PSA_ERROR_INVALID_ARGUMENT;
2758 }
2759 }
2760 } else {
2761 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2762 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002763 }
2764 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002765
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002767}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2770 int input_is_message,
2771 psa_algorithm_t alg,
2772 const uint8_t *input,
2773 size_t input_length,
2774 uint8_t *signature,
2775 size_t signature_size,
2776 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002777{
2778 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2779 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2780 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002781 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002782
2783 *signature_length = 0;
2784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 status = psa_sign_verify_check_alg(input_is_message, alg);
2786 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002787 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002789
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002790 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002791 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002792 * buffer can in principle be empty since it doesn't actually have
2793 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 if (signature_size == 0) {
2795 return PSA_ERROR_BUFFER_TOO_SMALL;
2796 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002797
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002798 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 key, &slot,
2800 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2801 PSA_KEY_USAGE_SIGN_HASH,
2802 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002805 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002807
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002809 status = PSA_ERROR_INVALID_ARGUMENT;
2810 goto exit;
2811 }
2812
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002813 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002815 };
2816
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002818 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002819 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002820 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 signature, signature_size, signature_length);
2822 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002823
2824 status = psa_driver_wrapper_sign_hash(
2825 &attributes, slot->key.data, slot->key.bytes,
2826 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002828 }
2829
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002830
2831exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002832 psa_wipe_tag_output_buffer(signature, status, signature_size,
2833 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002834
Ryan Everett1b70a072024-01-04 10:32:49 +00002835 unlock_status = psa_unregister_read(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002836
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002838}
2839
Gilles Peskine449bd832023-01-11 14:50:10 +01002840static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2841 int input_is_message,
2842 psa_algorithm_t alg,
2843 const uint8_t *input,
2844 size_t input_length,
2845 const uint8_t *signature,
2846 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002847{
2848 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2849 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2850 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 status = psa_sign_verify_check_alg(input_is_message, alg);
2853 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002854 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002856
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002857 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 key, &slot,
2859 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2860 PSA_KEY_USAGE_VERIFY_HASH,
2861 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002862
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 if (status != PSA_SUCCESS) {
2864 return status;
2865 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002866
2867 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002869 };
2870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002872 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002873 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002874 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 signature, signature_length);
2876 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002877 status = psa_driver_wrapper_verify_hash(
2878 &attributes, slot->key.data, slot->key.bytes,
2879 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002881 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002882
Ryan Everett1b70a072024-01-04 10:32:49 +00002883 unlock_status = psa_unregister_read(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002884
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002886
2887}
2888
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002889psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002890 const psa_key_attributes_t *attributes,
2891 const uint8_t *key_buffer,
2892 size_t key_buffer_size,
2893 psa_algorithm_t alg,
2894 const uint8_t *input,
2895 size_t input_length,
2896 uint8_t *signature,
2897 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002899{
2900 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002901
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002903 size_t hash_length;
2904 uint8_t hash[PSA_HASH_MAX_SIZE];
2905
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002906 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 PSA_ALG_SIGN_GET_HASH(alg),
2908 input, input_length,
2909 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002910
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002912 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002914
2915 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 attributes, key_buffer, key_buffer_size,
2917 alg, hash, hash_length,
2918 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002919 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002922}
2923
Gilles Peskine449bd832023-01-11 14:50:10 +01002924psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2925 psa_algorithm_t alg,
2926 const uint8_t *input,
2927 size_t input_length,
2928 uint8_t *signature,
2929 size_t signature_size,
2930 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002931{
2932 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002933 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002935}
2936
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002937psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002938 const psa_key_attributes_t *attributes,
2939 const uint8_t *key_buffer,
2940 size_t key_buffer_size,
2941 psa_algorithm_t alg,
2942 const uint8_t *input,
2943 size_t input_length,
2944 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002946{
2947 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002948
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002950 size_t hash_length;
2951 uint8_t hash[PSA_HASH_MAX_SIZE];
2952
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002953 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 PSA_ALG_SIGN_GET_HASH(alg),
2955 input, input_length,
2956 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002957
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002959 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002961
2962 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 attributes, key_buffer, key_buffer_size,
2964 alg, hash, hash_length,
2965 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002966 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002967
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002969}
2970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2972 psa_algorithm_t alg,
2973 const uint8_t *input,
2974 size_t input_length,
2975 const uint8_t *signature,
2976 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002977{
2978 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002979 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002981}
2982
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002983psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002984 const psa_key_attributes_t *attributes,
2985 const uint8_t *key_buffer, size_t key_buffer_size,
2986 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002988{
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2990 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2991 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002992#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2994 return mbedtls_psa_rsa_sign_hash(
2995 attributes,
2996 key_buffer, key_buffer_size,
2997 alg, hash, hash_length,
2998 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002999#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3000 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 } else {
3002 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003003 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3005 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003006#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3008 return mbedtls_psa_ecdsa_sign_hash(
3009 attributes,
3010 key_buffer, key_buffer_size,
3011 alg, hash, hash_length,
3012 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003013#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3014 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 } else {
3016 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003017 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003018 }
Ronald Cron4501c982021-03-19 20:09:32 +01003019
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 (void) key_buffer;
3021 (void) key_buffer_size;
3022 (void) hash;
3023 (void) hash_length;
3024 (void) signature;
3025 (void) signature_size;
3026 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003029}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003030
Gilles Peskine449bd832023-01-11 14:50:10 +01003031psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3032 psa_algorithm_t alg,
3033 const uint8_t *hash,
3034 size_t hash_length,
3035 uint8_t *signature,
3036 size_t signature_size,
3037 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003038{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003039 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003040 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003042}
3043
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003044psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003045 const psa_key_attributes_t *attributes,
3046 const uint8_t *key_buffer, size_t key_buffer_size,
3047 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003049{
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3051 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3052 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003053#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3055 return mbedtls_psa_rsa_verify_hash(
3056 attributes,
3057 key_buffer, key_buffer_size,
3058 alg, hash, hash_length,
3059 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003060#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3061 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 } else {
3063 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003064 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3066 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003067#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3069 return mbedtls_psa_ecdsa_verify_hash(
3070 attributes,
3071 key_buffer, key_buffer_size,
3072 alg, hash, hash_length,
3073 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003074#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3075 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 } else {
3077 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003078 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003079 }
Ronald Cron4501c982021-03-19 20:09:32 +01003080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 (void) key_buffer;
3082 (void) key_buffer_size;
3083 (void) hash;
3084 (void) hash_length;
3085 (void) signature;
3086 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003087
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003089}
3090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3092 psa_algorithm_t alg,
3093 const uint8_t *hash,
3094 size_t hash_length,
3095 const uint8_t *signature,
3096 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003097{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003098 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003099 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003101}
3102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3104 psa_algorithm_t alg,
3105 const uint8_t *input,
3106 size_t input_length,
3107 const uint8_t *salt,
3108 size_t salt_length,
3109 uint8_t *output,
3110 size_t output_size,
3111 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003112{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003113 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003114 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003115 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003116 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003117
Darryl Green5cc689a2018-07-24 15:34:10 +01003118 (void) input;
3119 (void) input_length;
3120 (void) salt;
3121 (void) output;
3122 (void) output_size;
3123
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003124 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003125
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3127 return PSA_ERROR_INVALID_ARGUMENT;
3128 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003129
Valerio Setti5bb454a2024-01-15 10:43:16 +01003130 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3132 if (status != PSA_SUCCESS) {
3133 return status;
3134 }
3135 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3136 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003137 status = PSA_ERROR_INVALID_ARGUMENT;
3138 goto exit;
3139 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003140
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003141 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003143 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003144
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003145 status = psa_driver_wrapper_asymmetric_encrypt(
3146 &attributes, slot->key.data, slot->key.bytes,
3147 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003149exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00003150 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003151
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003153}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003154
Gilles Peskine449bd832023-01-11 14:50:10 +01003155psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3156 psa_algorithm_t alg,
3157 const uint8_t *input,
3158 size_t input_length,
3159 const uint8_t *salt,
3160 size_t salt_length,
3161 uint8_t *output,
3162 size_t output_size,
3163 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003164{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003165 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003166 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003167 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003168 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003169
Darryl Green5cc689a2018-07-24 15:34:10 +01003170 (void) input;
3171 (void) input_length;
3172 (void) salt;
3173 (void) output;
3174 (void) output_size;
3175
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003176 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003177
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3179 return PSA_ERROR_INVALID_ARGUMENT;
3180 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003181
Valerio Setti5bb454a2024-01-15 10:43:16 +01003182 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3184 if (status != PSA_SUCCESS) {
3185 return status;
3186 }
3187 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003188 status = PSA_ERROR_INVALID_ARGUMENT;
3189 goto exit;
3190 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003192 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003194 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003195
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003196 status = psa_driver_wrapper_asymmetric_decrypt(
3197 &attributes, slot->key.data, slot->key.bytes,
3198 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003200
3201exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00003202 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003205}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003206
Paul Elliott9fe12f62022-11-30 19:16:02 +00003207/****************************************************************/
3208/* Asymmetric interruptible cryptography */
3209/****************************************************************/
3210
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003211static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3212
Paul Elliott9fe12f62022-11-30 19:16:02 +00003213void psa_interruptible_set_max_ops(uint32_t max_ops)
3214{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003215 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003216}
3217
3218uint32_t psa_interruptible_get_max_ops(void)
3219{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003220 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003221}
3222
Paul Elliott9fe12f62022-11-30 19:16:02 +00003223uint32_t psa_sign_hash_get_num_ops(
3224 const psa_sign_hash_interruptible_operation_t *operation)
3225{
Paul Elliott296ede92022-12-15 17:00:30 +00003226 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003227}
3228
3229uint32_t psa_verify_hash_get_num_ops(
3230 const psa_verify_hash_interruptible_operation_t *operation)
3231{
Paul Elliott296ede92022-12-15 17:00:30 +00003232 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003233}
3234
Paul Elliott59ad9452022-12-18 15:09:02 +00003235static psa_status_t psa_sign_hash_abort_internal(
3236 psa_sign_hash_interruptible_operation_t *operation)
3237{
3238 if (operation->id == 0) {
3239 /* The object has (apparently) been initialized but it is not (yet)
3240 * in use. It's ok to call abort on such an object, and there's
3241 * nothing to do. */
3242 return PSA_SUCCESS;
3243 }
3244
3245 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3246
3247 status = psa_driver_wrapper_sign_hash_abort(operation);
3248
3249 operation->id = 0;
3250
Paul Elliotte6145dc2023-02-07 12:51:21 +00003251 /* Do not clear either the error_occurred or num_ops elements here as they
3252 * only want to be cleared by the application calling abort, not by abort
3253 * being called at completion of an operation. */
3254
Paul Elliott59ad9452022-12-18 15:09:02 +00003255 return status;
3256}
3257
Paul Elliott9fe12f62022-11-30 19:16:02 +00003258psa_status_t psa_sign_hash_start(
3259 psa_sign_hash_interruptible_operation_t *operation,
3260 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3261 const uint8_t *hash, size_t hash_length)
3262{
3263 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3264 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3265 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003266 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003267
Paul Elliottc9774412023-02-06 15:14:07 +00003268 /* Check that start has not been previously called, or operation has not
3269 * previously errored. */
3270 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003271 return PSA_ERROR_BAD_STATE;
3272 }
3273
Paul Elliott9fe12f62022-11-30 19:16:02 +00003274 status = psa_sign_verify_check_alg(0, alg);
3275 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003276 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003277 return status;
3278 }
3279
3280 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3281 PSA_KEY_USAGE_SIGN_HASH,
3282 alg);
3283
3284 if (status != PSA_SUCCESS) {
3285 goto exit;
3286 }
3287
3288 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3289 status = PSA_ERROR_INVALID_ARGUMENT;
3290 goto exit;
3291 }
3292
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003293 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003294 .core = slot->attr
3295 };
3296
Paul Elliott296ede92022-12-15 17:00:30 +00003297 /* Ensure ops count gets reset, in case of operation re-use. */
3298 operation->num_ops = 0;
3299
Paul Elliott9fe12f62022-11-30 19:16:02 +00003300 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3301 slot->key.data,
3302 slot->key.bytes, alg,
3303 hash, hash_length);
3304exit:
3305
3306 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003307 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003308 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003309 }
3310
Ryan Everett1b70a072024-01-04 10:32:49 +00003311 unlock_status = psa_unregister_read(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003312
Paul Elliottc9774412023-02-06 15:14:07 +00003313 if (unlock_status != PSA_SUCCESS) {
3314 operation->error_occurred = 1;
3315 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003316
Paul Elliottc9774412023-02-06 15:14:07 +00003317 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003318}
3319
3320
3321psa_status_t psa_sign_hash_complete(
3322 psa_sign_hash_interruptible_operation_t *operation,
3323 uint8_t *signature, size_t signature_size,
3324 size_t *signature_length)
3325{
3326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3327
3328 *signature_length = 0;
3329
Paul Elliottc9774412023-02-06 15:14:07 +00003330 /* Check that start has been called first, and that operation has not
3331 * previously errored. */
3332 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003333 status = PSA_ERROR_BAD_STATE;
3334 goto exit;
3335 }
3336
Paul Elliott096abc42023-02-03 18:33:23 +00003337 /* Immediately reject a zero-length signature buffer. This guarantees that
3338 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003339 if (signature_size == 0) {
3340 status = PSA_ERROR_BUFFER_TOO_SMALL;
3341 goto exit;
3342 }
3343
3344 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3345 signature_size,
3346 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003347
Paul Elliott296ede92022-12-15 17:00:30 +00003348 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003349 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003350
Paul Elliottebe225c2023-02-10 14:32:53 +00003351exit:
3352
Paul Elliott59200a22023-02-21 15:07:40 +00003353 psa_wipe_tag_output_buffer(signature, status, signature_size,
3354 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003355
Paul Elliott53bb3122023-02-10 14:22:22 +00003356 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003357 if (status != PSA_SUCCESS) {
3358 operation->error_occurred = 1;
3359 }
3360
Paul Elliott59ad9452022-12-18 15:09:02 +00003361 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003362 }
3363
3364 return status;
3365}
3366
3367psa_status_t psa_sign_hash_abort(
3368 psa_sign_hash_interruptible_operation_t *operation)
3369{
Paul Elliott59ad9452022-12-18 15:09:02 +00003370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3371
3372 status = psa_sign_hash_abort_internal(operation);
3373
3374 /* We clear the number of ops done here, so that it is not cleared when
3375 * the operation fails or succeeds, only on manual abort. */
3376 operation->num_ops = 0;
3377
Paul Elliottc9774412023-02-06 15:14:07 +00003378 /* Likewise, failure state. */
3379 operation->error_occurred = 0;
3380
Paul Elliott59ad9452022-12-18 15:09:02 +00003381 return status;
3382}
3383
3384static psa_status_t psa_verify_hash_abort_internal(
3385 psa_verify_hash_interruptible_operation_t *operation)
3386{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003387 if (operation->id == 0) {
3388 /* The object has (apparently) been initialized but it is not (yet)
3389 * in use. It's ok to call abort on such an object, and there's
3390 * nothing to do. */
3391 return PSA_SUCCESS;
3392 }
3393
Paul Elliott59ad9452022-12-18 15:09:02 +00003394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3395
3396 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003397
3398 operation->id = 0;
3399
Paul Elliotte6145dc2023-02-07 12:51:21 +00003400 /* Do not clear either the error_occurred or num_ops elements here as they
3401 * only want to be cleared by the application calling abort, not by abort
3402 * being called at completion of an operation. */
3403
Paul Elliott59ad9452022-12-18 15:09:02 +00003404 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003405}
3406
3407psa_status_t psa_verify_hash_start(
3408 psa_verify_hash_interruptible_operation_t *operation,
3409 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3410 const uint8_t *hash, size_t hash_length,
3411 const uint8_t *signature, size_t signature_length)
3412{
3413 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3414 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3415 psa_key_slot_t *slot;
3416
Paul Elliottc9774412023-02-06 15:14:07 +00003417 /* Check that start has not been previously called, or operation has not
3418 * previously errored. */
3419 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003420 return PSA_ERROR_BAD_STATE;
3421 }
3422
3423 status = psa_sign_verify_check_alg(0, alg);
3424 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003425 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003426 return status;
3427 }
3428
3429 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3430 PSA_KEY_USAGE_VERIFY_HASH,
3431 alg);
3432
3433 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003434 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003435 return status;
3436 }
3437
3438 psa_key_attributes_t attributes = {
3439 .core = slot->attr
3440 };
3441
Paul Elliott296ede92022-12-15 17:00:30 +00003442 /* Ensure ops count gets reset, in case of operation re-use. */
3443 operation->num_ops = 0;
3444
Paul Elliott9fe12f62022-11-30 19:16:02 +00003445 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3446 slot->key.data,
3447 slot->key.bytes,
3448 alg, hash, hash_length,
3449 signature, signature_length);
3450
3451 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003452 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003453 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003454 }
3455
Ryan Everett1b70a072024-01-04 10:32:49 +00003456 unlock_status = psa_unregister_read(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003457
Paul Elliottc9774412023-02-06 15:14:07 +00003458 if (unlock_status != PSA_SUCCESS) {
3459 operation->error_occurred = 1;
3460 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003461
Paul Elliottc9774412023-02-06 15:14:07 +00003462 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003463}
3464
3465psa_status_t psa_verify_hash_complete(
3466 psa_verify_hash_interruptible_operation_t *operation)
3467{
3468 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3469
Paul Elliottc9774412023-02-06 15:14:07 +00003470 /* Check that start has been called first, and that operation has not
3471 * previously errored. */
3472 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003473 status = PSA_ERROR_BAD_STATE;
3474 goto exit;
3475 }
3476
3477 status = psa_driver_wrapper_verify_hash_complete(operation);
3478
Paul Elliott296ede92022-12-15 17:00:30 +00003479 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003480 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003481 operation);
3482
Paul Elliottebe225c2023-02-10 14:32:53 +00003483exit:
3484
Paul Elliott9fe12f62022-11-30 19:16:02 +00003485 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003486 if (status != PSA_SUCCESS) {
3487 operation->error_occurred = 1;
3488 }
3489
Paul Elliott59ad9452022-12-18 15:09:02 +00003490 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003491 }
3492
3493 return status;
3494}
3495
3496psa_status_t psa_verify_hash_abort(
3497 psa_verify_hash_interruptible_operation_t *operation)
3498{
Paul Elliott59ad9452022-12-18 15:09:02 +00003499 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003500
Paul Elliott59ad9452022-12-18 15:09:02 +00003501 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003502
Paul Elliott59ad9452022-12-18 15:09:02 +00003503 /* We clear the number of ops done here, so that it is not cleared when
3504 * the operation fails or succeeds, only on manual abort. */
3505 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003506
Paul Elliottc9774412023-02-06 15:14:07 +00003507 /* Likewise, failure state. */
3508 operation->error_occurred = 0;
3509
Paul Elliott59ad9452022-12-18 15:09:02 +00003510 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003511}
3512
Paul Elliott588f8ed2022-12-02 18:10:26 +00003513/****************************************************************/
3514/* Asymmetric interruptible cryptography internal */
3515/* implementations */
3516/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003517
Paul Elliott588f8ed2022-12-02 18:10:26 +00003518void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3519{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003520
Paul Elliott588f8ed2022-12-02 18:10:26 +00003521#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3522 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3523 defined(MBEDTLS_ECP_RESTARTABLE)
3524
3525 /* Internal implementation uses zero to indicate infinite number max ops,
3526 * therefore avoid this value, and set to minimum possible. */
3527 if (max_ops == 0) {
3528 max_ops = 1;
3529 }
3530
Paul Elliott588f8ed2022-12-02 18:10:26 +00003531 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003532#else
3533 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003534#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3535 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3536 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3537}
3538
Paul Elliott588f8ed2022-12-02 18:10:26 +00003539uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003540 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003541{
3542#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3543 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3544 defined(MBEDTLS_ECP_RESTARTABLE)
3545
Paul Elliott93d9ca82023-02-15 18:14:21 +00003546 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003547#else
3548 (void) operation;
3549 return 0;
3550#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3551 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3552 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3553}
3554
3555uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003556 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003557{
3558 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3559 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3560 defined(MBEDTLS_ECP_RESTARTABLE)
3561
Paul Elliott93d9ca82023-02-15 18:14:21 +00003562 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003563#else
3564 (void) operation;
3565 return 0;
3566#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3567 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3568 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3569}
3570
3571psa_status_t mbedtls_psa_sign_hash_start(
3572 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3573 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3574 size_t key_buffer_size, psa_algorithm_t alg,
3575 const uint8_t *hash, size_t hash_length)
3576{
3577 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003578 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003579
Paul Elliott068fe072023-01-16 13:59:15 +00003580 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3581 return PSA_ERROR_NOT_SUPPORTED;
3582 }
3583
3584 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003585 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003586 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003587
3588#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003589 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3590 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003591
Paul Elliotta1c94092023-02-15 16:38:04 +00003592 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3593
Paul Elliott93d9ca82023-02-15 18:14:21 +00003594 /* Ensure num_ops is zero'ed in case of context re-use. */
3595 operation->num_ops = 0;
3596
Paul Elliott068fe072023-01-16 13:59:15 +00003597 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3598 attributes->core.bits,
3599 key_buffer,
3600 key_buffer_size,
3601 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003602
Paul Elliott068fe072023-01-16 13:59:15 +00003603 if (status != PSA_SUCCESS) {
3604 return status;
3605 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003606
Paul Elliott1bc59df2023-02-05 13:41:57 +00003607 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003608 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003609
Paul Elliott068fe072023-01-16 13:59:15 +00003610 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003611 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003612 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003613
Paul Elliott0290a762023-02-09 14:30:24 +00003614 /* We only need to store the same length of hash as the private key size
3615 * here, it would be truncated by the internal implementation anyway. */
3616 required_hash_length = (hash_length < operation->coordinate_bytes ?
3617 hash_length : operation->coordinate_bytes);
3618
Paul Elliottba70ad42023-02-15 18:23:53 +00003619 if (required_hash_length > sizeof(operation->hash)) {
3620 /* Shouldn't happen, but better safe than sorry. */
3621 return PSA_ERROR_CORRUPTION_DETECTED;
3622 }
3623
Paul Elliott0290a762023-02-09 14:30:24 +00003624 memcpy(operation->hash, hash, required_hash_length);
3625 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003626
3627 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003628
3629#else
Paul Elliott068fe072023-01-16 13:59:15 +00003630 (void) operation;
3631 (void) key_buffer;
3632 (void) key_buffer_size;
3633 (void) alg;
3634 (void) hash;
3635 (void) hash_length;
3636 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003637 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638
Paul Elliott068fe072023-01-16 13:59:15 +00003639 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3641 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3642 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003643}
3644
3645psa_status_t mbedtls_psa_sign_hash_complete(
3646 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3647 uint8_t *signature, size_t signature_size,
3648 size_t *signature_length)
3649{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003650#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3651 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3652 defined(MBEDTLS_ECP_RESTARTABLE)
3653
Paul Elliotta1c94092023-02-15 16:38:04 +00003654 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003655 mbedtls_mpi r;
3656 mbedtls_mpi s;
3657
Paul Elliott46845252023-02-03 14:59:11 +00003658 mbedtls_mpi_init(&r);
3659 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003660
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003661 /* Ensure max_ops is set to the current value (or default). */
3662 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3663
Paul Elliotta1c94092023-02-15 16:38:04 +00003664 if (signature_size < 2 * operation->coordinate_bytes) {
3665 status = PSA_ERROR_BUFFER_TOO_SMALL;
3666 goto exit;
3667 }
3668
Paul Elliott588f8ed2022-12-02 18:10:26 +00003669 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003670
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3672 status = mbedtls_to_psa_error(
3673 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003674 &r,
3675 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003676 &operation->ctx->d,
3677 operation->hash,
3678 operation->hash_length,
3679 operation->md_alg,
3680 mbedtls_psa_get_random,
3681 MBEDTLS_PSA_RANDOM_STATE,
3682 &operation->restart_ctx));
3683#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003684 status = PSA_ERROR_NOT_SUPPORTED;
3685 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3687 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688 status = mbedtls_to_psa_error(
3689 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003690 &r,
3691 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003692 &operation->ctx->d,
3693 operation->hash,
3694 operation->hash_length,
3695 mbedtls_psa_get_random,
3696 MBEDTLS_PSA_RANDOM_STATE,
3697 mbedtls_psa_get_random,
3698 MBEDTLS_PSA_RANDOM_STATE,
3699 &operation->restart_ctx));
3700 }
3701
Paul Elliottf8e5b562023-02-19 18:43:45 +00003702 /* Hide the fact that the restart context only holds a delta of number of
3703 * ops done during the last operation, not an absolute value. */
3704 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3705
Paul Elliott724bd252023-02-08 12:35:08 +00003706 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003707 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003708 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003709 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003710 operation->coordinate_bytes)
3711 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712
3713 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003714 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715 }
3716
3717 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003718 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003719 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003720 operation->coordinate_bytes,
3721 operation->coordinate_bytes)
3722 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003723
3724 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003725 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003726 }
3727
Paul Elliott1bc59df2023-02-05 13:41:57 +00003728 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003729
Paul Elliott724bd252023-02-08 12:35:08 +00003730 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731 }
Paul Elliott724bd252023-02-08 12:35:08 +00003732
3733exit:
3734
3735 mbedtls_mpi_free(&r);
3736 mbedtls_mpi_free(&s);
3737 return status;
3738
Paul Elliott588f8ed2022-12-02 18:10:26 +00003739 #else
3740
3741 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003742 (void) signature;
3743 (void) signature_size;
3744 (void) signature_length;
3745
3746 return PSA_ERROR_NOT_SUPPORTED;
3747
3748#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3749 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3750 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3751}
3752
3753psa_status_t mbedtls_psa_sign_hash_abort(
3754 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3755{
3756
3757#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3758 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3759 defined(MBEDTLS_ECP_RESTARTABLE)
3760
3761 if (operation->ctx) {
3762 mbedtls_ecdsa_free(operation->ctx);
3763 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003764 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765 }
3766
3767 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3768
Paul Elliott93d9ca82023-02-15 18:14:21 +00003769 operation->num_ops = 0;
3770
Paul Elliott588f8ed2022-12-02 18:10:26 +00003771 return PSA_SUCCESS;
3772
3773#else
3774
3775 (void) operation;
3776
3777 return PSA_ERROR_NOT_SUPPORTED;
3778
3779#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3780 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3781 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3782}
3783
3784psa_status_t mbedtls_psa_verify_hash_start(
3785 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3786 const psa_key_attributes_t *attributes,
3787 const uint8_t *key_buffer, size_t key_buffer_size,
3788 psa_algorithm_t alg,
3789 const uint8_t *hash, size_t hash_length,
3790 const uint8_t *signature, size_t signature_length)
3791{
3792 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003793 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003794 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003795
Paul Elliott068fe072023-01-16 13:59:15 +00003796 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3797 return PSA_ERROR_NOT_SUPPORTED;
3798 }
3799
3800 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003801 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003802 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003803
3804#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003805 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3806 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003807
Paul Elliotta1c94092023-02-15 16:38:04 +00003808 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3809 mbedtls_mpi_init(&operation->r);
3810 mbedtls_mpi_init(&operation->s);
3811
Paul Elliott93d9ca82023-02-15 18:14:21 +00003812 /* Ensure num_ops is zero'ed in case of context re-use. */
3813 operation->num_ops = 0;
3814
Paul Elliott068fe072023-01-16 13:59:15 +00003815 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3816 attributes->core.bits,
3817 key_buffer,
3818 key_buffer_size,
3819 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003820
Paul Elliott068fe072023-01-16 13:59:15 +00003821 if (status != PSA_SUCCESS) {
3822 return status;
3823 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003824
Paul Elliottc569fc22023-02-10 13:02:54 +00003825 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826
Paul Elliott1bc59df2023-02-05 13:41:57 +00003827 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003828 return PSA_ERROR_INVALID_SIGNATURE;
3829 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003830
Paul Elliott068fe072023-01-16 13:59:15 +00003831 status = mbedtls_to_psa_error(
3832 mbedtls_mpi_read_binary(&operation->r,
3833 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003834 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003835
Paul Elliott068fe072023-01-16 13:59:15 +00003836 if (status != PSA_SUCCESS) {
3837 return status;
3838 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003839
Paul Elliott068fe072023-01-16 13:59:15 +00003840 status = mbedtls_to_psa_error(
3841 mbedtls_mpi_read_binary(&operation->s,
3842 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003843 coordinate_bytes,
3844 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003845
Paul Elliott068fe072023-01-16 13:59:15 +00003846 if (status != PSA_SUCCESS) {
3847 return status;
3848 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003849
Paul Elliott2c9843f2023-02-15 17:32:42 +00003850 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851
Paul Elliott2c9843f2023-02-15 17:32:42 +00003852 if (status != PSA_SUCCESS) {
3853 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003854 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003855
Paul Elliott0290a762023-02-09 14:30:24 +00003856 /* We only need to store the same length of hash as the private key size
3857 * here, it would be truncated by the internal implementation anyway. */
3858 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3859 coordinate_bytes);
3860
Paul Elliottba70ad42023-02-15 18:23:53 +00003861 if (required_hash_length > sizeof(operation->hash)) {
3862 /* Shouldn't happen, but better safe than sorry. */
3863 return PSA_ERROR_CORRUPTION_DETECTED;
3864 }
3865
Paul Elliott0290a762023-02-09 14:30:24 +00003866 memcpy(operation->hash, hash, required_hash_length);
3867 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003868
3869 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003870#else
Paul Elliott068fe072023-01-16 13:59:15 +00003871 (void) operation;
3872 (void) key_buffer;
3873 (void) key_buffer_size;
3874 (void) alg;
3875 (void) hash;
3876 (void) hash_length;
3877 (void) signature;
3878 (void) signature_length;
3879 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003880 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003881 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003882
Paul Elliott068fe072023-01-16 13:59:15 +00003883 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3885 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3886 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003887}
3888
3889psa_status_t mbedtls_psa_verify_hash_complete(
3890 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3891{
3892
3893#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3894 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3895 defined(MBEDTLS_ECP_RESTARTABLE)
3896
Paul Elliottf8e5b562023-02-19 18:43:45 +00003897 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3898
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003899 /* Ensure max_ops is set to the current value (or default). */
3900 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3901
Paul Elliottf8e5b562023-02-19 18:43:45 +00003902 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003903 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3904 operation->hash,
3905 operation->hash_length,
3906 &operation->ctx->Q,
3907 &operation->r,
3908 &operation->s,
3909 &operation->restart_ctx));
3910
Paul Elliottf8e5b562023-02-19 18:43:45 +00003911 /* Hide the fact that the restart context only holds a delta of number of
3912 * ops done during the last operation, not an absolute value. */
3913 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3914
3915 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003916#else
3917 (void) operation;
3918
3919 return PSA_ERROR_NOT_SUPPORTED;
3920
3921#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3922 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3923 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3924}
3925
3926psa_status_t mbedtls_psa_verify_hash_abort(
3927 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3928{
3929
3930#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3931 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3932 defined(MBEDTLS_ECP_RESTARTABLE)
3933
3934 if (operation->ctx) {
3935 mbedtls_ecdsa_free(operation->ctx);
3936 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003937 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003938 }
3939
3940 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3941
Paul Elliott93d9ca82023-02-15 18:14:21 +00003942 operation->num_ops = 0;
3943
Paul Elliott588f8ed2022-12-02 18:10:26 +00003944 mbedtls_mpi_free(&operation->r);
3945 mbedtls_mpi_free(&operation->s);
3946
3947 return PSA_SUCCESS;
3948
3949#else
3950 (void) operation;
3951
3952 return PSA_ERROR_NOT_SUPPORTED;
3953
3954#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3955 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3956 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3957}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003958
mohammad1603503973b2018-03-12 15:59:30 +02003959/****************************************************************/
3960/* Symmetric cryptography */
3961/****************************************************************/
3962
Gilles Peskine449bd832023-01-11 14:50:10 +01003963static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3964 mbedtls_svc_key_id_t key,
3965 psa_algorithm_t alg,
3966 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003967{
3968 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3969 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003970 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3972 PSA_KEY_USAGE_ENCRYPT :
3973 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003974 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02003975
3976 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003978 status = PSA_ERROR_BAD_STATE;
3979 goto exit;
3980 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003981
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003983 status = PSA_ERROR_INVALID_ARGUMENT;
3984 goto exit;
3985 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003986
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3988 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003989 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003991
Ronald Cron49fafa92021-03-10 08:34:23 +01003992 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003993 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003994 * so we only set it (in the driver wrapper) after resources have been
3995 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003996 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003998 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003999 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004000 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 }
4002 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004003
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004004 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004006 };
4007
Ronald Cronab99ac22020-10-01 16:38:28 +02004008 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 if (cipher_operation == MBEDTLS_ENCRYPT) {
4010 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4011 &attributes,
4012 slot->key.data,
4013 slot->key.bytes,
4014 alg);
4015 } else {
4016 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4017 &attributes,
4018 slot->key.data,
4019 slot->key.bytes,
4020 alg);
4021 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004022
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004023exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004024 if (status != PSA_SUCCESS) {
4025 psa_cipher_abort(operation);
4026 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004027
Ryan Everett1b70a072024-01-04 10:32:49 +00004028 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004029
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004031}
4032
Gilles Peskine449bd832023-01-11 14:50:10 +01004033psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4034 mbedtls_svc_key_id_t key,
4035 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004036{
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004038}
4039
Gilles Peskine449bd832023-01-11 14:50:10 +01004040psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4041 mbedtls_svc_key_id_t key,
4042 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004043{
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004045}
4046
Gilles Peskine449bd832023-01-11 14:50:10 +01004047psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4048 uint8_t *iv,
4049 size_t iv_size,
4050 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004051{
Ronald Cron6d051732020-10-01 14:10:20 +02004052 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004053 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004054 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004055
Gilles Peskine449bd832023-01-11 14:50:10 +01004056 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004057 status = PSA_ERROR_BAD_STATE;
4058 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004059 }
4060
Gilles Peskine449bd832023-01-11 14:50:10 +01004061 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004062 status = PSA_ERROR_BAD_STATE;
4063 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004064 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004065
Ronald Cron2fb90522021-07-05 12:31:44 +02004066 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004068 status = PSA_ERROR_BUFFER_TOO_SMALL;
4069 goto exit;
4070 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004071
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004073 status = PSA_ERROR_GENERIC_ERROR;
4074 goto exit;
4075 }
4076
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 status = psa_generate_random(local_iv, default_iv_length);
4078 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004079 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 }
Ronald Cron5618a392021-03-26 09:52:26 +01004081
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 status = psa_driver_wrapper_cipher_set_iv(operation,
4083 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004084
4085exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 if (status == PSA_SUCCESS) {
4087 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004088 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004089 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004091 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004093 }
Ronald Cron6d051732020-10-01 14:10:20 +02004094
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004096}
4097
Gilles Peskine449bd832023-01-11 14:50:10 +01004098psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4099 const uint8_t *iv,
4100 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004101{
Ronald Cron6d051732020-10-01 14:10:20 +02004102 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004105 status = PSA_ERROR_BAD_STATE;
4106 goto exit;
4107 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004108
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004110 status = PSA_ERROR_BAD_STATE;
4111 goto exit;
4112 }
Ronald Crona0d68172021-03-26 10:15:08 +01004113
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004115 status = PSA_ERROR_INVALID_ARGUMENT;
4116 goto exit;
4117 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004118
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 status = psa_driver_wrapper_cipher_set_iv(operation,
4120 iv,
4121 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004122
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004123exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004125 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 } else {
4127 psa_cipher_abort(operation);
4128 }
4129 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004130}
4131
Gilles Peskine449bd832023-01-11 14:50:10 +01004132psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4133 const uint8_t *input,
4134 size_t input_length,
4135 uint8_t *output,
4136 size_t output_size,
4137 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004138{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004139 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004142 status = PSA_ERROR_BAD_STATE;
4143 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004144 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004145
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004147 status = PSA_ERROR_BAD_STATE;
4148 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004149 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004150
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 status = psa_driver_wrapper_cipher_update(operation,
4152 input,
4153 input_length,
4154 output,
4155 output_size,
4156 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004157
4158exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 if (status != PSA_SUCCESS) {
4160 psa_cipher_abort(operation);
4161 }
Ronald Cron6d051732020-10-01 14:10:20 +02004162
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004164}
4165
Gilles Peskine449bd832023-01-11 14:50:10 +01004166psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4167 uint8_t *output,
4168 size_t output_size,
4169 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004170{
David Saadab4ecc272019-02-14 13:48:10 +02004171 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004172
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004174 status = PSA_ERROR_BAD_STATE;
4175 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004176 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004177
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004179 status = PSA_ERROR_BAD_STATE;
4180 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004181 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004182
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 status = psa_driver_wrapper_cipher_finish(operation,
4184 output,
4185 output_size,
4186 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004187
4188exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 if (status == PSA_SUCCESS) {
4190 return psa_cipher_abort(operation);
4191 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004192 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004194
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004196 }
mohammad1603503973b2018-03-12 15:59:30 +02004197}
4198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004200{
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004202 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004203 * in use. It's ok to call abort on such an object, and there's
4204 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004206 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004207
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004209
Ronald Cron49fafa92021-03-10 08:34:23 +01004210 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004211 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004212 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004213
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004215}
4216
Gilles Peskine449bd832023-01-11 14:50:10 +01004217psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4218 psa_algorithm_t alg,
4219 const uint8_t *input,
4220 size_t input_length,
4221 uint8_t *output,
4222 size_t output_size,
4223 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004224{
4225 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004226 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004227 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004228 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4229 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004230 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004231
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004233 status = PSA_ERROR_INVALID_ARGUMENT;
4234 goto exit;
4235 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004236
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4238 PSA_KEY_USAGE_ENCRYPT,
4239 alg);
4240 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004241 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004243
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004244 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004246 };
4247
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4249 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004250 status = PSA_ERROR_GENERIC_ERROR;
4251 goto exit;
4252 }
4253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 if (default_iv_length > 0) {
4255 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004256 status = PSA_ERROR_BUFFER_TOO_SMALL;
4257 goto exit;
4258 }
4259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 status = psa_generate_random(local_iv, default_iv_length);
4261 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004262 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004264 }
4265
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004266 status = psa_driver_wrapper_cipher_encrypt(
4267 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004268 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004269 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004271
4272exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004273 unlock_status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004275 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004276 }
Ronald Cron23919522021-07-08 19:00:07 +02004277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 if (status == PSA_SUCCESS) {
4279 if (default_iv_length > 0) {
4280 memcpy(output, local_iv, default_iv_length);
4281 }
4282 *output_length += default_iv_length;
4283 } else {
4284 *output_length = 0;
4285 }
4286
4287 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004288}
4289
Gilles Peskine449bd832023-01-11 14:50:10 +01004290psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4291 psa_algorithm_t alg,
4292 const uint8_t *input,
4293 size_t input_length,
4294 uint8_t *output,
4295 size_t output_size,
4296 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004297{
4298 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004299 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004300 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004301 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004304 status = PSA_ERROR_INVALID_ARGUMENT;
4305 goto exit;
4306 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004307
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4309 PSA_KEY_USAGE_DECRYPT,
4310 alg);
4311 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004312 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004314
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004315 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004317 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004318
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4320 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004321 status = PSA_ERROR_INVALID_ARGUMENT;
4322 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004324 status = PSA_ERROR_INVALID_ARGUMENT;
4325 goto exit;
4326 }
4327
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004328 status = psa_driver_wrapper_cipher_decrypt(
4329 &attributes, slot->key.data, slot->key.bytes,
4330 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004332
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004333exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004334 unlock_status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004336 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004337 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004338
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004340 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 }
Ronald Cron23919522021-07-08 19:00:07 +02004342
Gilles Peskine449bd832023-01-11 14:50:10 +01004343 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004344}
4345
4346
mohammad16035955c982018-04-26 00:53:03 +03004347/****************************************************************/
4348/* AEAD */
4349/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004350
Paul Elliottbaff51c2021-09-28 17:44:45 +01004351/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004352static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004353{
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004355}
4356
4357/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004358static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4359 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004360{
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004362
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004364#if defined(PSA_WANT_ALG_GCM)
4365 case PSA_ALG_GCM:
4366 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 * arbitrarily large nonces. Please note that we do not generally
4368 * recommend the usage of nonces of greater length than
4369 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4370 * size, which can then lead to collisions if you encrypt a very
4371 * large number of messages.*/
4372 if (nonce_length != 0) {
4373 return PSA_SUCCESS;
4374 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004375 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004376#endif /* PSA_WANT_ALG_GCM */
4377#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004378 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 if (nonce_length >= 7 && nonce_length <= 13) {
4380 return PSA_SUCCESS;
4381 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004382 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004383#endif /* PSA_WANT_ALG_CCM */
4384#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004385 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 if (nonce_length == 12) {
4387 return PSA_SUCCESS;
4388 } else if (nonce_length == 8) {
4389 return PSA_ERROR_NOT_SUPPORTED;
4390 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004391 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004392#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004393 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004394 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004396 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004397
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004399}
4400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004402{
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4404 return PSA_ERROR_INVALID_ARGUMENT;
4405 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004406
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004408}
4409
Gilles Peskine449bd832023-01-11 14:50:10 +01004410psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4411 psa_algorithm_t alg,
4412 const uint8_t *nonce,
4413 size_t nonce_length,
4414 const uint8_t *additional_data,
4415 size_t additional_data_length,
4416 const uint8_t *plaintext,
4417 size_t plaintext_length,
4418 uint8_t *ciphertext,
4419 size_t ciphertext_size,
4420 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004421{
Ronald Cron215633c2021-03-16 17:15:37 +01004422 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4423 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004424
mohammad1603f08a5502018-06-03 15:05:47 +03004425 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004426
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 status = psa_aead_check_algorithm(alg);
4428 if (status != PSA_SUCCESS) {
4429 return status;
4430 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004431
Ronald Cron9a986162021-03-26 12:40:07 +01004432 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4434 if (status != PSA_SUCCESS) {
4435 return status;
4436 }
mohammad16035955c982018-04-26 00:53:03 +03004437
Ronald Cron215633c2021-03-16 17:15:37 +01004438 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004440 };
mohammad16035955c982018-04-26 00:53:03 +03004441
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 status = psa_aead_check_nonce_length(alg, nonce_length);
4443 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004444 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004446
Ronald Cronde822812021-03-17 16:08:20 +01004447 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004448 &attributes, slot->key.data, slot->key.bytes,
4449 alg,
4450 nonce, nonce_length,
4451 additional_data, additional_data_length,
4452 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4456 memset(ciphertext, 0, ciphertext_size);
4457 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004458
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004459exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004460 psa_unregister_read(slot);
mohammad16035955c982018-04-26 00:53:03 +03004461
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004463}
4464
Gilles Peskine449bd832023-01-11 14:50:10 +01004465psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4466 psa_algorithm_t alg,
4467 const uint8_t *nonce,
4468 size_t nonce_length,
4469 const uint8_t *additional_data,
4470 size_t additional_data_length,
4471 const uint8_t *ciphertext,
4472 size_t ciphertext_length,
4473 uint8_t *plaintext,
4474 size_t plaintext_size,
4475 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004476{
Ronald Cron215633c2021-03-16 17:15:37 +01004477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4478 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004479
Gilles Peskineee652a32018-06-01 19:23:52 +02004480 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004481
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 status = psa_aead_check_algorithm(alg);
4483 if (status != PSA_SUCCESS) {
4484 return status;
4485 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004486
Ronald Cron9a986162021-03-26 12:40:07 +01004487 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4489 if (status != PSA_SUCCESS) {
4490 return status;
4491 }
mohammad16035955c982018-04-26 00:53:03 +03004492
Ronald Cron215633c2021-03-16 17:15:37 +01004493 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004495 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004496
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 status = psa_aead_check_nonce_length(alg, nonce_length);
4498 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004499 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004501
Ronald Cronde822812021-03-17 16:08:20 +01004502 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004503 &attributes, slot->key.data, slot->key.bytes,
4504 alg,
4505 nonce, nonce_length,
4506 additional_data, additional_data_length,
4507 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 if (status != PSA_SUCCESS && plaintext_size != 0) {
4511 memset(plaintext, 0, plaintext_size);
4512 }
mohammad160360a64d02018-06-03 17:20:42 +03004513
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004514exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004515 psa_unregister_read(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 return status;
mohammad16035955c982018-04-26 00:53:03 +03004518}
4519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4521{
4522 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004525#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004527 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4529 return PSA_ERROR_INVALID_ARGUMENT;
4530 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004531 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004532#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004533
Przemek Stekiel86679c72022-10-06 17:06:56 +02004534#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004536 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4538 return PSA_ERROR_INVALID_ARGUMENT;
4539 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004540 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004541#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004542
Przemek Stekiel86679c72022-10-06 17:06:56 +02004543#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004545 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 if (tag_len != 16) {
4547 return PSA_ERROR_INVALID_ARGUMENT;
4548 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004549 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004550#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004551
4552 default:
4553 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004555 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004557}
4558
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004559/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004560static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4561 int is_encrypt,
4562 mbedtls_svc_key_id_t key,
4563 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004564{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004565 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4566 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4567 psa_key_slot_t *slot = NULL;
4568 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004569 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004570
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 status = psa_aead_check_algorithm(alg);
4572 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004573 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004577 status = PSA_ERROR_BAD_STATE;
4578 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004579 }
4580
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 if (operation->nonce_set || operation->lengths_set ||
4582 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004583 status = PSA_ERROR_BAD_STATE;
4584 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004585 }
4586
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004588 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004590 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004592
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4594 alg);
4595 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004596 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004598
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004599 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004600 .core = slot->attr
4601 };
4602
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004604 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 if (is_encrypt) {
4608 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4609 &attributes,
4610 slot->key.data,
4611 slot->key.bytes,
4612 alg);
4613 } else {
4614 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4615 &attributes,
4616 slot->key.data,
4617 slot->key.bytes,
4618 alg);
4619 }
4620 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004621 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004625
4626exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004627 unlock_status = psa_unregister_read(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004630 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004632 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 } else {
4634 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004635 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004636
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004638}
4639
Paul Elliott302ff6b2021-04-20 18:10:30 +01004640/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004641psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4642 mbedtls_svc_key_id_t key,
4643 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004644{
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004646}
4647
4648/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004649psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4650 mbedtls_svc_key_id_t key,
4651 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004652{
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004654}
4655
4656/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004657psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4658 uint8_t *nonce,
4659 size_t nonce_size,
4660 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004661{
4662 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004663 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004664 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004665
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004666 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004667
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004669 status = PSA_ERROR_BAD_STATE;
4670 goto exit;
4671 }
4672
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004674 status = PSA_ERROR_BAD_STATE;
4675 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004676 }
4677
Paul Elliotte193ea82021-10-01 13:00:16 +01004678 /* For CCM, this size may not be correct according to the PSA
4679 * specification. The PSA Crypto 1.0.1 specification states:
4680 *
4681 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4682 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4683 *
4684 * However this restriction that L has to be the smallest integer is not
4685 * applied in practice, and it is not implementable here since the
4686 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4688 operation->alg);
4689 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004690 status = PSA_ERROR_BUFFER_TOO_SMALL;
4691 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004692 }
4693
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 status = psa_generate_random(local_nonce, required_nonce_size);
4695 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004696 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004698
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004700
Paul Elliott39dc6b82021-05-11 19:16:09 +01004701exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 if (status == PSA_SUCCESS) {
4703 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004704 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 } else {
4706 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004707 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004710}
4711
4712/* Set the nonce for a multipart authenticated encryption or decryption
4713 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004714psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4715 const uint8_t *nonce,
4716 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004717{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004718 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004721 status = PSA_ERROR_BAD_STATE;
4722 goto exit;
4723 }
4724
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004726 status = PSA_ERROR_BAD_STATE;
4727 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004728 }
4729
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4731 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004732 status = PSA_ERROR_INVALID_ARGUMENT;
4733 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004734 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004735
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4737 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004738
Paul Elliott39dc6b82021-05-11 19:16:09 +01004739exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004741 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 } else {
4743 psa_aead_abort(operation);
4744 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004745
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004747}
4748
4749/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004750psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4751 size_t ad_length,
4752 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004753{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004754 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4755
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004757 status = PSA_ERROR_BAD_STATE;
4758 goto exit;
4759 }
4760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 if (operation->lengths_set || operation->ad_started ||
4762 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004763 status = PSA_ERROR_BAD_STATE;
4764 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004765 }
4766
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004768#if defined(PSA_WANT_ALG_GCM)
4769 case PSA_ALG_GCM:
4770 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 * bits. Without the guard this code will generate warnings on 32bit
4772 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004773#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 if (((uint64_t) ad_length) >> 61 != 0 ||
4775 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004776 status = PSA_ERROR_INVALID_ARGUMENT;
4777 goto exit;
4778 }
Paul Elliott325d3742021-09-27 17:56:28 +01004779#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004780 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004781#endif /* PSA_WANT_ALG_GCM */
4782#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004783 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004785 status = PSA_ERROR_INVALID_ARGUMENT;
4786 goto exit;
4787 }
4788 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004789#endif /* PSA_WANT_ALG_CCM */
4790#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004791 case PSA_ALG_CHACHA20_POLY1305:
4792 /* No length restrictions for ChaChaPoly. */
4793 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004794#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004795 default:
4796 break;
4797 }
Paul Elliott325d3742021-09-27 17:56:28 +01004798
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4800 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004801
Paul Elliott39dc6b82021-05-11 19:16:09 +01004802exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004804 operation->ad_remaining = ad_length;
4805 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004806 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 } else {
4808 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004809 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004810
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004812}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004813
4814/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004815psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4816 const uint8_t *input,
4817 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004818{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004819 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4820
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004822 status = PSA_ERROR_BAD_STATE;
4823 goto exit;
4824 }
4825
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004827 status = PSA_ERROR_BAD_STATE;
4828 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004829 }
4830
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 if (operation->lengths_set) {
4832 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004833 status = PSA_ERROR_INVALID_ARGUMENT;
4834 goto exit;
4835 }
4836
4837 operation->ad_remaining -= input_length;
4838 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004839#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004841 status = PSA_ERROR_BAD_STATE;
4842 goto exit;
4843 }
4844#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004845
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 status = psa_driver_wrapper_aead_update_ad(operation, input,
4847 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004848
Paul Elliott39dc6b82021-05-11 19:16:09 +01004849exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004851 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 } else {
4853 psa_aead_abort(operation);
4854 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004855
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004857}
4858
4859/* Encrypt or decrypt a message fragment in an active multipart AEAD
4860 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004861psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4862 const uint8_t *input,
4863 size_t input_length,
4864 uint8_t *output,
4865 size_t output_size,
4866 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004867{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004868 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004869
4870 *output_length = 0;
4871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004873 status = PSA_ERROR_BAD_STATE;
4874 goto exit;
4875 }
4876
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004878 status = PSA_ERROR_BAD_STATE;
4879 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004880 }
4881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004883 /* Additional data length was supplied, but not all the additional
4884 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004886 status = PSA_ERROR_INVALID_ARGUMENT;
4887 goto exit;
4888 }
4889
4890 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004892 status = PSA_ERROR_INVALID_ARGUMENT;
4893 goto exit;
4894 }
4895
4896 operation->body_remaining -= input_length;
4897 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004898#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004900 status = PSA_ERROR_BAD_STATE;
4901 goto exit;
4902 }
4903#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004904
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4906 output, output_size,
4907 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004908
Paul Elliott39dc6b82021-05-11 19:16:09 +01004909exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004911 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 } else {
4913 psa_aead_abort(operation);
4914 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004915
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004917}
4918
Gilles Peskine449bd832023-01-11 14:50:10 +01004919static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004920{
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 if (operation->id == 0 || !operation->nonce_set) {
4922 return PSA_ERROR_BAD_STATE;
4923 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004924
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4926 operation->body_remaining != 0)) {
4927 return PSA_ERROR_INVALID_ARGUMENT;
4928 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004929
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004931}
4932
Paul Elliott302ff6b2021-04-20 18:10:30 +01004933/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004934psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4935 uint8_t *ciphertext,
4936 size_t ciphertext_size,
4937 size_t *ciphertext_length,
4938 uint8_t *tag,
4939 size_t tag_size,
4940 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004941{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004942 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4943
Paul Elliott302ff6b2021-04-20 18:10:30 +01004944 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004945 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004946
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 status = psa_aead_final_checks(operation);
4948 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004949 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004951
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004953 status = PSA_ERROR_BAD_STATE;
4954 goto exit;
4955 }
4956
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4958 ciphertext_size,
4959 ciphertext_length,
4960 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004961
Paul Elliott39dc6b82021-05-11 19:16:09 +01004962exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004963
4964
Paul Elliottf47b0952021-05-21 18:02:33 +01004965 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004966 * the zero tag size, make sure the tag is set to something implausible.
4967 * Even if the operation succeeds, make sure we clear the rest of the
4968 * buffer to prevent potential leakage of anything previously placed in
4969 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004970 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004971
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004973
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004975}
4976
4977/* Finish authenticating and decrypting a message in a multipart AEAD
4978 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004979psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4980 uint8_t *plaintext,
4981 size_t plaintext_size,
4982 size_t *plaintext_length,
4983 const uint8_t *tag,
4984 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004985{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004986 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4987
Paul Elliott302ff6b2021-04-20 18:10:30 +01004988 *plaintext_length = 0;
4989
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 status = psa_aead_final_checks(operation);
4991 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004992 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004994
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004996 status = PSA_ERROR_BAD_STATE;
4997 goto exit;
4998 }
4999
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5001 plaintext_size,
5002 plaintext_length,
5003 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005004
5005exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005007
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005009}
5010
5011/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005012psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005013{
5014 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5015
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005017 /* The object has (apparently) been initialized but it is not (yet)
5018 * in use. It's ok to call abort on such an object, and there's
5019 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005021 }
5022
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005024
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005026
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005028}
5029
Gilles Peskinee59236f2018-01-27 23:32:46 +01005030/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005031/* Generators */
5032/****************************************************************/
5033
Przemek Stekiel69c46792022-06-10 12:59:51 +02005034#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005035 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005036 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305037 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305038 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005039#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005040#endif /* At least one builtin KDF */
5041
Przemek Stekiel69c46792022-06-10 12:59:51 +02005042#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005043 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5044 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5045static psa_status_t psa_key_derivation_start_hmac(
5046 psa_mac_operation_t *operation,
5047 psa_algorithm_t hash_alg,
5048 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005050{
5051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5052 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5054 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5055 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005056
Steven Cooreman72f736a2021-05-07 14:14:37 +02005057 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005059
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 status = psa_driver_wrapper_mac_sign_setup(operation,
5061 &attributes,
5062 hmac_key, hmac_key_length,
5063 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005064
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 psa_reset_key_attributes(&attributes);
5066 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005067}
5068#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005069
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005070#define HKDF_STATE_INIT 0 /* no input yet */
5071#define HKDF_STATE_STARTED 1 /* got salt */
5072#define HKDF_STATE_KEYED 2 /* got key */
5073#define HKDF_STATE_OUTPUT 3 /* output started */
5074
Gilles Peskinecbe66502019-05-16 16:59:18 +02005075static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005077{
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5079 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5080 } else {
5081 return operation->alg;
5082 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005083}
5084
Gilles Peskine449bd832023-01-11 14:50:10 +01005085psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005086{
5087 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5089 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005090 /* The object has (apparently) been initialized but it is not
5091 * in use. It's ok to call abort on such an object, and there's
5092 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005094#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5096 mbedtls_free(operation->ctx.hkdf.info);
5097 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5098 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005099#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005100#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5101 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5103 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5104 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5105 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005106 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005108 }
5109
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005111 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005113 }
5114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005116 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005118 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005119#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005121 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005123 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005124#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005125 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005126
5127 /* We leave the fields Ai and output_block to be erased safely by the
5128 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005130#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5131 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005132#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5134 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5135 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5136 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005137#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305138#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305139 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305140 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005141 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305142 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305143 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305144
5145 status = PSA_SUCCESS;
5146 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305147#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005148 {
5149 status = PSA_ERROR_BAD_STATE;
5150 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 mbedtls_platform_zeroize(operation, sizeof(*operation));
5152 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005153}
5154
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005157{
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005159 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005161 }
5162
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005163 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005165}
5166
Gilles Peskine449bd832023-01-11 14:50:10 +01005167psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5168 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005169{
Gilles Peskine449bd832023-01-11 14:50:10 +01005170 if (operation->alg == 0) {
5171 return PSA_ERROR_BAD_STATE;
5172 }
5173 if (capacity > operation->capacity) {
5174 return PSA_ERROR_INVALID_ARGUMENT;
5175 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005176 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005178}
5179
Przemek Stekiel69c46792022-06-10 12:59:51 +02005180#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005181/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005182static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5183 psa_algorithm_t kdf_alg,
5184 uint8_t *output,
5185 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005186{
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5188 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005189 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005190 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005191#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005193#else
5194 const uint8_t last_block = 0xff;
5195#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005196
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 if (hkdf->state < HKDF_STATE_KEYED ||
5198 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005199#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005201#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 )) {
5203 return PSA_ERROR_BAD_STATE;
5204 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005205 hkdf->state = HKDF_STATE_OUTPUT;
5206
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005208 /* Copy what remains of the current block */
5209 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005211 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 }
5213 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005214 output += n;
5215 output_length -= n;
5216 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005218 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005220 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005221 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005222 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005223 * object was corrupted or if this function is called directly
5224 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 if (hkdf->block_number == last_block) {
5226 return PSA_ERROR_BAD_STATE;
5227 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005228
5229 /* We need a new block */
5230 ++hkdf->block_number;
5231 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005232
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5234 hash_alg,
5235 hkdf->prk,
5236 hash_length);
5237 if (status != PSA_SUCCESS) {
5238 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005239 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005240
5241 if (hkdf->block_number != 1) {
5242 status = psa_mac_update(&hkdf->hmac,
5243 hkdf->output_block,
5244 hash_length);
5245 if (status != PSA_SUCCESS) {
5246 return status;
5247 }
5248 }
5249 status = psa_mac_update(&hkdf->hmac,
5250 hkdf->info,
5251 hkdf->info_length);
5252 if (status != PSA_SUCCESS) {
5253 return status;
5254 }
5255 status = psa_mac_update(&hkdf->hmac,
5256 &hkdf->block_number, 1);
5257 if (status != PSA_SUCCESS) {
5258 return status;
5259 }
5260 status = psa_mac_sign_finish(&hkdf->hmac,
5261 hkdf->output_block,
5262 sizeof(hkdf->output_block),
5263 &hmac_output_length);
5264 if (status != PSA_SUCCESS) {
5265 return status;
5266 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005267 }
5268
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005270}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005271#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005272
John Durkop07cc04a2020-11-16 22:08:34 -08005273#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5274 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005275static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5276 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005278{
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5280 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005281 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5282 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005283 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005284
Janos Follath7742fee2019-06-17 12:58:10 +01005285 /* We can't be wanting more output after block 0xff, otherwise
5286 * the capacity check in psa_key_derivation_output_bytes() would have
5287 * prevented this call. It could happen only if the operation
5288 * object was corrupted or if this function is called directly
5289 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 if (tls12_prf->block_number == 0xff) {
5291 return PSA_ERROR_CORRUPTION_DETECTED;
5292 }
Janos Follath7742fee2019-06-17 12:58:10 +01005293
5294 /* We need a new block */
5295 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005296 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005297
5298 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5299 *
5300 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5301 *
5302 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5303 * HMAC_hash(secret, A(2) + seed) +
5304 * HMAC_hash(secret, A(3) + seed) + ...
5305 *
5306 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005307 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005308 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005309 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005310 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005311 * is currently extracted as `output_block` and where i is
5312 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005313 */
5314
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 status = psa_key_derivation_start_hmac(&hmac,
5316 hash_alg,
5317 tls12_prf->secret,
5318 tls12_prf->secret_length);
5319 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005320 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005322
5323 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005325 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5326 * the variable seed and in this instance means it in the context of the
5327 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 status = psa_mac_update(&hmac,
5329 tls12_prf->label,
5330 tls12_prf->label_length);
5331 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005332 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 }
5334 status = psa_mac_update(&hmac,
5335 tls12_prf->seed,
5336 tls12_prf->seed_length);
5337 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005338 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 }
5340 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005341 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5343 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005344 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005346 }
5347
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 status = psa_mac_sign_finish(&hmac,
5349 tls12_prf->Ai, hash_length,
5350 &hmac_output_length);
5351 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005352 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 }
5354 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005355 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005357
5358 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 status = psa_key_derivation_start_hmac(&hmac,
5360 hash_alg,
5361 tls12_prf->secret,
5362 tls12_prf->secret_length);
5363 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005364 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 }
5366 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5367 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005368 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 }
5370 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5371 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005372 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 }
5374 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5375 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005376 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 }
5378 status = psa_mac_sign_finish(&hmac,
5379 tls12_prf->output_block, hash_length,
5380 &hmac_output_length);
5381 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005382 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005384
Janos Follath7742fee2019-06-17 12:58:10 +01005385
5386cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 cleanup_status = psa_mac_abort(&hmac);
5388 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005389 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005393}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005394
Janos Follath844eb0e2019-06-19 12:10:49 +01005395static psa_status_t psa_key_derivation_tls12_prf_read(
5396 psa_tls12_prf_key_derivation_t *tls12_prf,
5397 psa_algorithm_t alg,
5398 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005400{
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5402 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005403 psa_status_t status;
5404 uint8_t offset, length;
5405
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005407 case PSA_TLS12_PRF_STATE_LABEL_SET:
5408 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5409 break;
5410 case PSA_TLS12_PRF_STATE_OUTPUT:
5411 break;
5412 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005414 }
5415
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005417 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 if (tls12_prf->left_in_block == 0) {
5419 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5420 alg);
5421 if (status != PSA_SUCCESS) {
5422 return status;
5423 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005424
5425 continue;
5426 }
5427
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005429 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005431 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005433
5434 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005436 output += length;
5437 output_length -= length;
5438 tls12_prf->left_in_block -= length;
5439 }
5440
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005442}
John Durkop07cc04a2020-11-16 22:08:34 -08005443#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5444 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005445
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005446#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5447static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5448 psa_tls12_ecjpake_to_pms_t *ecjpake,
5449 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005451{
5452 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005453 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005454
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 if (output_length != 32) {
5456 return PSA_ERROR_INVALID_ARGUMENT;
5457 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005458
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5460 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5461 &output_size);
5462 if (status != PSA_SUCCESS) {
5463 return status;
5464 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005465
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 if (output_size != output_length) {
5467 return PSA_ERROR_GENERIC_ERROR;
5468 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005471}
5472#endif
5473
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305474#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305475static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5476 psa_pbkdf2_key_derivation_t *pbkdf2,
5477 psa_algorithm_t prf_alg,
5478 uint8_t prf_output_length,
5479 psa_key_attributes_t *attributes)
5480{
5481 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305482 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305483 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305484 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305485 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305486 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305487 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305488
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305489 mac_operation.is_sign = 1;
5490 mac_operation.mac_size = prf_output_length;
5491 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305492
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305493 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5494 attributes,
5495 pbkdf2->password,
5496 pbkdf2->password_length,
5497 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305498 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305499 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305500 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305501 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5502 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305503 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305504 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305505 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305506 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305507 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305508 }
5509 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5510 &mac_output_length);
5511 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305512 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305513 }
5514
5515 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305516 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305517 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305518 }
5519
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305520 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305521
5522 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305523 /* We are passing prf_output_length as mac_size because the driver
5524 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305525 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305526 status = psa_driver_wrapper_mac_compute(attributes,
5527 pbkdf2->password,
5528 pbkdf2->password_length,
5529 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305530 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305531 &mac_output_length);
5532 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305533 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305534 }
5535
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305536 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305537 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305538
5539cleanup:
5540 /* Zeroise buffers to clear sensitive data from memory. */
5541 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5542 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305543}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305544
5545static psa_status_t psa_key_derivation_pbkdf2_read(
5546 psa_pbkdf2_key_derivation_t *pbkdf2,
5547 psa_algorithm_t kdf_alg,
5548 uint8_t *output,
5549 size_t output_length)
5550{
5551 psa_status_t status;
5552 psa_algorithm_t prf_alg;
5553 uint8_t prf_output_length;
5554 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5555 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5556 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5557
5558 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5559 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5560 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5561 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305562 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5563 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305564 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305565 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305566 } else {
5567 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305568 }
5569
5570 switch (pbkdf2->state) {
5571 case PSA_PBKDF2_STATE_PASSWORD_SET:
5572 /* Initially we need a new block so bytes_used is equal to block size*/
5573 pbkdf2->bytes_used = prf_output_length;
5574 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5575 break;
5576 case PSA_PBKDF2_STATE_OUTPUT:
5577 break;
5578 default:
5579 return PSA_ERROR_BAD_STATE;
5580 }
5581
5582 while (output_length != 0) {
5583 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5584 if (n > output_length) {
5585 n = (uint8_t) output_length;
5586 }
5587 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5588 output += n;
5589 output_length -= n;
5590 pbkdf2->bytes_used += n;
5591
5592 if (output_length == 0) {
5593 break;
5594 }
5595
5596 /* We need a new block */
5597 pbkdf2->bytes_used = 0;
5598 pbkdf2->block_number++;
5599
5600 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5601 prf_output_length,
5602 &attributes);
5603 if (status != PSA_SUCCESS) {
5604 return status;
5605 }
5606 }
5607
5608 return PSA_SUCCESS;
5609}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305610#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305611
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005612psa_status_t psa_key_derivation_output_bytes(
5613 psa_key_derivation_operation_t *operation,
5614 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005616{
5617 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005621 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005623 }
5624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005626 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005627 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005628 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005629 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005630 goto exit;
5631 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005633 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005634 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005635 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5636 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005637 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005638 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005640 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005641 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005642
Przemek Stekiel69c46792022-06-10 12:59:51 +02005643#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5645 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5646 output, output_length);
5647 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005648#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005649#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5650 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5652 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5653 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5654 kdf_alg, output,
5655 output_length);
5656 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005657#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5658 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005659#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005661 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5663 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005664#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305665#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305666 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305667 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5668 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305669 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305670#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005671
Gilles Peskineeab56e42018-07-12 17:12:33 +02005672 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005673 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005675 }
5676
5677exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005679 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005680 * This allows us to differentiate between exhausted operations and
5681 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5682 * operations. */
5683 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005685 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005687 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005689}
5690
David Brown7807bf72021-02-09 16:28:23 -07005691#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005692static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005693{
Gilles Peskine449bd832023-01-11 14:50:10 +01005694 if (data_size >= 8) {
5695 mbedtls_des_key_set_parity(data);
5696 }
5697 if (data_size >= 16) {
5698 mbedtls_des_key_set_parity(data + 8);
5699 }
5700 if (data_size >= 24) {
5701 mbedtls_des_key_set_parity(data + 16);
5702 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005703}
David Brown7807bf72021-02-09 16:28:23 -07005704#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005705
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005706/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 * ECC keys on a Weierstrass elliptic curve require the generation
5708 * of a private key which is an integer
5709 * in the range [1, N - 1], where N is the boundary of the private key domain:
5710 * N is the prime p for Diffie-Hellman, or the order of the
5711 * curve’s base point for ECC.
5712 *
5713 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5714 * This function generates the private key using the following process:
5715 *
5716 * 1. Draw a byte string of length ceiling(m/8) bytes.
5717 * 2. If m is not a multiple of 8, set the most significant
5718 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5719 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5720 * 4. If k > N - 2, discard the result and return to step 1.
5721 * 5. Output k + 1 as the private key.
5722 *
5723 * This method allows compliance to NIST standards, specifically the methods titled
5724 * Key-Pair Generation by Testing Candidates in the following publications:
5725 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5726 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5727 * Diffie-Hellman keys.
5728 *
5729 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5730 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5731 *
5732 * Note: Function allocates memory for *data buffer, so given *data should be
5733 * always NULL.
5734 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005735#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5736#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005737static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005738 psa_key_slot_t *slot,
5739 size_t bits,
5740 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005741 uint8_t **data
5742 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005743{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005744 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005745 mbedtls_mpi k;
5746 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005747 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005748 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005749 size_t m;
5750 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005751
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 mbedtls_mpi_init(&k);
5753 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005754
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005755 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005757 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01005758 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005761 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005762 goto cleanup;
5763 }
5764
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005765 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005769
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005770 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005771 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005772 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005773
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005774 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005775
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005776 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005778
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005779 /* Note: This function is always called with *data == NULL and it
5780 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 *data = mbedtls_calloc(1, m_bytes);
5782 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005783 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005784 goto cleanup;
5785 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005788 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005790 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005792
5793 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005795 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005796 * (8 * ceiling(m/8) - m) bits of the first byte in
5797 * the string to zero.
5798 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005800 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005801 }
5802
5803 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 * big-endian byte string.
5805 */
5806 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005807
5808 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 * Result of comparison is returned. When it indicates error
5810 * then this function is called again.
5811 */
5812 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005813 }
5814
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005815 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5817 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005818cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 if (ret != 0) {
5820 status = mbedtls_to_psa_error(ret);
5821 }
5822 if (status != PSA_SUCCESS) {
5823 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005824 *data = NULL;
5825 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 mbedtls_mpi_free(&k);
5827 mbedtls_mpi_free(&diff_N_2);
5828 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005829}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005830
5831/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5832 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5833 *
5834 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5835 * draw a 32-byte string and process it as specified in
5836 * Elliptic Curves for Security [RFC7748] §5.
5837 *
5838 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5839 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5840 *
5841 * Note: Function allocates memory for *data buffer, so given *data should be
5842 * always NULL.
5843 */
5844
5845static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5846 size_t bits,
5847 psa_key_derivation_operation_t *operation,
5848 uint8_t **data
5849 )
5850{
5851 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005852 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005853
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005855 case 255:
5856 output_length = 32;
5857 break;
5858 case 448:
5859 output_length = 56;
5860 break;
5861 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005863 break;
5864 }
5865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005867
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 if (*data == NULL) {
5869 return PSA_ERROR_INSUFFICIENT_MEMORY;
5870 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005871
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005873
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005875 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005879 case 255:
5880 (*data)[0] &= 248;
5881 (*data)[31] &= 127;
5882 (*data)[31] |= 64;
5883 break;
5884 case 448:
5885 (*data)[0] &= 252;
5886 (*data)[55] |= 128;
5887 break;
5888 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005890 break;
5891 }
5892
5893 return status;
5894}
Valerio Setti86587ab2023-06-27 13:09:30 +02005895#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5896static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5897 psa_key_slot_t *slot, size_t bits,
5898 psa_key_derivation_operation_t *operation, uint8_t **data)
5899{
5900 (void) slot;
5901 (void) bits;
5902 (void) operation;
5903 (void) data;
5904 return PSA_ERROR_NOT_SUPPORTED;
5905}
5906
5907static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5908 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5909{
5910 (void) bits;
5911 (void) operation;
5912 (void) data;
5913 return PSA_ERROR_NOT_SUPPORTED;
5914}
5915#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5916#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005917
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005918static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005919 psa_key_slot_t *slot,
5920 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005922{
5923 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305925 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005927 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005928
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5930 return PSA_ERROR_INVALID_ARGUMENT;
5931 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005932
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005933#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005934 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5936 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5937 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005938 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5940 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005941 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 }
5943 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005944 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5946 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005947 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005949 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005950 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005951#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005952 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 if (key_type_is_raw_bytes(slot->attr.type)) {
5954 if (bits % 8 != 0) {
5955 return PSA_ERROR_INVALID_ARGUMENT;
5956 }
5957 data = mbedtls_calloc(1, bytes);
5958 if (data == NULL) {
5959 return PSA_ERROR_INSUFFICIENT_MEMORY;
5960 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005961
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 status = psa_key_derivation_output_bytes(operation, data, bytes);
5963 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005964 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 }
David Brown12ca5032021-02-11 11:02:00 -07005966#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5968 psa_des_set_key_parity(data, bytes);
5969 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005970#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 } else {
5972 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005973 }
Ronald Crondd04d422020-11-28 15:14:42 +01005974
Ronald Cronbf33c932020-11-28 18:06:53 +01005975 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005976 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005978 };
5979
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5981 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5982 &storage_size);
5983 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305984 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 }
Archanad8a83dc2021-06-14 10:04:16 +05305986 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 status = psa_allocate_buffer_to_slot(slot, storage_size);
5988 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305989 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 }
Archanad8a83dc2021-06-14 10:04:16 +05305991
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 status = psa_driver_wrapper_import_key(&attributes,
5993 data, bytes,
5994 slot->key.data,
5995 slot->key.bytes,
5996 &slot->key.bytes, &bits);
5997 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005998 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006000
6001exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 mbedtls_free(data);
6003 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006004}
6005
Gilles Peskine449bd832023-01-11 14:50:10 +01006006psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6007 psa_key_derivation_operation_t *operation,
6008 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006009{
6010 psa_status_t status;
6011 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006012 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006013
Ronald Cron81709fc2020-11-14 12:10:32 +01006014 *key = MBEDTLS_SVC_KEY_ID_INIT;
6015
Gilles Peskine0f84d622019-09-12 19:03:13 +02006016 /* Reject any attempt to create a zero-length key so that we don't
6017 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 if (psa_get_key_bits(attributes) == 0) {
6019 return PSA_ERROR_INVALID_ARGUMENT;
6020 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 if (operation->alg == PSA_ALG_NONE) {
6023 return PSA_ERROR_BAD_STATE;
6024 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006025
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 if (!operation->can_output_key) {
6027 return PSA_ERROR_NOT_PERMITTED;
6028 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006029
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6031 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006034 /* Deriving a key in a secure element is not implemented yet. */
6035 status = PSA_ERROR_NOT_SUPPORTED;
6036 }
6037#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 if (status == PSA_SUCCESS) {
6039 status = psa_generate_derived_key_internal(slot,
6040 attributes->core.bits,
6041 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006042 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 if (status == PSA_SUCCESS) {
6044 status = psa_finish_key_creation(slot, driver, key);
6045 }
6046 if (status != PSA_SUCCESS) {
6047 psa_fail_key_creation(slot, driver);
6048 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006049
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006051}
6052
Gilles Peskineeab56e42018-07-12 17:12:33 +02006053
6054
6055/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006056/* Key derivation */
6057/****************************************************************/
6058
Steven Cooreman932ffb72021-02-15 12:14:32 +01006059#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006060static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006061{
6062#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6064 return 1;
6065 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006066#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006067#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6069 return 1;
6070 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006071#endif
6072#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6074 return 1;
6075 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006076#endif
6077#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6079 return 1;
6080 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006081#endif
6082#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6084 return 1;
6085 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006086#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006087#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6089 return 1;
6090 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006091#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306092#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6093 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6094 return 1;
6095 }
6096#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306097#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6098 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6099 return 1;
6100 }
6101#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006103}
6104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006106{
6107 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 psa_status_t status = psa_hash_setup(&operation, alg);
6109 psa_hash_abort(&operation);
6110 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006111}
6112
Gilles Peskine969c5d62019-01-16 15:53:06 +01006113static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006114 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006116{
Janos Follath5fe19732019-06-20 15:09:30 +01006117 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6118 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006120
Gilles Peskine969c5d62019-01-16 15:53:06 +01006121 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 if (!is_kdf_alg_supported(kdf_alg)) {
6123 return PSA_ERROR_NOT_SUPPORTED;
6124 }
John Durkop07cc04a2020-11-16 22:08:34 -08006125
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006126 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306127 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6129 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306130 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6131 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6132 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306133 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306134 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 if (hash_size == 0) {
6136 return PSA_ERROR_NOT_SUPPORTED;
6137 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006138
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006139 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6140 * we might fail later, which is somewhat unfriendly and potentially
6141 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 psa_status_t status = psa_hash_try_support(hash_alg);
6143 if (status != PSA_SUCCESS) {
6144 return status;
6145 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006146 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006147
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6149 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6150 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6151 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006152 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006153#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006154 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6156 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006157 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006159#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6160 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 operation->capacity = 255 * hash_size;
6162 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006163}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006164
Gilles Peskine449bd832023-01-11 14:50:10 +01006165static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006166{
6167#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 if (alg == PSA_ALG_ECDH) {
6169 return PSA_SUCCESS;
6170 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006171#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006172#if defined(PSA_WANT_ALG_FFDH)
6173 if (alg == PSA_ALG_FFDH) {
6174 return PSA_SUCCESS;
6175 }
6176#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006177 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006179}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006180
6181static int psa_key_derivation_allows_free_form_secret_input(
6182 psa_algorithm_t kdf_alg)
6183{
6184#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6185 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6186 return 0;
6187 }
6188#endif
6189 (void) kdf_alg;
6190 return 1;
6191}
John Durkop07cc04a2020-11-16 22:08:34 -08006192#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006193
Gilles Peskine449bd832023-01-11 14:50:10 +01006194psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6195 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006196{
6197 psa_status_t status;
6198
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 if (operation->alg != 0) {
6200 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006201 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6204 return PSA_ERROR_INVALID_ARGUMENT;
6205 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6206#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6207 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6208 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6209 status = psa_key_agreement_try_support(ka_alg);
6210 if (status != PSA_SUCCESS) {
6211 return status;
6212 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006213 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6214 return PSA_ERROR_INVALID_ARGUMENT;
6215 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6217#else
6218 return PSA_ERROR_NOT_SUPPORTED;
6219#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6220 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6221#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6222 status = psa_key_derivation_setup_kdf(operation, alg);
6223#else
6224 return PSA_ERROR_NOT_SUPPORTED;
6225#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6226 } else {
6227 return PSA_ERROR_INVALID_ARGUMENT;
6228 }
6229
6230 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006231 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 }
6233 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006234}
6235
Przemek Stekiel69c46792022-06-10 12:59:51 +02006236#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006237static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6238 psa_algorithm_t kdf_alg,
6239 psa_key_derivation_step_t step,
6240 const uint8_t *data,
6241 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006242{
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006244 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006246 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006247#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6249 return PSA_ERROR_INVALID_ARGUMENT;
6250 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006251#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 if (hkdf->state != HKDF_STATE_INIT) {
6253 return PSA_ERROR_BAD_STATE;
6254 } else {
6255 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6256 hash_alg,
6257 data, data_length);
6258 if (status != PSA_SUCCESS) {
6259 return status;
6260 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006261 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006263 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006264 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006265#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006267 /* We shouldn't be in different state as HKDF_EXPAND only allows
6268 * two inputs: SECRET (this case) and INFO which does not modify
6269 * the state. It could happen only if the hkdf
6270 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006271 if (hkdf->state != HKDF_STATE_INIT) {
6272 return PSA_ERROR_BAD_STATE;
6273 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006274
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006275 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6277 return PSA_ERROR_INVALID_ARGUMENT;
6278 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006279
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 memcpy(hkdf->prk, data, data_length);
6281 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006282#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006283 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006284 /* HKDF: If no salt was provided, use an empty salt.
6285 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006287#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6289 return PSA_ERROR_BAD_STATE;
6290 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006291#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6293 hash_alg,
6294 NULL, 0);
6295 if (status != PSA_SUCCESS) {
6296 return status;
6297 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006298 hkdf->state = HKDF_STATE_STARTED;
6299 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 if (hkdf->state != HKDF_STATE_STARTED) {
6301 return PSA_ERROR_BAD_STATE;
6302 }
6303 status = psa_mac_update(&hkdf->hmac,
6304 data, data_length);
6305 if (status != PSA_SUCCESS) {
6306 return status;
6307 }
6308 status = psa_mac_sign_finish(&hkdf->hmac,
6309 hkdf->prk,
6310 sizeof(hkdf->prk),
6311 &data_length);
6312 if (status != PSA_SUCCESS) {
6313 return status;
6314 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006315 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006316
Gilles Peskine2b522db2019-04-12 15:11:49 +02006317 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006318 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006319#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006321 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006323 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006325#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006326 {
6327 /* Block 0 is empty, and the next block will be
6328 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006330 }
6331
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006333 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006334#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6336 return PSA_ERROR_INVALID_ARGUMENT;
6337 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006338#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006339#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6341 hkdf->state == HKDF_STATE_INIT) {
6342 return PSA_ERROR_BAD_STATE;
6343 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006344#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 if (hkdf->state == HKDF_STATE_OUTPUT) {
6346 return PSA_ERROR_BAD_STATE;
6347 }
6348 if (hkdf->info_set) {
6349 return PSA_ERROR_BAD_STATE;
6350 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006351 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 if (data_length != 0) {
6353 hkdf->info = mbedtls_calloc(1, data_length);
6354 if (hkdf->info == NULL) {
6355 return PSA_ERROR_INSUFFICIENT_MEMORY;
6356 }
6357 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006358 }
6359 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006361 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006363 }
6364}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006365#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006366
John Durkop07cc04a2020-11-16 22:08:34 -08006367#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6368 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006369static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6370 const uint8_t *data,
6371 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006372{
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6374 return PSA_ERROR_BAD_STATE;
6375 }
Janos Follathf08e2652019-06-13 09:05:41 +01006376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 if (data_length != 0) {
6378 prf->seed = mbedtls_calloc(1, data_length);
6379 if (prf->seed == NULL) {
6380 return PSA_ERROR_INSUFFICIENT_MEMORY;
6381 }
Janos Follathf08e2652019-06-13 09:05:41 +01006382
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006384 prf->seed_length = data_length;
6385 }
Janos Follathf08e2652019-06-13 09:05:41 +01006386
Gilles Peskine43f958b2020-12-13 14:55:14 +01006387 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006390}
6391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6393 const uint8_t *data,
6394 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006395{
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6397 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6398 return PSA_ERROR_BAD_STATE;
6399 }
Janos Follath81550542019-06-13 14:26:34 +01006400
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 if (data_length != 0) {
6402 prf->secret = mbedtls_calloc(1, data_length);
6403 if (prf->secret == NULL) {
6404 return PSA_ERROR_INSUFFICIENT_MEMORY;
6405 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006406
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006408 prf->secret_length = data_length;
6409 }
Janos Follath81550542019-06-13 14:26:34 +01006410
Gilles Peskine43f958b2020-12-13 14:55:14 +01006411 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006412
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006414}
6415
Gilles Peskine449bd832023-01-11 14:50:10 +01006416static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6417 const uint8_t *data,
6418 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006419{
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6421 return PSA_ERROR_BAD_STATE;
6422 }
Janos Follath63028dd2019-06-13 09:15:47 +01006423
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 if (data_length != 0) {
6425 prf->label = mbedtls_calloc(1, data_length);
6426 if (prf->label == NULL) {
6427 return PSA_ERROR_INSUFFICIENT_MEMORY;
6428 }
Janos Follath63028dd2019-06-13 09:15:47 +01006429
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006431 prf->label_length = data_length;
6432 }
Janos Follath63028dd2019-06-13 09:15:47 +01006433
Gilles Peskine43f958b2020-12-13 14:55:14 +01006434 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006435
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006437}
6438
Gilles Peskine449bd832023-01-11 14:50:10 +01006439static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6440 psa_key_derivation_step_t step,
6441 const uint8_t *data,
6442 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006443{
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006445 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006447 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006449 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006451 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006453 }
6454}
John Durkop07cc04a2020-11-16 22:08:34 -08006455#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6456 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6457
6458#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6459static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6460 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006461 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006463{
6464 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6466 4 + data_length + prf->other_secret_length :
6467 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6470 return PSA_ERROR_INVALID_ARGUMENT;
6471 }
John Durkop07cc04a2020-11-16 22:08:34 -08006472
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 uint8_t *pms = mbedtls_calloc(1, pms_len);
6474 if (pms == NULL) {
6475 return PSA_ERROR_INSUFFICIENT_MEMORY;
6476 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006477 uint8_t *cur = pms;
6478
6479 /* pure-PSK:
6480 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006481 *
6482 * The premaster secret is formed as follows: if the PSK is N octets
6483 * long, concatenate a uint16 with the value N, N zero octets, a second
6484 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006485 *
6486 * mixed-PSK:
6487 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6488 * follows: concatenate a uint16 with the length of the other secret,
6489 * the other secret itself, uint16 with the length of PSK, and the
6490 * PSK itself.
6491 * For details please check:
6492 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6493 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6494 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006495 */
6496
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6498 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6499 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6500 if (prf->other_secret_length != 0) {
6501 memcpy(cur, prf->other_secret, prf->other_secret_length);
6502 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006503 cur += prf->other_secret_length;
6504 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 } else {
6506 *cur++ = MBEDTLS_BYTE_1(data_length);
6507 *cur++ = MBEDTLS_BYTE_0(data_length);
6508 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006509 cur += data_length;
6510 }
6511
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 *cur++ = MBEDTLS_BYTE_1(data_length);
6513 *cur++ = MBEDTLS_BYTE_0(data_length);
6514 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006515 cur += data_length;
6516
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006517 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006518
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006519 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006521}
Janos Follath6660f0e2019-06-17 08:44:03 +01006522
Przemek Stekiele47201b2022-04-19 13:53:28 +02006523static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6524 psa_tls12_prf_key_derivation_t *prf,
6525 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006527{
Gilles Peskine449bd832023-01-11 14:50:10 +01006528 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6529 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006530 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006531
6532 if (data_length != 0) {
6533 prf->other_secret = mbedtls_calloc(1, data_length);
6534 if (prf->other_secret == NULL) {
6535 return PSA_ERROR_INSUFFICIENT_MEMORY;
6536 }
6537
6538 memcpy(prf->other_secret, data, data_length);
6539 prf->other_secret_length = data_length;
6540 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006541 prf->other_secret_length = 0;
6542 }
6543
6544 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6545
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006547}
6548
Janos Follath6660f0e2019-06-17 08:44:03 +01006549static psa_status_t psa_tls12_prf_psk_to_ms_input(
6550 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006551 psa_key_derivation_step_t step,
6552 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006554{
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006556 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 return psa_tls12_prf_psk_to_ms_set_key(prf,
6558 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006559 break;
6560 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6562 data,
6563 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006564 break;
6565 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006566 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006567 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006568
Przemek Stekiele47201b2022-04-19 13:53:28 +02006569 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006570}
John Durkop07cc04a2020-11-16 22:08:34 -08006571#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006572
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006573#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6574static psa_status_t psa_tls12_ecjpake_to_pms_input(
6575 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006576 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006577 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006579{
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6581 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6582 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006583 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006584
6585 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 if (data[0] != 0x04) {
6587 return PSA_ERROR_INVALID_ARGUMENT;
6588 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006589
6590 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006592
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006594}
6595#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306596
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306597#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306598static psa_status_t psa_pbkdf2_set_input_cost(
6599 psa_pbkdf2_key_derivation_t *pbkdf2,
6600 psa_key_derivation_step_t step,
6601 uint64_t data)
6602{
6603 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6604 return PSA_ERROR_INVALID_ARGUMENT;
6605 }
6606
6607 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6608 return PSA_ERROR_BAD_STATE;
6609 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306610
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306611 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306612 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306613 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306614
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306615 if (data == 0) {
6616 return PSA_ERROR_INVALID_ARGUMENT;
6617 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306618
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306619 pbkdf2->input_cost = data;
6620 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6621
6622 return PSA_SUCCESS;
6623}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306624
6625static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6626 const uint8_t *data,
6627 size_t data_length)
6628{
Gilles Peskineead17662023-08-20 22:02:51 +02006629 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6630 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6631 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6632 /* Appending to existing salt. No state change. */
6633 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306634 return PSA_ERROR_BAD_STATE;
6635 }
6636
Gilles Peskineca57d782023-07-20 19:08:06 +02006637 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006638 /* Appending an empty string, nothing to do. */
6639 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306640 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306641
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306642 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6643 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306644 return PSA_ERROR_INSUFFICIENT_MEMORY;
6645 }
6646
Gilles Peskineead17662023-08-20 22:02:51 +02006647 if (pbkdf2->salt_length != 0) {
6648 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6649 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306650 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306651 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306652 mbedtls_free(pbkdf2->salt);
6653 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306654 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306655 return PSA_SUCCESS;
6656}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306657
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306658#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306659static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306660 const uint8_t *input,
6661 size_t input_len,
6662 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306663 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306664{
6665 psa_status_t status = PSA_SUCCESS;
6666 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6667 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306668 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306669 } else {
6670 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306671 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306672 }
6673 return status;
6674}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306675#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306676
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306677#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306678static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6679 size_t input_len,
6680 uint8_t *output,
6681 size_t *output_len)
6682{
6683 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306684 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306686 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306687 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6688 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6689 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306690 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306691 * mac_size as the driver function sets mac_output_length = mac_size
6692 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306693 status = psa_driver_wrapper_mac_compute(&attributes,
6694 zeros, sizeof(zeros),
6695 PSA_ALG_CMAC, input, input_len,
6696 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306697 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6698 128U,
6699 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306700 output_len);
6701 } else {
6702 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306703 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306704 }
6705 return status;
6706}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306707#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306708
6709static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306710 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306711 const uint8_t *data,
6712 size_t data_length)
6713{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306714 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306715 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6716 return PSA_ERROR_BAD_STATE;
6717 }
6718
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306719#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306720 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6721 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6722 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6723 pbkdf2->password,
6724 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306725 } else
6726#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6727#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6728 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306729 status = psa_pbkdf2_cmac_set_password(data, data_length,
6730 pbkdf2->password,
6731 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306732 } else
6733#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6734 {
6735 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306736 }
6737
6738 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6739
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306740 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306741}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306742
6743static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306744 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306745 psa_key_derivation_step_t step,
6746 const uint8_t *data,
6747 size_t data_length)
6748{
6749 switch (step) {
6750 case PSA_KEY_DERIVATION_INPUT_SALT:
6751 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6752 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306753 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306754 default:
6755 return PSA_ERROR_INVALID_ARGUMENT;
6756 }
6757}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306758#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306759
Gilles Peskineb8965192019-09-24 16:21:10 +02006760/** Check whether the given key type is acceptable for the given
6761 * input step of a key derivation.
6762 *
6763 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6764 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6765 * Both secret and non-secret inputs can alternatively have the type
6766 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6767 * that the input was passed as a buffer rather than via a key object.
6768 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006769static int psa_key_derivation_check_input_type(
6770 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006771 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006772{
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006774 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 if (key_type == PSA_KEY_TYPE_DERIVE) {
6776 return PSA_SUCCESS;
6777 }
6778 if (key_type == PSA_KEY_TYPE_NONE) {
6779 return PSA_SUCCESS;
6780 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006781 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006782 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006783 if (key_type == PSA_KEY_TYPE_DERIVE) {
6784 return PSA_SUCCESS;
6785 }
6786 if (key_type == PSA_KEY_TYPE_NONE) {
6787 return PSA_SUCCESS;
6788 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006789 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006790 case PSA_KEY_DERIVATION_INPUT_LABEL:
6791 case PSA_KEY_DERIVATION_INPUT_SALT:
6792 case PSA_KEY_DERIVATION_INPUT_INFO:
6793 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6795 return PSA_SUCCESS;
6796 }
6797 if (key_type == PSA_KEY_TYPE_NONE) {
6798 return PSA_SUCCESS;
6799 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006800 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306801 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6802 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6803 return PSA_SUCCESS;
6804 }
6805 if (key_type == PSA_KEY_TYPE_DERIVE) {
6806 return PSA_SUCCESS;
6807 }
6808 if (key_type == PSA_KEY_TYPE_NONE) {
6809 return PSA_SUCCESS;
6810 }
6811 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006812 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006814}
6815
Janos Follathb80a94e2019-06-12 15:54:46 +01006816static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006817 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006818 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006819 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006820 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006822{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006823 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006825
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 status = psa_key_derivation_check_input_type(step, key_type);
6827 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006828 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006829 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006830
Przemek Stekiel69c46792022-06-10 12:59:51 +02006831#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6833 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6834 step, data, data_length);
6835 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006836#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006837#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6839 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6840 step, data, data_length);
6841 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006842#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6843#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6845 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6846 step, data, data_length);
6847 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006848#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006849#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006850 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006851 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6853 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006854#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306855#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306856 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306857 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6858 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306859 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306860#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006861 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006862 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006863 (void) data;
6864 (void) data_length;
6865 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006867 }
6868
Gilles Peskine224b0d62019-09-23 18:13:17 +02006869exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006870 if (status != PSA_SUCCESS) {
6871 psa_key_derivation_abort(operation);
6872 }
6873 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006874}
6875
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306876static psa_status_t psa_key_derivation_input_integer_internal(
6877 psa_key_derivation_operation_t *operation,
6878 psa_key_derivation_step_t step,
6879 uint64_t value)
6880{
6881 psa_status_t status;
6882 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6883
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306884#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306885 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306886 status = psa_pbkdf2_set_input_cost(
6887 &operation->ctx.pbkdf2, step, value);
6888 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306889#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306890 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306891 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306892 (void) value;
6893 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306894 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306895 }
6896
6897 if (status != PSA_SUCCESS) {
6898 psa_key_derivation_abort(operation);
6899 }
6900 return status;
6901}
6902
Janos Follath51f4a0f2019-06-14 11:35:55 +01006903psa_status_t psa_key_derivation_input_bytes(
6904 psa_key_derivation_operation_t *operation,
6905 psa_key_derivation_step_t step,
6906 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006908{
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 return psa_key_derivation_input_internal(operation, step,
6910 PSA_KEY_TYPE_NONE,
6911 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006912}
6913
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306914psa_status_t psa_key_derivation_input_integer(
6915 psa_key_derivation_operation_t *operation,
6916 psa_key_derivation_step_t step,
6917 uint64_t value)
6918{
6919 return psa_key_derivation_input_integer_internal(operation, step, value);
6920}
6921
Janos Follath51f4a0f2019-06-14 11:35:55 +01006922psa_status_t psa_key_derivation_input_key(
6923 psa_key_derivation_operation_t *operation,
6924 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006926{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006927 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006928 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006929 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006930
Ronald Cron5c522922020-11-14 16:35:34 +01006931 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6933 if (status != PSA_SUCCESS) {
6934 psa_key_derivation_abort(operation);
6935 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006936 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006937
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306938 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6939 * permission to output to a key object. */
6940 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6941 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006942 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 status = psa_key_derivation_input_internal(operation,
6946 step, slot->attr.type,
6947 slot->key.data,
6948 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006949
Ryan Everett1b70a072024-01-04 10:32:49 +00006950 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006951
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006953}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006954
6955
6956
6957/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006958/* Key agreement */
6959/****************************************************************/
6960
Gilles Peskine449bd832023-01-11 14:50:10 +01006961psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6962 const uint8_t *key_buffer,
6963 size_t key_buffer_size,
6964 psa_algorithm_t alg,
6965 const uint8_t *peer_key,
6966 size_t peer_key_length,
6967 uint8_t *shared_secret,
6968 size_t shared_secret_size,
6969 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006970{
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006972#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006973 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006974 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6975 key_buffer_size, alg,
6976 peer_key, peer_key_length,
6977 shared_secret,
6978 shared_secret_size,
6979 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006980#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006981
6982#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6983 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006984 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006985 peer_key,
6986 peer_key_length,
6987 key_buffer,
6988 key_buffer_size,
6989 shared_secret,
6990 shared_secret_size,
6991 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006992#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6993
Gilles Peskine01d718c2018-09-18 12:01:02 +02006994 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006995 (void) attributes;
6996 (void) key_buffer;
6997 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006998 (void) peer_key;
6999 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007000 (void) shared_secret;
7001 (void) shared_secret_size;
7002 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007004 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007005}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007006
Aditya Deshpande17845b82022-10-13 17:21:01 +01007007/** Internal function for raw key agreement
7008 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007009 * to the driver's implementation if a driver is present.
7010 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007011 * (psa_key_agreement_raw_builtin).
7012 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007013static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7014 psa_key_slot_t *private_key,
7015 const uint8_t *peer_key,
7016 size_t peer_key_length,
7017 uint8_t *shared_secret,
7018 size_t shared_secret_size,
7019 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007020{
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7022 return PSA_ERROR_NOT_SUPPORTED;
7023 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007024
7025 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007027 };
7028
Gilles Peskine449bd832023-01-11 14:50:10 +01007029 return psa_driver_wrapper_key_agreement(&attributes,
7030 private_key->key.data,
7031 private_key->key.bytes, alg,
7032 peer_key, peer_key_length,
7033 shared_secret,
7034 shared_secret_size,
7035 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007036}
7037
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007038/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007039 * to potentially free embedded data structures and wipe confidential data.
7040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007041static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7042 psa_key_derivation_step_t step,
7043 psa_key_slot_t *private_key,
7044 const uint8_t *peer_key,
7045 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007046{
7047 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007048 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007049 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007051
7052 /* Step 1: run the secret agreement algorithm to generate the shared
7053 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 status = psa_key_agreement_raw_internal(ka_alg,
7055 private_key,
7056 peer_key, peer_key_length,
7057 shared_secret,
7058 sizeof(shared_secret),
7059 &shared_secret_length);
7060 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007061 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007063
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007064 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007065 * the shared secret. A shared secret is permitted wherever a key
7066 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 status = psa_key_derivation_input_internal(operation, step,
7068 PSA_KEY_TYPE_DERIVE,
7069 shared_secret,
7070 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007071exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7073 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007074}
7075
Gilles Peskine449bd832023-01-11 14:50:10 +01007076psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7077 psa_key_derivation_step_t step,
7078 mbedtls_svc_key_id_t private_key,
7079 const uint8_t *peer_key,
7080 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007081{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007082 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007083 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007084 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007085
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7087 return PSA_ERROR_INVALID_ARGUMENT;
7088 }
Ronald Cron5c522922020-11-14 16:35:34 +01007089 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7091 if (status != PSA_SUCCESS) {
7092 return status;
7093 }
7094 status = psa_key_agreement_internal(operation, step,
7095 slot,
7096 peer_key, peer_key_length);
7097 if (status != PSA_SUCCESS) {
7098 psa_key_derivation_abort(operation);
7099 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007100 /* If a private key has been added as SECRET, we allow the derived
7101 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007103 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007105 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007106
Ryan Everett1b70a072024-01-04 10:32:49 +00007107 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007108
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007110}
7111
Gilles Peskine449bd832023-01-11 14:50:10 +01007112psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7113 mbedtls_svc_key_id_t private_key,
7114 const uint8_t *peer_key,
7115 size_t peer_key_length,
7116 uint8_t *output,
7117 size_t output_size,
7118 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007119{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007120 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007121 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007122 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007123 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007124
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007126 status = PSA_ERROR_INVALID_ARGUMENT;
7127 goto exit;
7128 }
Ronald Cron5c522922020-11-14 16:35:34 +01007129 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7131 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007132 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007133 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007134
Gilles Peskine3e561302022-04-14 00:17:15 +02007135 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7136 * for the output size. The PSA specification only guarantees that this
7137 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7138 * but it might be nice to allow smaller buffers if the output fits.
7139 * At the time of writing this comment, with only ECDH implemented,
7140 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7141 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7142 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007143 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7145 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007146 status = PSA_ERROR_BUFFER_TOO_SMALL;
7147 goto exit;
7148 }
7149
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 status = psa_key_agreement_raw_internal(alg, slot,
7151 peer_key, peer_key_length,
7152 output, output_size,
7153 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007154
7155exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007156 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007157 /* If an error happens and is not handled properly, the output
7158 * may be used as a key to protect sensitive data. Arrange for such
7159 * a key to be random, which is likely to result in decryption or
7160 * verification errors. This is better than filling the buffer with
7161 * some constant data such as zeros, which would result in the data
7162 * being protected with a reproducible, easily knowable key.
7163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007164 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007165 *output_length = output_size;
7166 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007167
Ryan Everett1b70a072024-01-04 10:32:49 +00007168 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007169
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007171}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007172
7173
Gilles Peskine30524eb2020-11-13 17:02:26 +01007174
Gilles Peskine86a440b2018-11-12 18:39:40 +01007175/****************************************************************/
7176/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007177/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007178
Gilles Peskine672a7712023-04-28 21:00:28 +02007179#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7180#include "entropy_poll.h"
7181#endif
7182
Gilles Peskine30524eb2020-11-13 17:02:26 +01007183/** Initialize the PSA random generator.
7184 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007185static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007186{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007187#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007189#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7190
Gilles Peskine30524eb2020-11-13 17:02:26 +01007191 /* Set default configuration if
7192 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007193 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007194 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 }
7196 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007197 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007198 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007199
Gilles Peskine449bd832023-01-11 14:50:10 +01007200 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007201#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7202 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7203 /* The PSA entropy injection feature depends on using NV seed as an entropy
7204 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007205 mbedtls_entropy_add_source(&rng->entropy,
7206 mbedtls_nv_seed_poll, NULL,
7207 MBEDTLS_ENTROPY_BLOCK_SIZE,
7208 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007209#endif
7210
Gilles Peskine449bd832023-01-11 14:50:10 +01007211 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007212#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007213}
7214
7215/** Deinitialize the PSA random generator.
7216 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007217static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007218{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007219#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007220 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007221#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti83e0de82023-11-24 12:13:05 +01007222 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7223 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007224#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007225}
7226
7227/** Seed the PSA random generator.
7228 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007229static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007230{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007231#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7232 /* Do nothing: the external RNG seeds itself. */
7233 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007235#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007236 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7238 drbg_seed, sizeof(drbg_seed) - 1);
7239 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007240#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007241}
7242
Gilles Peskine449bd832023-01-11 14:50:10 +01007243psa_status_t psa_generate_random(uint8_t *output,
7244 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007245{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007246 GUARD_MODULE_INITIALIZED;
7247
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007248#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7249
7250 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007251 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7252 output, output_size,
7253 &output_length);
7254 if (status != PSA_SUCCESS) {
7255 return status;
7256 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007257 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007258 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 if (output_length != output_size) {
7260 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7261 }
7262 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007263
7264#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7265
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007267 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7269 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7270 output_size);
7271 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7272 output, request_size);
7273 if (ret != 0) {
7274 return mbedtls_to_psa_error(ret);
7275 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007276 output_size -= request_size;
7277 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007278 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007279 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007280#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007281}
7282
Gilles Peskine8814fc42020-12-14 15:33:44 +01007283/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007284 *
7285 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7286 * `psa_generate_random(...)`. The state parameter is ignored since the
7287 * PSA API doesn't support passing an explicit state.
7288 *
7289 * In the non-external case, psa_generate_random() calls an
7290 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7291 * and semantics as mbedtls_psa_get_random(). As an optimization,
7292 * instead of doing this back-and-forth between the PSA API and the
7293 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7294 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007296#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7297int mbedtls_psa_get_random(void *p_rng,
7298 unsigned char *output,
7299 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007300{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007301 /* This function takes a pointer to the RNG state because that's what
7302 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7303 * its own state internally and doesn't let the caller access that state.
7304 * So we just ignore the state parameter, and in practice we'll pass
7305 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007306 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007307 psa_status_t status = psa_generate_random(output, output_size);
7308 if (status == PSA_SUCCESS) {
7309 return 0;
7310 } else {
7311 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7312 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007313}
7314#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7315
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007316#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007317psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7318 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007319{
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 if (global_data.initialized) {
7321 return PSA_ERROR_NOT_PERMITTED;
7322 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007323
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7325 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7326 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7327 return PSA_ERROR_INVALID_ARGUMENT;
7328 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007329
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007331}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007332#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007333
Ronald Cron3772afe2021-02-08 16:10:05 +01007334/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007335 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007336 * \param type The key type
7337 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007338 *
7339 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007340 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007341 * \retval #PSA_ERROR_INVALID_ARGUMENT
7342 * The size in bits of the key is not valid.
7343 * \retval #PSA_ERROR_NOT_SUPPORTED
7344 * The type and/or the size in bits of the key or the combination of
7345 * the two is not supported.
7346 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007347static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007348 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007349{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007350 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007351
Gilles Peskine449bd832023-01-11 14:50:10 +01007352 if (key_type_is_raw_bytes(type)) {
7353 status = psa_validate_unstructured_key_bit_size(type, bits);
7354 if (status != PSA_SUCCESS) {
7355 return status;
7356 }
7357 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007358#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007359 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7360 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7361 return PSA_ERROR_NOT_SUPPORTED;
7362 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007363 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007364 return PSA_ERROR_NOT_SUPPORTED;
7365 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007366
Ronald Cron01b2aba2020-10-05 09:42:02 +02007367 /* Accept only byte-aligned keys, for the same reasons as
7368 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007369 if (bits % 8 != 0) {
7370 return PSA_ERROR_NOT_SUPPORTED;
7371 }
7372 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007373#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007374
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007375#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007376 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007377 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 return PSA_SUCCESS;
7379 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007380#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007381
Valerio Settia55f0422023-07-10 15:34:41 +02007382#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007383 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007384 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007385 return PSA_ERROR_NOT_SUPPORTED;
7386 }
7387 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007388#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007389 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007390 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007391 }
7392
Gilles Peskine449bd832023-01-11 14:50:10 +01007393 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007394}
7395
Ronald Cron55ed0592020-10-05 10:30:40 +02007396psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007397 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007399{
7400 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007401 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007402
Gilles Peskine449bd832023-01-11 14:50:10 +01007403 if ((attributes->domain_parameters == NULL) &&
7404 (attributes->domain_parameters_size != 0)) {
7405 return PSA_ERROR_INVALID_ARGUMENT;
7406 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007407
Gilles Peskine449bd832023-01-11 14:50:10 +01007408 if (key_type_is_raw_bytes(type)) {
7409 status = psa_generate_random(key_buffer, key_buffer_size);
7410 if (status != PSA_SUCCESS) {
7411 return status;
7412 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007413
David Brown12ca5032021-02-11 11:02:00 -07007414#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007415 if (type == PSA_KEY_TYPE_DES) {
7416 psa_des_set_key_parity(key_buffer, key_buffer_size);
7417 }
David Brown8107e312021-02-12 08:08:46 -07007418#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007419 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007420
Valerio Setti76df8c12023-07-11 14:11:28 +02007421#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007422 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7423 return mbedtls_psa_rsa_generate_key(attributes,
7424 key_buffer,
7425 key_buffer_size,
7426 key_buffer_length);
7427 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007428#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007429
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007430#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7432 return mbedtls_psa_ecp_generate_key(attributes,
7433 key_buffer,
7434 key_buffer_size,
7435 key_buffer_length);
7436 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007437#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007438
Valerio Settia55f0422023-07-10 15:34:41 +02007439#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007440 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7441 return mbedtls_psa_ffdh_generate_key(attributes,
7442 key_buffer,
7443 key_buffer_size,
7444 key_buffer_length);
7445 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007446#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007447 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 (void) key_buffer_length;
7449 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007450 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007451
Gilles Peskine449bd832023-01-11 14:50:10 +01007452 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007453}
Darryl Green0c6575a2018-11-07 16:05:30 +00007454
Gilles Peskine449bd832023-01-11 14:50:10 +01007455psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7456 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007457{
7458 psa_status_t status;
7459 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007460 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007461 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007462
Ronald Cron81709fc2020-11-14 12:10:32 +01007463 *key = MBEDTLS_SVC_KEY_ID_INIT;
7464
Gilles Peskine0f84d622019-09-12 19:03:13 +02007465 /* Reject any attempt to create a zero-length key so that we don't
7466 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007467 if (psa_get_key_bits(attributes) == 0) {
7468 return PSA_ERROR_INVALID_ARGUMENT;
7469 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007470
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007471 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7473 return PSA_ERROR_INVALID_ARGUMENT;
7474 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007475
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7477 &slot, &driver);
7478 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007479 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007480 }
Gilles Peskine11792082019-08-06 18:36:36 +02007481
Ronald Cron977c2472020-10-13 08:32:21 +02007482 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307483 * storage ( thus not in the case of generating a key in a secure element
7484 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7485 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 if (slot->key.data == NULL) {
7487 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7488 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007489 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007490 attributes->core.type, attributes->core.bits);
7491 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007492 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007494
7495 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 attributes->core.type,
7497 attributes->core.bits);
7498 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007499 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 attributes, &key_buffer_size);
7501 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007502 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 }
Ronald Cron977c2472020-10-13 08:32:21 +02007504 }
Ronald Cron977c2472020-10-13 08:32:21 +02007505
Gilles Peskine449bd832023-01-11 14:50:10 +01007506 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7507 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007508 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 }
Ronald Cron977c2472020-10-13 08:32:21 +02007510 }
7511
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 status = psa_driver_wrapper_generate_key(attributes,
7513 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007514
Gilles Peskine449bd832023-01-11 14:50:10 +01007515 if (status != PSA_SUCCESS) {
7516 psa_remove_key_data_from_memory(slot);
7517 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007518
Gilles Peskine11792082019-08-06 18:36:36 +02007519exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 if (status == PSA_SUCCESS) {
7521 status = psa_finish_key_creation(slot, driver, key);
7522 }
7523 if (status != PSA_SUCCESS) {
7524 psa_fail_key_creation(slot, driver);
7525 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007526
Gilles Peskine449bd832023-01-11 14:50:10 +01007527 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007528}
7529
Gilles Peskinee59236f2018-01-27 23:32:46 +01007530/****************************************************************/
7531/* Module setup */
7532/****************************************************************/
7533
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007534#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007535psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 void (* entropy_init)(mbedtls_entropy_context *ctx),
7537 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007538{
Gilles Peskine449bd832023-01-11 14:50:10 +01007539 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7540 return PSA_ERROR_BAD_STATE;
7541 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007542 global_data.rng.entropy_init = entropy_init;
7543 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007544 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007545}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007546#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007547
Gilles Peskine449bd832023-01-11 14:50:10 +01007548void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007549{
Gilles Peskine449bd832023-01-11 14:50:10 +01007550 psa_wipe_all_key_slots();
Valerio Setti83e0de82023-11-24 12:13:05 +01007551 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7552 mbedtls_psa_random_free(&global_data.rng);
7553 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007554 /* Wipe all remaining data, including configuration.
7555 * In particular, this sets all state indicator to the value
7556 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007557 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007558
7559 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007560 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007561}
7562
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007563#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7564/** Recover a transaction that was interrupted by a power failure.
7565 *
7566 * This function is called during initialization, before psa_crypto_init()
7567 * returns. If this function returns a failure status, the initialization
7568 * fails.
7569 */
7570static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007571 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007572{
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007574 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7575 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007576 /* TODO - fall through to the failure case until this
7577 * is implemented.
7578 * https://github.com/ARMmbed/mbed-crypto/issues/218
7579 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007580 default:
7581 /* We found an unsupported transaction in the storage.
7582 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007583 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007584 }
7585}
7586#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7587
Gilles Peskine449bd832023-01-11 14:50:10 +01007588psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007589{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007590 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007591
Gilles Peskinec6b69072018-11-20 21:42:52 +01007592 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007593 if (global_data.initialized != 0) {
7594 return PSA_SUCCESS;
7595 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007596
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007597 /* Init drivers */
7598 status = psa_driver_wrapper_init();
7599 if (status != PSA_SUCCESS) {
7600 goto exit;
7601 }
7602 global_data.drivers_initialized = 1;
7603
Valerio Setti402cfba2023-11-13 10:24:32 +01007604 status = psa_initialize_key_slots();
7605 if (status != PSA_SUCCESS) {
7606 goto exit;
7607 }
7608
Gilles Peskine30524eb2020-11-13 17:02:26 +01007609 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007611 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007612 status = mbedtls_psa_random_seed(&global_data.rng);
7613 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007614 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007616 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007617
Gilles Peskine4b734222019-07-24 15:56:31 +02007618#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007619 status = psa_crypto_load_transaction();
7620 if (status == PSA_SUCCESS) {
7621 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7622 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007623 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007624 }
7625 status = psa_crypto_stop_transaction();
7626 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007627 /* There's no transaction to complete. It's all good. */
7628 status = PSA_SUCCESS;
7629 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007630#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007631
Gilles Peskinec6b69072018-11-20 21:42:52 +01007632 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007633 global_data.initialized = 1;
7634
7635exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007636 if (status != PSA_SUCCESS) {
7637 mbedtls_psa_crypto_free();
7638 }
7639 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007640}
7641
Yanray Wangffc3c482023-07-11 12:01:04 +08007642#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007643psa_status_t psa_crypto_driver_pake_get_password_len(
7644 const psa_crypto_driver_pake_inputs_t *inputs,
7645 size_t *password_len)
7646{
7647 if (inputs->password_len == 0) {
7648 return PSA_ERROR_BAD_STATE;
7649 }
7650
7651 *password_len = inputs->password_len;
7652
7653 return PSA_SUCCESS;
7654}
7655
7656psa_status_t psa_crypto_driver_pake_get_password(
7657 const psa_crypto_driver_pake_inputs_t *inputs,
7658 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7659{
7660 if (inputs->password_len == 0) {
7661 return PSA_ERROR_BAD_STATE;
7662 }
7663
7664 if (buffer_size < inputs->password_len) {
7665 return PSA_ERROR_BUFFER_TOO_SMALL;
7666 }
7667
7668 memcpy(buffer, inputs->password, inputs->password_len);
7669 *buffer_length = inputs->password_len;
7670
7671 return PSA_SUCCESS;
7672}
7673
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007674psa_status_t psa_crypto_driver_pake_get_user_len(
7675 const psa_crypto_driver_pake_inputs_t *inputs,
7676 size_t *user_len)
7677{
7678 if (inputs->user_len == 0) {
7679 return PSA_ERROR_BAD_STATE;
7680 }
7681
7682 *user_len = inputs->user_len;
7683
7684 return PSA_SUCCESS;
7685}
7686
7687psa_status_t psa_crypto_driver_pake_get_user(
7688 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007689 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007690{
7691 if (inputs->user_len == 0) {
7692 return PSA_ERROR_BAD_STATE;
7693 }
7694
Przemek Stekielc0e62502023-03-14 11:49:36 +01007695 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007696 return PSA_ERROR_BUFFER_TOO_SMALL;
7697 }
7698
Przemek Stekielc0e62502023-03-14 11:49:36 +01007699 memcpy(user_id, inputs->user, inputs->user_len);
7700 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007701
7702 return PSA_SUCCESS;
7703}
7704
7705psa_status_t psa_crypto_driver_pake_get_peer_len(
7706 const psa_crypto_driver_pake_inputs_t *inputs,
7707 size_t *peer_len)
7708{
7709 if (inputs->peer_len == 0) {
7710 return PSA_ERROR_BAD_STATE;
7711 }
7712
7713 *peer_len = inputs->peer_len;
7714
7715 return PSA_SUCCESS;
7716}
7717
7718psa_status_t psa_crypto_driver_pake_get_peer(
7719 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007720 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007721{
7722 if (inputs->peer_len == 0) {
7723 return PSA_ERROR_BAD_STATE;
7724 }
7725
Przemek Stekielc0e62502023-03-14 11:49:36 +01007726 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007727 return PSA_ERROR_BUFFER_TOO_SMALL;
7728 }
7729
Przemek Stekielc0e62502023-03-14 11:49:36 +01007730 memcpy(peer_id, inputs->peer, inputs->peer_len);
7731 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007732
7733 return PSA_SUCCESS;
7734}
7735
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007736psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7737 const psa_crypto_driver_pake_inputs_t *inputs,
7738 psa_pake_cipher_suite_t *cipher_suite)
7739{
7740 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7741 return PSA_ERROR_BAD_STATE;
7742 }
7743
7744 *cipher_suite = inputs->cipher_suite;
7745
7746 return PSA_SUCCESS;
7747}
7748
Neil Armstronga7d08c32022-06-01 18:21:20 +02007749psa_status_t psa_pake_setup(
7750 psa_pake_operation_t *operation,
7751 const psa_pake_cipher_suite_t *cipher_suite)
7752{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007753 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007754
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007755 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007756 status = PSA_ERROR_BAD_STATE;
7757 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007758 }
7759
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007760 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007761 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007762 status = PSA_ERROR_INVALID_ARGUMENT;
7763 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007764 }
7765
Przemek Stekiel51eac532022-12-07 11:04:51 +01007766 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7767
Przemek Stekiele12ed362022-12-21 12:54:46 +01007768 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007769 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7770 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007771 operation->data.inputs.cipher_suite = *cipher_suite;
7772
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007773#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007774 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007775 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007776 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007777
David Horstmann16f01512023-06-14 17:21:07 +01007778 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007779 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007780 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007781#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007782 {
7783 status = PSA_ERROR_NOT_SUPPORTED;
7784 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007785 }
7786
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007787 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7788
Przemek Stekiel51eac532022-12-07 11:04:51 +01007789 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007790exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007791 psa_pake_abort(operation);
7792 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007793}
7794
7795psa_status_t psa_pake_set_password_key(
7796 psa_pake_operation_t *operation,
7797 mbedtls_svc_key_id_t password)
7798{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007799 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007800 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7801 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007802 psa_key_attributes_t attributes;
7803 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007804
Przemek Stekiel51eac532022-12-07 11:04:51 +01007805 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007806 status = PSA_ERROR_BAD_STATE;
7807 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007808 }
7809
Przemek Stekiel6c764412022-11-22 14:05:12 +01007810 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7811 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007812 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007813 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007814 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007815 }
7816
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007817 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007818 .core = slot->attr
7819 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007820
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007821 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007822
Przemek Stekiel51eac532022-12-07 11:04:51 +01007823 if (type != PSA_KEY_TYPE_PASSWORD &&
7824 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7825 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007826 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007827 }
7828
Przemek Stekiel51eac532022-12-07 11:04:51 +01007829 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7830 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007831 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007832 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007833 }
7834
7835 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7836 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007837 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007838exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007839 if (status != PSA_SUCCESS) {
7840 psa_pake_abort(operation);
7841 }
Ryan Everett1b70a072024-01-04 10:32:49 +00007842 unlock_status = psa_unregister_read(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007843 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007844}
7845
7846psa_status_t psa_pake_set_user(
7847 psa_pake_operation_t *operation,
7848 const uint8_t *user_id,
7849 size_t user_id_len)
7850{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007851 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007852
7853 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007854 status = PSA_ERROR_BAD_STATE;
7855 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007856 }
7857
Przemek Stekiel51eac532022-12-07 11:04:51 +01007858 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007859 status = PSA_ERROR_INVALID_ARGUMENT;
7860 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007861 }
7862
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007863 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007864 status = PSA_ERROR_BAD_STATE;
7865 goto exit;
7866 }
7867
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007868 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7869 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007870 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7871 goto exit;
7872 }
7873
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007874 memcpy(operation->data.inputs.user, user_id, user_id_len);
7875 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007876
7877 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007878exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007879 psa_pake_abort(operation);
7880 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007881}
7882
7883psa_status_t psa_pake_set_peer(
7884 psa_pake_operation_t *operation,
7885 const uint8_t *peer_id,
7886 size_t peer_id_len)
7887{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007888 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007889
7890 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007891 status = PSA_ERROR_BAD_STATE;
7892 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007893 }
7894
Przemek Stekiel51eac532022-12-07 11:04:51 +01007895 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007896 status = PSA_ERROR_INVALID_ARGUMENT;
7897 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007898 }
7899
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007900 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007901 status = PSA_ERROR_BAD_STATE;
7902 goto exit;
7903 }
7904
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007905 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7906 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007907 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7908 goto exit;
7909 }
7910
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007911 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7912 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007913
7914 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007915exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007916 psa_pake_abort(operation);
7917 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007918}
7919
7920psa_status_t psa_pake_set_role(
7921 psa_pake_operation_t *operation,
7922 psa_pake_role_t role)
7923{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007924 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007925
Przemek Stekiel51eac532022-12-07 11:04:51 +01007926 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007927 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007928 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007929 }
7930
Przemek Stekiel09104b82023-03-06 14:11:51 +01007931 switch (operation->alg) {
7932#if defined(PSA_WANT_ALG_JPAKE)
7933 case PSA_ALG_JPAKE:
7934 if (role == PSA_PAKE_ROLE_NONE) {
7935 return PSA_SUCCESS;
7936 }
7937 status = PSA_ERROR_INVALID_ARGUMENT;
7938 break;
7939#endif
7940 default:
7941 (void) role;
7942 status = PSA_ERROR_NOT_SUPPORTED;
7943 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007944 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007945exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007946 psa_pake_abort(operation);
7947 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007948}
7949
David Horstmanne7f21e62023-05-12 18:17:21 +01007950/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007951#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007952static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007953 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007954{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007955 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007956 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007957 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007958
David Horstmann024e5c52023-06-14 15:48:21 +01007959 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007960 is_x1 = (stage->outputs < 1);
7961 } else {
7962 is_x1 = (stage->inputs < 1);
7963 }
7964
David Horstmann74a3d8c2023-06-14 18:28:19 +01007965 key_share_step = is_x1 ?
7966 PSA_JPAKE_X1_STEP_KEY_SHARE :
7967 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007968 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007969 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7970 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7971 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7972 } else {
7973 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007974 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01007975 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01007976}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007977#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007978
Przemek Stekiel2797d372022-12-22 11:19:22 +01007979static psa_status_t psa_pake_complete_inputs(
7980 psa_pake_operation_t *operation)
7981{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007982 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007983 /* Create copy of the inputs on stack as inputs share memory
7984 with the driver context which will be setup by the driver. */
7985 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007986
Przemek Stekielfde11282023-03-13 16:06:09 +01007987 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007988 return PSA_ERROR_BAD_STATE;
7989 }
7990
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007991 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007992 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7993 return PSA_ERROR_BAD_STATE;
7994 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007995 }
7996
Przemek Stekiel18620a32023-01-17 16:34:52 +01007997 /* Clear driver context */
7998 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7999
8000 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008001
8002 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008003 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008004
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008005 /* User and peer are translated to role. */
8006 mbedtls_free(inputs.user);
8007 mbedtls_free(inputs.peer);
8008
Przemek Stekiel2797d372022-12-22 11:19:22 +01008009 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008010#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008011 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008012 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008013 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008014#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008015 {
8016 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008017 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008018 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008019 return status;
8020}
8021
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008022#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008023static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008024 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008025 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008026 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008027{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008028 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8029 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8030 step != PSA_PAKE_STEP_ZK_PROOF) {
8031 return PSA_ERROR_INVALID_ARGUMENT;
8032 }
8033
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008034 psa_jpake_computation_stage_t *computation_stage =
8035 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008036
David Horstmann5da95602023-06-08 15:37:12 +01008037 if (computation_stage->round != PSA_JPAKE_FIRST &&
8038 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008039 return PSA_ERROR_BAD_STATE;
8040 }
8041
David Horstmanne7f21e62023-05-12 18:17:21 +01008042 /* Check that the step we are given is the one we were expecting */
8043 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008044 return PSA_ERROR_BAD_STATE;
8045 }
8046
David Horstmanne7f21e62023-05-12 18:17:21 +01008047 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8048 computation_stage->inputs == 0 &&
8049 computation_stage->outputs == 0) {
8050 /* Start of the round, so function decides whether we are inputting
8051 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008052 computation_stage->io_mode = io_mode;
8053 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008054 /* Middle of the round so the mode we are in must match the function
8055 * called by the user */
8056 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008057 }
8058
8059 return PSA_SUCCESS;
8060}
8061
David Horstmanne7f21e62023-05-12 18:17:21 +01008062static psa_status_t psa_jpake_epilogue(
8063 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008064 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008065{
David Horstmanne7f21e62023-05-12 18:17:21 +01008066 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008067 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008068
David Horstmanne7f21e62023-05-12 18:17:21 +01008069 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8070 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008071 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008072 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008073 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008074 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008075 }
8076 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008077 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008078 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008079 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008080 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008081 }
8082 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008083 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8084 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008085 /* End of a round, move to the next round */
8086 stage->inputs = 0;
8087 stage->outputs = 0;
8088 stage->round++;
8089 }
8090 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008091 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008092 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008093 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008094 return PSA_SUCCESS;
8095}
David Horstmanne7f21e62023-05-12 18:17:21 +01008096
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008097#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008098
Neil Armstronga7d08c32022-06-01 18:21:20 +02008099psa_status_t psa_pake_output(
8100 psa_pake_operation_t *operation,
8101 psa_pake_step_t step,
8102 uint8_t *output,
8103 size_t output_size,
8104 size_t *output_length)
8105{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008106 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008107 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008108 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008109
8110 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008111 status = psa_pake_complete_inputs(operation);
8112 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008113 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008114 }
8115 }
8116
8117 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008118 status = PSA_ERROR_BAD_STATE;
8119 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008120 }
8121
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008122 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008123 status = PSA_ERROR_INVALID_ARGUMENT;
8124 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008125 }
8126
Przemek Stekiele12ed362022-12-21 12:54:46 +01008127 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008128#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008129 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008130 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008131 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008132 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008133 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008134 driver_step = convert_jpake_computation_stage_to_driver_step(
8135 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008136 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008137#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008138 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008139 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008140 status = PSA_ERROR_NOT_SUPPORTED;
8141 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008142 }
8143
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008144 status = psa_driver_wrapper_pake_output(operation, driver_step,
8145 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008146
8147 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008148 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008149 }
8150
8151 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008152#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008153 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008154 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008155 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008156 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008157 }
8158 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008159#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008160 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008161 status = PSA_ERROR_NOT_SUPPORTED;
8162 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008163 }
8164
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008165 return PSA_SUCCESS;
8166exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008167 psa_pake_abort(operation);
8168 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008169}
8170
8171psa_status_t psa_pake_input(
8172 psa_pake_operation_t *operation,
8173 psa_pake_step_t step,
8174 const uint8_t *input,
8175 size_t input_length)
8176{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008177 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008178 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008179 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8180 operation->primitive,
8181 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008182
8183 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008184 status = psa_pake_complete_inputs(operation);
8185 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008186 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008187 }
8188 }
8189
8190 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008191 status = PSA_ERROR_BAD_STATE;
8192 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008193 }
8194
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008195 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008196 status = PSA_ERROR_INVALID_ARGUMENT;
8197 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008198 }
8199
Przemek Stekiele12ed362022-12-21 12:54:46 +01008200 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008201#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008202 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008203 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008204 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008205 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008206 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008207 driver_step = convert_jpake_computation_stage_to_driver_step(
8208 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008209 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008210#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008211 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008212 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008213 status = PSA_ERROR_NOT_SUPPORTED;
8214 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008215 }
8216
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008217 status = psa_driver_wrapper_pake_input(operation, driver_step,
8218 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008219
8220 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008221 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008222 }
8223
8224 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008225#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008226 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008227 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008228 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008229 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008230 }
8231 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008232#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008233 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008234 status = PSA_ERROR_NOT_SUPPORTED;
8235 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008236 }
8237
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008238 return PSA_SUCCESS;
8239exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008240 psa_pake_abort(operation);
8241 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008242}
8243
8244psa_status_t psa_pake_get_implicit_key(
8245 psa_pake_operation_t *operation,
8246 psa_key_derivation_operation_t *output)
8247{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008248 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008249 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008250 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008251 size_t shared_key_len = 0;
8252
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008253 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008254 status = PSA_ERROR_BAD_STATE;
8255 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008256 }
8257
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008258#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008259 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008260 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008261 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008262 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008263 status = PSA_ERROR_BAD_STATE;
8264 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008265 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008266 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008267#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008268 {
8269 status = PSA_ERROR_NOT_SUPPORTED;
8270 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008271 }
8272
Przemek Stekiel0c781802022-11-29 14:53:13 +01008273 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8274 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008275 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008276 &shared_key_len);
8277
8278 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008279 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008280 }
8281
8282 status = psa_key_derivation_input_bytes(output,
8283 PSA_KEY_DERIVATION_INPUT_SECRET,
8284 shared_key,
8285 shared_key_len);
8286
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008287 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008288exit:
8289 abort_status = psa_pake_abort(operation);
8290 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008291}
8292
8293psa_status_t psa_pake_abort(
8294 psa_pake_operation_t *operation)
8295{
Przemek Stekield69dca92023-01-31 19:59:20 +01008296 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008297
Przemek Stekield69dca92023-01-31 19:59:20 +01008298 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008299 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008300 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008301
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008302 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8303 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008304 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008305 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008306 }
8307 if (operation->data.inputs.user != NULL) {
8308 mbedtls_free(operation->data.inputs.user);
8309 }
8310 if (operation->data.inputs.peer != NULL) {
8311 mbedtls_free(operation->data.inputs.peer);
8312 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008313 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008314 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008315
Przemek Stekield69dca92023-01-31 19:59:20 +01008316 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008317}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008318#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008319
Gilles Peskinee59236f2018-01-27 23:32:46 +01008320#endif /* MBEDTLS_PSA_CRYPTO_C */