blob: e6d3851ba80115eeeee6bb158e1784157e4333ba [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 Everett7ed542e2024-01-17 11:39:09 +00001074 /* Set the key slot containing the key description's state to
1075 * PENDING_DELETION. This stops new operations from registering
1076 * to read the slot. Current readers can safely continue to access
1077 * the key within the slot; the last registered reader will
1078 * automatically wipe the slot when they call psa_unregister_read().
1079 * If the key is persistent, we can now delete the copy of the key
1080 * from memory. If the key is opaque, we require the driver to
1081 * deal with the deletion. */
Ryan Everettc70ce572024-01-03 16:04:33 +00001082 slot->state = PSA_SLOT_PENDING_DELETION;
Ronald Cronf2911112020-10-29 17:51:10 +01001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001085 /* Refuse the destruction of a read-only key (which may or may not work
1086 * if we attempt it, depending on whether the key is merely read-only
1087 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001088 * Just do the best we can, which is to wipe the copy in memory
1089 * (done in this function's cleanup code). */
1090 overall_status = PSA_ERROR_NOT_PERMITTED;
1091 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001092 }
1093
Gilles Peskine354f7672019-07-12 23:46:38 +02001094#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1096 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001097 /* For a key in a secure element, we need to do three things:
1098 * remove the key file in internal storage, destroy the
1099 * key inside the secure element, and update the driver's
1100 * persistent data. Start a transaction that will encompass these
1101 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001103 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001105 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 status = psa_crypto_save_transaction();
1107 if (status != PSA_SUCCESS) {
1108 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001109 /* We should still try to destroy the key in the secure
1110 * element and the key metadata in storage. This is especially
1111 * important if the error is that the storage is full.
1112 * But how to do it exactly without risking an inconsistent
1113 * state after a reset?
1114 * https://github.com/ARMmbed/mbed-crypto/issues/215
1115 */
1116 overall_status = status;
1117 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001118 }
1119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 status = psa_destroy_se_key(driver,
1121 psa_key_slot_get_slot_number(slot));
1122 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001123 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001125 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001126#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1127
Darryl Greend49a4992018-06-18 17:27:26 +01001128#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001130 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001131 * The slot will still hold a copy of the key until the last reader
1132 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 status = psa_destroy_persistent_key(slot->attr.id);
1134 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001135 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001137
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001138 /* TODO: other slots may have a copy of the same key. We should
1139 * invalidate them.
1140 * https://github.com/ARMmbed/mbed-crypto/issues/214
1141 */
Darryl Greend49a4992018-06-18 17:27:26 +01001142 }
1143#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001144
Gilles Peskinefc762652019-07-22 19:30:34 +02001145#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (driver != NULL) {
1147 status = psa_save_se_persistent_data(driver);
1148 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001149 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 }
1151 status = psa_crypto_stop_transaction();
1152 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001153 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001155 }
1156#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1157
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001158exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001159 /* Unregister from reading the slot. If we are the last active reader
1160 * then this will wipe the slot. */
1161 status = psa_unregister_read(slot);
1162 /* Prioritize CORRUPTION_DETECTED from unregistering over
1163 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001165 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 }
1167 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001168}
1169
Valerio Settib2bcedb2023-07-10 11:24:00 +02001170#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001171 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001172static psa_status_t psa_get_rsa_public_exponent(
1173 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001175{
1176 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001177 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001178 uint8_t *buffer = NULL;
1179 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1183 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001184 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 }
1186 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001187 /* It's the default value, which is reported as an empty string,
1188 * so there's nothing to do. */
1189 goto exit;
1190 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 buflen = mbedtls_mpi_size(&mpi);
1193 buffer = mbedtls_calloc(1, buflen);
1194 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001195 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1196 goto exit;
1197 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1199 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001200 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001202 attributes->domain_parameters = buffer;
1203 attributes->domain_parameters_size = buflen;
1204
1205exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 mbedtls_mpi_free(&mpi);
1207 if (ret != 0) {
1208 mbedtls_free(buffer);
1209 }
1210 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001211}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001212#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001213 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001214
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001215/** Retrieve all the publicly-accessible attributes of a key.
1216 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001217psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1218 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001219{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001220 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001221 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001222 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1227 if (status != PSA_SUCCESS) {
1228 return status;
1229 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001230
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001231 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1233 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001234
1235#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1237 psa_set_key_slot_number(attributes,
1238 psa_key_slot_get_slot_number(slot));
1239 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001240#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001243#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1244 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001245 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001246 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001247 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Pengyu Lvf75893b2023-12-08 17:21:39 +08001248 /* TODO: This is a temporary situation where domain parameters are deprecated,
1249 * but we need it for namely generating an RSA key with a non-default exponent.
1250 * This would be improved after https://github.com/Mbed-TLS/mbedtls/issues/6494.
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001251 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001253 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001254
Ronald Cron90857082020-11-25 14:58:33 +01001255 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 slot->attr.type,
1257 slot->key.data,
1258 slot->key.bytes,
1259 &rsa);
1260 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001261 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 status = psa_get_rsa_public_exponent(rsa,
1265 attributes);
1266 mbedtls_rsa_free(rsa);
1267 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001268 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001269 break;
Pengyu Lve9efbc22023-12-08 16:59:08 +08001270#else
1271 case PSA_KEY_TYPE_RSA_KEY_PAIR:
1272 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1273 attributes->domain_parameters = NULL;
1274 attributes->domain_parameters_size = SIZE_MAX;
1275 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001276#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1277 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001278 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001279 default:
1280 /* Nothing else to do. */
1281 break;
1282 }
1283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (status != PSA_SUCCESS) {
1285 psa_reset_key_attributes(attributes);
1286 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001287
Ryan Everett1b70a072024-01-04 10:32:49 +00001288 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001291}
1292
Gilles Peskinec8000c02019-08-02 20:15:51 +02001293#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1294psa_status_t psa_get_key_slot_number(
1295 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001297{
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001299 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 return PSA_SUCCESS;
1301 } else {
1302 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001303 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001304}
1305#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1308 size_t key_buffer_size,
1309 uint8_t *data,
1310 size_t data_size,
1311 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001312{
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if (key_buffer_size > data_size) {
1314 return PSA_ERROR_BUFFER_TOO_SMALL;
1315 }
1316 memcpy(data, key_buffer, key_buffer_size);
1317 memset(data + key_buffer_size, 0,
1318 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001319 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001321}
1322
Ronald Cron7285cda2020-11-26 14:46:37 +01001323psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001324 const psa_key_attributes_t *attributes,
1325 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001327{
Ronald Crond18b5f82020-11-25 16:46:05 +01001328 psa_key_type_t type = attributes->core.type;
1329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if (key_type_is_raw_bytes(type) ||
1331 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001332 PSA_KEY_TYPE_IS_ECC(type) ||
1333 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 return psa_export_key_buffer_internal(
1335 key_buffer, key_buffer_size,
1336 data, data_size, data_length);
1337 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001338 /* This shouldn't happen in the reference implementation, but
1339 it is valid for a special-purpose implementation to omit
1340 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001342 }
1343}
1344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1346 uint8_t *data,
1347 size_t data_size,
1348 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001349{
1350 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1351 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1352 psa_key_slot_t *slot;
1353
Ronald Crone907e552021-01-18 13:22:38 +01001354 /* Reject a zero-length output buffer now, since this can never be a
1355 * valid key representation. This way we know that data must be a valid
1356 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 if (data_size == 0) {
1358 return PSA_ERROR_BUFFER_TOO_SMALL;
1359 }
Ronald Crone907e552021-01-18 13:22:38 +01001360
Ronald Cron9486f9d2020-11-26 13:36:23 +01001361 /* Set the key to empty now, so that even when there are errors, we always
1362 * set data_length to a value between 0 and data_size. On error, setting
1363 * the key to empty is a good choice because an empty key representation is
1364 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001365 *data_length = 0;
1366
Ronald Cron9486f9d2020-11-26 13:36:23 +01001367 /* Export requires the EXPORT flag. There is an exception for public keys,
1368 * which don't require any flag, but
1369 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1370 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1372 PSA_KEY_USAGE_EXPORT, 0);
1373 if (status != PSA_SUCCESS) {
1374 return status;
1375 }
mohammad160306e79202018-03-28 13:17:44 +03001376
Ronald Crond18b5f82020-11-25 16:46:05 +01001377 psa_key_attributes_t attributes = {
1378 .core = slot->attr
1379 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 status = psa_driver_wrapper_export_key(&attributes,
1381 slot->key.data, slot->key.bytes,
1382 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001383
Ryan Everett1b70a072024-01-04 10:32:49 +00001384 unlock_status = psa_unregister_read(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001387}
1388
Ronald Cron7285cda2020-11-26 14:46:37 +01001389psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001390 const psa_key_attributes_t *attributes,
1391 const uint8_t *key_buffer,
1392 size_t key_buffer_size,
1393 uint8_t *data,
1394 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001396{
Ronald Crond18b5f82020-11-25 16:46:05 +01001397 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001398
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001399 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001400 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1401 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001402 /* Exporting public -> public */
1403 return psa_export_key_buffer_internal(
1404 key_buffer, key_buffer_size,
1405 data, data_size, data_length);
1406 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001407#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001408 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1409 return mbedtls_psa_rsa_export_public_key(attributes,
1410 key_buffer,
1411 key_buffer_size,
1412 data,
1413 data_size,
1414 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001415#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001416 /* We don't know how to convert a private RSA key to public. */
1417 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001418#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001419 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001420 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001421#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001422 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1423 return mbedtls_psa_ecp_export_public_key(attributes,
1424 key_buffer,
1425 key_buffer_size,
1426 data,
1427 data_size,
1428 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001429#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001430 /* We don't know how to convert a private ECC key to public */
1431 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001432#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001433 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001434 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001435#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001436 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001437 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001438 key_buffer,
1439 key_buffer_size,
1440 data, data_size,
1441 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001442#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001443 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001444#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001445 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001447 (void) key_buffer;
1448 (void) key_buffer_size;
1449 (void) data;
1450 (void) data_size;
1451 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001453 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001454}
Ronald Crond18b5f82020-11-25 16:46:05 +01001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1457 uint8_t *data,
1458 size_t data_size,
1459 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001460{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001461 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001462 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001463 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001464 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001465
Ronald Crone907e552021-01-18 13:22:38 +01001466 /* Reject a zero-length output buffer now, since this can never be a
1467 * valid key representation. This way we know that data must be a valid
1468 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if (data_size == 0) {
1470 return PSA_ERROR_BUFFER_TOO_SMALL;
1471 }
Ronald Crone907e552021-01-18 13:22:38 +01001472
Darryl Greendd8fb772018-11-07 16:00:44 +00001473 /* Set the key to empty now, so that even when there are errors, we always
1474 * set data_length to a value between 0 and data_size. On error, setting
1475 * the key to empty is a good choice because an empty key representation is
1476 * unlikely to be accepted anywhere. */
1477 *data_length = 0;
1478
1479 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1481 if (status != PSA_SUCCESS) {
1482 return status;
1483 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1486 status = PSA_ERROR_INVALID_ARGUMENT;
1487 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001488 }
1489
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001490 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001491 .core = slot->attr
1492 };
Ronald Cron67227982020-11-26 15:16:05 +01001493 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001494 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001496
1497exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00001498 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001501}
1502
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001503MBEDTLS_STATIC_ASSERT(
1504 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1505 "One or more key attribute flag is listed as both external-only and dual-use")
1506MBEDTLS_STATIC_ASSERT(
1507 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1508 "One or more key attribute flag is listed as both internal-only and dual-use")
1509MBEDTLS_STATIC_ASSERT(
1510 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1511 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001512
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001513/** Validate that a key policy is internally well-formed.
1514 *
1515 * This function only rejects invalid policies. It does not validate the
1516 * consistency of the policy with respect to other attributes of the key
1517 * such as the key type.
1518 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001519static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001520{
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1522 PSA_KEY_USAGE_COPY |
1523 PSA_KEY_USAGE_ENCRYPT |
1524 PSA_KEY_USAGE_DECRYPT |
1525 PSA_KEY_USAGE_SIGN_MESSAGE |
1526 PSA_KEY_USAGE_VERIFY_MESSAGE |
1527 PSA_KEY_USAGE_SIGN_HASH |
1528 PSA_KEY_USAGE_VERIFY_HASH |
1529 PSA_KEY_USAGE_VERIFY_DERIVATION |
1530 PSA_KEY_USAGE_DERIVE)) != 0) {
1531 return PSA_ERROR_INVALID_ARGUMENT;
1532 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001535}
1536
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001537/** Validate the internal consistency of key attributes.
1538 *
1539 * This function only rejects invalid attribute values. If does not
1540 * validate the consistency of the attributes with any key data that may
1541 * be involved in the creation of the key.
1542 *
1543 * Call this function early in the key creation process.
1544 *
1545 * \param[in] attributes Key attributes for the new key.
1546 * \param[out] p_drv On any return, the driver for the key, if any.
1547 * NULL for a transparent key.
1548 *
1549 */
1550static psa_status_t psa_validate_key_attributes(
1551 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001553{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001554 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1556 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 status = psa_validate_key_location(lifetime, p_drv);
1559 if (status != PSA_SUCCESS) {
1560 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001561 }
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 status = psa_validate_key_persistence(lifetime);
1564 if (status != PSA_SUCCESS) {
1565 return status;
1566 }
1567
1568 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1569 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1570 return PSA_ERROR_INVALID_ARGUMENT;
1571 }
1572 } else {
1573 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1574 return PSA_ERROR_INVALID_ARGUMENT;
1575 }
1576 }
1577
1578 status = psa_validate_key_policy(&attributes->core.policy);
1579 if (status != PSA_SUCCESS) {
1580 return status;
1581 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001582
1583 /* Refuse to create overly large keys.
1584 * Note that this doesn't trigger on import if the attributes don't
1585 * explicitly specify a size (so psa_get_key_bits returns 0), so
1586 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1588 return PSA_ERROR_NOT_SUPPORTED;
1589 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001590
Gilles Peskine91e8c332019-08-02 19:19:39 +02001591 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1593 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1594 return PSA_ERROR_INVALID_ARGUMENT;
1595 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001598}
1599
Gilles Peskine4747d192019-04-17 15:05:45 +02001600/** Prepare a key slot to receive key material.
1601 *
1602 * This function allocates a key slot and sets its metadata.
1603 *
1604 * If this function fails, call psa_fail_key_creation().
1605 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001606 * This function is intended to be used as follows:
1607 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001608 * it with the specified attributes, and in case of a volatile key assign it
1609 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001610 * -# Populate the slot with the key material.
1611 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1612 * In case of failure at any step, stop the sequence and call
1613 * psa_fail_key_creation().
1614 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001615 * On success, the key slot's state is PSA_SLOT_FILLING.
1616 * It is the responsibility of the caller to change the slot's state to
1617 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001618 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001619 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001620 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001621 * \param[out] p_slot On success, a pointer to the prepared slot.
1622 * \param[out] p_drv On any return, the driver for the key, if any.
1623 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001624 *
1625 * \retval #PSA_SUCCESS
1626 * The key slot is ready to receive key material.
1627 * \return If this function fails, the key slot is an invalid state.
1628 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001629 */
1630static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001631 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001632 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001633 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001635{
1636 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001637 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001638 psa_key_slot_t *slot;
1639
Gilles Peskinedf179142019-07-15 22:02:14 +02001640 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001641 *p_drv = NULL;
1642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 status = psa_validate_key_attributes(attributes, p_drv);
1644 if (status != PSA_SUCCESS) {
1645 return status;
1646 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001647
Ryan Everettb69118e2024-01-02 15:54:32 +00001648 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 if (status != PSA_SUCCESS) {
1650 return status;
1651 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 slot = *p_slot;
1653
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001654 /* We're storing the declared bit-size of the key. It's up to each
1655 * creation mechanism to verify that this information is correct.
1656 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001657 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001658 * is optional (import, copy). In case of a volatile key, assign it the
1659 * volatile key identifier associated to the slot returned to contain its
1660 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001661
1662 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001664#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1665 slot->attr.id = volatile_key_id;
1666#else
1667 slot->attr.id.key_id = volatile_key_id;
1668#endif
1669 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001670
Gilles Peskine91e8c332019-08-02 19:19:39 +02001671 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001672 * external-only flags, query `attributes`. Thanks to the check
1673 * in psa_validate_key_attributes(), this leaves the dual-use
Ryan Everettb69118e2024-01-02 15:54:32 +00001674 * flags and any internal flag that psa_reserve_free_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001675 * may have set. */
1676 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001677
Gilles Peskinecbaff462019-07-12 23:46:04 +02001678#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001679 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001680 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001681 * create the key file in internal storage, create the
1682 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001683 * persistent data. This is done by starting a transaction that will
1684 * encompass these three actions.
1685 * For registering a volatile key, we just need to find an appropriate
1686 * slot number inside the SE. Since the key is designated volatile, creating
1687 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001688 /* The first thing to do is to find a slot number for the new key.
1689 * We save the slot number in persistent storage as part of the
1690 * transaction data. It will be needed to recover if the power
1691 * fails during the key creation process, to clean up on the secure
1692 * element side after restarting. Obtaining a slot number from the
1693 * secure element driver updates its persistent state, but we do not yet
1694 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001695 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001697 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1699 &slot_number);
1700 if (status != PSA_SUCCESS) {
1701 return status;
1702 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1705 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001706 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001707 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001708 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 status = psa_crypto_save_transaction();
1710 if (status != PSA_SUCCESS) {
1711 (void) psa_crypto_stop_transaction();
1712 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001713 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001714 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001715
1716 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001718 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001721 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001723 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001724#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001727}
Gilles Peskine4747d192019-04-17 15:05:45 +02001728
1729/** Finalize the creation of a key once its key material has been set.
1730 *
1731 * This entails writing the key to persistent storage.
1732 *
1733 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001734 * See the documentation of psa_start_key_creation() for the intended use
1735 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001736 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001737 * If the finalization succeeds, the function sets the key slot's state to
1738 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1739 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001740 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001741 * \param[in,out] slot Pointer to the slot with key material.
1742 * \param[in] driver The secure element driver for the key,
1743 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001744 * \param[out] key On success, identifier of the key. Note that the
1745 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001746 *
1747 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001748 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001749 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1750 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1751 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1752 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1753 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1754 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001755 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001756 * \return If this function fails, the key slot is an invalid state.
1757 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001758 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001759static psa_status_t psa_finish_key_creation(
1760 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001761 psa_se_drv_table_entry_t *driver,
1762 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001763{
1764 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001765 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001766 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001767
1768#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001770#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001772 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001773 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001775
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001776 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1777 sizeof(data.slot_number),
1778 "Slot number size does not match psa_se_key_data_storage_t");
1779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1781 status = psa_save_persistent_key(&slot->attr,
1782 (uint8_t *) &data,
1783 sizeof(data));
1784 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001785#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1786 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001787 /* Key material is saved in export representation in the slot, so
1788 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 status = psa_save_persistent_key(&slot->attr,
1790 slot->key.data,
1791 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001792 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001793 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001794#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1795
Gilles Peskinecbaff462019-07-12 23:46:04 +02001796#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001797 /* Finish the transaction for a key creation. This does not
1798 * happen when registering an existing key. Detect this case
1799 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001800 * creation of a persistent key in a secure element requires a transaction,
1801 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if (driver != NULL &&
1803 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1804 status = psa_save_se_persistent_data(driver);
1805 if (status != PSA_SUCCESS) {
1806 psa_destroy_persistent_key(slot->attr.id);
1807 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001808 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001810 }
1811#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001814 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001815 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1816 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001818 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001820 }
Ronald Cron50972942020-11-14 11:28:25 +01001821
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001823}
1824
1825/** Abort the creation of a key.
1826 *
1827 * You may call this function after calling psa_start_key_creation(),
1828 * or after psa_finish_key_creation() fails. In other circumstances, this
1829 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001830 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001831 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001832 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001833 * \param[in,out] slot Pointer to the slot with key material.
1834 * \param[in] driver The secure element driver for the key,
1835 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001836 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001837static void psa_fail_key_creation(psa_key_slot_t *slot,
1838 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001839{
Gilles Peskine011e4282019-06-26 18:34:38 +02001840 (void) driver;
1841
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001843 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001845
1846#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001847 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001848 * element, and the failure happened later (when saving metadata
1849 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001850 * element.
1851 * https://github.com/ARMmbed/mbed-crypto/issues/217
1852 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001853
Gilles Peskined7729582019-08-05 15:55:54 +02001854 /* Abort the ongoing transaction if any (there may not be one if
1855 * the creation process failed before starting one, or if the
1856 * key creation is a registration of a key in a secure element).
1857 * Earlier functions must already have done what it takes to undo any
1858 * partial creation. All that's left is to update the transaction data
1859 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001861#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1862
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001864}
1865
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001866/** Validate optional attributes during key creation.
1867 *
1868 * Some key attributes are optional during key creation. If they are
1869 * specified in the attributes structure, check that they are consistent
1870 * with the data in the slot.
1871 *
1872 * This function should be called near the end of key creation, after
1873 * the slot in memory is fully populated but before saving persistent data.
1874 */
1875static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001876 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001878{
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 if (attributes->core.type != 0) {
1880 if (attributes->core.type != slot->attr.type) {
1881 return PSA_ERROR_INVALID_ARGUMENT;
1882 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001883 }
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001886#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1887 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1889 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001890 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001891 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001893
Ronald Cron90857082020-11-25 14:58:33 +01001894 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 slot->attr.type,
1896 slot->key.data,
1897 slot->key.bytes,
1898 &rsa);
1899 if (status != PSA_SUCCESS) {
1900 return status;
1901 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 mbedtls_mpi_init(&actual);
1904 mbedtls_mpi_init(&required);
1905 ret = mbedtls_rsa_export(rsa,
1906 NULL, NULL, NULL, NULL, &actual);
1907 mbedtls_rsa_free(rsa);
1908 mbedtls_free(rsa);
1909 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001910 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 }
1912 ret = mbedtls_mpi_read_binary(&required,
1913 attributes->domain_parameters,
1914 attributes->domain_parameters_size);
1915 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001916 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 }
1918 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001919 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 }
1921rsa_exit:
1922 mbedtls_mpi_free(&actual);
1923 mbedtls_mpi_free(&required);
1924 if (ret != 0) {
1925 return mbedtls_to_psa_error(ret);
1926 }
1927 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02001928#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
1929 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001930 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001931 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001933 }
1934 }
1935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if (attributes->core.bits != 0) {
1937 if (attributes->core.bits != slot->attr.bits) {
1938 return PSA_ERROR_INVALID_ARGUMENT;
1939 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001940 }
1941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001943}
1944
Gilles Peskine449bd832023-01-11 14:50:10 +01001945psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1946 const uint8_t *data,
1947 size_t data_length,
1948 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001949{
1950 psa_status_t status;
1951 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001952 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001953 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301954 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001955
Ronald Cron81709fc2020-11-14 12:10:32 +01001956 *key = MBEDTLS_SVC_KEY_ID_INIT;
1957
Gilles Peskine0f84d622019-09-12 19:03:13 +02001958 /* Reject zero-length symmetric keys (including raw data key objects).
1959 * This also rejects any key which might be encoded as an empty string,
1960 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 if (data_length == 0) {
1962 return PSA_ERROR_INVALID_ARGUMENT;
1963 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001964
Archana449608b2021-09-08 15:36:05 +05301965 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 if (data_length > SIZE_MAX / 8) {
1967 return PSA_ERROR_NOT_SUPPORTED;
1968 }
Archana6ed4bda2021-08-04 10:47:15 +05301969
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1971 &slot, &driver);
1972 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001973 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001975
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001976 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301977 * storage ( thus not in the case of importing a key in a secure element
1978 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1979 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if (slot->key.data == NULL) {
1981 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301982 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 attributes, data, data_length, &storage_size);
1984 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301985 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 }
Archanad8a83dc2021-06-14 10:04:16 +05301987 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 status = psa_allocate_buffer_to_slot(slot, storage_size);
1989 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001990 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001992 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001993
1994 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 status = psa_driver_wrapper_import_key(attributes,
1996 data, data_length,
1997 slot->key.data,
1998 slot->key.bytes,
1999 &slot->key.bytes, &bits);
2000 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002001 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002003
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002005 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002007 status = PSA_ERROR_INVALID_ARGUMENT;
2008 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002009 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002010
Archana6ed4bda2021-08-04 10:47:15 +05302011 /* Enforce a size limit, and in particular ensure that the bit
2012 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302014 status = PSA_ERROR_NOT_SUPPORTED;
2015 goto exit;
2016 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 status = psa_validate_optional_attributes(slot, attributes);
2018 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002019 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002023exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (status != PSA_SUCCESS) {
2025 psa_fail_key_creation(slot, driver);
2026 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002029}
2030
Gilles Peskined7729582019-08-05 15:55:54 +02002031#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2032psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002034{
2035 psa_status_t status;
2036 psa_key_slot_t *slot = NULL;
2037 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002038 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002039
2040 /* Leaving attributes unspecified is not currently supported.
2041 * It could make sense to query the key type and size from the
2042 * secure element, but not all secure elements support this
2043 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2045 return PSA_ERROR_NOT_SUPPORTED;
2046 }
2047 if (psa_get_key_bits(attributes) == 0) {
2048 return PSA_ERROR_NOT_SUPPORTED;
2049 }
Gilles Peskined7729582019-08-05 15:55:54 +02002050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2052 &slot, &driver);
2053 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002054 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 }
Gilles Peskined7729582019-08-05 15:55:54 +02002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002058
2059exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 if (status != PSA_SUCCESS) {
2061 psa_fail_key_creation(slot, driver);
2062 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002063
Gilles Peskined7729582019-08-05 15:55:54 +02002064 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 psa_close_key(key);
2066 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002067}
2068#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2071 const psa_key_attributes_t *specified_attributes,
2072 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002073{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002074 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002075 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002076 psa_key_slot_t *source_slot = NULL;
2077 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002078 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002079 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302080 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002081
Ronald Cron81709fc2020-11-14 12:10:32 +01002082 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2083
Archana8a180362021-07-05 02:18:48 +05302084 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2086 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002087 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 status = psa_validate_optional_attributes(source_slot,
2091 specified_attributes);
2092 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002093 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002095
Archana9d17bf42021-09-10 06:22:44 +05302096 /* The target key type and number of bits have been validated by
2097 * psa_validate_optional_attributes() to be either equal to zero or
2098 * equal to the ones of the source key. So it is safe to inherit
2099 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302100 * */
Archana9d17bf42021-09-10 06:22:44 +05302101 actual_attributes.core.bits = source_slot->attr.bits;
2102 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302103
2104
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 status = psa_restrict_key_policy(source_slot->attr.type,
2106 &actual_attributes.core.policy,
2107 &source_slot->attr.policy);
2108 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002109 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2113 &target_slot, &driver);
2114 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 }
2117 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2118 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302119 /*
Archana9d17bf42021-09-10 06:22:44 +05302120 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302121 * the source key would need to be exported as plaintext and re-imported
2122 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302123 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302124 * appropriate API invocations from the application, if needed.
2125 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002126 status = PSA_ERROR_NOT_SUPPORTED;
2127 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002128 }
Archana8a180362021-07-05 02:18:48 +05302129 /*
2130 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302131 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302132 * - For opaque keys this translates to an invocation of the drivers'
2133 * copy_key entry point through the dispatch layer.
2134 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2136 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2137 &storage_size);
2138 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302139 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 }
Archana374fe5b2021-09-08 15:50:28 +05302141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2143 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302144 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 }
Archana374fe5b2021-09-08 15:50:28 +05302146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 status = psa_driver_wrapper_copy_key(&actual_attributes,
2148 source_slot->key.data,
2149 source_slot->key.bytes,
2150 target_slot->key.data,
2151 target_slot->key.bytes,
2152 &target_slot->key.bytes);
2153 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302154 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 }
2156 } else {
2157 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302158 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 source_slot->key.bytes);
2160 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 }
Archana8a180362021-07-05 02:18:48 +05302163 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002165exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (status != PSA_SUCCESS) {
2167 psa_fail_key_creation(target_slot, driver);
2168 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002169
Ryan Everett1b70a072024-01-04 10:32:49 +00002170 unlock_status = psa_unregister_read(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002173}
2174
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002175
2176
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002177/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002178/* Message digests */
2179/****************************************************************/
2180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002182{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002183 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (operation->id == 0) {
2185 return PSA_SUCCESS;
2186 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002189 operation->id = 0;
2190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002192}
2193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2195 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002196{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002197 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002198
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002199 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002201 status = PSA_ERROR_BAD_STATE;
2202 goto exit;
2203 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002206 status = PSA_ERROR_INVALID_ARGUMENT;
2207 goto exit;
2208 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002209
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002210 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2211 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002215
2216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if (status != PSA_SUCCESS) {
2218 psa_hash_abort(operation);
2219 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002220
2221 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002222}
2223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2225 const uint8_t *input,
2226 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002227{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002228 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002231 status = PSA_ERROR_BAD_STATE;
2232 goto exit;
2233 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002234
Steven Cooremanc8288352021-03-04 14:02:19 +01002235 /* Don't require hash implementations to behave correctly on a
2236 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 if (input_length == 0) {
2238 return PSA_SUCCESS;
2239 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002242
2243exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 if (status != PSA_SUCCESS) {
2245 psa_hash_abort(operation);
2246 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249}
2250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2252 uint8_t *hash,
2253 size_t hash_size,
2254 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002255{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002256 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 if (operation->id == 0) {
2258 return PSA_ERROR_BAD_STATE;
2259 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260
Steven Cooreman1e582352021-02-18 17:24:37 +01002261 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 operation, hash, hash_size, hash_length);
2263 psa_hash_abort(operation);
2264 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002265}
2266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2268 const uint8_t *hash,
2269 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002270{
Ronald Cron69a63422021-10-18 09:47:58 +02002271 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002273 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 operation,
2275 actual_hash, sizeof(actual_hash),
2276 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002279 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002283 status = PSA_ERROR_INVALID_SIGNATURE;
2284 goto exit;
2285 }
2286
Dave Rodgman78701152023-08-29 14:20:18 +01002287 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002288 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002290
2291exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2293 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002294 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002298}
2299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300psa_status_t psa_hash_compute(psa_algorithm_t alg,
2301 const uint8_t *input, size_t input_length,
2302 uint8_t *hash, size_t hash_size,
2303 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002304{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002305 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 if (!PSA_ALG_IS_HASH(alg)) {
2307 return PSA_ERROR_INVALID_ARGUMENT;
2308 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002309
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2311 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002312}
2313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314psa_status_t psa_hash_compare(psa_algorithm_t alg,
2315 const uint8_t *input, size_t input_length,
2316 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002317{
Ronald Cron69a63422021-10-18 09:47:58 +02002318 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002319 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 if (!PSA_ALG_IS_HASH(alg)) {
2322 return PSA_ERROR_INVALID_ARGUMENT;
2323 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002324
Steven Cooreman1e582352021-02-18 17:24:37 +01002325 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 alg, input, input_length,
2327 actual_hash, sizeof(actual_hash),
2328 &actual_hash_length);
2329 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002330 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 }
2332 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002333 status = PSA_ERROR_INVALID_SIGNATURE;
2334 goto exit;
2335 }
Dave Rodgman78701152023-08-29 14:20:18 +01002336 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002337 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002339
2340exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2342 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002343}
2344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2346 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002347{
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 if (source_operation->id == 0 ||
2349 target_operation->id != 0) {
2350 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002351 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2354 target_operation);
2355 if (status != PSA_SUCCESS) {
2356 psa_hash_abort(target_operation);
2357 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002360}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002361
2362
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002363/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002364/* MAC */
2365/****************************************************************/
2366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002368{
Steven Cooremane6804192021-03-19 18:28:56 +01002369 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 if (operation->id == 0) {
2371 return PSA_SUCCESS;
2372 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002375 operation->mac_size = 0;
2376 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002377 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002380}
2381
Ronald Cron2dff3b22021-06-17 16:33:22 +02002382static psa_status_t psa_mac_finalize_alg_and_key_validation(
2383 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002384 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002386{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002387 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 psa_key_type_t key_type = psa_get_key_type(attributes);
2389 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 if (!PSA_ALG_IS_MAC(alg)) {
2392 return PSA_ERROR_INVALID_ARGUMENT;
2393 }
Steven Cooremane6804192021-03-19 18:28:56 +01002394
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002395 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 status = psa_mac_key_can_do(alg, key_type);
2397 if (status != PSA_SUCCESS) {
2398 return status;
2399 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002400
Steven Cooremand1a68f12021-05-10 11:13:41 +02002401 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002405 /* A very short MAC is too short for security since it can be
2406 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2407 * so we make this our minimum, even though 32 bits is still
2408 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002410 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002411
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2413 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002414 /* It's impossible to "truncate" to a larger length than the full length
2415 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002417 }
2418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002420 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2421 * that is disabled in the compile-time configuration. The result can
2422 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2423 * configuration into account. In this case, force a return of
2424 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2425 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2426 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2427 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2428 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002430 }
2431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002433}
2434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2436 mbedtls_svc_key_id_t key,
2437 psa_algorithm_t alg,
2438 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002439{
2440 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2441 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002442 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002443 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002444
2445 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002447 status = PSA_ERROR_BAD_STATE;
2448 goto exit;
2449 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002450
2451 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 key,
2453 &slot,
2454 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2455 alg);
2456 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002457 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002460 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002461 .core = slot->attr
2462 };
2463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2465 &operation->mac_size);
2466 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002467 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002469
2470 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002471 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 if (is_sign) {
2473 status = psa_driver_wrapper_mac_sign_setup(operation,
2474 &attributes,
2475 slot->key.data,
2476 slot->key.bytes,
2477 alg);
2478 } else {
2479 status = psa_driver_wrapper_mac_verify_setup(operation,
2480 &attributes,
2481 slot->key.data,
2482 slot->key.bytes,
2483 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002484 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002485
Gilles Peskinefbfac682018-07-08 20:51:54 +02002486exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 if (status != PSA_SUCCESS) {
2488 psa_mac_abort(operation);
2489 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002490
Ryan Everett1b70a072024-01-04 10:32:49 +00002491 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494}
2495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2497 mbedtls_svc_key_id_t key,
2498 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002499{
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002501}
2502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2504 mbedtls_svc_key_id_t key,
2505 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002506{
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002508}
2509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2511 const uint8_t *input,
2512 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513{
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 if (operation->id == 0) {
2515 return PSA_ERROR_BAD_STATE;
2516 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002517
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002518 /* Don't require hash implementations to behave correctly on a
2519 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 if (input_length == 0) {
2521 return PSA_SUCCESS;
2522 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2525 input, input_length);
2526 if (status != PSA_SUCCESS) {
2527 psa_mac_abort(operation);
2528 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531}
2532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2534 uint8_t *mac,
2535 size_t mac_size,
2536 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002537{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002538 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2539 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002542 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002543 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002544 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002547 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002548 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002549 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002550
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002551 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2552 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002554 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002555 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002556 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002559 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002560 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002561 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 status = psa_driver_wrapper_mac_sign_finish(operation,
2564 mac, operation->mac_size,
2565 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002566
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002567exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002568 /* In case of success, set the potential excess room in the output buffer
2569 * to an invalid value, to avoid potentially leaking a longer MAC.
2570 * In case of error, set the output length and content to a safe default,
2571 * such that in case the caller misses an error check, the output would be
2572 * an unachievable MAC.
2573 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002575 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002576 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002577 }
mohammad16036df908f2018-04-02 08:34:15 -07002578
Paul Elliottdc42ca82023-02-24 18:11:59 +00002579 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002584}
2585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2587 const uint8_t *mac,
2588 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002589{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002590 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2591 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002594 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002595 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002596 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002599 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002600 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002601 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002604 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002605 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002606 }
2607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 status = psa_driver_wrapper_mac_verify_finish(operation,
2609 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002610
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002611exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002615}
2616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2618 psa_algorithm_t alg,
2619 const uint8_t *input,
2620 size_t input_length,
2621 uint8_t *mac,
2622 size_t mac_size,
2623 size_t *mac_length,
2624 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002625{
2626 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002627 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2628 psa_key_slot_t *slot;
2629 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002630 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002631
Ronald Cron51131b52021-06-17 17:17:20 +02002632 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 key,
2634 &slot,
2635 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2636 alg);
2637 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002638 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002640
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002641 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002642 .core = slot->attr
2643 };
2644
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2646 &operation_mac_size);
2647 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002648 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002650
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002652 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002653 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002654 }
2655
2656 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 &attributes,
2658 slot->key.data, slot->key.bytes,
2659 alg,
2660 input, input_length,
2661 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002662
2663exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002664 /* In case of success, set the potential excess room in the output buffer
2665 * to an invalid value, to avoid potentially leaking a longer MAC.
2666 * In case of error, set the output length and content to a safe default,
2667 * such that in case the caller misses an error check, the output would be
2668 * an unachievable MAC.
2669 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002671 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002672 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002673 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002674
2675 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002676
Ryan Everett1b70a072024-01-04 10:32:49 +00002677 unlock_status = psa_unregister_read(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002680}
2681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002683 psa_algorithm_t alg,
2684 const uint8_t *input,
2685 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 uint8_t *mac,
2687 size_t mac_size,
2688 size_t *mac_length)
2689{
2690 return psa_mac_compute_internal(key, alg,
2691 input, input_length,
2692 mac, mac_size, mac_length, 1);
2693}
2694
2695psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2696 psa_algorithm_t alg,
2697 const uint8_t *input,
2698 size_t input_length,
2699 const uint8_t *mac,
2700 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002701{
2702 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002703 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2704 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 status = psa_mac_compute_internal(key, alg,
2707 input, input_length,
2708 actual_mac, sizeof(actual_mac),
2709 &actual_mac_length, 0);
2710 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002711 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002715 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002716 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002717 }
Dave Rodgman78701152023-08-29 14:20:18 +01002718 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002719 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002720 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002721 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002722
2723exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002727}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002728
Gilles Peskinee59236f2018-01-27 23:32:46 +01002729/****************************************************************/
2730/* Asymmetric cryptography */
2731/****************************************************************/
2732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2734 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002735{
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 if (input_is_message) {
2737 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2738 return PSA_ERROR_INVALID_ARGUMENT;
2739 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2742 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2743 return PSA_ERROR_INVALID_ARGUMENT;
2744 }
2745 }
2746 } else {
2747 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2748 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002749 }
2750 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002753}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2756 int input_is_message,
2757 psa_algorithm_t alg,
2758 const uint8_t *input,
2759 size_t input_length,
2760 uint8_t *signature,
2761 size_t signature_size,
2762 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002763{
2764 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2765 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2766 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002767 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002768
2769 *signature_length = 0;
2770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 status = psa_sign_verify_check_alg(input_is_message, alg);
2772 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002773 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002775
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002776 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002777 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002778 * buffer can in principle be empty since it doesn't actually have
2779 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 if (signature_size == 0) {
2781 return PSA_ERROR_BUFFER_TOO_SMALL;
2782 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002783
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002784 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 key, &slot,
2786 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2787 PSA_KEY_USAGE_SIGN_HASH,
2788 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002791 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002793
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002795 status = PSA_ERROR_INVALID_ARGUMENT;
2796 goto exit;
2797 }
2798
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002799 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002801 };
2802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002804 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002805 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002806 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 signature, signature_size, signature_length);
2808 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002809
2810 status = psa_driver_wrapper_sign_hash(
2811 &attributes, slot->key.data, slot->key.bytes,
2812 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002814 }
2815
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002816
2817exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002818 psa_wipe_tag_output_buffer(signature, status, signature_size,
2819 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002820
Ryan Everett1b70a072024-01-04 10:32:49 +00002821 unlock_status = psa_unregister_read(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002822
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002824}
2825
Gilles Peskine449bd832023-01-11 14:50:10 +01002826static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2827 int input_is_message,
2828 psa_algorithm_t alg,
2829 const uint8_t *input,
2830 size_t input_length,
2831 const uint8_t *signature,
2832 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002833{
2834 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2835 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2836 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002837
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 status = psa_sign_verify_check_alg(input_is_message, alg);
2839 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002840 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002842
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002843 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 key, &slot,
2845 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2846 PSA_KEY_USAGE_VERIFY_HASH,
2847 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002848
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 if (status != PSA_SUCCESS) {
2850 return status;
2851 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002852
2853 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002855 };
2856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002858 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002859 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002860 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 signature, signature_length);
2862 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002863 status = psa_driver_wrapper_verify_hash(
2864 &attributes, slot->key.data, slot->key.bytes,
2865 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002867 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002868
Ryan Everett1b70a072024-01-04 10:32:49 +00002869 unlock_status = psa_unregister_read(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002872
2873}
2874
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002875psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002876 const psa_key_attributes_t *attributes,
2877 const uint8_t *key_buffer,
2878 size_t key_buffer_size,
2879 psa_algorithm_t alg,
2880 const uint8_t *input,
2881 size_t input_length,
2882 uint8_t *signature,
2883 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002885{
2886 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002889 size_t hash_length;
2890 uint8_t hash[PSA_HASH_MAX_SIZE];
2891
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002892 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 PSA_ALG_SIGN_GET_HASH(alg),
2894 input, input_length,
2895 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002896
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002898 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002900
2901 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 attributes, key_buffer, key_buffer_size,
2903 alg, hash, hash_length,
2904 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002905 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002908}
2909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2911 psa_algorithm_t alg,
2912 const uint8_t *input,
2913 size_t input_length,
2914 uint8_t *signature,
2915 size_t signature_size,
2916 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002917{
2918 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002919 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002921}
2922
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002923psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002924 const psa_key_attributes_t *attributes,
2925 const uint8_t *key_buffer,
2926 size_t key_buffer_size,
2927 psa_algorithm_t alg,
2928 const uint8_t *input,
2929 size_t input_length,
2930 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002932{
2933 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002936 size_t hash_length;
2937 uint8_t hash[PSA_HASH_MAX_SIZE];
2938
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002939 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 PSA_ALG_SIGN_GET_HASH(alg),
2941 input, input_length,
2942 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002943
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002945 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002947
2948 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 attributes, key_buffer, key_buffer_size,
2950 alg, hash, hash_length,
2951 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002952 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002955}
2956
Gilles Peskine449bd832023-01-11 14:50:10 +01002957psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2958 psa_algorithm_t alg,
2959 const uint8_t *input,
2960 size_t input_length,
2961 const uint8_t *signature,
2962 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002963{
2964 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002965 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002967}
2968
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002969psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002970 const psa_key_attributes_t *attributes,
2971 const uint8_t *key_buffer, size_t key_buffer_size,
2972 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002974{
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2976 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2977 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002978#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2980 return mbedtls_psa_rsa_sign_hash(
2981 attributes,
2982 key_buffer, key_buffer_size,
2983 alg, hash, hash_length,
2984 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002985#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2986 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 } else {
2988 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002989 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2991 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002992#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2994 return mbedtls_psa_ecdsa_sign_hash(
2995 attributes,
2996 key_buffer, key_buffer_size,
2997 alg, hash, hash_length,
2998 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002999#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3000 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 } else {
3002 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003003 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003004 }
Ronald Cron4501c982021-03-19 20:09:32 +01003005
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 (void) key_buffer;
3007 (void) key_buffer_size;
3008 (void) hash;
3009 (void) hash_length;
3010 (void) signature;
3011 (void) signature_size;
3012 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003013
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003015}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003016
Gilles Peskine449bd832023-01-11 14:50:10 +01003017psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3018 psa_algorithm_t alg,
3019 const uint8_t *hash,
3020 size_t hash_length,
3021 uint8_t *signature,
3022 size_t signature_size,
3023 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003024{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003025 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003026 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003028}
3029
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003030psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003031 const psa_key_attributes_t *attributes,
3032 const uint8_t *key_buffer, size_t key_buffer_size,
3033 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003035{
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3037 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3038 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003039#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3041 return mbedtls_psa_rsa_verify_hash(
3042 attributes,
3043 key_buffer, key_buffer_size,
3044 alg, hash, hash_length,
3045 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003046#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3047 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 } else {
3049 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003050 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3052 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003053#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3055 return mbedtls_psa_ecdsa_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_ECDSA) ||
3061 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 } else {
3063 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003064 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003065 }
Ronald Cron4501c982021-03-19 20:09:32 +01003066
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 (void) key_buffer;
3068 (void) key_buffer_size;
3069 (void) hash;
3070 (void) hash_length;
3071 (void) signature;
3072 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003075}
3076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3078 psa_algorithm_t alg,
3079 const uint8_t *hash,
3080 size_t hash_length,
3081 const uint8_t *signature,
3082 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003083{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003084 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003085 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003087}
3088
Gilles Peskine449bd832023-01-11 14:50:10 +01003089psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3090 psa_algorithm_t alg,
3091 const uint8_t *input,
3092 size_t input_length,
3093 const uint8_t *salt,
3094 size_t salt_length,
3095 uint8_t *output,
3096 size_t output_size,
3097 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003098{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003100 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003101 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003102 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003103
Darryl Green5cc689a2018-07-24 15:34:10 +01003104 (void) input;
3105 (void) input_length;
3106 (void) salt;
3107 (void) output;
3108 (void) output_size;
3109
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003110 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003111
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3113 return PSA_ERROR_INVALID_ARGUMENT;
3114 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003115
Valerio Setti5bb454a2024-01-15 10:43:16 +01003116 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3118 if (status != PSA_SUCCESS) {
3119 return status;
3120 }
3121 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3122 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003123 status = PSA_ERROR_INVALID_ARGUMENT;
3124 goto exit;
3125 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003126
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003127 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003129 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003130
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003131 status = psa_driver_wrapper_asymmetric_encrypt(
3132 &attributes, slot->key.data, slot->key.bytes,
3133 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003135exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00003136 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003137
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003139}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003140
Gilles Peskine449bd832023-01-11 14:50:10 +01003141psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3142 psa_algorithm_t alg,
3143 const uint8_t *input,
3144 size_t input_length,
3145 const uint8_t *salt,
3146 size_t salt_length,
3147 uint8_t *output,
3148 size_t output_size,
3149 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003150{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003151 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003152 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003153 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003154 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003155
Darryl Green5cc689a2018-07-24 15:34:10 +01003156 (void) input;
3157 (void) input_length;
3158 (void) salt;
3159 (void) output;
3160 (void) output_size;
3161
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003162 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003163
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3165 return PSA_ERROR_INVALID_ARGUMENT;
3166 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003167
Valerio Setti5bb454a2024-01-15 10:43:16 +01003168 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3170 if (status != PSA_SUCCESS) {
3171 return status;
3172 }
3173 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003174 status = PSA_ERROR_INVALID_ARGUMENT;
3175 goto exit;
3176 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003177
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003178 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003180 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003181
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003182 status = psa_driver_wrapper_asymmetric_decrypt(
3183 &attributes, slot->key.data, slot->key.bytes,
3184 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003186
3187exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00003188 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003189
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003191}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003192
Paul Elliott9fe12f62022-11-30 19:16:02 +00003193/****************************************************************/
3194/* Asymmetric interruptible cryptography */
3195/****************************************************************/
3196
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003197static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3198
Paul Elliott9fe12f62022-11-30 19:16:02 +00003199void psa_interruptible_set_max_ops(uint32_t max_ops)
3200{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003201 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003202}
3203
3204uint32_t psa_interruptible_get_max_ops(void)
3205{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003206 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003207}
3208
Paul Elliott9fe12f62022-11-30 19:16:02 +00003209uint32_t psa_sign_hash_get_num_ops(
3210 const psa_sign_hash_interruptible_operation_t *operation)
3211{
Paul Elliott296ede92022-12-15 17:00:30 +00003212 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003213}
3214
3215uint32_t psa_verify_hash_get_num_ops(
3216 const psa_verify_hash_interruptible_operation_t *operation)
3217{
Paul Elliott296ede92022-12-15 17:00:30 +00003218 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003219}
3220
Paul Elliott59ad9452022-12-18 15:09:02 +00003221static psa_status_t psa_sign_hash_abort_internal(
3222 psa_sign_hash_interruptible_operation_t *operation)
3223{
3224 if (operation->id == 0) {
3225 /* The object has (apparently) been initialized but it is not (yet)
3226 * in use. It's ok to call abort on such an object, and there's
3227 * nothing to do. */
3228 return PSA_SUCCESS;
3229 }
3230
3231 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3232
3233 status = psa_driver_wrapper_sign_hash_abort(operation);
3234
3235 operation->id = 0;
3236
Paul Elliotte6145dc2023-02-07 12:51:21 +00003237 /* Do not clear either the error_occurred or num_ops elements here as they
3238 * only want to be cleared by the application calling abort, not by abort
3239 * being called at completion of an operation. */
3240
Paul Elliott59ad9452022-12-18 15:09:02 +00003241 return status;
3242}
3243
Paul Elliott9fe12f62022-11-30 19:16:02 +00003244psa_status_t psa_sign_hash_start(
3245 psa_sign_hash_interruptible_operation_t *operation,
3246 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3247 const uint8_t *hash, size_t hash_length)
3248{
3249 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3250 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3251 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003252 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003253
Paul Elliottc9774412023-02-06 15:14:07 +00003254 /* Check that start has not been previously called, or operation has not
3255 * previously errored. */
3256 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003257 return PSA_ERROR_BAD_STATE;
3258 }
3259
Paul Elliott9fe12f62022-11-30 19:16:02 +00003260 status = psa_sign_verify_check_alg(0, alg);
3261 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003262 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003263 return status;
3264 }
3265
3266 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3267 PSA_KEY_USAGE_SIGN_HASH,
3268 alg);
3269
3270 if (status != PSA_SUCCESS) {
3271 goto exit;
3272 }
3273
3274 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3275 status = PSA_ERROR_INVALID_ARGUMENT;
3276 goto exit;
3277 }
3278
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003279 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003280 .core = slot->attr
3281 };
3282
Paul Elliott296ede92022-12-15 17:00:30 +00003283 /* Ensure ops count gets reset, in case of operation re-use. */
3284 operation->num_ops = 0;
3285
Paul Elliott9fe12f62022-11-30 19:16:02 +00003286 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3287 slot->key.data,
3288 slot->key.bytes, alg,
3289 hash, hash_length);
3290exit:
3291
3292 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003293 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003294 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003295 }
3296
Ryan Everett1b70a072024-01-04 10:32:49 +00003297 unlock_status = psa_unregister_read(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003298
Paul Elliottc9774412023-02-06 15:14:07 +00003299 if (unlock_status != PSA_SUCCESS) {
3300 operation->error_occurred = 1;
3301 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003302
Paul Elliottc9774412023-02-06 15:14:07 +00003303 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003304}
3305
3306
3307psa_status_t psa_sign_hash_complete(
3308 psa_sign_hash_interruptible_operation_t *operation,
3309 uint8_t *signature, size_t signature_size,
3310 size_t *signature_length)
3311{
3312 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3313
3314 *signature_length = 0;
3315
Paul Elliottc9774412023-02-06 15:14:07 +00003316 /* Check that start has been called first, and that operation has not
3317 * previously errored. */
3318 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003319 status = PSA_ERROR_BAD_STATE;
3320 goto exit;
3321 }
3322
Paul Elliott096abc42023-02-03 18:33:23 +00003323 /* Immediately reject a zero-length signature buffer. This guarantees that
3324 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003325 if (signature_size == 0) {
3326 status = PSA_ERROR_BUFFER_TOO_SMALL;
3327 goto exit;
3328 }
3329
3330 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3331 signature_size,
3332 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003333
Paul Elliott296ede92022-12-15 17:00:30 +00003334 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003335 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003336
Paul Elliottebe225c2023-02-10 14:32:53 +00003337exit:
3338
Paul Elliott59200a22023-02-21 15:07:40 +00003339 psa_wipe_tag_output_buffer(signature, status, signature_size,
3340 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003341
Paul Elliott53bb3122023-02-10 14:22:22 +00003342 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003343 if (status != PSA_SUCCESS) {
3344 operation->error_occurred = 1;
3345 }
3346
Paul Elliott59ad9452022-12-18 15:09:02 +00003347 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003348 }
3349
3350 return status;
3351}
3352
3353psa_status_t psa_sign_hash_abort(
3354 psa_sign_hash_interruptible_operation_t *operation)
3355{
Paul Elliott59ad9452022-12-18 15:09:02 +00003356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3357
3358 status = psa_sign_hash_abort_internal(operation);
3359
3360 /* We clear the number of ops done here, so that it is not cleared when
3361 * the operation fails or succeeds, only on manual abort. */
3362 operation->num_ops = 0;
3363
Paul Elliottc9774412023-02-06 15:14:07 +00003364 /* Likewise, failure state. */
3365 operation->error_occurred = 0;
3366
Paul Elliott59ad9452022-12-18 15:09:02 +00003367 return status;
3368}
3369
3370static psa_status_t psa_verify_hash_abort_internal(
3371 psa_verify_hash_interruptible_operation_t *operation)
3372{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003373 if (operation->id == 0) {
3374 /* The object has (apparently) been initialized but it is not (yet)
3375 * in use. It's ok to call abort on such an object, and there's
3376 * nothing to do. */
3377 return PSA_SUCCESS;
3378 }
3379
Paul Elliott59ad9452022-12-18 15:09:02 +00003380 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3381
3382 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003383
3384 operation->id = 0;
3385
Paul Elliotte6145dc2023-02-07 12:51:21 +00003386 /* Do not clear either the error_occurred or num_ops elements here as they
3387 * only want to be cleared by the application calling abort, not by abort
3388 * being called at completion of an operation. */
3389
Paul Elliott59ad9452022-12-18 15:09:02 +00003390 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003391}
3392
3393psa_status_t psa_verify_hash_start(
3394 psa_verify_hash_interruptible_operation_t *operation,
3395 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3396 const uint8_t *hash, size_t hash_length,
3397 const uint8_t *signature, size_t signature_length)
3398{
3399 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3400 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3401 psa_key_slot_t *slot;
3402
Paul Elliottc9774412023-02-06 15:14:07 +00003403 /* Check that start has not been previously called, or operation has not
3404 * previously errored. */
3405 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003406 return PSA_ERROR_BAD_STATE;
3407 }
3408
3409 status = psa_sign_verify_check_alg(0, alg);
3410 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003411 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003412 return status;
3413 }
3414
3415 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3416 PSA_KEY_USAGE_VERIFY_HASH,
3417 alg);
3418
3419 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003420 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003421 return status;
3422 }
3423
3424 psa_key_attributes_t attributes = {
3425 .core = slot->attr
3426 };
3427
Paul Elliott296ede92022-12-15 17:00:30 +00003428 /* Ensure ops count gets reset, in case of operation re-use. */
3429 operation->num_ops = 0;
3430
Paul Elliott9fe12f62022-11-30 19:16:02 +00003431 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3432 slot->key.data,
3433 slot->key.bytes,
3434 alg, hash, hash_length,
3435 signature, signature_length);
3436
3437 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003438 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003439 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003440 }
3441
Ryan Everett1b70a072024-01-04 10:32:49 +00003442 unlock_status = psa_unregister_read(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003443
Paul Elliottc9774412023-02-06 15:14:07 +00003444 if (unlock_status != PSA_SUCCESS) {
3445 operation->error_occurred = 1;
3446 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003447
Paul Elliottc9774412023-02-06 15:14:07 +00003448 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003449}
3450
3451psa_status_t psa_verify_hash_complete(
3452 psa_verify_hash_interruptible_operation_t *operation)
3453{
3454 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3455
Paul Elliottc9774412023-02-06 15:14:07 +00003456 /* Check that start has been called first, and that operation has not
3457 * previously errored. */
3458 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003459 status = PSA_ERROR_BAD_STATE;
3460 goto exit;
3461 }
3462
3463 status = psa_driver_wrapper_verify_hash_complete(operation);
3464
Paul Elliott296ede92022-12-15 17:00:30 +00003465 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003466 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003467 operation);
3468
Paul Elliottebe225c2023-02-10 14:32:53 +00003469exit:
3470
Paul Elliott9fe12f62022-11-30 19:16:02 +00003471 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003472 if (status != PSA_SUCCESS) {
3473 operation->error_occurred = 1;
3474 }
3475
Paul Elliott59ad9452022-12-18 15:09:02 +00003476 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003477 }
3478
3479 return status;
3480}
3481
3482psa_status_t psa_verify_hash_abort(
3483 psa_verify_hash_interruptible_operation_t *operation)
3484{
Paul Elliott59ad9452022-12-18 15:09:02 +00003485 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003486
Paul Elliott59ad9452022-12-18 15:09:02 +00003487 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003488
Paul Elliott59ad9452022-12-18 15:09:02 +00003489 /* We clear the number of ops done here, so that it is not cleared when
3490 * the operation fails or succeeds, only on manual abort. */
3491 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003492
Paul Elliottc9774412023-02-06 15:14:07 +00003493 /* Likewise, failure state. */
3494 operation->error_occurred = 0;
3495
Paul Elliott59ad9452022-12-18 15:09:02 +00003496 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003497}
3498
Paul Elliott588f8ed2022-12-02 18:10:26 +00003499/****************************************************************/
3500/* Asymmetric interruptible cryptography internal */
3501/* implementations */
3502/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003503
Paul Elliott588f8ed2022-12-02 18:10:26 +00003504void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3505{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003506
Paul Elliott588f8ed2022-12-02 18:10:26 +00003507#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3508 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3509 defined(MBEDTLS_ECP_RESTARTABLE)
3510
3511 /* Internal implementation uses zero to indicate infinite number max ops,
3512 * therefore avoid this value, and set to minimum possible. */
3513 if (max_ops == 0) {
3514 max_ops = 1;
3515 }
3516
Paul Elliott588f8ed2022-12-02 18:10:26 +00003517 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003518#else
3519 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003520#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3521 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3522 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3523}
3524
Paul Elliott588f8ed2022-12-02 18:10:26 +00003525uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003526 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003527{
3528#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3529 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3530 defined(MBEDTLS_ECP_RESTARTABLE)
3531
Paul Elliott93d9ca82023-02-15 18:14:21 +00003532 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003533#else
3534 (void) operation;
3535 return 0;
3536#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3537 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3538 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3539}
3540
3541uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003542 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003543{
3544 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3545 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3546 defined(MBEDTLS_ECP_RESTARTABLE)
3547
Paul Elliott93d9ca82023-02-15 18:14:21 +00003548 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003549#else
3550 (void) operation;
3551 return 0;
3552#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3553 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3554 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3555}
3556
3557psa_status_t mbedtls_psa_sign_hash_start(
3558 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3559 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3560 size_t key_buffer_size, psa_algorithm_t alg,
3561 const uint8_t *hash, size_t hash_length)
3562{
3563 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003564 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003565
Paul Elliott068fe072023-01-16 13:59:15 +00003566 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3567 return PSA_ERROR_NOT_SUPPORTED;
3568 }
3569
3570 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003571 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003572 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003573
3574#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003575 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3576 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003577
Paul Elliotta1c94092023-02-15 16:38:04 +00003578 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3579
Paul Elliott93d9ca82023-02-15 18:14:21 +00003580 /* Ensure num_ops is zero'ed in case of context re-use. */
3581 operation->num_ops = 0;
3582
Paul Elliott068fe072023-01-16 13:59:15 +00003583 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3584 attributes->core.bits,
3585 key_buffer,
3586 key_buffer_size,
3587 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003588
Paul Elliott068fe072023-01-16 13:59:15 +00003589 if (status != PSA_SUCCESS) {
3590 return status;
3591 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003592
Paul Elliott1bc59df2023-02-05 13:41:57 +00003593 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003594 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003595
Paul Elliott068fe072023-01-16 13:59:15 +00003596 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003597 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003598 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003599
Paul Elliott0290a762023-02-09 14:30:24 +00003600 /* We only need to store the same length of hash as the private key size
3601 * here, it would be truncated by the internal implementation anyway. */
3602 required_hash_length = (hash_length < operation->coordinate_bytes ?
3603 hash_length : operation->coordinate_bytes);
3604
Paul Elliottba70ad42023-02-15 18:23:53 +00003605 if (required_hash_length > sizeof(operation->hash)) {
3606 /* Shouldn't happen, but better safe than sorry. */
3607 return PSA_ERROR_CORRUPTION_DETECTED;
3608 }
3609
Paul Elliott0290a762023-02-09 14:30:24 +00003610 memcpy(operation->hash, hash, required_hash_length);
3611 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003612
3613 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003614
3615#else
Paul Elliott068fe072023-01-16 13:59:15 +00003616 (void) operation;
3617 (void) key_buffer;
3618 (void) key_buffer_size;
3619 (void) alg;
3620 (void) hash;
3621 (void) hash_length;
3622 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003623 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003624
Paul Elliott068fe072023-01-16 13:59:15 +00003625 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003626#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3627 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3628 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003629}
3630
3631psa_status_t mbedtls_psa_sign_hash_complete(
3632 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3633 uint8_t *signature, size_t signature_size,
3634 size_t *signature_length)
3635{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003636#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3637 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3638 defined(MBEDTLS_ECP_RESTARTABLE)
3639
Paul Elliotta1c94092023-02-15 16:38:04 +00003640 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003641 mbedtls_mpi r;
3642 mbedtls_mpi s;
3643
Paul Elliott46845252023-02-03 14:59:11 +00003644 mbedtls_mpi_init(&r);
3645 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003647 /* Ensure max_ops is set to the current value (or default). */
3648 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3649
Paul Elliotta1c94092023-02-15 16:38:04 +00003650 if (signature_size < 2 * operation->coordinate_bytes) {
3651 status = PSA_ERROR_BUFFER_TOO_SMALL;
3652 goto exit;
3653 }
3654
Paul Elliott588f8ed2022-12-02 18:10:26 +00003655 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003656
Paul Elliott588f8ed2022-12-02 18:10:26 +00003657#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3658 status = mbedtls_to_psa_error(
3659 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003660 &r,
3661 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003662 &operation->ctx->d,
3663 operation->hash,
3664 operation->hash_length,
3665 operation->md_alg,
3666 mbedtls_psa_get_random,
3667 MBEDTLS_PSA_RANDOM_STATE,
3668 &operation->restart_ctx));
3669#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003670 status = PSA_ERROR_NOT_SUPPORTED;
3671 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003672#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3673 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003674 status = mbedtls_to_psa_error(
3675 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003676 &r,
3677 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003678 &operation->ctx->d,
3679 operation->hash,
3680 operation->hash_length,
3681 mbedtls_psa_get_random,
3682 MBEDTLS_PSA_RANDOM_STATE,
3683 mbedtls_psa_get_random,
3684 MBEDTLS_PSA_RANDOM_STATE,
3685 &operation->restart_ctx));
3686 }
3687
Paul Elliottf8e5b562023-02-19 18:43:45 +00003688 /* Hide the fact that the restart context only holds a delta of number of
3689 * ops done during the last operation, not an absolute value. */
3690 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3691
Paul Elliott724bd252023-02-08 12:35:08 +00003692 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003693 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003694 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003695 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003696 operation->coordinate_bytes)
3697 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003698
3699 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003700 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003701 }
3702
3703 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003704 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003705 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003706 operation->coordinate_bytes,
3707 operation->coordinate_bytes)
3708 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003709
3710 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003711 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712 }
3713
Paul Elliott1bc59df2023-02-05 13:41:57 +00003714 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715
Paul Elliott724bd252023-02-08 12:35:08 +00003716 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003717 }
Paul Elliott724bd252023-02-08 12:35:08 +00003718
3719exit:
3720
3721 mbedtls_mpi_free(&r);
3722 mbedtls_mpi_free(&s);
3723 return status;
3724
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725 #else
3726
3727 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003728 (void) signature;
3729 (void) signature_size;
3730 (void) signature_length;
3731
3732 return PSA_ERROR_NOT_SUPPORTED;
3733
3734#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3735 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3736 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3737}
3738
3739psa_status_t mbedtls_psa_sign_hash_abort(
3740 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3741{
3742
3743#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3744 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3745 defined(MBEDTLS_ECP_RESTARTABLE)
3746
3747 if (operation->ctx) {
3748 mbedtls_ecdsa_free(operation->ctx);
3749 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003750 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003751 }
3752
3753 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3754
Paul Elliott93d9ca82023-02-15 18:14:21 +00003755 operation->num_ops = 0;
3756
Paul Elliott588f8ed2022-12-02 18:10:26 +00003757 return PSA_SUCCESS;
3758
3759#else
3760
3761 (void) operation;
3762
3763 return PSA_ERROR_NOT_SUPPORTED;
3764
3765#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3766 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3767 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3768}
3769
3770psa_status_t mbedtls_psa_verify_hash_start(
3771 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3772 const psa_key_attributes_t *attributes,
3773 const uint8_t *key_buffer, size_t key_buffer_size,
3774 psa_algorithm_t alg,
3775 const uint8_t *hash, size_t hash_length,
3776 const uint8_t *signature, size_t signature_length)
3777{
3778 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003779 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003780 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003781
Paul Elliott068fe072023-01-16 13:59:15 +00003782 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3783 return PSA_ERROR_NOT_SUPPORTED;
3784 }
3785
3786 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003787 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003788 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003789
3790#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003791 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3792 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003793
Paul Elliotta1c94092023-02-15 16:38:04 +00003794 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3795 mbedtls_mpi_init(&operation->r);
3796 mbedtls_mpi_init(&operation->s);
3797
Paul Elliott93d9ca82023-02-15 18:14:21 +00003798 /* Ensure num_ops is zero'ed in case of context re-use. */
3799 operation->num_ops = 0;
3800
Paul Elliott068fe072023-01-16 13:59:15 +00003801 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3802 attributes->core.bits,
3803 key_buffer,
3804 key_buffer_size,
3805 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003806
Paul Elliott068fe072023-01-16 13:59:15 +00003807 if (status != PSA_SUCCESS) {
3808 return status;
3809 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003810
Paul Elliottc569fc22023-02-10 13:02:54 +00003811 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003812
Paul Elliott1bc59df2023-02-05 13:41:57 +00003813 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003814 return PSA_ERROR_INVALID_SIGNATURE;
3815 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003816
Paul Elliott068fe072023-01-16 13:59:15 +00003817 status = mbedtls_to_psa_error(
3818 mbedtls_mpi_read_binary(&operation->r,
3819 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003820 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003821
Paul Elliott068fe072023-01-16 13:59:15 +00003822 if (status != PSA_SUCCESS) {
3823 return status;
3824 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003825
Paul Elliott068fe072023-01-16 13:59:15 +00003826 status = mbedtls_to_psa_error(
3827 mbedtls_mpi_read_binary(&operation->s,
3828 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003829 coordinate_bytes,
3830 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003831
Paul Elliott068fe072023-01-16 13:59:15 +00003832 if (status != PSA_SUCCESS) {
3833 return status;
3834 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003835
Paul Elliott2c9843f2023-02-15 17:32:42 +00003836 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003837
Paul Elliott2c9843f2023-02-15 17:32:42 +00003838 if (status != PSA_SUCCESS) {
3839 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003840 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003841
Paul Elliott0290a762023-02-09 14:30:24 +00003842 /* We only need to store the same length of hash as the private key size
3843 * here, it would be truncated by the internal implementation anyway. */
3844 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3845 coordinate_bytes);
3846
Paul Elliottba70ad42023-02-15 18:23:53 +00003847 if (required_hash_length > sizeof(operation->hash)) {
3848 /* Shouldn't happen, but better safe than sorry. */
3849 return PSA_ERROR_CORRUPTION_DETECTED;
3850 }
3851
Paul Elliott0290a762023-02-09 14:30:24 +00003852 memcpy(operation->hash, hash, required_hash_length);
3853 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003854
3855 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003856#else
Paul Elliott068fe072023-01-16 13:59:15 +00003857 (void) operation;
3858 (void) key_buffer;
3859 (void) key_buffer_size;
3860 (void) alg;
3861 (void) hash;
3862 (void) hash_length;
3863 (void) signature;
3864 (void) signature_length;
3865 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003866 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003867 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003868
Paul Elliott068fe072023-01-16 13:59:15 +00003869 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003870#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3871 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3872 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003873}
3874
3875psa_status_t mbedtls_psa_verify_hash_complete(
3876 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3877{
3878
3879#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3880 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3881 defined(MBEDTLS_ECP_RESTARTABLE)
3882
Paul Elliottf8e5b562023-02-19 18:43:45 +00003883 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3884
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003885 /* Ensure max_ops is set to the current value (or default). */
3886 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3887
Paul Elliottf8e5b562023-02-19 18:43:45 +00003888 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003889 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3890 operation->hash,
3891 operation->hash_length,
3892 &operation->ctx->Q,
3893 &operation->r,
3894 &operation->s,
3895 &operation->restart_ctx));
3896
Paul Elliottf8e5b562023-02-19 18:43:45 +00003897 /* Hide the fact that the restart context only holds a delta of number of
3898 * ops done during the last operation, not an absolute value. */
3899 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3900
3901 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003902#else
3903 (void) operation;
3904
3905 return PSA_ERROR_NOT_SUPPORTED;
3906
3907#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3908 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3909 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3910}
3911
3912psa_status_t mbedtls_psa_verify_hash_abort(
3913 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3914{
3915
3916#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3917 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3918 defined(MBEDTLS_ECP_RESTARTABLE)
3919
3920 if (operation->ctx) {
3921 mbedtls_ecdsa_free(operation->ctx);
3922 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003923 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003924 }
3925
3926 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3927
Paul Elliott93d9ca82023-02-15 18:14:21 +00003928 operation->num_ops = 0;
3929
Paul Elliott588f8ed2022-12-02 18:10:26 +00003930 mbedtls_mpi_free(&operation->r);
3931 mbedtls_mpi_free(&operation->s);
3932
3933 return PSA_SUCCESS;
3934
3935#else
3936 (void) operation;
3937
3938 return PSA_ERROR_NOT_SUPPORTED;
3939
3940#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3941 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3942 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3943}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003944
mohammad1603503973b2018-03-12 15:59:30 +02003945/****************************************************************/
3946/* Symmetric cryptography */
3947/****************************************************************/
3948
Gilles Peskine449bd832023-01-11 14:50:10 +01003949static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3950 mbedtls_svc_key_id_t key,
3951 psa_algorithm_t alg,
3952 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003953{
3954 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3955 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003956 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003957 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3958 PSA_KEY_USAGE_ENCRYPT :
3959 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003960 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02003961
3962 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003963 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003964 status = PSA_ERROR_BAD_STATE;
3965 goto exit;
3966 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003967
Gilles Peskine449bd832023-01-11 14:50:10 +01003968 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003969 status = PSA_ERROR_INVALID_ARGUMENT;
3970 goto exit;
3971 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003972
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3974 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003975 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003977
Ronald Cron49fafa92021-03-10 08:34:23 +01003978 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003979 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003980 * so we only set it (in the driver wrapper) after resources have been
3981 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003982 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003984 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003986 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 }
3988 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003989
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003990 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003991 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003992 };
3993
Ronald Cronab99ac22020-10-01 16:38:28 +02003994 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 if (cipher_operation == MBEDTLS_ENCRYPT) {
3996 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3997 &attributes,
3998 slot->key.data,
3999 slot->key.bytes,
4000 alg);
4001 } else {
4002 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4003 &attributes,
4004 slot->key.data,
4005 slot->key.bytes,
4006 alg);
4007 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004008
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004009exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 if (status != PSA_SUCCESS) {
4011 psa_cipher_abort(operation);
4012 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004013
Ryan Everett1b70a072024-01-04 10:32:49 +00004014 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004015
Gilles Peskine449bd832023-01-11 14:50:10 +01004016 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004017}
4018
Gilles Peskine449bd832023-01-11 14:50:10 +01004019psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4020 mbedtls_svc_key_id_t key,
4021 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004022{
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004024}
4025
Gilles Peskine449bd832023-01-11 14:50:10 +01004026psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4027 mbedtls_svc_key_id_t key,
4028 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004029{
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004031}
4032
Gilles Peskine449bd832023-01-11 14:50:10 +01004033psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4034 uint8_t *iv,
4035 size_t iv_size,
4036 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004037{
Ronald Cron6d051732020-10-01 14:10:20 +02004038 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004039 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004040 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004041
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004043 status = PSA_ERROR_BAD_STATE;
4044 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004045 }
4046
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004048 status = PSA_ERROR_BAD_STATE;
4049 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004050 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004051
Ronald Cron2fb90522021-07-05 12:31:44 +02004052 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004054 status = PSA_ERROR_BUFFER_TOO_SMALL;
4055 goto exit;
4056 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004057
Gilles Peskine449bd832023-01-11 14:50:10 +01004058 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004059 status = PSA_ERROR_GENERIC_ERROR;
4060 goto exit;
4061 }
4062
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 status = psa_generate_random(local_iv, default_iv_length);
4064 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004065 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004066 }
Ronald Cron5618a392021-03-26 09:52:26 +01004067
Gilles Peskine449bd832023-01-11 14:50:10 +01004068 status = psa_driver_wrapper_cipher_set_iv(operation,
4069 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004070
4071exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 if (status == PSA_SUCCESS) {
4073 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004074 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004075 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004077 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004079 }
Ronald Cron6d051732020-10-01 14:10:20 +02004080
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004082}
4083
Gilles Peskine449bd832023-01-11 14:50:10 +01004084psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4085 const uint8_t *iv,
4086 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004087{
Ronald Cron6d051732020-10-01 14:10:20 +02004088 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004089
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004091 status = PSA_ERROR_BAD_STATE;
4092 goto exit;
4093 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004094
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004096 status = PSA_ERROR_BAD_STATE;
4097 goto exit;
4098 }
Ronald Crona0d68172021-03-26 10:15:08 +01004099
Gilles Peskine449bd832023-01-11 14:50:10 +01004100 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004101 status = PSA_ERROR_INVALID_ARGUMENT;
4102 goto exit;
4103 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004104
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 status = psa_driver_wrapper_cipher_set_iv(operation,
4106 iv,
4107 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004108
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004109exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004111 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 } else {
4113 psa_cipher_abort(operation);
4114 }
4115 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004116}
4117
Gilles Peskine449bd832023-01-11 14:50:10 +01004118psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4119 const uint8_t *input,
4120 size_t input_length,
4121 uint8_t *output,
4122 size_t output_size,
4123 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004124{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004125 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004126
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004128 status = PSA_ERROR_BAD_STATE;
4129 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004130 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004131
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004133 status = PSA_ERROR_BAD_STATE;
4134 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004135 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004136
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 status = psa_driver_wrapper_cipher_update(operation,
4138 input,
4139 input_length,
4140 output,
4141 output_size,
4142 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004143
4144exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 if (status != PSA_SUCCESS) {
4146 psa_cipher_abort(operation);
4147 }
Ronald Cron6d051732020-10-01 14:10:20 +02004148
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004150}
4151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4153 uint8_t *output,
4154 size_t output_size,
4155 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004156{
David Saadab4ecc272019-02-14 13:48:10 +02004157 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004160 status = PSA_ERROR_BAD_STATE;
4161 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004162 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004165 status = PSA_ERROR_BAD_STATE;
4166 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004167 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004168
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 status = psa_driver_wrapper_cipher_finish(operation,
4170 output,
4171 output_size,
4172 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004173
4174exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 if (status == PSA_SUCCESS) {
4176 return psa_cipher_abort(operation);
4177 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004178 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004180
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004182 }
mohammad1603503973b2018-03-12 15:59:30 +02004183}
4184
Gilles Peskine449bd832023-01-11 14:50:10 +01004185psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004186{
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004188 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004189 * in use. It's ok to call abort on such an object, and there's
4190 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004192 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004195
Ronald Cron49fafa92021-03-10 08:34:23 +01004196 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004197 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004198 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004201}
4202
Gilles Peskine449bd832023-01-11 14:50:10 +01004203psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4204 psa_algorithm_t alg,
4205 const uint8_t *input,
4206 size_t input_length,
4207 uint8_t *output,
4208 size_t output_size,
4209 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004210{
4211 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004212 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004213 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004214 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4215 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004216 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004217
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004219 status = PSA_ERROR_INVALID_ARGUMENT;
4220 goto exit;
4221 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4224 PSA_KEY_USAGE_ENCRYPT,
4225 alg);
4226 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004227 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004229
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004230 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004232 };
4233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4235 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004236 status = PSA_ERROR_GENERIC_ERROR;
4237 goto exit;
4238 }
4239
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 if (default_iv_length > 0) {
4241 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004242 status = PSA_ERROR_BUFFER_TOO_SMALL;
4243 goto exit;
4244 }
4245
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 status = psa_generate_random(local_iv, default_iv_length);
4247 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004248 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004250 }
4251
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004252 status = psa_driver_wrapper_cipher_encrypt(
4253 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004254 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004255 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004257
4258exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004259 unlock_status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004261 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004262 }
Ronald Cron23919522021-07-08 19:00:07 +02004263
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 if (status == PSA_SUCCESS) {
4265 if (default_iv_length > 0) {
4266 memcpy(output, local_iv, default_iv_length);
4267 }
4268 *output_length += default_iv_length;
4269 } else {
4270 *output_length = 0;
4271 }
4272
4273 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004274}
4275
Gilles Peskine449bd832023-01-11 14:50:10 +01004276psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4277 psa_algorithm_t alg,
4278 const uint8_t *input,
4279 size_t input_length,
4280 uint8_t *output,
4281 size_t output_size,
4282 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004283{
4284 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004285 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004286 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004287 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004288
Gilles Peskine449bd832023-01-11 14:50:10 +01004289 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004290 status = PSA_ERROR_INVALID_ARGUMENT;
4291 goto exit;
4292 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004293
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4295 PSA_KEY_USAGE_DECRYPT,
4296 alg);
4297 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004298 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004300
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004301 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004303 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004304
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4306 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004307 status = PSA_ERROR_INVALID_ARGUMENT;
4308 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004310 status = PSA_ERROR_INVALID_ARGUMENT;
4311 goto exit;
4312 }
4313
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004314 status = psa_driver_wrapper_cipher_decrypt(
4315 &attributes, slot->key.data, slot->key.bytes,
4316 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004318
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004319exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004320 unlock_status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004322 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004324
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004326 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 }
Ronald Cron23919522021-07-08 19:00:07 +02004328
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004330}
4331
4332
mohammad16035955c982018-04-26 00:53:03 +03004333/****************************************************************/
4334/* AEAD */
4335/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004336
Paul Elliottbaff51c2021-09-28 17:44:45 +01004337/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004338static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004339{
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004341}
4342
4343/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004344static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4345 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004346{
Gilles Peskine449bd832023-01-11 14:50:10 +01004347 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004348
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004350#if defined(PSA_WANT_ALG_GCM)
4351 case PSA_ALG_GCM:
4352 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 * arbitrarily large nonces. Please note that we do not generally
4354 * recommend the usage of nonces of greater length than
4355 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4356 * size, which can then lead to collisions if you encrypt a very
4357 * large number of messages.*/
4358 if (nonce_length != 0) {
4359 return PSA_SUCCESS;
4360 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004361 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004362#endif /* PSA_WANT_ALG_GCM */
4363#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004364 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 if (nonce_length >= 7 && nonce_length <= 13) {
4366 return PSA_SUCCESS;
4367 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004368 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004369#endif /* PSA_WANT_ALG_CCM */
4370#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004371 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 if (nonce_length == 12) {
4373 return PSA_SUCCESS;
4374 } else if (nonce_length == 8) {
4375 return PSA_ERROR_NOT_SUPPORTED;
4376 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004377 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004378#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004379 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004380 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004382 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004383
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004385}
4386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004388{
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4390 return PSA_ERROR_INVALID_ARGUMENT;
4391 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004392
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004394}
4395
Gilles Peskine449bd832023-01-11 14:50:10 +01004396psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4397 psa_algorithm_t alg,
4398 const uint8_t *nonce,
4399 size_t nonce_length,
4400 const uint8_t *additional_data,
4401 size_t additional_data_length,
4402 const uint8_t *plaintext,
4403 size_t plaintext_length,
4404 uint8_t *ciphertext,
4405 size_t ciphertext_size,
4406 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004407{
Ronald Cron215633c2021-03-16 17:15:37 +01004408 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4409 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004410
mohammad1603f08a5502018-06-03 15:05:47 +03004411 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004412
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 status = psa_aead_check_algorithm(alg);
4414 if (status != PSA_SUCCESS) {
4415 return status;
4416 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004417
Ronald Cron9a986162021-03-26 12:40:07 +01004418 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4420 if (status != PSA_SUCCESS) {
4421 return status;
4422 }
mohammad16035955c982018-04-26 00:53:03 +03004423
Ronald Cron215633c2021-03-16 17:15:37 +01004424 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004426 };
mohammad16035955c982018-04-26 00:53:03 +03004427
Gilles Peskine449bd832023-01-11 14:50:10 +01004428 status = psa_aead_check_nonce_length(alg, nonce_length);
4429 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004430 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004432
Ronald Cronde822812021-03-17 16:08:20 +01004433 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004434 &attributes, slot->key.data, slot->key.bytes,
4435 alg,
4436 nonce, nonce_length,
4437 additional_data, additional_data_length,
4438 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4442 memset(ciphertext, 0, ciphertext_size);
4443 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004444
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004445exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004446 psa_unregister_read(slot);
mohammad16035955c982018-04-26 00:53:03 +03004447
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004449}
4450
Gilles Peskine449bd832023-01-11 14:50:10 +01004451psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4452 psa_algorithm_t alg,
4453 const uint8_t *nonce,
4454 size_t nonce_length,
4455 const uint8_t *additional_data,
4456 size_t additional_data_length,
4457 const uint8_t *ciphertext,
4458 size_t ciphertext_length,
4459 uint8_t *plaintext,
4460 size_t plaintext_size,
4461 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004462{
Ronald Cron215633c2021-03-16 17:15:37 +01004463 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4464 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004465
Gilles Peskineee652a32018-06-01 19:23:52 +02004466 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 status = psa_aead_check_algorithm(alg);
4469 if (status != PSA_SUCCESS) {
4470 return status;
4471 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004472
Ronald Cron9a986162021-03-26 12:40:07 +01004473 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4475 if (status != PSA_SUCCESS) {
4476 return status;
4477 }
mohammad16035955c982018-04-26 00:53:03 +03004478
Ronald Cron215633c2021-03-16 17:15:37 +01004479 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004481 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004482
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 status = psa_aead_check_nonce_length(alg, nonce_length);
4484 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004485 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004487
Ronald Cronde822812021-03-17 16:08:20 +01004488 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004489 &attributes, slot->key.data, slot->key.bytes,
4490 alg,
4491 nonce, nonce_length,
4492 additional_data, additional_data_length,
4493 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004495
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 if (status != PSA_SUCCESS && plaintext_size != 0) {
4497 memset(plaintext, 0, plaintext_size);
4498 }
mohammad160360a64d02018-06-03 17:20:42 +03004499
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004500exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004501 psa_unregister_read(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004502
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 return status;
mohammad16035955c982018-04-26 00:53:03 +03004504}
4505
Gilles Peskine449bd832023-01-11 14:50:10 +01004506static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4507{
4508 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004511#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004513 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4515 return PSA_ERROR_INVALID_ARGUMENT;
4516 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004517 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004518#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004519
Przemek Stekiel86679c72022-10-06 17:06:56 +02004520#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004522 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4524 return PSA_ERROR_INVALID_ARGUMENT;
4525 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004526 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004527#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004528
Przemek Stekiel86679c72022-10-06 17:06:56 +02004529#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004531 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 if (tag_len != 16) {
4533 return PSA_ERROR_INVALID_ARGUMENT;
4534 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004535 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004536#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004537
4538 default:
4539 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004541 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004543}
4544
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004545/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004546static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4547 int is_encrypt,
4548 mbedtls_svc_key_id_t key,
4549 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004550{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004551 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4552 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4553 psa_key_slot_t *slot = NULL;
4554 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004555 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004556
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 status = psa_aead_check_algorithm(alg);
4558 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004559 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004561
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004563 status = PSA_ERROR_BAD_STATE;
4564 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004565 }
4566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 if (operation->nonce_set || operation->lengths_set ||
4568 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004569 status = PSA_ERROR_BAD_STATE;
4570 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004571 }
4572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004574 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004576 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4580 alg);
4581 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004582 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004584
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004585 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004586 .core = slot->attr
4587 };
4588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004590 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004592
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 if (is_encrypt) {
4594 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4595 &attributes,
4596 slot->key.data,
4597 slot->key.bytes,
4598 alg);
4599 } else {
4600 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4601 &attributes,
4602 slot->key.data,
4603 slot->key.bytes,
4604 alg);
4605 }
4606 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004607 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004609
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004611
4612exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004613 unlock_status = psa_unregister_read(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004614
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004616 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004618 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 } else {
4620 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004621 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004624}
4625
Paul Elliott302ff6b2021-04-20 18:10:30 +01004626/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004627psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4628 mbedtls_svc_key_id_t key,
4629 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004630{
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004632}
4633
4634/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004635psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4636 mbedtls_svc_key_id_t key,
4637 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004638{
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004640}
4641
4642/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004643psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4644 uint8_t *nonce,
4645 size_t nonce_size,
4646 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004647{
4648 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004649 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004650 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004651
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004652 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004653
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004655 status = PSA_ERROR_BAD_STATE;
4656 goto exit;
4657 }
4658
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004660 status = PSA_ERROR_BAD_STATE;
4661 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004662 }
4663
Paul Elliotte193ea82021-10-01 13:00:16 +01004664 /* For CCM, this size may not be correct according to the PSA
4665 * specification. The PSA Crypto 1.0.1 specification states:
4666 *
4667 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4668 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4669 *
4670 * However this restriction that L has to be the smallest integer is not
4671 * applied in practice, and it is not implementable here since the
4672 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4674 operation->alg);
4675 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004676 status = PSA_ERROR_BUFFER_TOO_SMALL;
4677 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004678 }
4679
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 status = psa_generate_random(local_nonce, required_nonce_size);
4681 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004682 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004684
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004686
Paul Elliott39dc6b82021-05-11 19:16:09 +01004687exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 if (status == PSA_SUCCESS) {
4689 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004690 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 } else {
4692 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004693 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004696}
4697
4698/* Set the nonce for a multipart authenticated encryption or decryption
4699 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004700psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4701 const uint8_t *nonce,
4702 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004703{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004704 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4705
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004707 status = PSA_ERROR_BAD_STATE;
4708 goto exit;
4709 }
4710
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004712 status = PSA_ERROR_BAD_STATE;
4713 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004714 }
4715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4717 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004718 status = PSA_ERROR_INVALID_ARGUMENT;
4719 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004720 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4723 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004724
Paul Elliott39dc6b82021-05-11 19:16:09 +01004725exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004727 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004728 } else {
4729 psa_aead_abort(operation);
4730 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004733}
4734
4735/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004736psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4737 size_t ad_length,
4738 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004739{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004740 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4741
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004743 status = PSA_ERROR_BAD_STATE;
4744 goto exit;
4745 }
4746
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 if (operation->lengths_set || operation->ad_started ||
4748 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004749 status = PSA_ERROR_BAD_STATE;
4750 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004751 }
4752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004754#if defined(PSA_WANT_ALG_GCM)
4755 case PSA_ALG_GCM:
4756 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 * bits. Without the guard this code will generate warnings on 32bit
4758 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004759#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 if (((uint64_t) ad_length) >> 61 != 0 ||
4761 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004762 status = PSA_ERROR_INVALID_ARGUMENT;
4763 goto exit;
4764 }
Paul Elliott325d3742021-09-27 17:56:28 +01004765#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004766 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004767#endif /* PSA_WANT_ALG_GCM */
4768#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004769 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004771 status = PSA_ERROR_INVALID_ARGUMENT;
4772 goto exit;
4773 }
4774 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004775#endif /* PSA_WANT_ALG_CCM */
4776#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004777 case PSA_ALG_CHACHA20_POLY1305:
4778 /* No length restrictions for ChaChaPoly. */
4779 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004780#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004781 default:
4782 break;
4783 }
Paul Elliott325d3742021-09-27 17:56:28 +01004784
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4786 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004787
Paul Elliott39dc6b82021-05-11 19:16:09 +01004788exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004790 operation->ad_remaining = ad_length;
4791 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004792 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 } else {
4794 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004795 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004796
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004798}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004799
4800/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004801psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4802 const uint8_t *input,
4803 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004804{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004805 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004808 status = PSA_ERROR_BAD_STATE;
4809 goto exit;
4810 }
4811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004813 status = PSA_ERROR_BAD_STATE;
4814 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004815 }
4816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 if (operation->lengths_set) {
4818 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004819 status = PSA_ERROR_INVALID_ARGUMENT;
4820 goto exit;
4821 }
4822
4823 operation->ad_remaining -= input_length;
4824 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004825#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004827 status = PSA_ERROR_BAD_STATE;
4828 goto exit;
4829 }
4830#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004831
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 status = psa_driver_wrapper_aead_update_ad(operation, input,
4833 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004834
Paul Elliott39dc6b82021-05-11 19:16:09 +01004835exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004837 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 } else {
4839 psa_aead_abort(operation);
4840 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004843}
4844
4845/* Encrypt or decrypt a message fragment in an active multipart AEAD
4846 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004847psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4848 const uint8_t *input,
4849 size_t input_length,
4850 uint8_t *output,
4851 size_t output_size,
4852 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004853{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004854 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004855
4856 *output_length = 0;
4857
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004859 status = PSA_ERROR_BAD_STATE;
4860 goto exit;
4861 }
4862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004864 status = PSA_ERROR_BAD_STATE;
4865 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004866 }
4867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004869 /* Additional data length was supplied, but not all the additional
4870 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004872 status = PSA_ERROR_INVALID_ARGUMENT;
4873 goto exit;
4874 }
4875
4876 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004878 status = PSA_ERROR_INVALID_ARGUMENT;
4879 goto exit;
4880 }
4881
4882 operation->body_remaining -= input_length;
4883 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004884#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004886 status = PSA_ERROR_BAD_STATE;
4887 goto exit;
4888 }
4889#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4892 output, output_size,
4893 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004894
Paul Elliott39dc6b82021-05-11 19:16:09 +01004895exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004897 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 } else {
4899 psa_aead_abort(operation);
4900 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004901
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004903}
4904
Gilles Peskine449bd832023-01-11 14:50:10 +01004905static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004906{
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 if (operation->id == 0 || !operation->nonce_set) {
4908 return PSA_ERROR_BAD_STATE;
4909 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004910
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4912 operation->body_remaining != 0)) {
4913 return PSA_ERROR_INVALID_ARGUMENT;
4914 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004915
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004917}
4918
Paul Elliott302ff6b2021-04-20 18:10:30 +01004919/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004920psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4921 uint8_t *ciphertext,
4922 size_t ciphertext_size,
4923 size_t *ciphertext_length,
4924 uint8_t *tag,
4925 size_t tag_size,
4926 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004927{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004928 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4929
Paul Elliott302ff6b2021-04-20 18:10:30 +01004930 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004931 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 status = psa_aead_final_checks(operation);
4934 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004935 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004937
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004939 status = PSA_ERROR_BAD_STATE;
4940 goto exit;
4941 }
4942
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4944 ciphertext_size,
4945 ciphertext_length,
4946 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004947
Paul Elliott39dc6b82021-05-11 19:16:09 +01004948exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004949
4950
Paul Elliottf47b0952021-05-21 18:02:33 +01004951 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004952 * the zero tag size, make sure the tag is set to something implausible.
4953 * Even if the operation succeeds, make sure we clear the rest of the
4954 * buffer to prevent potential leakage of anything previously placed in
4955 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004956 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004957
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004959
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004961}
4962
4963/* Finish authenticating and decrypting a message in a multipart AEAD
4964 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004965psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4966 uint8_t *plaintext,
4967 size_t plaintext_size,
4968 size_t *plaintext_length,
4969 const uint8_t *tag,
4970 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004971{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004972 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4973
Paul Elliott302ff6b2021-04-20 18:10:30 +01004974 *plaintext_length = 0;
4975
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 status = psa_aead_final_checks(operation);
4977 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004978 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004980
Gilles Peskine449bd832023-01-11 14:50:10 +01004981 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004982 status = PSA_ERROR_BAD_STATE;
4983 goto exit;
4984 }
4985
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4987 plaintext_size,
4988 plaintext_length,
4989 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004990
4991exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004992 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004993
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004995}
4996
4997/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004998psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004999{
5000 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5001
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005003 /* The object has (apparently) been initialized but it is not (yet)
5004 * in use. It's ok to call abort on such an object, and there's
5005 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005007 }
5008
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005010
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005012
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005014}
5015
Gilles Peskinee59236f2018-01-27 23:32:46 +01005016/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005017/* Generators */
5018/****************************************************************/
5019
Przemek Stekiel69c46792022-06-10 12:59:51 +02005020#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005021 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005022 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305023 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305024 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005025#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005026#endif /* At least one builtin KDF */
5027
Przemek Stekiel69c46792022-06-10 12:59:51 +02005028#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005029 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5030 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5031static psa_status_t psa_key_derivation_start_hmac(
5032 psa_mac_operation_t *operation,
5033 psa_algorithm_t hash_alg,
5034 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005036{
5037 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5038 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5040 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5041 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005042
Steven Cooreman72f736a2021-05-07 14:14:37 +02005043 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005045
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 status = psa_driver_wrapper_mac_sign_setup(operation,
5047 &attributes,
5048 hmac_key, hmac_key_length,
5049 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005050
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 psa_reset_key_attributes(&attributes);
5052 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005053}
5054#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005055
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005056#define HKDF_STATE_INIT 0 /* no input yet */
5057#define HKDF_STATE_STARTED 1 /* got salt */
5058#define HKDF_STATE_KEYED 2 /* got key */
5059#define HKDF_STATE_OUTPUT 3 /* output started */
5060
Gilles Peskinecbe66502019-05-16 16:59:18 +02005061static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005063{
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5065 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5066 } else {
5067 return operation->alg;
5068 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005069}
5070
Gilles Peskine449bd832023-01-11 14:50:10 +01005071psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005072{
5073 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5075 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005076 /* The object has (apparently) been initialized but it is not
5077 * in use. It's ok to call abort on such an object, and there's
5078 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005080#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5082 mbedtls_free(operation->ctx.hkdf.info);
5083 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5084 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005085#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005086#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5087 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5089 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5090 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5091 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005092 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005094 }
5095
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005097 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005099 }
5100
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005102 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005104 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005105#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005107 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005109 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005110#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005111 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005112
5113 /* We leave the fields Ai and output_block to be erased safely by the
5114 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005116#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5117 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005118#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5120 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5121 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5122 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005123#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305124#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305125 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305126 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005127 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305128 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305129 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305130
5131 status = PSA_SUCCESS;
5132 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305133#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005134 {
5135 status = PSA_ERROR_BAD_STATE;
5136 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 mbedtls_platform_zeroize(operation, sizeof(*operation));
5138 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005139}
5140
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005141psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005143{
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005145 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005147 }
5148
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005149 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005151}
5152
Gilles Peskine449bd832023-01-11 14:50:10 +01005153psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5154 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005155{
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 if (operation->alg == 0) {
5157 return PSA_ERROR_BAD_STATE;
5158 }
5159 if (capacity > operation->capacity) {
5160 return PSA_ERROR_INVALID_ARGUMENT;
5161 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005162 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005164}
5165
Przemek Stekiel69c46792022-06-10 12:59:51 +02005166#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005167/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005168static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5169 psa_algorithm_t kdf_alg,
5170 uint8_t *output,
5171 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005172{
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5174 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005175 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005176 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005177#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005179#else
5180 const uint8_t last_block = 0xff;
5181#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005182
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 if (hkdf->state < HKDF_STATE_KEYED ||
5184 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005185#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005187#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 )) {
5189 return PSA_ERROR_BAD_STATE;
5190 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005191 hkdf->state = HKDF_STATE_OUTPUT;
5192
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005194 /* Copy what remains of the current block */
5195 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005197 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 }
5199 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005200 output += n;
5201 output_length -= n;
5202 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005204 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005206 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005207 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005209 * object was corrupted or if this function is called directly
5210 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 if (hkdf->block_number == last_block) {
5212 return PSA_ERROR_BAD_STATE;
5213 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005214
5215 /* We need a new block */
5216 ++hkdf->block_number;
5217 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005218
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5220 hash_alg,
5221 hkdf->prk,
5222 hash_length);
5223 if (status != PSA_SUCCESS) {
5224 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005225 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005226
5227 if (hkdf->block_number != 1) {
5228 status = psa_mac_update(&hkdf->hmac,
5229 hkdf->output_block,
5230 hash_length);
5231 if (status != PSA_SUCCESS) {
5232 return status;
5233 }
5234 }
5235 status = psa_mac_update(&hkdf->hmac,
5236 hkdf->info,
5237 hkdf->info_length);
5238 if (status != PSA_SUCCESS) {
5239 return status;
5240 }
5241 status = psa_mac_update(&hkdf->hmac,
5242 &hkdf->block_number, 1);
5243 if (status != PSA_SUCCESS) {
5244 return status;
5245 }
5246 status = psa_mac_sign_finish(&hkdf->hmac,
5247 hkdf->output_block,
5248 sizeof(hkdf->output_block),
5249 &hmac_output_length);
5250 if (status != PSA_SUCCESS) {
5251 return status;
5252 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005253 }
5254
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005256}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005257#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005258
John Durkop07cc04a2020-11-16 22:08:34 -08005259#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5260 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005261static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5262 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005264{
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5266 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005267 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5268 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005269 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005270
Janos Follath7742fee2019-06-17 12:58:10 +01005271 /* We can't be wanting more output after block 0xff, otherwise
5272 * the capacity check in psa_key_derivation_output_bytes() would have
5273 * prevented this call. It could happen only if the operation
5274 * object was corrupted or if this function is called directly
5275 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 if (tls12_prf->block_number == 0xff) {
5277 return PSA_ERROR_CORRUPTION_DETECTED;
5278 }
Janos Follath7742fee2019-06-17 12:58:10 +01005279
5280 /* We need a new block */
5281 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005282 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005283
5284 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5285 *
5286 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5287 *
5288 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5289 * HMAC_hash(secret, A(2) + seed) +
5290 * HMAC_hash(secret, A(3) + seed) + ...
5291 *
5292 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005293 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005294 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005295 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005296 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005297 * is currently extracted as `output_block` and where i is
5298 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005299 */
5300
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 status = psa_key_derivation_start_hmac(&hmac,
5302 hash_alg,
5303 tls12_prf->secret,
5304 tls12_prf->secret_length);
5305 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005306 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005308
5309 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005311 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5312 * the variable seed and in this instance means it in the context of the
5313 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 status = psa_mac_update(&hmac,
5315 tls12_prf->label,
5316 tls12_prf->label_length);
5317 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005318 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 }
5320 status = psa_mac_update(&hmac,
5321 tls12_prf->seed,
5322 tls12_prf->seed_length);
5323 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005324 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 }
5326 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005327 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5329 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005330 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005332 }
5333
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 status = psa_mac_sign_finish(&hmac,
5335 tls12_prf->Ai, hash_length,
5336 &hmac_output_length);
5337 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005338 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 }
5340 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005341 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005343
5344 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 status = psa_key_derivation_start_hmac(&hmac,
5346 hash_alg,
5347 tls12_prf->secret,
5348 tls12_prf->secret_length);
5349 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005350 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 }
5352 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5353 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005354 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 }
5356 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5357 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005358 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 }
5360 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5361 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005362 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 }
5364 status = psa_mac_sign_finish(&hmac,
5365 tls12_prf->output_block, hash_length,
5366 &hmac_output_length);
5367 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005368 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005370
Janos Follath7742fee2019-06-17 12:58:10 +01005371
5372cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 cleanup_status = psa_mac_abort(&hmac);
5374 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005375 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005379}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005380
Janos Follath844eb0e2019-06-19 12:10:49 +01005381static psa_status_t psa_key_derivation_tls12_prf_read(
5382 psa_tls12_prf_key_derivation_t *tls12_prf,
5383 psa_algorithm_t alg,
5384 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005386{
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5388 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005389 psa_status_t status;
5390 uint8_t offset, length;
5391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005393 case PSA_TLS12_PRF_STATE_LABEL_SET:
5394 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5395 break;
5396 case PSA_TLS12_PRF_STATE_OUTPUT:
5397 break;
5398 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005400 }
5401
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005403 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 if (tls12_prf->left_in_block == 0) {
5405 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5406 alg);
5407 if (status != PSA_SUCCESS) {
5408 return status;
5409 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005410
5411 continue;
5412 }
5413
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005415 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005417 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005419
5420 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005422 output += length;
5423 output_length -= length;
5424 tls12_prf->left_in_block -= length;
5425 }
5426
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005428}
John Durkop07cc04a2020-11-16 22:08:34 -08005429#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5430 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005431
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005432#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5433static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5434 psa_tls12_ecjpake_to_pms_t *ecjpake,
5435 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005437{
5438 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005439 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005440
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 if (output_length != 32) {
5442 return PSA_ERROR_INVALID_ARGUMENT;
5443 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005444
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5446 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5447 &output_size);
5448 if (status != PSA_SUCCESS) {
5449 return status;
5450 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005451
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 if (output_size != output_length) {
5453 return PSA_ERROR_GENERIC_ERROR;
5454 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005455
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005457}
5458#endif
5459
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305460#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305461static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5462 psa_pbkdf2_key_derivation_t *pbkdf2,
5463 psa_algorithm_t prf_alg,
5464 uint8_t prf_output_length,
5465 psa_key_attributes_t *attributes)
5466{
5467 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305468 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305469 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305470 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305471 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305472 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305473 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305474
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305475 mac_operation.is_sign = 1;
5476 mac_operation.mac_size = prf_output_length;
5477 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305478
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305479 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5480 attributes,
5481 pbkdf2->password,
5482 pbkdf2->password_length,
5483 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305484 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305485 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305486 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305487 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5488 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305489 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305490 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305491 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305492 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305493 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305494 }
5495 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5496 &mac_output_length);
5497 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305498 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305499 }
5500
5501 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305502 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305503 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305504 }
5505
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305506 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305507
5508 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305509 /* We are passing prf_output_length as mac_size because the driver
5510 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305511 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305512 status = psa_driver_wrapper_mac_compute(attributes,
5513 pbkdf2->password,
5514 pbkdf2->password_length,
5515 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305516 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305517 &mac_output_length);
5518 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305519 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305520 }
5521
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305522 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305523 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305524
5525cleanup:
5526 /* Zeroise buffers to clear sensitive data from memory. */
5527 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5528 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305529}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305530
5531static psa_status_t psa_key_derivation_pbkdf2_read(
5532 psa_pbkdf2_key_derivation_t *pbkdf2,
5533 psa_algorithm_t kdf_alg,
5534 uint8_t *output,
5535 size_t output_length)
5536{
5537 psa_status_t status;
5538 psa_algorithm_t prf_alg;
5539 uint8_t prf_output_length;
5540 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5541 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5542 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5543
5544 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5545 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5546 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5547 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305548 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5549 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305550 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305551 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305552 } else {
5553 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305554 }
5555
5556 switch (pbkdf2->state) {
5557 case PSA_PBKDF2_STATE_PASSWORD_SET:
5558 /* Initially we need a new block so bytes_used is equal to block size*/
5559 pbkdf2->bytes_used = prf_output_length;
5560 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5561 break;
5562 case PSA_PBKDF2_STATE_OUTPUT:
5563 break;
5564 default:
5565 return PSA_ERROR_BAD_STATE;
5566 }
5567
5568 while (output_length != 0) {
5569 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5570 if (n > output_length) {
5571 n = (uint8_t) output_length;
5572 }
5573 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5574 output += n;
5575 output_length -= n;
5576 pbkdf2->bytes_used += n;
5577
5578 if (output_length == 0) {
5579 break;
5580 }
5581
5582 /* We need a new block */
5583 pbkdf2->bytes_used = 0;
5584 pbkdf2->block_number++;
5585
5586 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5587 prf_output_length,
5588 &attributes);
5589 if (status != PSA_SUCCESS) {
5590 return status;
5591 }
5592 }
5593
5594 return PSA_SUCCESS;
5595}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305596#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305597
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005598psa_status_t psa_key_derivation_output_bytes(
5599 psa_key_derivation_operation_t *operation,
5600 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005602{
5603 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005605
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005607 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005609 }
5610
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005612 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005613 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005614 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005615 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005616 goto exit;
5617 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005619 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005620 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005621 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5622 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005623 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005624 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005626 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005627 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005628
Przemek Stekiel69c46792022-06-10 12:59:51 +02005629#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5631 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5632 output, output_length);
5633 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005634#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005635#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5636 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005637 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5638 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5639 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5640 kdf_alg, output,
5641 output_length);
5642 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005643#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5644 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005645#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005647 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005648 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5649 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005650#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305651#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305652 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305653 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5654 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305655 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305656#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005657
Gilles Peskineeab56e42018-07-12 17:12:33 +02005658 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005659 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005661 }
5662
5663exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005665 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005666 * This allows us to differentiate between exhausted operations and
5667 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5668 * operations. */
5669 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005671 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005673 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005675}
5676
David Brown7807bf72021-02-09 16:28:23 -07005677#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005678static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005679{
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 if (data_size >= 8) {
5681 mbedtls_des_key_set_parity(data);
5682 }
5683 if (data_size >= 16) {
5684 mbedtls_des_key_set_parity(data + 8);
5685 }
5686 if (data_size >= 24) {
5687 mbedtls_des_key_set_parity(data + 16);
5688 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005689}
David Brown7807bf72021-02-09 16:28:23 -07005690#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005691
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005692/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 * ECC keys on a Weierstrass elliptic curve require the generation
5694 * of a private key which is an integer
5695 * in the range [1, N - 1], where N is the boundary of the private key domain:
5696 * N is the prime p for Diffie-Hellman, or the order of the
5697 * curve’s base point for ECC.
5698 *
5699 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5700 * This function generates the private key using the following process:
5701 *
5702 * 1. Draw a byte string of length ceiling(m/8) bytes.
5703 * 2. If m is not a multiple of 8, set the most significant
5704 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5705 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5706 * 4. If k > N - 2, discard the result and return to step 1.
5707 * 5. Output k + 1 as the private key.
5708 *
5709 * This method allows compliance to NIST standards, specifically the methods titled
5710 * Key-Pair Generation by Testing Candidates in the following publications:
5711 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5712 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5713 * Diffie-Hellman keys.
5714 *
5715 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5716 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5717 *
5718 * Note: Function allocates memory for *data buffer, so given *data should be
5719 * always NULL.
5720 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005721#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5722#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005723static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005724 psa_key_slot_t *slot,
5725 size_t bits,
5726 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005727 uint8_t **data
5728 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005729{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005730 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005731 mbedtls_mpi k;
5732 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005733 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005734 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005735 size_t m;
5736 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 mbedtls_mpi_init(&k);
5739 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005740
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005741 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005743 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01005744 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005745
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005747 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005748 goto cleanup;
5749 }
5750
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005751 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005755
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005756 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005757 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005758 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005759
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005760 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005761
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005762 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005764
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005765 /* Note: This function is always called with *data == NULL and it
5766 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 *data = mbedtls_calloc(1, m_bytes);
5768 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005769 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005770 goto cleanup;
5771 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005774 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005776 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005778
5779 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005781 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005782 * (8 * ceiling(m/8) - m) bits of the first byte in
5783 * the string to zero.
5784 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005786 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005787 }
5788
5789 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 * big-endian byte string.
5791 */
5792 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005793
5794 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 * Result of comparison is returned. When it indicates error
5796 * then this function is called again.
5797 */
5798 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005799 }
5800
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005801 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5803 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005804cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 if (ret != 0) {
5806 status = mbedtls_to_psa_error(ret);
5807 }
5808 if (status != PSA_SUCCESS) {
5809 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005810 *data = NULL;
5811 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 mbedtls_mpi_free(&k);
5813 mbedtls_mpi_free(&diff_N_2);
5814 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005815}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005816
5817/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5818 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5819 *
5820 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5821 * draw a 32-byte string and process it as specified in
5822 * Elliptic Curves for Security [RFC7748] §5.
5823 *
5824 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5825 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5826 *
5827 * Note: Function allocates memory for *data buffer, so given *data should be
5828 * always NULL.
5829 */
5830
5831static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5832 size_t bits,
5833 psa_key_derivation_operation_t *operation,
5834 uint8_t **data
5835 )
5836{
5837 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005838 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005839
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005841 case 255:
5842 output_length = 32;
5843 break;
5844 case 448:
5845 output_length = 56;
5846 break;
5847 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005849 break;
5850 }
5851
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005853
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 if (*data == NULL) {
5855 return PSA_ERROR_INSUFFICIENT_MEMORY;
5856 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005857
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005859
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005861 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005865 case 255:
5866 (*data)[0] &= 248;
5867 (*data)[31] &= 127;
5868 (*data)[31] |= 64;
5869 break;
5870 case 448:
5871 (*data)[0] &= 252;
5872 (*data)[55] |= 128;
5873 break;
5874 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005876 break;
5877 }
5878
5879 return status;
5880}
Valerio Setti86587ab2023-06-27 13:09:30 +02005881#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5882static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5883 psa_key_slot_t *slot, size_t bits,
5884 psa_key_derivation_operation_t *operation, uint8_t **data)
5885{
5886 (void) slot;
5887 (void) bits;
5888 (void) operation;
5889 (void) data;
5890 return PSA_ERROR_NOT_SUPPORTED;
5891}
5892
5893static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5894 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5895{
5896 (void) bits;
5897 (void) operation;
5898 (void) data;
5899 return PSA_ERROR_NOT_SUPPORTED;
5900}
5901#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5902#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005903
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005904static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005905 psa_key_slot_t *slot,
5906 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005908{
5909 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305911 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005912 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005913 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005914
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5916 return PSA_ERROR_INVALID_ARGUMENT;
5917 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005918
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005919#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005920 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5922 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5923 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005924 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5926 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005927 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 }
5929 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005930 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5932 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005933 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005935 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005936 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005937#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005938 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 if (key_type_is_raw_bytes(slot->attr.type)) {
5940 if (bits % 8 != 0) {
5941 return PSA_ERROR_INVALID_ARGUMENT;
5942 }
5943 data = mbedtls_calloc(1, bytes);
5944 if (data == NULL) {
5945 return PSA_ERROR_INSUFFICIENT_MEMORY;
5946 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005947
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 status = psa_key_derivation_output_bytes(operation, data, bytes);
5949 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005950 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 }
David Brown12ca5032021-02-11 11:02:00 -07005952#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5954 psa_des_set_key_parity(data, bytes);
5955 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005956#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 } else {
5958 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005959 }
Ronald Crondd04d422020-11-28 15:14:42 +01005960
Ronald Cronbf33c932020-11-28 18:06:53 +01005961 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005962 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005964 };
5965
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5967 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5968 &storage_size);
5969 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305970 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 }
Archanad8a83dc2021-06-14 10:04:16 +05305972 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 status = psa_allocate_buffer_to_slot(slot, storage_size);
5974 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305975 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 }
Archanad8a83dc2021-06-14 10:04:16 +05305977
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 status = psa_driver_wrapper_import_key(&attributes,
5979 data, bytes,
5980 slot->key.data,
5981 slot->key.bytes,
5982 &slot->key.bytes, &bits);
5983 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005984 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005986
5987exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 mbedtls_free(data);
5989 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005990}
5991
Gilles Peskine449bd832023-01-11 14:50:10 +01005992psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5993 psa_key_derivation_operation_t *operation,
5994 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005995{
5996 psa_status_t status;
5997 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005998 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005999
Ronald Cron81709fc2020-11-14 12:10:32 +01006000 *key = MBEDTLS_SVC_KEY_ID_INIT;
6001
Gilles Peskine0f84d622019-09-12 19:03:13 +02006002 /* Reject any attempt to create a zero-length key so that we don't
6003 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 if (psa_get_key_bits(attributes) == 0) {
6005 return PSA_ERROR_INVALID_ARGUMENT;
6006 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 if (operation->alg == PSA_ALG_NONE) {
6009 return PSA_ERROR_BAD_STATE;
6010 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 if (!operation->can_output_key) {
6013 return PSA_ERROR_NOT_PERMITTED;
6014 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006015
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6017 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006018#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006020 /* Deriving a key in a secure element is not implemented yet. */
6021 status = PSA_ERROR_NOT_SUPPORTED;
6022 }
6023#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 if (status == PSA_SUCCESS) {
6025 status = psa_generate_derived_key_internal(slot,
6026 attributes->core.bits,
6027 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006028 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 if (status == PSA_SUCCESS) {
6030 status = psa_finish_key_creation(slot, driver, key);
6031 }
6032 if (status != PSA_SUCCESS) {
6033 psa_fail_key_creation(slot, driver);
6034 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006037}
6038
Gilles Peskineeab56e42018-07-12 17:12:33 +02006039
6040
6041/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006042/* Key derivation */
6043/****************************************************************/
6044
Steven Cooreman932ffb72021-02-15 12:14:32 +01006045#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006046static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006047{
6048#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6050 return 1;
6051 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006052#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006053#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6055 return 1;
6056 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006057#endif
6058#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6060 return 1;
6061 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006062#endif
6063#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6065 return 1;
6066 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006067#endif
6068#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6070 return 1;
6071 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006072#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006073#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6075 return 1;
6076 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006077#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306078#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6079 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6080 return 1;
6081 }
6082#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306083#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6084 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6085 return 1;
6086 }
6087#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006089}
6090
Gilles Peskine449bd832023-01-11 14:50:10 +01006091static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006092{
6093 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 psa_status_t status = psa_hash_setup(&operation, alg);
6095 psa_hash_abort(&operation);
6096 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006097}
6098
Gilles Peskine969c5d62019-01-16 15:53:06 +01006099static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006100 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006102{
Janos Follath5fe19732019-06-20 15:09:30 +01006103 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6104 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006106
Gilles Peskine969c5d62019-01-16 15:53:06 +01006107 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (!is_kdf_alg_supported(kdf_alg)) {
6109 return PSA_ERROR_NOT_SUPPORTED;
6110 }
John Durkop07cc04a2020-11-16 22:08:34 -08006111
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006112 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306113 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6115 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306116 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6117 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6118 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306119 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306120 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 if (hash_size == 0) {
6122 return PSA_ERROR_NOT_SUPPORTED;
6123 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006124
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006125 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6126 * we might fail later, which is somewhat unfriendly and potentially
6127 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 psa_status_t status = psa_hash_try_support(hash_alg);
6129 if (status != PSA_SUCCESS) {
6130 return status;
6131 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006132 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6135 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6136 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6137 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006138 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006139#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006140 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6142 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006143 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006145#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6146 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 operation->capacity = 255 * hash_size;
6148 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006149}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006150
Gilles Peskine449bd832023-01-11 14:50:10 +01006151static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006152{
6153#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 if (alg == PSA_ALG_ECDH) {
6155 return PSA_SUCCESS;
6156 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006157#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006158#if defined(PSA_WANT_ALG_FFDH)
6159 if (alg == PSA_ALG_FFDH) {
6160 return PSA_SUCCESS;
6161 }
6162#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006163 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006165}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006166
6167static int psa_key_derivation_allows_free_form_secret_input(
6168 psa_algorithm_t kdf_alg)
6169{
6170#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6171 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6172 return 0;
6173 }
6174#endif
6175 (void) kdf_alg;
6176 return 1;
6177}
John Durkop07cc04a2020-11-16 22:08:34 -08006178#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006179
Gilles Peskine449bd832023-01-11 14:50:10 +01006180psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6181 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006182{
6183 psa_status_t status;
6184
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 if (operation->alg != 0) {
6186 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006187 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006188
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6190 return PSA_ERROR_INVALID_ARGUMENT;
6191 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6192#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6193 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6194 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6195 status = psa_key_agreement_try_support(ka_alg);
6196 if (status != PSA_SUCCESS) {
6197 return status;
6198 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006199 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6200 return PSA_ERROR_INVALID_ARGUMENT;
6201 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6203#else
6204 return PSA_ERROR_NOT_SUPPORTED;
6205#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6206 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6207#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6208 status = psa_key_derivation_setup_kdf(operation, alg);
6209#else
6210 return PSA_ERROR_NOT_SUPPORTED;
6211#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6212 } else {
6213 return PSA_ERROR_INVALID_ARGUMENT;
6214 }
6215
6216 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006217 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 }
6219 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006220}
6221
Przemek Stekiel69c46792022-06-10 12:59:51 +02006222#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006223static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6224 psa_algorithm_t kdf_alg,
6225 psa_key_derivation_step_t step,
6226 const uint8_t *data,
6227 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006228{
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006230 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006232 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006233#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6235 return PSA_ERROR_INVALID_ARGUMENT;
6236 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006237#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 if (hkdf->state != HKDF_STATE_INIT) {
6239 return PSA_ERROR_BAD_STATE;
6240 } else {
6241 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6242 hash_alg,
6243 data, data_length);
6244 if (status != PSA_SUCCESS) {
6245 return status;
6246 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006247 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006249 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006250 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006251#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006253 /* We shouldn't be in different state as HKDF_EXPAND only allows
6254 * two inputs: SECRET (this case) and INFO which does not modify
6255 * the state. It could happen only if the hkdf
6256 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 if (hkdf->state != HKDF_STATE_INIT) {
6258 return PSA_ERROR_BAD_STATE;
6259 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006260
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006261 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6263 return PSA_ERROR_INVALID_ARGUMENT;
6264 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006265
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 memcpy(hkdf->prk, data, data_length);
6267 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006268#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006269 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006270 /* HKDF: If no salt was provided, use an empty salt.
6271 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006273#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6275 return PSA_ERROR_BAD_STATE;
6276 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006277#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6279 hash_alg,
6280 NULL, 0);
6281 if (status != PSA_SUCCESS) {
6282 return status;
6283 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006284 hkdf->state = HKDF_STATE_STARTED;
6285 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 if (hkdf->state != HKDF_STATE_STARTED) {
6287 return PSA_ERROR_BAD_STATE;
6288 }
6289 status = psa_mac_update(&hkdf->hmac,
6290 data, data_length);
6291 if (status != PSA_SUCCESS) {
6292 return status;
6293 }
6294 status = psa_mac_sign_finish(&hkdf->hmac,
6295 hkdf->prk,
6296 sizeof(hkdf->prk),
6297 &data_length);
6298 if (status != PSA_SUCCESS) {
6299 return status;
6300 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006301 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006302
Gilles Peskine2b522db2019-04-12 15:11:49 +02006303 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006304 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006305#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006307 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006309 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006311#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006312 {
6313 /* Block 0 is empty, and the next block will be
6314 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006316 }
6317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006319 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006320#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6322 return PSA_ERROR_INVALID_ARGUMENT;
6323 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006324#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006325#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6327 hkdf->state == HKDF_STATE_INIT) {
6328 return PSA_ERROR_BAD_STATE;
6329 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006330#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 if (hkdf->state == HKDF_STATE_OUTPUT) {
6332 return PSA_ERROR_BAD_STATE;
6333 }
6334 if (hkdf->info_set) {
6335 return PSA_ERROR_BAD_STATE;
6336 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006337 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 if (data_length != 0) {
6339 hkdf->info = mbedtls_calloc(1, data_length);
6340 if (hkdf->info == NULL) {
6341 return PSA_ERROR_INSUFFICIENT_MEMORY;
6342 }
6343 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006344 }
6345 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006347 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006349 }
6350}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006351#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006352
John Durkop07cc04a2020-11-16 22:08:34 -08006353#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6354 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006355static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6356 const uint8_t *data,
6357 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006358{
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6360 return PSA_ERROR_BAD_STATE;
6361 }
Janos Follathf08e2652019-06-13 09:05:41 +01006362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 if (data_length != 0) {
6364 prf->seed = mbedtls_calloc(1, data_length);
6365 if (prf->seed == NULL) {
6366 return PSA_ERROR_INSUFFICIENT_MEMORY;
6367 }
Janos Follathf08e2652019-06-13 09:05:41 +01006368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006370 prf->seed_length = data_length;
6371 }
Janos Follathf08e2652019-06-13 09:05:41 +01006372
Gilles Peskine43f958b2020-12-13 14:55:14 +01006373 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006376}
6377
Gilles Peskine449bd832023-01-11 14:50:10 +01006378static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6379 const uint8_t *data,
6380 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006381{
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6383 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6384 return PSA_ERROR_BAD_STATE;
6385 }
Janos Follath81550542019-06-13 14:26:34 +01006386
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 if (data_length != 0) {
6388 prf->secret = mbedtls_calloc(1, data_length);
6389 if (prf->secret == NULL) {
6390 return PSA_ERROR_INSUFFICIENT_MEMORY;
6391 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006394 prf->secret_length = data_length;
6395 }
Janos Follath81550542019-06-13 14:26:34 +01006396
Gilles Peskine43f958b2020-12-13 14:55:14 +01006397 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006398
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006400}
6401
Gilles Peskine449bd832023-01-11 14:50:10 +01006402static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6403 const uint8_t *data,
6404 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006405{
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6407 return PSA_ERROR_BAD_STATE;
6408 }
Janos Follath63028dd2019-06-13 09:15:47 +01006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 if (data_length != 0) {
6411 prf->label = mbedtls_calloc(1, data_length);
6412 if (prf->label == NULL) {
6413 return PSA_ERROR_INSUFFICIENT_MEMORY;
6414 }
Janos Follath63028dd2019-06-13 09:15:47 +01006415
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006417 prf->label_length = data_length;
6418 }
Janos Follath63028dd2019-06-13 09:15:47 +01006419
Gilles Peskine43f958b2020-12-13 14:55:14 +01006420 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006421
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006423}
6424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6426 psa_key_derivation_step_t step,
6427 const uint8_t *data,
6428 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006429{
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006431 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006433 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006435 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006437 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006439 }
6440}
John Durkop07cc04a2020-11-16 22:08:34 -08006441#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6442 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6443
6444#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6445static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6446 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006447 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006449{
6450 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6452 4 + data_length + prf->other_secret_length :
6453 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006454
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6456 return PSA_ERROR_INVALID_ARGUMENT;
6457 }
John Durkop07cc04a2020-11-16 22:08:34 -08006458
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 uint8_t *pms = mbedtls_calloc(1, pms_len);
6460 if (pms == NULL) {
6461 return PSA_ERROR_INSUFFICIENT_MEMORY;
6462 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006463 uint8_t *cur = pms;
6464
6465 /* pure-PSK:
6466 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006467 *
6468 * The premaster secret is formed as follows: if the PSK is N octets
6469 * long, concatenate a uint16 with the value N, N zero octets, a second
6470 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006471 *
6472 * mixed-PSK:
6473 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6474 * follows: concatenate a uint16 with the length of the other secret,
6475 * the other secret itself, uint16 with the length of PSK, and the
6476 * PSK itself.
6477 * For details please check:
6478 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6479 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6480 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006481 */
6482
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6484 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6485 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6486 if (prf->other_secret_length != 0) {
6487 memcpy(cur, prf->other_secret, prf->other_secret_length);
6488 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006489 cur += prf->other_secret_length;
6490 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 } else {
6492 *cur++ = MBEDTLS_BYTE_1(data_length);
6493 *cur++ = MBEDTLS_BYTE_0(data_length);
6494 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006495 cur += data_length;
6496 }
6497
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 *cur++ = MBEDTLS_BYTE_1(data_length);
6499 *cur++ = MBEDTLS_BYTE_0(data_length);
6500 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006501 cur += data_length;
6502
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006503 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006504
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006505 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006507}
Janos Follath6660f0e2019-06-17 08:44:03 +01006508
Przemek Stekiele47201b2022-04-19 13:53:28 +02006509static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6510 psa_tls12_prf_key_derivation_t *prf,
6511 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006513{
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6515 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006516 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006517
6518 if (data_length != 0) {
6519 prf->other_secret = mbedtls_calloc(1, data_length);
6520 if (prf->other_secret == NULL) {
6521 return PSA_ERROR_INSUFFICIENT_MEMORY;
6522 }
6523
6524 memcpy(prf->other_secret, data, data_length);
6525 prf->other_secret_length = data_length;
6526 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006527 prf->other_secret_length = 0;
6528 }
6529
6530 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6531
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006533}
6534
Janos Follath6660f0e2019-06-17 08:44:03 +01006535static psa_status_t psa_tls12_prf_psk_to_ms_input(
6536 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006537 psa_key_derivation_step_t step,
6538 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006540{
Gilles Peskine449bd832023-01-11 14:50:10 +01006541 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006542 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 return psa_tls12_prf_psk_to_ms_set_key(prf,
6544 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006545 break;
6546 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6548 data,
6549 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006550 break;
6551 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006552 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006553 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006554
Przemek Stekiele47201b2022-04-19 13:53:28 +02006555 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006556}
John Durkop07cc04a2020-11-16 22:08:34 -08006557#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006558
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006559#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6560static psa_status_t psa_tls12_ecjpake_to_pms_input(
6561 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006562 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006563 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006564 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006565{
Gilles Peskine449bd832023-01-11 14:50:10 +01006566 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6567 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6568 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006569 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006570
6571 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 if (data[0] != 0x04) {
6573 return PSA_ERROR_INVALID_ARGUMENT;
6574 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006575
6576 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006578
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006580}
6581#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306582
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306583#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306584static psa_status_t psa_pbkdf2_set_input_cost(
6585 psa_pbkdf2_key_derivation_t *pbkdf2,
6586 psa_key_derivation_step_t step,
6587 uint64_t data)
6588{
6589 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6590 return PSA_ERROR_INVALID_ARGUMENT;
6591 }
6592
6593 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6594 return PSA_ERROR_BAD_STATE;
6595 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306596
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306597 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306598 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306599 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306600
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306601 if (data == 0) {
6602 return PSA_ERROR_INVALID_ARGUMENT;
6603 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306604
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306605 pbkdf2->input_cost = data;
6606 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6607
6608 return PSA_SUCCESS;
6609}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306610
6611static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6612 const uint8_t *data,
6613 size_t data_length)
6614{
Gilles Peskineead17662023-08-20 22:02:51 +02006615 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6616 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6617 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6618 /* Appending to existing salt. No state change. */
6619 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306620 return PSA_ERROR_BAD_STATE;
6621 }
6622
Gilles Peskineca57d782023-07-20 19:08:06 +02006623 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006624 /* Appending an empty string, nothing to do. */
6625 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306626 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306627
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306628 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6629 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306630 return PSA_ERROR_INSUFFICIENT_MEMORY;
6631 }
6632
Gilles Peskineead17662023-08-20 22:02:51 +02006633 if (pbkdf2->salt_length != 0) {
6634 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6635 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306636 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306637 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306638 mbedtls_free(pbkdf2->salt);
6639 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306640 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306641 return PSA_SUCCESS;
6642}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306643
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306644#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306645static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306646 const uint8_t *input,
6647 size_t input_len,
6648 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306649 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306650{
6651 psa_status_t status = PSA_SUCCESS;
6652 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6653 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306654 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306655 } else {
6656 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306657 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306658 }
6659 return status;
6660}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306661#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306662
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306663#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306664static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6665 size_t input_len,
6666 uint8_t *output,
6667 size_t *output_len)
6668{
6669 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306670 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306672 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306673 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6674 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6675 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306676 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306677 * mac_size as the driver function sets mac_output_length = mac_size
6678 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306679 status = psa_driver_wrapper_mac_compute(&attributes,
6680 zeros, sizeof(zeros),
6681 PSA_ALG_CMAC, input, input_len,
6682 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306683 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6684 128U,
6685 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306686 output_len);
6687 } else {
6688 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306689 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306690 }
6691 return status;
6692}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306693#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306694
6695static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306696 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306697 const uint8_t *data,
6698 size_t data_length)
6699{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306700 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306701 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6702 return PSA_ERROR_BAD_STATE;
6703 }
6704
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306705#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306706 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6707 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6708 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6709 pbkdf2->password,
6710 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306711 } else
6712#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6713#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6714 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306715 status = psa_pbkdf2_cmac_set_password(data, data_length,
6716 pbkdf2->password,
6717 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306718 } else
6719#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6720 {
6721 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306722 }
6723
6724 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6725
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306726 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306727}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306728
6729static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306730 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306731 psa_key_derivation_step_t step,
6732 const uint8_t *data,
6733 size_t data_length)
6734{
6735 switch (step) {
6736 case PSA_KEY_DERIVATION_INPUT_SALT:
6737 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6738 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306739 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306740 default:
6741 return PSA_ERROR_INVALID_ARGUMENT;
6742 }
6743}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306744#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306745
Gilles Peskineb8965192019-09-24 16:21:10 +02006746/** Check whether the given key type is acceptable for the given
6747 * input step of a key derivation.
6748 *
6749 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6750 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6751 * Both secret and non-secret inputs can alternatively have the type
6752 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6753 * that the input was passed as a buffer rather than via a key object.
6754 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006755static int psa_key_derivation_check_input_type(
6756 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006757 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006758{
Gilles Peskine449bd832023-01-11 14:50:10 +01006759 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006760 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 if (key_type == PSA_KEY_TYPE_DERIVE) {
6762 return PSA_SUCCESS;
6763 }
6764 if (key_type == PSA_KEY_TYPE_NONE) {
6765 return PSA_SUCCESS;
6766 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006767 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006768 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 if (key_type == PSA_KEY_TYPE_DERIVE) {
6770 return PSA_SUCCESS;
6771 }
6772 if (key_type == PSA_KEY_TYPE_NONE) {
6773 return PSA_SUCCESS;
6774 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006775 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006776 case PSA_KEY_DERIVATION_INPUT_LABEL:
6777 case PSA_KEY_DERIVATION_INPUT_SALT:
6778 case PSA_KEY_DERIVATION_INPUT_INFO:
6779 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006780 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6781 return PSA_SUCCESS;
6782 }
6783 if (key_type == PSA_KEY_TYPE_NONE) {
6784 return PSA_SUCCESS;
6785 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006786 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306787 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6788 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6789 return PSA_SUCCESS;
6790 }
6791 if (key_type == PSA_KEY_TYPE_DERIVE) {
6792 return PSA_SUCCESS;
6793 }
6794 if (key_type == PSA_KEY_TYPE_NONE) {
6795 return PSA_SUCCESS;
6796 }
6797 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006798 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006799 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006800}
6801
Janos Follathb80a94e2019-06-12 15:54:46 +01006802static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006803 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006804 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006805 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006806 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006808{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006809 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006810 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006811
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 status = psa_key_derivation_check_input_type(step, key_type);
6813 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006814 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006815 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006816
Przemek Stekiel69c46792022-06-10 12:59:51 +02006817#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006818 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6819 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6820 step, data, data_length);
6821 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006822#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006823#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6825 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6826 step, data, data_length);
6827 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006828#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6829#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6831 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6832 step, data, data_length);
6833 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006834#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006835#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006837 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6839 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006840#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306841#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306842 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306843 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6844 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306845 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306846#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006847 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006848 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006849 (void) data;
6850 (void) data_length;
6851 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006853 }
6854
Gilles Peskine224b0d62019-09-23 18:13:17 +02006855exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 if (status != PSA_SUCCESS) {
6857 psa_key_derivation_abort(operation);
6858 }
6859 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006860}
6861
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306862static psa_status_t psa_key_derivation_input_integer_internal(
6863 psa_key_derivation_operation_t *operation,
6864 psa_key_derivation_step_t step,
6865 uint64_t value)
6866{
6867 psa_status_t status;
6868 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6869
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306870#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306871 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306872 status = psa_pbkdf2_set_input_cost(
6873 &operation->ctx.pbkdf2, step, value);
6874 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306875#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306876 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306877 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306878 (void) value;
6879 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306880 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306881 }
6882
6883 if (status != PSA_SUCCESS) {
6884 psa_key_derivation_abort(operation);
6885 }
6886 return status;
6887}
6888
Janos Follath51f4a0f2019-06-14 11:35:55 +01006889psa_status_t psa_key_derivation_input_bytes(
6890 psa_key_derivation_operation_t *operation,
6891 psa_key_derivation_step_t step,
6892 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006894{
Gilles Peskine449bd832023-01-11 14:50:10 +01006895 return psa_key_derivation_input_internal(operation, step,
6896 PSA_KEY_TYPE_NONE,
6897 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006898}
6899
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306900psa_status_t psa_key_derivation_input_integer(
6901 psa_key_derivation_operation_t *operation,
6902 psa_key_derivation_step_t step,
6903 uint64_t value)
6904{
6905 return psa_key_derivation_input_integer_internal(operation, step, value);
6906}
6907
Janos Follath51f4a0f2019-06-14 11:35:55 +01006908psa_status_t psa_key_derivation_input_key(
6909 psa_key_derivation_operation_t *operation,
6910 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006912{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006913 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006914 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006915 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006916
Ronald Cron5c522922020-11-14 16:35:34 +01006917 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006918 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6919 if (status != PSA_SUCCESS) {
6920 psa_key_derivation_abort(operation);
6921 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006922 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006923
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306924 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6925 * permission to output to a key object. */
6926 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6927 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006928 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006929 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006930
Gilles Peskine449bd832023-01-11 14:50:10 +01006931 status = psa_key_derivation_input_internal(operation,
6932 step, slot->attr.type,
6933 slot->key.data,
6934 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006935
Ryan Everett1b70a072024-01-04 10:32:49 +00006936 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006937
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006939}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006940
6941
6942
6943/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006944/* Key agreement */
6945/****************************************************************/
6946
Gilles Peskine449bd832023-01-11 14:50:10 +01006947psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6948 const uint8_t *key_buffer,
6949 size_t key_buffer_size,
6950 psa_algorithm_t alg,
6951 const uint8_t *peer_key,
6952 size_t peer_key_length,
6953 uint8_t *shared_secret,
6954 size_t shared_secret_size,
6955 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006956{
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006958#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006959 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006960 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6961 key_buffer_size, alg,
6962 peer_key, peer_key_length,
6963 shared_secret,
6964 shared_secret_size,
6965 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006966#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006967
6968#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6969 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006970 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006971 peer_key,
6972 peer_key_length,
6973 key_buffer,
6974 key_buffer_size,
6975 shared_secret,
6976 shared_secret_size,
6977 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006978#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6979
Gilles Peskine01d718c2018-09-18 12:01:02 +02006980 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006981 (void) attributes;
6982 (void) key_buffer;
6983 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006984 (void) peer_key;
6985 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006986 (void) shared_secret;
6987 (void) shared_secret_size;
6988 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006990 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006991}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006992
Aditya Deshpande17845b82022-10-13 17:21:01 +01006993/** Internal function for raw key agreement
6994 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006995 * to the driver's implementation if a driver is present.
6996 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006997 * (psa_key_agreement_raw_builtin).
6998 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006999static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7000 psa_key_slot_t *private_key,
7001 const uint8_t *peer_key,
7002 size_t peer_key_length,
7003 uint8_t *shared_secret,
7004 size_t shared_secret_size,
7005 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007006{
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7008 return PSA_ERROR_NOT_SUPPORTED;
7009 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007010
7011 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007012 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007013 };
7014
Gilles Peskine449bd832023-01-11 14:50:10 +01007015 return psa_driver_wrapper_key_agreement(&attributes,
7016 private_key->key.data,
7017 private_key->key.bytes, alg,
7018 peer_key, peer_key_length,
7019 shared_secret,
7020 shared_secret_size,
7021 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007022}
7023
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007024/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007025 * to potentially free embedded data structures and wipe confidential data.
7026 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007027static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7028 psa_key_derivation_step_t step,
7029 psa_key_slot_t *private_key,
7030 const uint8_t *peer_key,
7031 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007032{
7033 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007034 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007035 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007037
7038 /* Step 1: run the secret agreement algorithm to generate the shared
7039 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 status = psa_key_agreement_raw_internal(ka_alg,
7041 private_key,
7042 peer_key, peer_key_length,
7043 shared_secret,
7044 sizeof(shared_secret),
7045 &shared_secret_length);
7046 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007047 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007048 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007049
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007050 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007051 * the shared secret. A shared secret is permitted wherever a key
7052 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 status = psa_key_derivation_input_internal(operation, step,
7054 PSA_KEY_TYPE_DERIVE,
7055 shared_secret,
7056 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007057exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7059 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007060}
7061
Gilles Peskine449bd832023-01-11 14:50:10 +01007062psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7063 psa_key_derivation_step_t step,
7064 mbedtls_svc_key_id_t private_key,
7065 const uint8_t *peer_key,
7066 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007067{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007068 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007069 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007070 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007071
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7073 return PSA_ERROR_INVALID_ARGUMENT;
7074 }
Ronald Cron5c522922020-11-14 16:35:34 +01007075 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007076 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7077 if (status != PSA_SUCCESS) {
7078 return status;
7079 }
7080 status = psa_key_agreement_internal(operation, step,
7081 slot,
7082 peer_key, peer_key_length);
7083 if (status != PSA_SUCCESS) {
7084 psa_key_derivation_abort(operation);
7085 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007086 /* If a private key has been added as SECRET, we allow the derived
7087 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007089 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007091 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007092
Ryan Everett1b70a072024-01-04 10:32:49 +00007093 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007094
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007096}
7097
Gilles Peskine449bd832023-01-11 14:50:10 +01007098psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7099 mbedtls_svc_key_id_t private_key,
7100 const uint8_t *peer_key,
7101 size_t peer_key_length,
7102 uint8_t *output,
7103 size_t output_size,
7104 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007105{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007106 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007107 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007108 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007109 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007110
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007112 status = PSA_ERROR_INVALID_ARGUMENT;
7113 goto exit;
7114 }
Ronald Cron5c522922020-11-14 16:35:34 +01007115 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7117 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007118 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007119 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007120
Gilles Peskine3e561302022-04-14 00:17:15 +02007121 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7122 * for the output size. The PSA specification only guarantees that this
7123 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7124 * but it might be nice to allow smaller buffers if the output fits.
7125 * At the time of writing this comment, with only ECDH implemented,
7126 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7127 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7128 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007129 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7131 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007132 status = PSA_ERROR_BUFFER_TOO_SMALL;
7133 goto exit;
7134 }
7135
Gilles Peskine449bd832023-01-11 14:50:10 +01007136 status = psa_key_agreement_raw_internal(alg, slot,
7137 peer_key, peer_key_length,
7138 output, output_size,
7139 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007140
7141exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007142 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007143 /* If an error happens and is not handled properly, the output
7144 * may be used as a key to protect sensitive data. Arrange for such
7145 * a key to be random, which is likely to result in decryption or
7146 * verification errors. This is better than filling the buffer with
7147 * some constant data such as zeros, which would result in the data
7148 * being protected with a reproducible, easily knowable key.
7149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007151 *output_length = output_size;
7152 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007153
Ryan Everett1b70a072024-01-04 10:32:49 +00007154 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007155
Gilles Peskine449bd832023-01-11 14:50:10 +01007156 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007157}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007158
7159
Gilles Peskine30524eb2020-11-13 17:02:26 +01007160
Gilles Peskine86a440b2018-11-12 18:39:40 +01007161/****************************************************************/
7162/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007163/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007164
Gilles Peskine672a7712023-04-28 21:00:28 +02007165#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7166#include "entropy_poll.h"
7167#endif
7168
Gilles Peskine30524eb2020-11-13 17:02:26 +01007169/** Initialize the PSA random generator.
7170 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007171static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007172{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007173#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007175#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7176
Gilles Peskine30524eb2020-11-13 17:02:26 +01007177 /* Set default configuration if
7178 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007180 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 }
7182 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007183 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007185
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007187#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7188 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7189 /* The PSA entropy injection feature depends on using NV seed as an entropy
7190 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 mbedtls_entropy_add_source(&rng->entropy,
7192 mbedtls_nv_seed_poll, NULL,
7193 MBEDTLS_ENTROPY_BLOCK_SIZE,
7194 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007195#endif
7196
Gilles Peskine449bd832023-01-11 14:50:10 +01007197 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007198#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007199}
7200
7201/** Deinitialize the PSA random generator.
7202 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007203static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007204{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007205#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007206 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007207#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti83e0de82023-11-24 12:13:05 +01007208 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7209 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007210#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007211}
7212
7213/** Seed the PSA random generator.
7214 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007215static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007216{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007217#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7218 /* Do nothing: the external RNG seeds itself. */
7219 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007221#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007222 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7224 drbg_seed, sizeof(drbg_seed) - 1);
7225 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007226#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007227}
7228
Gilles Peskine449bd832023-01-11 14:50:10 +01007229psa_status_t psa_generate_random(uint8_t *output,
7230 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007231{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007232 GUARD_MODULE_INITIALIZED;
7233
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007234#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7235
7236 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7238 output, output_size,
7239 &output_length);
7240 if (status != PSA_SUCCESS) {
7241 return status;
7242 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007243 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007244 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007245 if (output_length != output_size) {
7246 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7247 }
7248 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007249
7250#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7251
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007253 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7255 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7256 output_size);
7257 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7258 output, request_size);
7259 if (ret != 0) {
7260 return mbedtls_to_psa_error(ret);
7261 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007262 output_size -= request_size;
7263 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007264 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007266#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007267}
7268
Gilles Peskine8814fc42020-12-14 15:33:44 +01007269/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007270 *
7271 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7272 * `psa_generate_random(...)`. The state parameter is ignored since the
7273 * PSA API doesn't support passing an explicit state.
7274 *
7275 * In the non-external case, psa_generate_random() calls an
7276 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7277 * and semantics as mbedtls_psa_get_random(). As an optimization,
7278 * instead of doing this back-and-forth between the PSA API and the
7279 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7280 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007282#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7283int mbedtls_psa_get_random(void *p_rng,
7284 unsigned char *output,
7285 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007286{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007287 /* This function takes a pointer to the RNG state because that's what
7288 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7289 * its own state internally and doesn't let the caller access that state.
7290 * So we just ignore the state parameter, and in practice we'll pass
7291 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007292 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007293 psa_status_t status = psa_generate_random(output, output_size);
7294 if (status == PSA_SUCCESS) {
7295 return 0;
7296 } else {
7297 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7298 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007299}
7300#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7301
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007302#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007303psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7304 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007305{
Gilles Peskine449bd832023-01-11 14:50:10 +01007306 if (global_data.initialized) {
7307 return PSA_ERROR_NOT_PERMITTED;
7308 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007309
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7311 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7312 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7313 return PSA_ERROR_INVALID_ARGUMENT;
7314 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007315
Gilles Peskine449bd832023-01-11 14:50:10 +01007316 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007317}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007318#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007319
Ronald Cron3772afe2021-02-08 16:10:05 +01007320/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007321 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007322 * \param type The key type
7323 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007324 *
7325 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007326 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007327 * \retval #PSA_ERROR_INVALID_ARGUMENT
7328 * The size in bits of the key is not valid.
7329 * \retval #PSA_ERROR_NOT_SUPPORTED
7330 * The type and/or the size in bits of the key or the combination of
7331 * the two is not supported.
7332 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007333static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007334 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007335{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007336 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007337
Gilles Peskine449bd832023-01-11 14:50:10 +01007338 if (key_type_is_raw_bytes(type)) {
7339 status = psa_validate_unstructured_key_bit_size(type, bits);
7340 if (status != PSA_SUCCESS) {
7341 return status;
7342 }
7343 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007344#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7346 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7347 return PSA_ERROR_NOT_SUPPORTED;
7348 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007349 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007350 return PSA_ERROR_NOT_SUPPORTED;
7351 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007352
Ronald Cron01b2aba2020-10-05 09:42:02 +02007353 /* Accept only byte-aligned keys, for the same reasons as
7354 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007355 if (bits % 8 != 0) {
7356 return PSA_ERROR_NOT_SUPPORTED;
7357 }
7358 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007359#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007360
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007361#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007363 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007364 return PSA_SUCCESS;
7365 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007366#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007367
Valerio Settia55f0422023-07-10 15:34:41 +02007368#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007369 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007370 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007371 return PSA_ERROR_NOT_SUPPORTED;
7372 }
7373 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007374#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007375 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007376 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007377 }
7378
Gilles Peskine449bd832023-01-11 14:50:10 +01007379 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007380}
7381
Ronald Cron55ed0592020-10-05 10:30:40 +02007382psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007383 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007384 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007385{
7386 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007387 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007388
Gilles Peskine449bd832023-01-11 14:50:10 +01007389 if ((attributes->domain_parameters == NULL) &&
7390 (attributes->domain_parameters_size != 0)) {
7391 return PSA_ERROR_INVALID_ARGUMENT;
7392 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007393
Gilles Peskine449bd832023-01-11 14:50:10 +01007394 if (key_type_is_raw_bytes(type)) {
7395 status = psa_generate_random(key_buffer, key_buffer_size);
7396 if (status != PSA_SUCCESS) {
7397 return status;
7398 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007399
David Brown12ca5032021-02-11 11:02:00 -07007400#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007401 if (type == PSA_KEY_TYPE_DES) {
7402 psa_des_set_key_parity(key_buffer, key_buffer_size);
7403 }
David Brown8107e312021-02-12 08:08:46 -07007404#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007405 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007406
Valerio Setti76df8c12023-07-11 14:11:28 +02007407#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007408 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7409 return mbedtls_psa_rsa_generate_key(attributes,
7410 key_buffer,
7411 key_buffer_size,
7412 key_buffer_length);
7413 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007414#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007415
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007416#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007417 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7418 return mbedtls_psa_ecp_generate_key(attributes,
7419 key_buffer,
7420 key_buffer_size,
7421 key_buffer_length);
7422 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007423#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007424
Valerio Settia55f0422023-07-10 15:34:41 +02007425#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007426 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7427 return mbedtls_psa_ffdh_generate_key(attributes,
7428 key_buffer,
7429 key_buffer_size,
7430 key_buffer_length);
7431 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007432#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007433 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 (void) key_buffer_length;
7435 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007436 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007437
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007439}
Darryl Green0c6575a2018-11-07 16:05:30 +00007440
Gilles Peskine449bd832023-01-11 14:50:10 +01007441psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7442 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007443{
7444 psa_status_t status;
7445 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007446 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007447 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007448
Ronald Cron81709fc2020-11-14 12:10:32 +01007449 *key = MBEDTLS_SVC_KEY_ID_INIT;
7450
Gilles Peskine0f84d622019-09-12 19:03:13 +02007451 /* Reject any attempt to create a zero-length key so that we don't
7452 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 if (psa_get_key_bits(attributes) == 0) {
7454 return PSA_ERROR_INVALID_ARGUMENT;
7455 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007456
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007457 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007458 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7459 return PSA_ERROR_INVALID_ARGUMENT;
7460 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007461
Gilles Peskine449bd832023-01-11 14:50:10 +01007462 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7463 &slot, &driver);
7464 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007465 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007466 }
Gilles Peskine11792082019-08-06 18:36:36 +02007467
Ronald Cron977c2472020-10-13 08:32:21 +02007468 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307469 * storage ( thus not in the case of generating a key in a secure element
7470 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7471 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 if (slot->key.data == NULL) {
7473 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7474 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007475 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 attributes->core.type, attributes->core.bits);
7477 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007478 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007480
7481 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 attributes->core.type,
7483 attributes->core.bits);
7484 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007485 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 attributes, &key_buffer_size);
7487 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007488 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 }
Ronald Cron977c2472020-10-13 08:32:21 +02007490 }
Ronald Cron977c2472020-10-13 08:32:21 +02007491
Gilles Peskine449bd832023-01-11 14:50:10 +01007492 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7493 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007494 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007495 }
Ronald Cron977c2472020-10-13 08:32:21 +02007496 }
7497
Gilles Peskine449bd832023-01-11 14:50:10 +01007498 status = psa_driver_wrapper_generate_key(attributes,
7499 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007500
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 if (status != PSA_SUCCESS) {
7502 psa_remove_key_data_from_memory(slot);
7503 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007504
Gilles Peskine11792082019-08-06 18:36:36 +02007505exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007506 if (status == PSA_SUCCESS) {
7507 status = psa_finish_key_creation(slot, driver, key);
7508 }
7509 if (status != PSA_SUCCESS) {
7510 psa_fail_key_creation(slot, driver);
7511 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007512
Gilles Peskine449bd832023-01-11 14:50:10 +01007513 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007514}
7515
Gilles Peskinee59236f2018-01-27 23:32:46 +01007516/****************************************************************/
7517/* Module setup */
7518/****************************************************************/
7519
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007520#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007521psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007522 void (* entropy_init)(mbedtls_entropy_context *ctx),
7523 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007524{
Gilles Peskine449bd832023-01-11 14:50:10 +01007525 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7526 return PSA_ERROR_BAD_STATE;
7527 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007528 global_data.rng.entropy_init = entropy_init;
7529 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007531}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007532#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007533
Gilles Peskine449bd832023-01-11 14:50:10 +01007534void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007535{
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 psa_wipe_all_key_slots();
Valerio Setti83e0de82023-11-24 12:13:05 +01007537 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7538 mbedtls_psa_random_free(&global_data.rng);
7539 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007540 /* Wipe all remaining data, including configuration.
7541 * In particular, this sets all state indicator to the value
7542 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007543 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007544
7545 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007547}
7548
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007549#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7550/** Recover a transaction that was interrupted by a power failure.
7551 *
7552 * This function is called during initialization, before psa_crypto_init()
7553 * returns. If this function returns a failure status, the initialization
7554 * fails.
7555 */
7556static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007557 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007558{
Gilles Peskine449bd832023-01-11 14:50:10 +01007559 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007560 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7561 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 /* TODO - fall through to the failure case until this
7563 * is implemented.
7564 * https://github.com/ARMmbed/mbed-crypto/issues/218
7565 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007566 default:
7567 /* We found an unsupported transaction in the storage.
7568 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007569 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007570 }
7571}
7572#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7573
Gilles Peskine449bd832023-01-11 14:50:10 +01007574psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007575{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007576 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007577
Gilles Peskinec6b69072018-11-20 21:42:52 +01007578 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 if (global_data.initialized != 0) {
7580 return PSA_SUCCESS;
7581 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007582
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007583 /* Init drivers */
7584 status = psa_driver_wrapper_init();
7585 if (status != PSA_SUCCESS) {
7586 goto exit;
7587 }
7588 global_data.drivers_initialized = 1;
7589
Valerio Setti402cfba2023-11-13 10:24:32 +01007590 status = psa_initialize_key_slots();
7591 if (status != PSA_SUCCESS) {
7592 goto exit;
7593 }
7594
Gilles Peskine30524eb2020-11-13 17:02:26 +01007595 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007597 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007598 status = mbedtls_psa_random_seed(&global_data.rng);
7599 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007600 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007602 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007603
Gilles Peskine4b734222019-07-24 15:56:31 +02007604#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007605 status = psa_crypto_load_transaction();
7606 if (status == PSA_SUCCESS) {
7607 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7608 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007609 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 }
7611 status = psa_crypto_stop_transaction();
7612 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007613 /* There's no transaction to complete. It's all good. */
7614 status = PSA_SUCCESS;
7615 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007616#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007617
Gilles Peskinec6b69072018-11-20 21:42:52 +01007618 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007619 global_data.initialized = 1;
7620
7621exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007622 if (status != PSA_SUCCESS) {
7623 mbedtls_psa_crypto_free();
7624 }
7625 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007626}
7627
Yanray Wangffc3c482023-07-11 12:01:04 +08007628#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007629psa_status_t psa_crypto_driver_pake_get_password_len(
7630 const psa_crypto_driver_pake_inputs_t *inputs,
7631 size_t *password_len)
7632{
7633 if (inputs->password_len == 0) {
7634 return PSA_ERROR_BAD_STATE;
7635 }
7636
7637 *password_len = inputs->password_len;
7638
7639 return PSA_SUCCESS;
7640}
7641
7642psa_status_t psa_crypto_driver_pake_get_password(
7643 const psa_crypto_driver_pake_inputs_t *inputs,
7644 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7645{
7646 if (inputs->password_len == 0) {
7647 return PSA_ERROR_BAD_STATE;
7648 }
7649
7650 if (buffer_size < inputs->password_len) {
7651 return PSA_ERROR_BUFFER_TOO_SMALL;
7652 }
7653
7654 memcpy(buffer, inputs->password, inputs->password_len);
7655 *buffer_length = inputs->password_len;
7656
7657 return PSA_SUCCESS;
7658}
7659
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007660psa_status_t psa_crypto_driver_pake_get_user_len(
7661 const psa_crypto_driver_pake_inputs_t *inputs,
7662 size_t *user_len)
7663{
7664 if (inputs->user_len == 0) {
7665 return PSA_ERROR_BAD_STATE;
7666 }
7667
7668 *user_len = inputs->user_len;
7669
7670 return PSA_SUCCESS;
7671}
7672
7673psa_status_t psa_crypto_driver_pake_get_user(
7674 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007675 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007676{
7677 if (inputs->user_len == 0) {
7678 return PSA_ERROR_BAD_STATE;
7679 }
7680
Przemek Stekielc0e62502023-03-14 11:49:36 +01007681 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007682 return PSA_ERROR_BUFFER_TOO_SMALL;
7683 }
7684
Przemek Stekielc0e62502023-03-14 11:49:36 +01007685 memcpy(user_id, inputs->user, inputs->user_len);
7686 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007687
7688 return PSA_SUCCESS;
7689}
7690
7691psa_status_t psa_crypto_driver_pake_get_peer_len(
7692 const psa_crypto_driver_pake_inputs_t *inputs,
7693 size_t *peer_len)
7694{
7695 if (inputs->peer_len == 0) {
7696 return PSA_ERROR_BAD_STATE;
7697 }
7698
7699 *peer_len = inputs->peer_len;
7700
7701 return PSA_SUCCESS;
7702}
7703
7704psa_status_t psa_crypto_driver_pake_get_peer(
7705 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007706 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007707{
7708 if (inputs->peer_len == 0) {
7709 return PSA_ERROR_BAD_STATE;
7710 }
7711
Przemek Stekielc0e62502023-03-14 11:49:36 +01007712 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007713 return PSA_ERROR_BUFFER_TOO_SMALL;
7714 }
7715
Przemek Stekielc0e62502023-03-14 11:49:36 +01007716 memcpy(peer_id, inputs->peer, inputs->peer_len);
7717 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007718
7719 return PSA_SUCCESS;
7720}
7721
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007722psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7723 const psa_crypto_driver_pake_inputs_t *inputs,
7724 psa_pake_cipher_suite_t *cipher_suite)
7725{
7726 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7727 return PSA_ERROR_BAD_STATE;
7728 }
7729
7730 *cipher_suite = inputs->cipher_suite;
7731
7732 return PSA_SUCCESS;
7733}
7734
Neil Armstronga7d08c32022-06-01 18:21:20 +02007735psa_status_t psa_pake_setup(
7736 psa_pake_operation_t *operation,
7737 const psa_pake_cipher_suite_t *cipher_suite)
7738{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007739 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007740
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007741 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007742 status = PSA_ERROR_BAD_STATE;
7743 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007744 }
7745
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007746 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007747 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007748 status = PSA_ERROR_INVALID_ARGUMENT;
7749 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007750 }
7751
Przemek Stekiel51eac532022-12-07 11:04:51 +01007752 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7753
Przemek Stekiele12ed362022-12-21 12:54:46 +01007754 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007755 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7756 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007757 operation->data.inputs.cipher_suite = *cipher_suite;
7758
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007759#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007760 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007761 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007762 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007763
David Horstmann16f01512023-06-14 17:21:07 +01007764 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007765 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007766 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007767#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007768 {
7769 status = PSA_ERROR_NOT_SUPPORTED;
7770 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007771 }
7772
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007773 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7774
Przemek Stekiel51eac532022-12-07 11:04:51 +01007775 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007776exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007777 psa_pake_abort(operation);
7778 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007779}
7780
7781psa_status_t psa_pake_set_password_key(
7782 psa_pake_operation_t *operation,
7783 mbedtls_svc_key_id_t password)
7784{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007785 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007786 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7787 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007788 psa_key_attributes_t attributes;
7789 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007790
Przemek Stekiel51eac532022-12-07 11:04:51 +01007791 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007792 status = PSA_ERROR_BAD_STATE;
7793 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007794 }
7795
Przemek Stekiel6c764412022-11-22 14:05:12 +01007796 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7797 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007798 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007799 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007800 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007801 }
7802
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007803 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007804 .core = slot->attr
7805 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007806
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007807 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007808
Przemek Stekiel51eac532022-12-07 11:04:51 +01007809 if (type != PSA_KEY_TYPE_PASSWORD &&
7810 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7811 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007812 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007813 }
7814
Przemek Stekiel51eac532022-12-07 11:04:51 +01007815 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7816 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007817 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007818 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007819 }
7820
7821 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7822 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007823 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007824exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007825 if (status != PSA_SUCCESS) {
7826 psa_pake_abort(operation);
7827 }
Ryan Everett1b70a072024-01-04 10:32:49 +00007828 unlock_status = psa_unregister_read(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007829 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007830}
7831
7832psa_status_t psa_pake_set_user(
7833 psa_pake_operation_t *operation,
7834 const uint8_t *user_id,
7835 size_t user_id_len)
7836{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007837 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007838
7839 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007840 status = PSA_ERROR_BAD_STATE;
7841 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007842 }
7843
Przemek Stekiel51eac532022-12-07 11:04:51 +01007844 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007845 status = PSA_ERROR_INVALID_ARGUMENT;
7846 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007847 }
7848
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007849 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007850 status = PSA_ERROR_BAD_STATE;
7851 goto exit;
7852 }
7853
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007854 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7855 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007856 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7857 goto exit;
7858 }
7859
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007860 memcpy(operation->data.inputs.user, user_id, user_id_len);
7861 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007862
7863 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007864exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007865 psa_pake_abort(operation);
7866 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007867}
7868
7869psa_status_t psa_pake_set_peer(
7870 psa_pake_operation_t *operation,
7871 const uint8_t *peer_id,
7872 size_t peer_id_len)
7873{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007874 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007875
7876 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007877 status = PSA_ERROR_BAD_STATE;
7878 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007879 }
7880
Przemek Stekiel51eac532022-12-07 11:04:51 +01007881 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007882 status = PSA_ERROR_INVALID_ARGUMENT;
7883 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007884 }
7885
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007886 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007887 status = PSA_ERROR_BAD_STATE;
7888 goto exit;
7889 }
7890
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007891 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7892 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007893 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7894 goto exit;
7895 }
7896
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007897 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7898 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007899
7900 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007901exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007902 psa_pake_abort(operation);
7903 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007904}
7905
7906psa_status_t psa_pake_set_role(
7907 psa_pake_operation_t *operation,
7908 psa_pake_role_t role)
7909{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007910 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007911
Przemek Stekiel51eac532022-12-07 11:04:51 +01007912 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007913 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007914 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007915 }
7916
Przemek Stekiel09104b82023-03-06 14:11:51 +01007917 switch (operation->alg) {
7918#if defined(PSA_WANT_ALG_JPAKE)
7919 case PSA_ALG_JPAKE:
7920 if (role == PSA_PAKE_ROLE_NONE) {
7921 return PSA_SUCCESS;
7922 }
7923 status = PSA_ERROR_INVALID_ARGUMENT;
7924 break;
7925#endif
7926 default:
7927 (void) role;
7928 status = PSA_ERROR_NOT_SUPPORTED;
7929 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007930 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007931exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007932 psa_pake_abort(operation);
7933 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007934}
7935
David Horstmanne7f21e62023-05-12 18:17:21 +01007936/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007937#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007938static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007939 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007940{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007941 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007942 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007943 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007944
David Horstmann024e5c52023-06-14 15:48:21 +01007945 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007946 is_x1 = (stage->outputs < 1);
7947 } else {
7948 is_x1 = (stage->inputs < 1);
7949 }
7950
David Horstmann74a3d8c2023-06-14 18:28:19 +01007951 key_share_step = is_x1 ?
7952 PSA_JPAKE_X1_STEP_KEY_SHARE :
7953 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007954 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007955 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7956 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7957 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7958 } else {
7959 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007960 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01007961 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01007962}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007963#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007964
Przemek Stekiel2797d372022-12-22 11:19:22 +01007965static psa_status_t psa_pake_complete_inputs(
7966 psa_pake_operation_t *operation)
7967{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007968 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007969 /* Create copy of the inputs on stack as inputs share memory
7970 with the driver context which will be setup by the driver. */
7971 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007972
Przemek Stekielfde11282023-03-13 16:06:09 +01007973 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007974 return PSA_ERROR_BAD_STATE;
7975 }
7976
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007977 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007978 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7979 return PSA_ERROR_BAD_STATE;
7980 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007981 }
7982
Przemek Stekiel18620a32023-01-17 16:34:52 +01007983 /* Clear driver context */
7984 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7985
7986 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007987
7988 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01007989 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007990
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007991 /* User and peer are translated to role. */
7992 mbedtls_free(inputs.user);
7993 mbedtls_free(inputs.peer);
7994
Przemek Stekiel2797d372022-12-22 11:19:22 +01007995 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007996#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007997 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007998 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007999 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008000#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008001 {
8002 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008003 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008004 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008005 return status;
8006}
8007
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008008#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008009static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008010 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008011 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008012 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008013{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008014 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8015 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8016 step != PSA_PAKE_STEP_ZK_PROOF) {
8017 return PSA_ERROR_INVALID_ARGUMENT;
8018 }
8019
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008020 psa_jpake_computation_stage_t *computation_stage =
8021 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008022
David Horstmann5da95602023-06-08 15:37:12 +01008023 if (computation_stage->round != PSA_JPAKE_FIRST &&
8024 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008025 return PSA_ERROR_BAD_STATE;
8026 }
8027
David Horstmanne7f21e62023-05-12 18:17:21 +01008028 /* Check that the step we are given is the one we were expecting */
8029 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008030 return PSA_ERROR_BAD_STATE;
8031 }
8032
David Horstmanne7f21e62023-05-12 18:17:21 +01008033 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8034 computation_stage->inputs == 0 &&
8035 computation_stage->outputs == 0) {
8036 /* Start of the round, so function decides whether we are inputting
8037 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008038 computation_stage->io_mode = io_mode;
8039 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008040 /* Middle of the round so the mode we are in must match the function
8041 * called by the user */
8042 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008043 }
8044
8045 return PSA_SUCCESS;
8046}
8047
David Horstmanne7f21e62023-05-12 18:17:21 +01008048static psa_status_t psa_jpake_epilogue(
8049 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008050 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008051{
David Horstmanne7f21e62023-05-12 18:17:21 +01008052 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008053 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008054
David Horstmanne7f21e62023-05-12 18:17:21 +01008055 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8056 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008057 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008058 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008059 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008060 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008061 }
8062 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008063 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008064 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008065 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008066 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008067 }
8068 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008069 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8070 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008071 /* End of a round, move to the next round */
8072 stage->inputs = 0;
8073 stage->outputs = 0;
8074 stage->round++;
8075 }
8076 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008077 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008078 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008079 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008080 return PSA_SUCCESS;
8081}
David Horstmanne7f21e62023-05-12 18:17:21 +01008082
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008083#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008084
Neil Armstronga7d08c32022-06-01 18:21:20 +02008085psa_status_t psa_pake_output(
8086 psa_pake_operation_t *operation,
8087 psa_pake_step_t step,
8088 uint8_t *output,
8089 size_t output_size,
8090 size_t *output_length)
8091{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008093 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008094 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008095
8096 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008097 status = psa_pake_complete_inputs(operation);
8098 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008099 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008100 }
8101 }
8102
8103 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008104 status = PSA_ERROR_BAD_STATE;
8105 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008106 }
8107
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008108 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008109 status = PSA_ERROR_INVALID_ARGUMENT;
8110 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008111 }
8112
Przemek Stekiele12ed362022-12-21 12:54:46 +01008113 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008114#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008115 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008116 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008117 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008118 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008119 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008120 driver_step = convert_jpake_computation_stage_to_driver_step(
8121 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008122 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008123#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008124 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008125 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008126 status = PSA_ERROR_NOT_SUPPORTED;
8127 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008128 }
8129
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008130 status = psa_driver_wrapper_pake_output(operation, driver_step,
8131 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008132
8133 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008134 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008135 }
8136
8137 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008138#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008139 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008140 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008141 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008142 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008143 }
8144 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008145#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008146 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008147 status = PSA_ERROR_NOT_SUPPORTED;
8148 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008149 }
8150
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008151 return PSA_SUCCESS;
8152exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008153 psa_pake_abort(operation);
8154 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008155}
8156
8157psa_status_t psa_pake_input(
8158 psa_pake_operation_t *operation,
8159 psa_pake_step_t step,
8160 const uint8_t *input,
8161 size_t input_length)
8162{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008163 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008164 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008165 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8166 operation->primitive,
8167 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008168
8169 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008170 status = psa_pake_complete_inputs(operation);
8171 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008172 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008173 }
8174 }
8175
8176 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008177 status = PSA_ERROR_BAD_STATE;
8178 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008179 }
8180
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008181 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008182 status = PSA_ERROR_INVALID_ARGUMENT;
8183 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008184 }
8185
Przemek Stekiele12ed362022-12-21 12:54:46 +01008186 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008187#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008188 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008189 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008190 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008191 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008192 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008193 driver_step = convert_jpake_computation_stage_to_driver_step(
8194 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008195 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008196#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008197 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008198 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008199 status = PSA_ERROR_NOT_SUPPORTED;
8200 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008201 }
8202
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008203 status = psa_driver_wrapper_pake_input(operation, driver_step,
8204 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008205
8206 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008207 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008208 }
8209
8210 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008211#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008212 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008213 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008214 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008215 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008216 }
8217 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008218#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008219 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008220 status = PSA_ERROR_NOT_SUPPORTED;
8221 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008222 }
8223
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008224 return PSA_SUCCESS;
8225exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008226 psa_pake_abort(operation);
8227 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008228}
8229
8230psa_status_t psa_pake_get_implicit_key(
8231 psa_pake_operation_t *operation,
8232 psa_key_derivation_operation_t *output)
8233{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008234 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008235 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008236 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008237 size_t shared_key_len = 0;
8238
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008239 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008240 status = PSA_ERROR_BAD_STATE;
8241 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008242 }
8243
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008244#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008245 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008246 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008247 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008248 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008249 status = PSA_ERROR_BAD_STATE;
8250 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008251 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008252 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008253#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008254 {
8255 status = PSA_ERROR_NOT_SUPPORTED;
8256 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008257 }
8258
Przemek Stekiel0c781802022-11-29 14:53:13 +01008259 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8260 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008261 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008262 &shared_key_len);
8263
8264 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008265 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008266 }
8267
8268 status = psa_key_derivation_input_bytes(output,
8269 PSA_KEY_DERIVATION_INPUT_SECRET,
8270 shared_key,
8271 shared_key_len);
8272
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008273 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008274exit:
8275 abort_status = psa_pake_abort(operation);
8276 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008277}
8278
8279psa_status_t psa_pake_abort(
8280 psa_pake_operation_t *operation)
8281{
Przemek Stekield69dca92023-01-31 19:59:20 +01008282 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008283
Przemek Stekield69dca92023-01-31 19:59:20 +01008284 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008285 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008286 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008287
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008288 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8289 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008290 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008291 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008292 }
8293 if (operation->data.inputs.user != NULL) {
8294 mbedtls_free(operation->data.inputs.user);
8295 }
8296 if (operation->data.inputs.peer != NULL) {
8297 mbedtls_free(operation->data.inputs.peer);
8298 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008299 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008300 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008301
Przemek Stekield69dca92023-01-31 19:59:20 +01008302 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008303}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008304#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008305
Gilles Peskinee59236f2018-01-27 23:32:46 +01008306#endif /* MBEDTLS_PSA_CRYPTO_C */