blob: c81666818c27c1e710771801e3dd8459463270ee [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinee59236f2018-01-27 23:32:46 +01007 */
8
Gilles Peskinedb09ef62020-06-03 01:43:33 +02009#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010010#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010011
12#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030013
John Durkop07cc04a2020-11-16 22:08:34 -080014#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
15#include "check_crypto_config.h"
16#endif
17
Gilles Peskinee59236f2018-01-27 23:32:46 +010018#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010019#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010020
Ronald Crond6d28882020-12-14 14:56:02 +010021#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010022#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010023#include "psa_crypto_invasive.h"
Xiaokang Qiane9c39c42023-08-09 08:50:19 +000024#include "psa_crypto_driver_wrappers.h"
Xiaokang Qianfe9666b2023-09-11 10:36:20 +000025#include "psa_crypto_driver_wrappers_no_static.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010027#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010028#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010029#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010031#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020033#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020034#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskineb2b64d32020-12-14 16:43:58 +010040#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010041
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include <stdlib.h>
43#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010046#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020047#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000048#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020049#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010050#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020051#include "mbedtls/chacha20.h"
52#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/cipher.h"
54#include "mbedtls/ccm.h"
55#include "mbedtls/cmac.h"
Dave Rodgman78701152023-08-29 14:20:18 +010056#include "mbedtls/constant_time.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020058#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010059#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010060#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/error.h"
62#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/md5.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010064#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000065#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010066#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000067#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/sha1.h"
71#include "mbedtls/sha256.h"
72#include "mbedtls/sha512.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010073#include "mbedtls/psa_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020075#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
76 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
77 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020078#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020079#endif
80
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010081/****************************************************************/
82/* Global data, support functions and library management */
83/****************************************************************/
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020086{
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020088}
89
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010090/* Values for psa_global_data_t::rng_state */
91#define RNG_NOT_INITIALIZED 0
92#define RNG_INITIALIZED 1
93#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010094
Gilles Peskine449bd832023-01-11 14:50:10 +010095typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +010096 uint8_t initialized;
97 uint8_t rng_state;
98 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +010099 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100100} psa_global_data_t;
101
102static psa_global_data_t global_data;
103
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100104#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
105mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
106 &global_data.rng.drbg;
107#endif
108
itayzafrir0adf0fc2018-09-06 16:24:41 +0300109#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (global_data.initialized == 0) \
111 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300112
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100113int psa_can_do_hash(psa_algorithm_t hash_alg)
114{
115 (void) hash_alg;
116 return global_data.drivers_initialized;
117}
Valerio Settic6f004f2023-12-12 11:27:36 +0100118
Valerio Setti1fff4f22023-12-28 14:19:34 +0100119int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
Valerio Settic6f004f2023-12-12 11:27:36 +0100120{
Valerio Setti1fff4f22023-12-28 14:19:34 +0100121 (void) key_type;
Valerio Settic6f004f2023-12-12 11:27:36 +0100122 (void) cipher_alg;
123 return global_data.drivers_initialized;
124}
125
126
Valerio Settia55f0422023-07-10 15:34:41 +0200127#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200128 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200129 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200130static int psa_is_dh_key_size_valid(size_t bits)
131{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200132 if (bits != 2048 && bits != 3072 && bits != 4096 &&
133 bits != 6144 && bits != 8192) {
134 return 0;
135 }
136
137 return 1;
138}
Valerio Settia55f0422023-07-10 15:34:41 +0200139#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200140 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200141 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100144{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100145 /* Mbed TLS error codes can combine a high-level error code and a
146 * low-level error code. The low-level error usually reflects the
147 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 int low_level_ret = -(-ret & 0x007f);
149 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100150 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100152
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100153#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100154 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
155 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100157 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
158 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100159#endif
160
161#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200162 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
163 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
164 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
165 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
166 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200168 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200170 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100172#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200173
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100174#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000175 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100178#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100179
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100180#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100185#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100186
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100187#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100190#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200191
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100192#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200193 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200195 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100197#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200198
Dave Rodgman509b5672023-08-16 19:26:23 +0100199#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100200 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100204 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100212 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100214#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
217 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100218 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
219 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100222 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
223 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100227#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100228
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100229#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100232#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
Gilles Peskinee59236f2018-01-27 23:32:46 +0100234 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
235 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
236 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100238
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100239#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100240 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200242 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100244 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100246#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100247
Gilles Peskine82e57d12020-11-13 21:31:17 +0100248#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100250 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
251 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100252 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100254 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
255 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100257 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100259#endif
260
Dave Rodgmandea266f2023-08-31 11:52:43 +0100261#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100262 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100264 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100268#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100271#endif
272#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100273
Dave Rodgman509b5672023-08-16 19:26:23 +0100274#if defined(MBEDTLS_BIGNUM_C)
275#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100276 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100278#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100279 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100281 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100283 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100285 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100287 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100289 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100291 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100293#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100294
Dave Rodgman509b5672023-08-16 19:26:23 +0100295#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100296 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100298 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
299 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100301#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
302 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100303 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100305#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
307 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100311 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
312 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100314 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100316 case MBEDTLS_ERR_PK_INVALID_ALG:
317 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
318 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100320 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200322 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100324#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100325
Gilles Peskineff2d2002019-05-06 15:26:23 +0200326 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200328 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200330
Dave Rodgman509b5672023-08-16 19:26:23 +0100331#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100332 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100334 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100336 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100338 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100340 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
341 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100343 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100345 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100349#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200350
Dave Rodgman1dab4452023-09-01 09:59:51 +0100351#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300352 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300353 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300355 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300357 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300359 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
360 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300362 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100364 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100366
Dave Rodgman509b5672023-08-16 19:26:23 +0100367#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000368 case MBEDTLS_ERR_ECP_IN_PROGRESS:
369 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100370#endif
371#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000372
Janos Follatha13b9052019-11-22 12:48:59 +0000373 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300375
Gilles Peskinee59236f2018-01-27 23:32:46 +0100376 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100378 }
379}
380
Paul Elliottdc42ca82023-02-24 18:11:59 +0000381/**
382 * \brief For output buffers which contain "tags"
383 * (outputs that may be checked for validity like
384 * hashes, MACs and signatures), fill the unused
385 * part of the output buffer (the whole buffer on
386 * error, the trailing part on success) with
387 * something that isn't a valid tag (barring an
388 * attack on the tag and deliberately-crafted
389 * input), in case the caller doesn't check the
390 * return status properly.
391 *
392 * \param output_buffer Pointer to buffer to wipe. May not be NULL
393 * unless \p output_buffer_size is zero.
394 * \param status Status of function called to generate
395 * output_buffer originally
396 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
397 * could be NULL.
398 * \param output_buffer_length Length of data written to output_buffer, must be
399 * less than \p output_buffer_size
400 */
401static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000402 size_t output_buffer_size, size_t output_buffer_length)
403{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000404 size_t offset = 0;
405
406 if (output_buffer_size == 0) {
407 /* If output_buffer_size is 0 then we have nothing to do. We must not
408 call memset because output_buffer may be NULL in this case */
409 return;
410 }
411
412 if (status == PSA_SUCCESS) {
413 offset = output_buffer_length;
414 }
415
416 memset(output_buffer + offset, '!', output_buffer_size - offset);
417}
418
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
421 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200422{
423 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200425 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200426 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200427 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200428 case PSA_KEY_TYPE_PASSWORD:
429 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200430 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100431#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200432 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (bits != 128 && bits != 192 && bits != 256) {
434 return PSA_ERROR_INVALID_ARGUMENT;
435 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200436 break;
437#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200438#if defined(PSA_WANT_KEY_TYPE_ARIA)
439 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (bits != 128 && bits != 192 && bits != 256) {
441 return PSA_ERROR_INVALID_ARGUMENT;
442 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200443 break;
444#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100445#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (bits != 128 && bits != 192 && bits != 256) {
448 return PSA_ERROR_INVALID_ARGUMENT;
449 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200450 break;
451#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100452#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200453 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (bits != 64 && bits != 128 && bits != 192) {
455 return PSA_ERROR_INVALID_ARGUMENT;
456 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200457 break;
458#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100459#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200460 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (bits != 256) {
462 return PSA_ERROR_INVALID_ARGUMENT;
463 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200464 break;
465#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200468 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (bits % 8 != 0) {
470 return PSA_ERROR_INVALID_ARGUMENT;
471 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200474}
475
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100476/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100477 *
Steven Cooremancd640932021-03-08 12:00:27 +0100478 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
479 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100480 *
481 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100482 * \param[in] key_type The key type of the key to be used with the
483 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100484 *
485 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100486 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100487 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100488 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100489 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100490MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100491 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100493{
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (PSA_ALG_IS_HMAC(algorithm)) {
495 if (key_type == PSA_KEY_TYPE_HMAC) {
496 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100497 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100498 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
501 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
502 * key. */
503 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
504 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
505 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
506 * the block length (larger than 1) for block ciphers. */
507 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
508 return PSA_SUCCESS;
509 }
510 }
511 }
512
513 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100514}
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
517 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200518{
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (slot->key.data != NULL) {
520 return PSA_ERROR_ALREADY_EXISTS;
521 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 slot->key.data = mbedtls_calloc(1, buffer_length);
524 if (slot->key.data == NULL) {
525 return PSA_ERROR_INSUFFICIENT_MEMORY;
526 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200527
Ronald Cronea0f8a62020-11-25 17:52:23 +0100528 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200530}
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
533 const uint8_t *data,
534 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200535{
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 psa_status_t status = psa_allocate_buffer_to_slot(slot,
537 data_length);
538 if (status != PSA_SUCCESS) {
539 return status;
540 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 memcpy(slot->key.data, data, data_length);
543 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200544}
545
Ronald Crona0fe59f2020-11-29 11:14:53 +0100546psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100547 const psa_key_attributes_t *attributes,
548 const uint8_t *data, size_t data_length,
549 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100552 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
553 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200555 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (data_length == 0) {
557 return PSA_ERROR_NOT_SUPPORTED;
558 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (key_type_is_raw_bytes(type)) {
561 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
564 *bits);
565 if (status != PSA_SUCCESS) {
566 return status;
567 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200568
Ronald Crondd04d422020-11-28 15:14:42 +0100569 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100571 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 return PSA_SUCCESS;
575 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200576#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200577 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100578 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200579 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100580 return PSA_ERROR_INVALID_ARGUMENT;
581 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200582 return mbedtls_psa_ffdh_import_key(attributes,
583 data, data_length,
584 key_buffer, key_buffer_size,
585 key_buffer_length,
586 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100587 }
Valerio Settia55f0422023-07-10 15:34:41 +0200588#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200589 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200590#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
592 if (PSA_KEY_TYPE_IS_ECC(type)) {
593 return mbedtls_psa_ecp_import_key(attributes,
594 data, data_length,
595 key_buffer, key_buffer_size,
596 key_buffer_length,
597 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100598 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200599#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800600 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200601#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
602 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
604 if (PSA_KEY_TYPE_IS_RSA(type)) {
605 return mbedtls_psa_rsa_import_key(attributes,
606 data, data_length,
607 key_buffer, key_buffer_size,
608 key_buffer_length,
609 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200610 }
Valerio Settife478902023-07-25 12:27:19 +0200611#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
612 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800613 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100614 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100617}
618
Gilles Peskinef603c712019-01-19 13:40:11 +0100619/** Calculate the intersection of two algorithm usage policies.
620 *
621 * Return 0 (which allows no operation) on incompatibility.
622 */
623static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100624 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100625 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100627{
Gilles Peskine549ea862019-05-22 11:45:59 +0200628 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (alg1 == alg2) {
630 return alg1;
631 }
Steven Cooreman03488022021-02-18 12:07:20 +0100632 /* If the policies are from the same hash-and-sign family, check
633 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
635 PSA_ALG_IS_SIGN_HASH(alg2) &&
636 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
637 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
638 return alg2;
639 }
640 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
641 return alg1;
642 }
Steven Cooreman03488022021-02-18 12:07:20 +0100643 }
644 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100645 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100646 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
648 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
649 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
650 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
651 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100652 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100653
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100654 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
656 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
657 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
658 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100659 }
660 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
662 (alg1_len <= alg2_len)) {
663 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100664 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
666 (alg2_len <= alg1_len)) {
667 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100668 }
669 }
670 /* If the policies are from the same MAC family, check whether one
671 * of them is a minimum-MAC-length policy. Calculate the most
672 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
674 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
675 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100676 /* Validate the combination of key type and algorithm. Since the base
677 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
679 return 0;
680 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100681
Steven Cooreman31a876d2021-03-03 20:47:40 +0100682 /* Get the (exact or at-least) output lengths for both sides of the
683 * requested intersection. None of the currently supported algorithms
684 * have an output length dependent on the actual key size, so setting it
685 * to a bogus value of 0 is currently OK.
686 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100687 * Note that for at-least-this-length wildcard algorithms, the output
688 * length is set to the shortest allowed length, which allows us to
689 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
691 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100692 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100693
Steven Cooremana1d83222021-02-25 10:20:29 +0100694 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
696 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
697 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100698 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100699
700 /* If only one is an at-least-this-length policy, the intersection would
701 * be the other (fixed-length) policy as long as said fixed length is
702 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
704 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100705 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
707 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100708 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100709
Steven Cooremancd640932021-03-08 12:00:27 +0100710 /* If none of them are wildcards, check whether they define the same tag
711 * length. This is still possible here when one is default-length and
712 * the other specific-length. Ensure to always return the
713 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (alg1_len == alg2_len) {
715 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
716 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100717 }
718 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100720}
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722static int psa_key_algorithm_permits(psa_key_type_t key_type,
723 psa_algorithm_t policy_alg,
724 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200725{
Gilles Peskine549ea862019-05-22 11:45:59 +0200726 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (requested_alg == policy_alg) {
728 return 1;
729 }
Steven Cooreman03488022021-02-18 12:07:20 +0100730 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
731 * and requested_alg is the same hash-and-sign family with any hash,
732 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
734 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
735 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
736 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100737 }
738 /* If policy_alg is a wildcard AEAD algorithm of the same base as
739 * the requested algorithm, check the requested tag length to be
740 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (PSA_ALG_IS_AEAD(policy_alg) &&
742 PSA_ALG_IS_AEAD(requested_alg) &&
743 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
744 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
745 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
746 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
747 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100748 }
Steven Cooremancd640932021-03-08 12:00:27 +0100749 /* If policy_alg is a MAC algorithm of the same base as the requested
750 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (PSA_ALG_IS_MAC(policy_alg) &&
752 PSA_ALG_IS_MAC(requested_alg) &&
753 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
754 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100755 /* Validate the combination of key type and algorithm. Since the policy
756 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
758 return 0;
759 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100760
Steven Cooreman31a876d2021-03-03 20:47:40 +0100761 /* Get both the requested output length for the algorithm which is to be
762 * verified, and the default output length for the base algorithm.
763 * Note that none of the currently supported algorithms have an output
764 * length dependent on actual key size, so setting it to a bogus value
765 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100766 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100768 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 key_type, 0,
770 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100771
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100772 /* If the policy is default-length, only allow an algorithm with
773 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
775 return requested_output_length == default_output_length;
776 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100777
778 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100779 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
781 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
782 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100783 }
784
Steven Cooremancd640932021-03-08 12:00:27 +0100785 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
786 * check for the requested MAC length to be equal to or longer than the
787 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
789 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
790 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100791 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200792 }
Steven Cooremance48e852020-10-05 16:02:45 +0200793 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200794 * a key derivation with that key agreement should also be allowed. This
795 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
797 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
798 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
799 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200800 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100801 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200803}
804
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100805/** Test whether a policy permits an algorithm.
806 *
807 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100808 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100809 * \note This function requires providing the key type for which the policy is
810 * being validated, since some algorithm policy definitions (e.g. MAC)
811 * have different properties depending on what kind of cipher it is
812 * combined with.
813 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100814 * \retval PSA_SUCCESS When \p alg is a specific algorithm
815 * allowed by the \p policy.
816 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
817 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
818 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100819 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
821 psa_key_type_t key_type,
822 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100823{
Steven Cooremand788fab2021-02-25 11:29:17 +0100824 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (alg == 0) {
826 return PSA_ERROR_INVALID_ARGUMENT;
827 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100828
Steven Cooreman7e39f052021-02-23 14:18:32 +0100829 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if (PSA_ALG_IS_WILDCARD(alg)) {
831 return PSA_ERROR_INVALID_ARGUMENT;
832 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
835 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
836 return PSA_SUCCESS;
837 } else {
838 return PSA_ERROR_NOT_PERMITTED;
839 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100840}
841
Gilles Peskinef603c712019-01-19 13:40:11 +0100842/** Restrict a key policy based on a constraint.
843 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100844 * \note This function requires providing the key type for which the policy is
845 * being restricted, since some algorithm policy definitions (e.g. MAC)
846 * have different properties depending on what kind of cipher it is
847 * combined with.
848 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100849 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100850 * \param[in,out] policy The policy to restrict.
851 * \param[in] constraint The policy constraint to apply.
852 *
853 * \retval #PSA_SUCCESS
854 * \c *policy contains the intersection of the original value of
855 * \c *policy and \c *constraint.
856 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100857 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100858 * \c *policy is unchanged.
859 */
860static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100861 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100862 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100864{
865 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 psa_key_policy_algorithm_intersection(key_type, policy->alg,
867 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200868 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
870 constraint->alg2);
871 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
872 return PSA_ERROR_INVALID_ARGUMENT;
873 }
874 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
875 return PSA_ERROR_INVALID_ARGUMENT;
876 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100877 policy->usage &= constraint->usage;
878 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200879 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100881}
882
Przemek Stekiel061f6942022-11-30 10:51:35 +0100883/** Get the description of a key given its identifier and policy constraints
884 * and lock it.
885 *
886 * The key must have allow all the usage flags set in \p usage. If \p alg is
887 * nonzero, the key must allow operations with this algorithm. If \p alg is
888 * zero, the algorithm is not checked.
889 *
890 * In case of a persistent key, the function loads the description of the key
891 * into a key slot if not already done.
892 *
Ryan Everett098c6652024-01-03 13:03:36 +0000893 * On success, the returned key slot has been registered for reading.
894 * It is the responsibility of the caller to call psa_unregister_read(slot)
895 * when they have finished reading the contents of the slot.
Przemek Stekiel061f6942022-11-30 10:51:35 +0100896 */
897static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100898 mbedtls_svc_key_id_t key,
899 psa_key_slot_t **p_slot,
900 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100902{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100904 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 status = psa_get_and_lock_key_slot(key, p_slot);
907 if (status != PSA_SUCCESS) {
908 return status;
909 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200910 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100911
912 /* Enforce that usage policy for the key slot contains all the flags
913 * required by the usage parameter. There is one exception: public
914 * keys can always be exported, so we treat public key objects as
915 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100921 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200922 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100923 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100924
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800925 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if (alg != 0) {
927 status = psa_key_policy_permits(&slot->attr.policy,
928 slot->attr.type,
929 alg);
930 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100931 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100933 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200936
937error:
938 *p_slot = NULL;
Ryan Everett098c6652024-01-03 13:03:36 +0000939 psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100942}
Darryl Green940d72c2018-07-13 13:18:51 +0100943
Ronald Cron5c522922020-11-14 16:35:34 +0100944/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200945 *
946 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200947 * available, as opposed to a key in a secure element and/or to be used
948 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200949 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200950 * This is a temporary function that may be used instead of
951 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
952 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200953 *
Ryan Everett098c6652024-01-03 13:03:36 +0000954 * On success, the returned key slot has been registered for reading.
955 * It is the responsibility of the caller to call psa_unregister_read(slot)
956 * when they have finished reading the contents of the slot.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200957 */
Ronald Cron5c522922020-11-14 16:35:34 +0100958static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
959 mbedtls_svc_key_id_t key,
960 psa_key_slot_t **p_slot,
961 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200963{
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
965 usage, alg);
966 if (status != PSA_SUCCESS) {
967 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200968 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everett098c6652024-01-03 13:03:36 +0000971 psa_unregister_read(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 *p_slot = NULL;
973 return PSA_ERROR_NOT_SUPPORTED;
974 }
975
976 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200977}
Gilles Peskine28f8f302019-07-24 13:30:31 +0200978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +0000980{
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +0100982 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 }
Ronald Cronea0f8a62020-11-25 17:52:23 +0100984
Ronald Cronea0f8a62020-11-25 17:52:23 +0100985 slot->key.data = NULL;
986 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +0000989}
990
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100991/** Completely wipe a slot in memory, including its policy.
992 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100993psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100994{
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +0100996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 /*
998 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +0000999 * do our best to report an unexpected amount of registered readers or
1000 * an unexpected state.
1001 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1002 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1004 * function is called as part of the execution of a test suite, the
1005 * execution of the test suite is stopped in error if the assertion fails.
1006 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001007 switch (slot->state) {
1008 case PSA_SLOT_FULL:
1009 /* In this state psa_wipe_key_slot() must only be called if the
1010 * caller is the last reader. */
1011 case PSA_SLOT_PENDING_DELETION:
1012 /* In this state psa_wipe_key_slot() must only be called if the
1013 * caller is the last reader. */
1014 if (slot->registered_readers != 1) {
1015 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1);
1016 status = PSA_ERROR_CORRUPTION_DETECTED;
1017 }
1018 break;
1019 case PSA_SLOT_FILLING:
1020 /* In this state registered_readers must be 0. */
1021 if (slot->registered_readers != 0) {
1022 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0);
1023 status = PSA_ERROR_CORRUPTION_DETECTED;
1024 }
1025 break;
1026 case PSA_SLOT_EMPTY:
1027 /* The slot is already empty, it cannot be wiped. */
1028 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1029 status = PSA_ERROR_CORRUPTION_DETECTED;
1030 break;
1031 default:
1032 /* The slot's state is invalid. */
1033 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001034 }
1035
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001036 /* Multipart operations may still be using the key. This is safe
1037 * because all multipart operation objects are independent from
1038 * the key slot: if they need to access the key after the setup
1039 * phase, they have a copy of the key. Note that this means that
1040 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001041 /* At this point, key material and other type-specific content has
1042 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001043 * zeroize because the metadata is not particularly sensitive.
1044 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 memset(slot, 0, sizeof(*slot));
1046 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001047}
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001050{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001051 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001052 psa_status_t status; /* status of the last operation */
1053 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001054#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1055 psa_se_drv_table_entry_t *driver;
1056#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (mbedtls_svc_key_id_is_null(key)) {
1059 return PSA_SUCCESS;
1060 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001061
Ronald Cronf2911112020-10-29 17:51:10 +01001062 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001063 * Get the description of the key in a key slot, and register to read it.
1064 * In the case of a persistent key, this will load the key description
1065 * from persistent memory if not done yet.
1066 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001067 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001068 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 status = psa_get_and_lock_key_slot(key, &slot);
1070 if (status != PSA_SUCCESS) {
1071 return status;
1072 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001073
Ryan Everettc053d962024-01-25 17:56:32 +00001074#if defined(MBEDTLS_THREADING_C)
1075 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1076 &mbedtls_threading_key_slot_mutex));
1077#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001078 /* Set the key slot containing the key description's state to
1079 * PENDING_DELETION. This stops new operations from registering
1080 * to read the slot. Current readers can safely continue to access
1081 * the key within the slot; the last registered reader will
1082 * automatically wipe the slot when they call psa_unregister_read().
1083 * If the key is persistent, we can now delete the copy of the key
1084 * from memory. If the key is opaque, we require the driver to
1085 * deal with the deletion. */
Ryan Everettc053d962024-01-25 17:56:32 +00001086 status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1087 PSA_SLOT_PENDING_DELETION);
1088
1089 if (status != PSA_SUCCESS) {
1090 goto exit;
1091 }
Ronald Cronf2911112020-10-29 17:51:10 +01001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001094 /* Refuse the destruction of a read-only key (which may or may not work
1095 * if we attempt it, depending on whether the key is merely read-only
1096 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001097 * Just do the best we can, which is to wipe the copy in memory
1098 * (done in this function's cleanup code). */
1099 overall_status = PSA_ERROR_NOT_PERMITTED;
1100 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001101 }
1102
Gilles Peskine354f7672019-07-12 23:46:38 +02001103#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1105 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001106 /* For a key in a secure element, we need to do three things:
1107 * remove the key file in internal storage, destroy the
1108 * key inside the secure element, and update the driver's
1109 * persistent data. Start a transaction that will encompass these
1110 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001112 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001114 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 status = psa_crypto_save_transaction();
1116 if (status != PSA_SUCCESS) {
1117 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001118 /* We should still try to destroy the key in the secure
1119 * element and the key metadata in storage. This is especially
1120 * important if the error is that the storage is full.
1121 * But how to do it exactly without risking an inconsistent
1122 * state after a reset?
1123 * https://github.com/ARMmbed/mbed-crypto/issues/215
1124 */
1125 overall_status = status;
1126 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001127 }
1128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 status = psa_destroy_se_key(driver,
1130 psa_key_slot_get_slot_number(slot));
1131 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001132 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001134 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001135#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1136
Darryl Greend49a4992018-06-18 17:27:26 +01001137#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001139 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001140 * The slot will still hold a copy of the key until the last reader
1141 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 status = psa_destroy_persistent_key(slot->attr.id);
1143 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001144 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 }
Darryl Greend49a4992018-06-18 17:27:26 +01001146 }
1147#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001148
Gilles Peskinefc762652019-07-22 19:30:34 +02001149#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if (driver != NULL) {
1151 status = psa_save_se_persistent_data(driver);
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 }
1155 status = psa_crypto_stop_transaction();
1156 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001157 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001159 }
1160#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1161
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001162exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001163 /* Unregister from reading the slot. If we are the last active reader
1164 * then this will wipe the slot. */
1165 status = psa_unregister_read(slot);
Ryan Everettc053d962024-01-25 17:56:32 +00001166
1167#if defined(MBEDTLS_THREADING_C)
1168 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1169 &mbedtls_threading_key_slot_mutex));
1170#endif
1171
1172 /* Prioritize CORRUPTION_DETECTED from unregistering or
1173 * SERVICE_FAILURE from unlocking over a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001175 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 }
1177 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001178}
1179
Valerio Settib2bcedb2023-07-10 11:24:00 +02001180#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001181 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001182static psa_status_t psa_get_rsa_public_exponent(
1183 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001185{
1186 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001188 uint8_t *buffer = NULL;
1189 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1193 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001194 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 }
1196 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001197 /* It's the default value, which is reported as an empty string,
1198 * so there's nothing to do. */
1199 goto exit;
1200 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 buflen = mbedtls_mpi_size(&mpi);
1203 buffer = mbedtls_calloc(1, buflen);
1204 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001205 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1206 goto exit;
1207 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1209 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001210 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001212 attributes->domain_parameters = buffer;
1213 attributes->domain_parameters_size = buflen;
1214
1215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 mbedtls_mpi_free(&mpi);
1217 if (ret != 0) {
1218 mbedtls_free(buffer);
1219 }
1220 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001221}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001222#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001223 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001224
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001225/** Retrieve all the publicly-accessible attributes of a key.
1226 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001227psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1228 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001229{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001231 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001232 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1237 if (status != PSA_SUCCESS) {
1238 return status;
1239 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001240
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001241 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1243 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001244
1245#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1247 psa_set_key_slot_number(attributes,
1248 psa_key_slot_get_slot_number(slot));
1249 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001250#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001253#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1254 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001255 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001256 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001257 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Pengyu Lvf75893b2023-12-08 17:21:39 +08001258 /* TODO: This is a temporary situation where domain parameters are deprecated,
1259 * but we need it for namely generating an RSA key with a non-default exponent.
1260 * This would be improved after https://github.com/Mbed-TLS/mbedtls/issues/6494.
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001261 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001263 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001264
Ronald Cron90857082020-11-25 14:58:33 +01001265 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 slot->attr.type,
1267 slot->key.data,
1268 slot->key.bytes,
1269 &rsa);
1270 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001271 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 status = psa_get_rsa_public_exponent(rsa,
1275 attributes);
1276 mbedtls_rsa_free(rsa);
1277 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001278 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001279 break;
Pengyu Lve9efbc22023-12-08 16:59:08 +08001280#else
1281 case PSA_KEY_TYPE_RSA_KEY_PAIR:
1282 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1283 attributes->domain_parameters = NULL;
1284 attributes->domain_parameters_size = SIZE_MAX;
1285 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001286#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1287 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001288 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001289 default:
1290 /* Nothing else to do. */
1291 break;
1292 }
1293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 if (status != PSA_SUCCESS) {
1295 psa_reset_key_attributes(attributes);
1296 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001297
Ryan Everett1b70a072024-01-04 10:32:49 +00001298 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001301}
1302
Gilles Peskinec8000c02019-08-02 20:15:51 +02001303#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1304psa_status_t psa_get_key_slot_number(
1305 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001307{
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001309 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 return PSA_SUCCESS;
1311 } else {
1312 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001313 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001314}
1315#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1318 size_t key_buffer_size,
1319 uint8_t *data,
1320 size_t data_size,
1321 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001322{
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (key_buffer_size > data_size) {
1324 return PSA_ERROR_BUFFER_TOO_SMALL;
1325 }
1326 memcpy(data, key_buffer, key_buffer_size);
1327 memset(data + key_buffer_size, 0,
1328 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001329 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001331}
1332
Ronald Cron7285cda2020-11-26 14:46:37 +01001333psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001334 const psa_key_attributes_t *attributes,
1335 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001337{
Ronald Crond18b5f82020-11-25 16:46:05 +01001338 psa_key_type_t type = attributes->core.type;
1339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 if (key_type_is_raw_bytes(type) ||
1341 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001342 PSA_KEY_TYPE_IS_ECC(type) ||
1343 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 return psa_export_key_buffer_internal(
1345 key_buffer, key_buffer_size,
1346 data, data_size, data_length);
1347 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001348 /* This shouldn't happen in the reference implementation, but
1349 it is valid for a special-purpose implementation to omit
1350 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001352 }
1353}
1354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1356 uint8_t *data,
1357 size_t data_size,
1358 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001359{
1360 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1361 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1362 psa_key_slot_t *slot;
1363
Ronald Crone907e552021-01-18 13:22:38 +01001364 /* Reject a zero-length output buffer now, since this can never be a
1365 * valid key representation. This way we know that data must be a valid
1366 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 if (data_size == 0) {
1368 return PSA_ERROR_BUFFER_TOO_SMALL;
1369 }
Ronald Crone907e552021-01-18 13:22:38 +01001370
Ronald Cron9486f9d2020-11-26 13:36:23 +01001371 /* Set the key to empty now, so that even when there are errors, we always
1372 * set data_length to a value between 0 and data_size. On error, setting
1373 * the key to empty is a good choice because an empty key representation is
1374 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001375 *data_length = 0;
1376
Ronald Cron9486f9d2020-11-26 13:36:23 +01001377 /* Export requires the EXPORT flag. There is an exception for public keys,
1378 * which don't require any flag, but
1379 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1380 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1382 PSA_KEY_USAGE_EXPORT, 0);
1383 if (status != PSA_SUCCESS) {
1384 return status;
1385 }
mohammad160306e79202018-03-28 13:17:44 +03001386
Ronald Crond18b5f82020-11-25 16:46:05 +01001387 psa_key_attributes_t attributes = {
1388 .core = slot->attr
1389 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 status = psa_driver_wrapper_export_key(&attributes,
1391 slot->key.data, slot->key.bytes,
1392 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001393
Ryan Everett1b70a072024-01-04 10:32:49 +00001394 unlock_status = psa_unregister_read(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001397}
1398
Ronald Cron7285cda2020-11-26 14:46:37 +01001399psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001400 const psa_key_attributes_t *attributes,
1401 const uint8_t *key_buffer,
1402 size_t key_buffer_size,
1403 uint8_t *data,
1404 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001406{
Ronald Crond18b5f82020-11-25 16:46:05 +01001407 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001408
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001409 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001410 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1411 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001412 /* Exporting public -> public */
1413 return psa_export_key_buffer_internal(
1414 key_buffer, key_buffer_size,
1415 data, data_size, data_length);
1416 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001417#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001418 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1419 return mbedtls_psa_rsa_export_public_key(attributes,
1420 key_buffer,
1421 key_buffer_size,
1422 data,
1423 data_size,
1424 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001425#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001426 /* We don't know how to convert a private RSA key to public. */
1427 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001428#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001429 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001430 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001431#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001432 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1433 return mbedtls_psa_ecp_export_public_key(attributes,
1434 key_buffer,
1435 key_buffer_size,
1436 data,
1437 data_size,
1438 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001439#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001440 /* We don't know how to convert a private ECC key to public */
1441 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001442#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001443 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001444 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001445#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001446 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001447 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001448 key_buffer,
1449 key_buffer_size,
1450 data, data_size,
1451 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001452#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001453 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001454#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001455 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001457 (void) key_buffer;
1458 (void) key_buffer_size;
1459 (void) data;
1460 (void) data_size;
1461 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001463 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001464}
Ronald Crond18b5f82020-11-25 16:46:05 +01001465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1467 uint8_t *data,
1468 size_t data_size,
1469 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001470{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001471 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001472 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001473 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001474 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001475
Ronald Crone907e552021-01-18 13:22:38 +01001476 /* Reject a zero-length output buffer now, since this can never be a
1477 * valid key representation. This way we know that data must be a valid
1478 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (data_size == 0) {
1480 return PSA_ERROR_BUFFER_TOO_SMALL;
1481 }
Ronald Crone907e552021-01-18 13:22:38 +01001482
Darryl Greendd8fb772018-11-07 16:00:44 +00001483 /* Set the key to empty now, so that even when there are errors, we always
1484 * set data_length to a value between 0 and data_size. On error, setting
1485 * the key to empty is a good choice because an empty key representation is
1486 * unlikely to be accepted anywhere. */
1487 *data_length = 0;
1488
1489 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1491 if (status != PSA_SUCCESS) {
1492 return status;
1493 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1496 status = PSA_ERROR_INVALID_ARGUMENT;
1497 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001498 }
1499
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001500 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001501 .core = slot->attr
1502 };
Ronald Cron67227982020-11-26 15:16:05 +01001503 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001504 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001506
1507exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00001508 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001511}
1512
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001513MBEDTLS_STATIC_ASSERT(
1514 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1515 "One or more key attribute flag is listed as both external-only and dual-use")
1516MBEDTLS_STATIC_ASSERT(
1517 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1518 "One or more key attribute flag is listed as both internal-only and dual-use")
1519MBEDTLS_STATIC_ASSERT(
1520 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1521 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001522
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001523/** Validate that a key policy is internally well-formed.
1524 *
1525 * This function only rejects invalid policies. It does not validate the
1526 * consistency of the policy with respect to other attributes of the key
1527 * such as the key type.
1528 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001530{
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1532 PSA_KEY_USAGE_COPY |
1533 PSA_KEY_USAGE_ENCRYPT |
1534 PSA_KEY_USAGE_DECRYPT |
1535 PSA_KEY_USAGE_SIGN_MESSAGE |
1536 PSA_KEY_USAGE_VERIFY_MESSAGE |
1537 PSA_KEY_USAGE_SIGN_HASH |
1538 PSA_KEY_USAGE_VERIFY_HASH |
1539 PSA_KEY_USAGE_VERIFY_DERIVATION |
1540 PSA_KEY_USAGE_DERIVE)) != 0) {
1541 return PSA_ERROR_INVALID_ARGUMENT;
1542 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001545}
1546
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001547/** Validate the internal consistency of key attributes.
1548 *
1549 * This function only rejects invalid attribute values. If does not
1550 * validate the consistency of the attributes with any key data that may
1551 * be involved in the creation of the key.
1552 *
1553 * Call this function early in the key creation process.
1554 *
1555 * \param[in] attributes Key attributes for the new key.
1556 * \param[out] p_drv On any return, the driver for the key, if any.
1557 * NULL for a transparent key.
1558 *
1559 */
1560static psa_status_t psa_validate_key_attributes(
1561 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001563{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001564 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1566 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 status = psa_validate_key_location(lifetime, p_drv);
1569 if (status != PSA_SUCCESS) {
1570 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001571 }
1572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 status = psa_validate_key_persistence(lifetime);
1574 if (status != PSA_SUCCESS) {
1575 return status;
1576 }
1577
1578 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1579 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1580 return PSA_ERROR_INVALID_ARGUMENT;
1581 }
1582 } else {
1583 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1584 return PSA_ERROR_INVALID_ARGUMENT;
1585 }
1586 }
1587
1588 status = psa_validate_key_policy(&attributes->core.policy);
1589 if (status != PSA_SUCCESS) {
1590 return status;
1591 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001592
1593 /* Refuse to create overly large keys.
1594 * Note that this doesn't trigger on import if the attributes don't
1595 * explicitly specify a size (so psa_get_key_bits returns 0), so
1596 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1598 return PSA_ERROR_NOT_SUPPORTED;
1599 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001600
Gilles Peskine91e8c332019-08-02 19:19:39 +02001601 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1603 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1604 return PSA_ERROR_INVALID_ARGUMENT;
1605 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001608}
1609
Gilles Peskine4747d192019-04-17 15:05:45 +02001610/** Prepare a key slot to receive key material.
1611 *
1612 * This function allocates a key slot and sets its metadata.
1613 *
1614 * If this function fails, call psa_fail_key_creation().
1615 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001616 * This function is intended to be used as follows:
1617 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001618 * it with the specified attributes, and in case of a volatile key assign it
1619 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001620 * -# Populate the slot with the key material.
1621 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1622 * In case of failure at any step, stop the sequence and call
1623 * psa_fail_key_creation().
1624 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001625 * On success, the key slot's state is PSA_SLOT_FILLING.
1626 * It is the responsibility of the caller to change the slot's state to
1627 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001628 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001629 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001630 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001631 * \param[out] p_slot On success, a pointer to the prepared slot.
1632 * \param[out] p_drv On any return, the driver for the key, if any.
1633 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001634 *
1635 * \retval #PSA_SUCCESS
1636 * The key slot is ready to receive key material.
1637 * \return If this function fails, the key slot is an invalid state.
1638 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001639 */
1640static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001641 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001642 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001643 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001645{
1646 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001647 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001648 psa_key_slot_t *slot;
1649
Gilles Peskinedf179142019-07-15 22:02:14 +02001650 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001651 *p_drv = NULL;
1652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 status = psa_validate_key_attributes(attributes, p_drv);
1654 if (status != PSA_SUCCESS) {
1655 return status;
1656 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001657
Ryan Everettb69118e2024-01-02 15:54:32 +00001658 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 if (status != PSA_SUCCESS) {
1660 return status;
1661 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001662 slot = *p_slot;
1663
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001664 /* We're storing the declared bit-size of the key. It's up to each
1665 * creation mechanism to verify that this information is correct.
1666 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001667 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001668 * is optional (import, copy). In case of a volatile key, assign it the
1669 * volatile key identifier associated to the slot returned to contain its
1670 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001671
1672 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001674#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1675 slot->attr.id = volatile_key_id;
1676#else
1677 slot->attr.id.key_id = volatile_key_id;
1678#endif
1679 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001680
Gilles Peskine91e8c332019-08-02 19:19:39 +02001681 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001682 * external-only flags, query `attributes`. Thanks to the check
1683 * in psa_validate_key_attributes(), this leaves the dual-use
Ryan Everettb69118e2024-01-02 15:54:32 +00001684 * flags and any internal flag that psa_reserve_free_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001685 * may have set. */
1686 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001687
Gilles Peskinecbaff462019-07-12 23:46:04 +02001688#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001689 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001690 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001691 * create the key file in internal storage, create the
1692 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001693 * persistent data. This is done by starting a transaction that will
1694 * encompass these three actions.
1695 * For registering a volatile key, we just need to find an appropriate
1696 * slot number inside the SE. Since the key is designated volatile, creating
1697 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001698 /* The first thing to do is to find a slot number for the new key.
1699 * We save the slot number in persistent storage as part of the
1700 * transaction data. It will be needed to recover if the power
1701 * fails during the key creation process, to clean up on the secure
1702 * element side after restarting. Obtaining a slot number from the
1703 * secure element driver updates its persistent state, but we do not yet
1704 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001705 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001707 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1709 &slot_number);
1710 if (status != PSA_SUCCESS) {
1711 return status;
1712 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1715 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001716 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001717 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001718 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 status = psa_crypto_save_transaction();
1720 if (status != PSA_SUCCESS) {
1721 (void) psa_crypto_stop_transaction();
1722 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001723 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001724 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001725
1726 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001728 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001731 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001733 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001734#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1735
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001737}
Gilles Peskine4747d192019-04-17 15:05:45 +02001738
1739/** Finalize the creation of a key once its key material has been set.
1740 *
1741 * This entails writing the key to persistent storage.
1742 *
1743 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001744 * See the documentation of psa_start_key_creation() for the intended use
1745 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001746 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001747 * If the finalization succeeds, the function sets the key slot's state to
1748 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1749 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001750 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001751 * \param[in,out] slot Pointer to the slot with key material.
1752 * \param[in] driver The secure element driver for the key,
1753 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001754 * \param[out] key On success, identifier of the key. Note that the
1755 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001756 *
1757 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001758 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001759 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1760 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1761 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1762 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1763 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1764 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001765 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001766 * \return If this function fails, the key slot is an invalid state.
1767 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001768 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001769static psa_status_t psa_finish_key_creation(
1770 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001771 psa_se_drv_table_entry_t *driver,
1772 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001773{
1774 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001775 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001776 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001777
1778#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001780#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001782 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001783 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001785
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001786 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1787 sizeof(data.slot_number),
1788 "Slot number size does not match psa_se_key_data_storage_t");
1789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1791 status = psa_save_persistent_key(&slot->attr,
1792 (uint8_t *) &data,
1793 sizeof(data));
1794 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001795#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1796 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001797 /* Key material is saved in export representation in the slot, so
1798 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 status = psa_save_persistent_key(&slot->attr,
1800 slot->key.data,
1801 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001802 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001803 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001804#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1805
Gilles Peskinecbaff462019-07-12 23:46:04 +02001806#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001807 /* Finish the transaction for a key creation. This does not
1808 * happen when registering an existing key. Detect this case
1809 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001810 * creation of a persistent key in a secure element requires a transaction,
1811 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 if (driver != NULL &&
1813 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1814 status = psa_save_se_persistent_data(driver);
1815 if (status != PSA_SUCCESS) {
1816 psa_destroy_persistent_key(slot->attr.id);
1817 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001818 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001820 }
1821#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001824 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001825 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1826 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001828 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001830 }
Ronald Cron50972942020-11-14 11:28:25 +01001831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001833}
1834
1835/** Abort the creation of a key.
1836 *
1837 * You may call this function after calling psa_start_key_creation(),
1838 * or after psa_finish_key_creation() fails. In other circumstances, this
1839 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001840 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001841 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001842 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001843 * \param[in,out] slot Pointer to the slot with key material.
1844 * \param[in] driver The secure element driver for the key,
1845 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001846 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001847static void psa_fail_key_creation(psa_key_slot_t *slot,
1848 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001849{
Gilles Peskine011e4282019-06-26 18:34:38 +02001850 (void) driver;
1851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001853 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001855
1856#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001857 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001858 * element, and the failure happened later (when saving metadata
1859 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001860 * element.
1861 * https://github.com/ARMmbed/mbed-crypto/issues/217
1862 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001863
Gilles Peskined7729582019-08-05 15:55:54 +02001864 /* Abort the ongoing transaction if any (there may not be one if
1865 * the creation process failed before starting one, or if the
1866 * key creation is a registration of a key in a secure element).
1867 * Earlier functions must already have done what it takes to undo any
1868 * partial creation. All that's left is to update the transaction data
1869 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001871#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1872
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001874}
1875
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001876/** Validate optional attributes during key creation.
1877 *
1878 * Some key attributes are optional during key creation. If they are
1879 * specified in the attributes structure, check that they are consistent
1880 * with the data in the slot.
1881 *
1882 * This function should be called near the end of key creation, after
1883 * the slot in memory is fully populated but before saving persistent data.
1884 */
1885static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001886 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001888{
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 if (attributes->core.type != 0) {
1890 if (attributes->core.type != slot->attr.type) {
1891 return PSA_ERROR_INVALID_ARGUMENT;
1892 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001893 }
1894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001896#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1897 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1899 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001900 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001901 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001903
Ronald Cron90857082020-11-25 14:58:33 +01001904 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 slot->attr.type,
1906 slot->key.data,
1907 slot->key.bytes,
1908 &rsa);
1909 if (status != PSA_SUCCESS) {
1910 return status;
1911 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 mbedtls_mpi_init(&actual);
1914 mbedtls_mpi_init(&required);
1915 ret = mbedtls_rsa_export(rsa,
1916 NULL, NULL, NULL, NULL, &actual);
1917 mbedtls_rsa_free(rsa);
1918 mbedtls_free(rsa);
1919 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001920 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 }
1922 ret = mbedtls_mpi_read_binary(&required,
1923 attributes->domain_parameters,
1924 attributes->domain_parameters_size);
1925 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001926 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 }
1928 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001929 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 }
1931rsa_exit:
1932 mbedtls_mpi_free(&actual);
1933 mbedtls_mpi_free(&required);
1934 if (ret != 0) {
1935 return mbedtls_to_psa_error(ret);
1936 }
1937 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02001938#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
1939 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001940 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001941 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001943 }
1944 }
1945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (attributes->core.bits != 0) {
1947 if (attributes->core.bits != slot->attr.bits) {
1948 return PSA_ERROR_INVALID_ARGUMENT;
1949 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001950 }
1951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001953}
1954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1956 const uint8_t *data,
1957 size_t data_length,
1958 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001959{
1960 psa_status_t status;
1961 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001962 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001963 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301964 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001965
Ronald Cron81709fc2020-11-14 12:10:32 +01001966 *key = MBEDTLS_SVC_KEY_ID_INIT;
1967
Gilles Peskine0f84d622019-09-12 19:03:13 +02001968 /* Reject zero-length symmetric keys (including raw data key objects).
1969 * This also rejects any key which might be encoded as an empty string,
1970 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 if (data_length == 0) {
1972 return PSA_ERROR_INVALID_ARGUMENT;
1973 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001974
Archana449608b2021-09-08 15:36:05 +05301975 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 if (data_length > SIZE_MAX / 8) {
1977 return PSA_ERROR_NOT_SUPPORTED;
1978 }
Archana6ed4bda2021-08-04 10:47:15 +05301979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1981 &slot, &driver);
1982 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001983 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001985
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001986 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301987 * storage ( thus not in the case of importing a key in a secure element
1988 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1989 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 if (slot->key.data == NULL) {
1991 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301992 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 attributes, data, data_length, &storage_size);
1994 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301995 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 }
Archanad8a83dc2021-06-14 10:04:16 +05301997 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 status = psa_allocate_buffer_to_slot(slot, storage_size);
1999 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002000 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002002 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002003
2004 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 status = psa_driver_wrapper_import_key(attributes,
2006 data, data_length,
2007 slot->key.data,
2008 slot->key.bytes,
2009 &slot->key.bytes, &bits);
2010 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002011 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002015 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002017 status = PSA_ERROR_INVALID_ARGUMENT;
2018 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002019 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002020
Archana6ed4bda2021-08-04 10:47:15 +05302021 /* Enforce a size limit, and in particular ensure that the bit
2022 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302024 status = PSA_ERROR_NOT_SUPPORTED;
2025 goto exit;
2026 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 status = psa_validate_optional_attributes(slot, attributes);
2028 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002029 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002033exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 if (status != PSA_SUCCESS) {
2035 psa_fail_key_creation(slot, driver);
2036 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002039}
2040
Gilles Peskined7729582019-08-05 15:55:54 +02002041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2042psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002044{
2045 psa_status_t status;
2046 psa_key_slot_t *slot = NULL;
2047 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002048 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002049
2050 /* Leaving attributes unspecified is not currently supported.
2051 * It could make sense to query the key type and size from the
2052 * secure element, but not all secure elements support this
2053 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2055 return PSA_ERROR_NOT_SUPPORTED;
2056 }
2057 if (psa_get_key_bits(attributes) == 0) {
2058 return PSA_ERROR_NOT_SUPPORTED;
2059 }
Gilles Peskined7729582019-08-05 15:55:54 +02002060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2062 &slot, &driver);
2063 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002064 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 }
Gilles Peskined7729582019-08-05 15:55:54 +02002066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002068
2069exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 if (status != PSA_SUCCESS) {
2071 psa_fail_key_creation(slot, driver);
2072 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002073
Gilles Peskined7729582019-08-05 15:55:54 +02002074 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 psa_close_key(key);
2076 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002077}
2078#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2081 const psa_key_attributes_t *specified_attributes,
2082 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002083{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002084 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002085 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002086 psa_key_slot_t *source_slot = NULL;
2087 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002088 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002089 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302090 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002091
Ronald Cron81709fc2020-11-14 12:10:32 +01002092 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2093
Archana8a180362021-07-05 02:18:48 +05302094 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2096 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002097 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 status = psa_validate_optional_attributes(source_slot,
2101 specified_attributes);
2102 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002105
Archana9d17bf42021-09-10 06:22:44 +05302106 /* The target key type and number of bits have been validated by
2107 * psa_validate_optional_attributes() to be either equal to zero or
2108 * equal to the ones of the source key. So it is safe to inherit
2109 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302110 * */
Archana9d17bf42021-09-10 06:22:44 +05302111 actual_attributes.core.bits = source_slot->attr.bits;
2112 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302113
2114
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 status = psa_restrict_key_policy(source_slot->attr.type,
2116 &actual_attributes.core.policy,
2117 &source_slot->attr.policy);
2118 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002119 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2123 &target_slot, &driver);
2124 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002125 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 }
2127 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2128 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302129 /*
Archana9d17bf42021-09-10 06:22:44 +05302130 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302131 * the source key would need to be exported as plaintext and re-imported
2132 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302133 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302134 * appropriate API invocations from the application, if needed.
2135 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002136 status = PSA_ERROR_NOT_SUPPORTED;
2137 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002138 }
Archana8a180362021-07-05 02:18:48 +05302139 /*
2140 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302141 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302142 * - For opaque keys this translates to an invocation of the drivers'
2143 * copy_key entry point through the dispatch layer.
2144 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2146 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2147 &storage_size);
2148 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302149 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 }
Archana374fe5b2021-09-08 15:50:28 +05302151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2153 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302154 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 }
Archana374fe5b2021-09-08 15:50:28 +05302156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 status = psa_driver_wrapper_copy_key(&actual_attributes,
2158 source_slot->key.data,
2159 source_slot->key.bytes,
2160 target_slot->key.data,
2161 target_slot->key.bytes,
2162 &target_slot->key.bytes);
2163 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302164 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 }
2166 } else {
2167 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302168 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 source_slot->key.bytes);
2170 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302171 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 }
Archana8a180362021-07-05 02:18:48 +05302173 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002175exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 if (status != PSA_SUCCESS) {
2177 psa_fail_key_creation(target_slot, driver);
2178 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002179
Ryan Everett1b70a072024-01-04 10:32:49 +00002180 unlock_status = psa_unregister_read(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002183}
2184
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002185
2186
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002187/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002188/* Message digests */
2189/****************************************************************/
2190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002192{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002193 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 if (operation->id == 0) {
2195 return PSA_SUCCESS;
2196 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002199 operation->id = 0;
2200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002202}
2203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2205 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002206{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002207 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002208
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002209 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002211 status = PSA_ERROR_BAD_STATE;
2212 goto exit;
2213 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002216 status = PSA_ERROR_INVALID_ARGUMENT;
2217 goto exit;
2218 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002219
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002220 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2221 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002225
2226exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if (status != PSA_SUCCESS) {
2228 psa_hash_abort(operation);
2229 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002230
2231 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002232}
2233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2235 const uint8_t *input,
2236 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002237{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002238 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2239
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002241 status = PSA_ERROR_BAD_STATE;
2242 goto exit;
2243 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002244
Steven Cooremanc8288352021-03-04 14:02:19 +01002245 /* Don't require hash implementations to behave correctly on a
2246 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 if (input_length == 0) {
2248 return PSA_SUCCESS;
2249 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002252
2253exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 if (status != PSA_SUCCESS) {
2255 psa_hash_abort(operation);
2256 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002259}
2260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2262 uint8_t *hash,
2263 size_t hash_size,
2264 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002265{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002266 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 if (operation->id == 0) {
2268 return PSA_ERROR_BAD_STATE;
2269 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002270
Steven Cooreman1e582352021-02-18 17:24:37 +01002271 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 operation, hash, hash_size, hash_length);
2273 psa_hash_abort(operation);
2274 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002275}
2276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2278 const uint8_t *hash,
2279 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280{
Ronald Cron69a63422021-10-18 09:47:58 +02002281 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002282 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002283 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 operation,
2285 actual_hash, sizeof(actual_hash),
2286 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002289 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002293 status = PSA_ERROR_INVALID_SIGNATURE;
2294 goto exit;
2295 }
2296
Dave Rodgman78701152023-08-29 14:20:18 +01002297 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002298 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002300
2301exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2303 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002304 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002308}
2309
Gilles Peskine449bd832023-01-11 14:50:10 +01002310psa_status_t psa_hash_compute(psa_algorithm_t alg,
2311 const uint8_t *input, size_t input_length,
2312 uint8_t *hash, size_t hash_size,
2313 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002314{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002315 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if (!PSA_ALG_IS_HASH(alg)) {
2317 return PSA_ERROR_INVALID_ARGUMENT;
2318 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2321 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002322}
2323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324psa_status_t psa_hash_compare(psa_algorithm_t alg,
2325 const uint8_t *input, size_t input_length,
2326 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002327{
Ronald Cron69a63422021-10-18 09:47:58 +02002328 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002329 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if (!PSA_ALG_IS_HASH(alg)) {
2332 return PSA_ERROR_INVALID_ARGUMENT;
2333 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002334
Steven Cooreman1e582352021-02-18 17:24:37 +01002335 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 alg, input, input_length,
2337 actual_hash, sizeof(actual_hash),
2338 &actual_hash_length);
2339 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002340 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 }
2342 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002343 status = PSA_ERROR_INVALID_SIGNATURE;
2344 goto exit;
2345 }
Dave Rodgman78701152023-08-29 14:20:18 +01002346 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002347 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002349
2350exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2352 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002353}
2354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2356 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002357{
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (source_operation->id == 0 ||
2359 target_operation->id != 0) {
2360 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002361 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2364 target_operation);
2365 if (status != PSA_SUCCESS) {
2366 psa_hash_abort(target_operation);
2367 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002370}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002371
2372
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002373/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374/* MAC */
2375/****************************************************************/
2376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002378{
Steven Cooremane6804192021-03-19 18:28:56 +01002379 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 if (operation->id == 0) {
2381 return PSA_SUCCESS;
2382 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002385 operation->mac_size = 0;
2386 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002387 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002390}
2391
Ronald Cron2dff3b22021-06-17 16:33:22 +02002392static psa_status_t psa_mac_finalize_alg_and_key_validation(
2393 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002394 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002396{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002397 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 psa_key_type_t key_type = psa_get_key_type(attributes);
2399 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 if (!PSA_ALG_IS_MAC(alg)) {
2402 return PSA_ERROR_INVALID_ARGUMENT;
2403 }
Steven Cooremane6804192021-03-19 18:28:56 +01002404
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002405 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 status = psa_mac_key_can_do(alg, key_type);
2407 if (status != PSA_SUCCESS) {
2408 return status;
2409 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002410
Steven Cooremand1a68f12021-05-10 11:13:41 +02002411 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002415 /* A very short MAC is too short for security since it can be
2416 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2417 * so we make this our minimum, even though 32 bits is still
2418 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002420 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2423 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002424 /* It's impossible to "truncate" to a larger length than the full length
2425 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002427 }
2428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002430 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2431 * that is disabled in the compile-time configuration. The result can
2432 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2433 * configuration into account. In this case, force a return of
2434 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2435 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2436 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2437 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2438 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002440 }
2441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002443}
2444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2446 mbedtls_svc_key_id_t key,
2447 psa_algorithm_t alg,
2448 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002449{
2450 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2451 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002452 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002453 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002454
2455 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002457 status = PSA_ERROR_BAD_STATE;
2458 goto exit;
2459 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002460
2461 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 key,
2463 &slot,
2464 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2465 alg);
2466 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002467 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002469
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002470 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002471 .core = slot->attr
2472 };
2473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2475 &operation->mac_size);
2476 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002477 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002479
2480 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002481 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 if (is_sign) {
2483 status = psa_driver_wrapper_mac_sign_setup(operation,
2484 &attributes,
2485 slot->key.data,
2486 slot->key.bytes,
2487 alg);
2488 } else {
2489 status = psa_driver_wrapper_mac_verify_setup(operation,
2490 &attributes,
2491 slot->key.data,
2492 slot->key.bytes,
2493 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002495
Gilles Peskinefbfac682018-07-08 20:51:54 +02002496exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 if (status != PSA_SUCCESS) {
2498 psa_mac_abort(operation);
2499 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002500
Ryan Everett1b70a072024-01-04 10:32:49 +00002501 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002504}
2505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2507 mbedtls_svc_key_id_t key,
2508 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002509{
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002511}
2512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2514 mbedtls_svc_key_id_t key,
2515 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002516{
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002518}
2519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2521 const uint8_t *input,
2522 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002523{
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if (operation->id == 0) {
2525 return PSA_ERROR_BAD_STATE;
2526 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002527
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002528 /* Don't require hash implementations to behave correctly on a
2529 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (input_length == 0) {
2531 return PSA_SUCCESS;
2532 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2535 input, input_length);
2536 if (status != PSA_SUCCESS) {
2537 psa_mac_abort(operation);
2538 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002541}
2542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2544 uint8_t *mac,
2545 size_t mac_size,
2546 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002547{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002548 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2549 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002552 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002553 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002554 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002557 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002558 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002559 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002560
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002561 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2562 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002564 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002565 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002566 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002569 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002570 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002571 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 status = psa_driver_wrapper_mac_sign_finish(operation,
2574 mac, operation->mac_size,
2575 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002576
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002577exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002578 /* In case of success, set the potential excess room in the output buffer
2579 * to an invalid value, to avoid potentially leaking a longer MAC.
2580 * In case of error, set the output length and content to a safe default,
2581 * such that in case the caller misses an error check, the output would be
2582 * an unachievable MAC.
2583 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002585 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002586 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002587 }
mohammad16036df908f2018-04-02 08:34:15 -07002588
Paul Elliottdc42ca82023-02-24 18:11:59 +00002589 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002594}
2595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2597 const uint8_t *mac,
2598 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002599{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2601 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002604 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002605 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002606 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002609 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002610 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002611 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002614 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002615 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002616 }
2617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 status = psa_driver_wrapper_mac_verify_finish(operation,
2619 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002620
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002621exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002625}
2626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2628 psa_algorithm_t alg,
2629 const uint8_t *input,
2630 size_t input_length,
2631 uint8_t *mac,
2632 size_t mac_size,
2633 size_t *mac_length,
2634 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002635{
2636 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002637 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2638 psa_key_slot_t *slot;
2639 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002640 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002641
Ronald Cron51131b52021-06-17 17:17:20 +02002642 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 key,
2644 &slot,
2645 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2646 alg);
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
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002651 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002652 .core = slot->attr
2653 };
2654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2656 &operation_mac_size);
2657 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002658 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002662 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002663 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002664 }
2665
2666 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 &attributes,
2668 slot->key.data, slot->key.bytes,
2669 alg,
2670 input, input_length,
2671 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002672
2673exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002674 /* In case of success, set the potential excess room in the output buffer
2675 * to an invalid value, to avoid potentially leaking a longer MAC.
2676 * In case of error, set the output length and content to a safe default,
2677 * such that in case the caller misses an error check, the output would be
2678 * an unachievable MAC.
2679 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002681 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002682 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002683 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002684
2685 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002686
Ryan Everett1b70a072024-01-04 10:32:49 +00002687 unlock_status = psa_unregister_read(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002690}
2691
Gilles Peskine449bd832023-01-11 14:50:10 +01002692psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002693 psa_algorithm_t alg,
2694 const uint8_t *input,
2695 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 uint8_t *mac,
2697 size_t mac_size,
2698 size_t *mac_length)
2699{
2700 return psa_mac_compute_internal(key, alg,
2701 input, input_length,
2702 mac, mac_size, mac_length, 1);
2703}
2704
2705psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2706 psa_algorithm_t alg,
2707 const uint8_t *input,
2708 size_t input_length,
2709 const uint8_t *mac,
2710 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002711{
2712 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002713 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2714 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 status = psa_mac_compute_internal(key, alg,
2717 input, input_length,
2718 actual_mac, sizeof(actual_mac),
2719 &actual_mac_length, 0);
2720 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002721 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002725 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002726 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002727 }
Dave Rodgman78701152023-08-29 14:20:18 +01002728 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002729 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002730 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002731 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002732
2733exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002737}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002738
Gilles Peskinee59236f2018-01-27 23:32:46 +01002739/****************************************************************/
2740/* Asymmetric cryptography */
2741/****************************************************************/
2742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2744 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002745{
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 if (input_is_message) {
2747 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2748 return PSA_ERROR_INVALID_ARGUMENT;
2749 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2752 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2753 return PSA_ERROR_INVALID_ARGUMENT;
2754 }
2755 }
2756 } else {
2757 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2758 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002759 }
2760 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002761
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002763}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002764
Gilles Peskine449bd832023-01-11 14:50:10 +01002765static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2766 int input_is_message,
2767 psa_algorithm_t alg,
2768 const uint8_t *input,
2769 size_t input_length,
2770 uint8_t *signature,
2771 size_t signature_size,
2772 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002773{
2774 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2775 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2776 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002777 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002778
2779 *signature_length = 0;
2780
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 status = psa_sign_verify_check_alg(input_is_message, alg);
2782 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002783 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002785
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002786 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002787 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002788 * buffer can in principle be empty since it doesn't actually have
2789 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 if (signature_size == 0) {
2791 return PSA_ERROR_BUFFER_TOO_SMALL;
2792 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002793
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002794 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 key, &slot,
2796 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2797 PSA_KEY_USAGE_SIGN_HASH,
2798 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002799
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002801 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002805 status = PSA_ERROR_INVALID_ARGUMENT;
2806 goto exit;
2807 }
2808
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002809 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002811 };
2812
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002814 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002815 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002816 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 signature, signature_size, signature_length);
2818 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002819
2820 status = psa_driver_wrapper_sign_hash(
2821 &attributes, slot->key.data, slot->key.bytes,
2822 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002824 }
2825
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002826
2827exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002828 psa_wipe_tag_output_buffer(signature, status, signature_size,
2829 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002830
Ryan Everett1b70a072024-01-04 10:32:49 +00002831 unlock_status = psa_unregister_read(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002832
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002834}
2835
Gilles Peskine449bd832023-01-11 14:50:10 +01002836static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2837 int input_is_message,
2838 psa_algorithm_t alg,
2839 const uint8_t *input,
2840 size_t input_length,
2841 const uint8_t *signature,
2842 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002843{
2844 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2845 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2846 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002847
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 status = psa_sign_verify_check_alg(input_is_message, alg);
2849 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002850 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002852
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002853 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 key, &slot,
2855 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2856 PSA_KEY_USAGE_VERIFY_HASH,
2857 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002858
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 if (status != PSA_SUCCESS) {
2860 return status;
2861 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002862
2863 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002865 };
2866
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002868 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002869 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002870 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 signature, signature_length);
2872 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002873 status = psa_driver_wrapper_verify_hash(
2874 &attributes, slot->key.data, slot->key.bytes,
2875 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002877 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002878
Ryan Everett1b70a072024-01-04 10:32:49 +00002879 unlock_status = psa_unregister_read(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002880
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002882
2883}
2884
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002885psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002886 const psa_key_attributes_t *attributes,
2887 const uint8_t *key_buffer,
2888 size_t key_buffer_size,
2889 psa_algorithm_t alg,
2890 const uint8_t *input,
2891 size_t input_length,
2892 uint8_t *signature,
2893 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002895{
2896 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002899 size_t hash_length;
2900 uint8_t hash[PSA_HASH_MAX_SIZE];
2901
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002902 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 PSA_ALG_SIGN_GET_HASH(alg),
2904 input, input_length,
2905 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002908 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002910
2911 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 attributes, key_buffer, key_buffer_size,
2913 alg, hash, hash_length,
2914 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002915 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002916
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002918}
2919
Gilles Peskine449bd832023-01-11 14:50:10 +01002920psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2921 psa_algorithm_t alg,
2922 const uint8_t *input,
2923 size_t input_length,
2924 uint8_t *signature,
2925 size_t signature_size,
2926 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002927{
2928 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002929 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002931}
2932
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002933psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002934 const psa_key_attributes_t *attributes,
2935 const uint8_t *key_buffer,
2936 size_t key_buffer_size,
2937 psa_algorithm_t alg,
2938 const uint8_t *input,
2939 size_t input_length,
2940 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002942{
2943 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002946 size_t hash_length;
2947 uint8_t hash[PSA_HASH_MAX_SIZE];
2948
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002949 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 PSA_ALG_SIGN_GET_HASH(alg),
2951 input, input_length,
2952 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002955 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002957
2958 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 attributes, key_buffer, key_buffer_size,
2960 alg, hash, hash_length,
2961 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002962 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002965}
2966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2968 psa_algorithm_t alg,
2969 const uint8_t *input,
2970 size_t input_length,
2971 const uint8_t *signature,
2972 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002973{
2974 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002975 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002977}
2978
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002979psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002980 const psa_key_attributes_t *attributes,
2981 const uint8_t *key_buffer, size_t key_buffer_size,
2982 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002984{
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2986 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2987 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002988#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2990 return mbedtls_psa_rsa_sign_hash(
2991 attributes,
2992 key_buffer, key_buffer_size,
2993 alg, hash, hash_length,
2994 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002995#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2996 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 } else {
2998 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002999 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3001 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003002#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3004 return mbedtls_psa_ecdsa_sign_hash(
3005 attributes,
3006 key_buffer, key_buffer_size,
3007 alg, hash, hash_length,
3008 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003009#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3010 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 } else {
3012 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003013 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003014 }
Ronald Cron4501c982021-03-19 20:09:32 +01003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 (void) key_buffer;
3017 (void) key_buffer_size;
3018 (void) hash;
3019 (void) hash_length;
3020 (void) signature;
3021 (void) signature_size;
3022 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003025}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003026
Gilles Peskine449bd832023-01-11 14:50:10 +01003027psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3028 psa_algorithm_t alg,
3029 const uint8_t *hash,
3030 size_t hash_length,
3031 uint8_t *signature,
3032 size_t signature_size,
3033 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003034{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003035 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003036 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003038}
3039
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003040psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003041 const psa_key_attributes_t *attributes,
3042 const uint8_t *key_buffer, size_t key_buffer_size,
3043 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003045{
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3047 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3048 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003049#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3051 return mbedtls_psa_rsa_verify_hash(
3052 attributes,
3053 key_buffer, key_buffer_size,
3054 alg, hash, hash_length,
3055 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003056#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3057 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 } else {
3059 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003060 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3062 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003063#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3065 return mbedtls_psa_ecdsa_verify_hash(
3066 attributes,
3067 key_buffer, key_buffer_size,
3068 alg, hash, hash_length,
3069 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003070#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3071 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 } else {
3073 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003074 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003075 }
Ronald Cron4501c982021-03-19 20:09:32 +01003076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 (void) key_buffer;
3078 (void) key_buffer_size;
3079 (void) hash;
3080 (void) hash_length;
3081 (void) signature;
3082 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003085}
3086
Gilles Peskine449bd832023-01-11 14:50:10 +01003087psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3088 psa_algorithm_t alg,
3089 const uint8_t *hash,
3090 size_t hash_length,
3091 const uint8_t *signature,
3092 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003093{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003094 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003095 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003097}
3098
Gilles Peskine449bd832023-01-11 14:50:10 +01003099psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3100 psa_algorithm_t alg,
3101 const uint8_t *input,
3102 size_t input_length,
3103 const uint8_t *salt,
3104 size_t salt_length,
3105 uint8_t *output,
3106 size_t output_size,
3107 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003108{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003109 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003110 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003111 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003112 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003113
Darryl Green5cc689a2018-07-24 15:34:10 +01003114 (void) input;
3115 (void) input_length;
3116 (void) salt;
3117 (void) output;
3118 (void) output_size;
3119
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003120 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3123 return PSA_ERROR_INVALID_ARGUMENT;
3124 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003125
Valerio Setti5bb454a2024-01-15 10:43:16 +01003126 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3128 if (status != PSA_SUCCESS) {
3129 return status;
3130 }
3131 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3132 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003133 status = PSA_ERROR_INVALID_ARGUMENT;
3134 goto exit;
3135 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003136
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003137 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003139 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003140
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003141 status = psa_driver_wrapper_asymmetric_encrypt(
3142 &attributes, slot->key.data, slot->key.bytes,
3143 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003145exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00003146 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003147
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003149}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3152 psa_algorithm_t alg,
3153 const uint8_t *input,
3154 size_t input_length,
3155 const uint8_t *salt,
3156 size_t salt_length,
3157 uint8_t *output,
3158 size_t output_size,
3159 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003160{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003161 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003162 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003163 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003164 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003165
Darryl Green5cc689a2018-07-24 15:34:10 +01003166 (void) input;
3167 (void) input_length;
3168 (void) salt;
3169 (void) output;
3170 (void) output_size;
3171
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003172 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003173
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3175 return PSA_ERROR_INVALID_ARGUMENT;
3176 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003177
Valerio Setti5bb454a2024-01-15 10:43:16 +01003178 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3180 if (status != PSA_SUCCESS) {
3181 return status;
3182 }
3183 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003184 status = PSA_ERROR_INVALID_ARGUMENT;
3185 goto exit;
3186 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003187
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003188 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003190 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003192 status = psa_driver_wrapper_asymmetric_decrypt(
3193 &attributes, slot->key.data, slot->key.bytes,
3194 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003196
3197exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00003198 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003199
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003201}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003202
Paul Elliott9fe12f62022-11-30 19:16:02 +00003203/****************************************************************/
3204/* Asymmetric interruptible cryptography */
3205/****************************************************************/
3206
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003207static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3208
Paul Elliott9fe12f62022-11-30 19:16:02 +00003209void psa_interruptible_set_max_ops(uint32_t max_ops)
3210{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003211 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003212}
3213
3214uint32_t psa_interruptible_get_max_ops(void)
3215{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003216 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003217}
3218
Paul Elliott9fe12f62022-11-30 19:16:02 +00003219uint32_t psa_sign_hash_get_num_ops(
3220 const psa_sign_hash_interruptible_operation_t *operation)
3221{
Paul Elliott296ede92022-12-15 17:00:30 +00003222 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003223}
3224
3225uint32_t psa_verify_hash_get_num_ops(
3226 const psa_verify_hash_interruptible_operation_t *operation)
3227{
Paul Elliott296ede92022-12-15 17:00:30 +00003228 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003229}
3230
Paul Elliott59ad9452022-12-18 15:09:02 +00003231static psa_status_t psa_sign_hash_abort_internal(
3232 psa_sign_hash_interruptible_operation_t *operation)
3233{
3234 if (operation->id == 0) {
3235 /* The object has (apparently) been initialized but it is not (yet)
3236 * in use. It's ok to call abort on such an object, and there's
3237 * nothing to do. */
3238 return PSA_SUCCESS;
3239 }
3240
3241 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3242
3243 status = psa_driver_wrapper_sign_hash_abort(operation);
3244
3245 operation->id = 0;
3246
Paul Elliotte6145dc2023-02-07 12:51:21 +00003247 /* Do not clear either the error_occurred or num_ops elements here as they
3248 * only want to be cleared by the application calling abort, not by abort
3249 * being called at completion of an operation. */
3250
Paul Elliott59ad9452022-12-18 15:09:02 +00003251 return status;
3252}
3253
Paul Elliott9fe12f62022-11-30 19:16:02 +00003254psa_status_t psa_sign_hash_start(
3255 psa_sign_hash_interruptible_operation_t *operation,
3256 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3257 const uint8_t *hash, size_t hash_length)
3258{
3259 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3260 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3261 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003262 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003263
Paul Elliottc9774412023-02-06 15:14:07 +00003264 /* Check that start has not been previously called, or operation has not
3265 * previously errored. */
3266 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003267 return PSA_ERROR_BAD_STATE;
3268 }
3269
Paul Elliott9fe12f62022-11-30 19:16:02 +00003270 status = psa_sign_verify_check_alg(0, alg);
3271 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003272 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003273 return status;
3274 }
3275
3276 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3277 PSA_KEY_USAGE_SIGN_HASH,
3278 alg);
3279
3280 if (status != PSA_SUCCESS) {
3281 goto exit;
3282 }
3283
3284 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3285 status = PSA_ERROR_INVALID_ARGUMENT;
3286 goto exit;
3287 }
3288
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003289 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003290 .core = slot->attr
3291 };
3292
Paul Elliott296ede92022-12-15 17:00:30 +00003293 /* Ensure ops count gets reset, in case of operation re-use. */
3294 operation->num_ops = 0;
3295
Paul Elliott9fe12f62022-11-30 19:16:02 +00003296 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3297 slot->key.data,
3298 slot->key.bytes, alg,
3299 hash, hash_length);
3300exit:
3301
3302 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003303 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003304 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003305 }
3306
Ryan Everett1b70a072024-01-04 10:32:49 +00003307 unlock_status = psa_unregister_read(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003308
Paul Elliottc9774412023-02-06 15:14:07 +00003309 if (unlock_status != PSA_SUCCESS) {
3310 operation->error_occurred = 1;
3311 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003312
Paul Elliottc9774412023-02-06 15:14:07 +00003313 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003314}
3315
3316
3317psa_status_t psa_sign_hash_complete(
3318 psa_sign_hash_interruptible_operation_t *operation,
3319 uint8_t *signature, size_t signature_size,
3320 size_t *signature_length)
3321{
3322 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3323
3324 *signature_length = 0;
3325
Paul Elliottc9774412023-02-06 15:14:07 +00003326 /* Check that start has been called first, and that operation has not
3327 * previously errored. */
3328 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003329 status = PSA_ERROR_BAD_STATE;
3330 goto exit;
3331 }
3332
Paul Elliott096abc42023-02-03 18:33:23 +00003333 /* Immediately reject a zero-length signature buffer. This guarantees that
3334 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003335 if (signature_size == 0) {
3336 status = PSA_ERROR_BUFFER_TOO_SMALL;
3337 goto exit;
3338 }
3339
3340 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3341 signature_size,
3342 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003343
Paul Elliott296ede92022-12-15 17:00:30 +00003344 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003345 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003346
Paul Elliottebe225c2023-02-10 14:32:53 +00003347exit:
3348
Paul Elliott59200a22023-02-21 15:07:40 +00003349 psa_wipe_tag_output_buffer(signature, status, signature_size,
3350 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003351
Paul Elliott53bb3122023-02-10 14:22:22 +00003352 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003353 if (status != PSA_SUCCESS) {
3354 operation->error_occurred = 1;
3355 }
3356
Paul Elliott59ad9452022-12-18 15:09:02 +00003357 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003358 }
3359
3360 return status;
3361}
3362
3363psa_status_t psa_sign_hash_abort(
3364 psa_sign_hash_interruptible_operation_t *operation)
3365{
Paul Elliott59ad9452022-12-18 15:09:02 +00003366 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3367
3368 status = psa_sign_hash_abort_internal(operation);
3369
3370 /* We clear the number of ops done here, so that it is not cleared when
3371 * the operation fails or succeeds, only on manual abort. */
3372 operation->num_ops = 0;
3373
Paul Elliottc9774412023-02-06 15:14:07 +00003374 /* Likewise, failure state. */
3375 operation->error_occurred = 0;
3376
Paul Elliott59ad9452022-12-18 15:09:02 +00003377 return status;
3378}
3379
3380static psa_status_t psa_verify_hash_abort_internal(
3381 psa_verify_hash_interruptible_operation_t *operation)
3382{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003383 if (operation->id == 0) {
3384 /* The object has (apparently) been initialized but it is not (yet)
3385 * in use. It's ok to call abort on such an object, and there's
3386 * nothing to do. */
3387 return PSA_SUCCESS;
3388 }
3389
Paul Elliott59ad9452022-12-18 15:09:02 +00003390 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3391
3392 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003393
3394 operation->id = 0;
3395
Paul Elliotte6145dc2023-02-07 12:51:21 +00003396 /* Do not clear either the error_occurred or num_ops elements here as they
3397 * only want to be cleared by the application calling abort, not by abort
3398 * being called at completion of an operation. */
3399
Paul Elliott59ad9452022-12-18 15:09:02 +00003400 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003401}
3402
3403psa_status_t psa_verify_hash_start(
3404 psa_verify_hash_interruptible_operation_t *operation,
3405 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3406 const uint8_t *hash, size_t hash_length,
3407 const uint8_t *signature, size_t signature_length)
3408{
3409 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3410 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3411 psa_key_slot_t *slot;
3412
Paul Elliottc9774412023-02-06 15:14:07 +00003413 /* Check that start has not been previously called, or operation has not
3414 * previously errored. */
3415 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003416 return PSA_ERROR_BAD_STATE;
3417 }
3418
3419 status = psa_sign_verify_check_alg(0, alg);
3420 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003421 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003422 return status;
3423 }
3424
3425 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3426 PSA_KEY_USAGE_VERIFY_HASH,
3427 alg);
3428
3429 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003430 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003431 return status;
3432 }
3433
3434 psa_key_attributes_t attributes = {
3435 .core = slot->attr
3436 };
3437
Paul Elliott296ede92022-12-15 17:00:30 +00003438 /* Ensure ops count gets reset, in case of operation re-use. */
3439 operation->num_ops = 0;
3440
Paul Elliott9fe12f62022-11-30 19:16:02 +00003441 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3442 slot->key.data,
3443 slot->key.bytes,
3444 alg, hash, hash_length,
3445 signature, signature_length);
3446
3447 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003448 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003449 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003450 }
3451
Ryan Everett1b70a072024-01-04 10:32:49 +00003452 unlock_status = psa_unregister_read(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003453
Paul Elliottc9774412023-02-06 15:14:07 +00003454 if (unlock_status != PSA_SUCCESS) {
3455 operation->error_occurred = 1;
3456 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003457
Paul Elliottc9774412023-02-06 15:14:07 +00003458 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003459}
3460
3461psa_status_t psa_verify_hash_complete(
3462 psa_verify_hash_interruptible_operation_t *operation)
3463{
3464 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3465
Paul Elliottc9774412023-02-06 15:14:07 +00003466 /* Check that start has been called first, and that operation has not
3467 * previously errored. */
3468 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003469 status = PSA_ERROR_BAD_STATE;
3470 goto exit;
3471 }
3472
3473 status = psa_driver_wrapper_verify_hash_complete(operation);
3474
Paul Elliott296ede92022-12-15 17:00:30 +00003475 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003476 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003477 operation);
3478
Paul Elliottebe225c2023-02-10 14:32:53 +00003479exit:
3480
Paul Elliott9fe12f62022-11-30 19:16:02 +00003481 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003482 if (status != PSA_SUCCESS) {
3483 operation->error_occurred = 1;
3484 }
3485
Paul Elliott59ad9452022-12-18 15:09:02 +00003486 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003487 }
3488
3489 return status;
3490}
3491
3492psa_status_t psa_verify_hash_abort(
3493 psa_verify_hash_interruptible_operation_t *operation)
3494{
Paul Elliott59ad9452022-12-18 15:09:02 +00003495 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003496
Paul Elliott59ad9452022-12-18 15:09:02 +00003497 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003498
Paul Elliott59ad9452022-12-18 15:09:02 +00003499 /* We clear the number of ops done here, so that it is not cleared when
3500 * the operation fails or succeeds, only on manual abort. */
3501 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003502
Paul Elliottc9774412023-02-06 15:14:07 +00003503 /* Likewise, failure state. */
3504 operation->error_occurred = 0;
3505
Paul Elliott59ad9452022-12-18 15:09:02 +00003506 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003507}
3508
Paul Elliott588f8ed2022-12-02 18:10:26 +00003509/****************************************************************/
3510/* Asymmetric interruptible cryptography internal */
3511/* implementations */
3512/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003513
Paul Elliott588f8ed2022-12-02 18:10:26 +00003514void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3515{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003516
Paul Elliott588f8ed2022-12-02 18:10:26 +00003517#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3518 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3519 defined(MBEDTLS_ECP_RESTARTABLE)
3520
3521 /* Internal implementation uses zero to indicate infinite number max ops,
3522 * therefore avoid this value, and set to minimum possible. */
3523 if (max_ops == 0) {
3524 max_ops = 1;
3525 }
3526
Paul Elliott588f8ed2022-12-02 18:10:26 +00003527 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003528#else
3529 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003530#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3531 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3532 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3533}
3534
Paul Elliott588f8ed2022-12-02 18:10:26 +00003535uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003536 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003537{
3538#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3539 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3540 defined(MBEDTLS_ECP_RESTARTABLE)
3541
Paul Elliott93d9ca82023-02-15 18:14:21 +00003542 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003543#else
3544 (void) operation;
3545 return 0;
3546#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3547 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3548 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3549}
3550
3551uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003552 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003553{
3554 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3555 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3556 defined(MBEDTLS_ECP_RESTARTABLE)
3557
Paul Elliott93d9ca82023-02-15 18:14:21 +00003558 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003559#else
3560 (void) operation;
3561 return 0;
3562#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3563 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3564 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3565}
3566
3567psa_status_t mbedtls_psa_sign_hash_start(
3568 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3569 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3570 size_t key_buffer_size, psa_algorithm_t alg,
3571 const uint8_t *hash, size_t hash_length)
3572{
3573 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003574 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003575
Paul Elliott068fe072023-01-16 13:59:15 +00003576 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3577 return PSA_ERROR_NOT_SUPPORTED;
3578 }
3579
3580 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003581 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003582 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003583
3584#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003585 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3586 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003587
Paul Elliotta1c94092023-02-15 16:38:04 +00003588 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3589
Paul Elliott93d9ca82023-02-15 18:14:21 +00003590 /* Ensure num_ops is zero'ed in case of context re-use. */
3591 operation->num_ops = 0;
3592
Paul Elliott068fe072023-01-16 13:59:15 +00003593 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3594 attributes->core.bits,
3595 key_buffer,
3596 key_buffer_size,
3597 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003598
Paul Elliott068fe072023-01-16 13:59:15 +00003599 if (status != PSA_SUCCESS) {
3600 return status;
3601 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003602
Paul Elliott1bc59df2023-02-05 13:41:57 +00003603 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003604 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003605
Paul Elliott068fe072023-01-16 13:59:15 +00003606 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003607 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003608 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003609
Paul Elliott0290a762023-02-09 14:30:24 +00003610 /* We only need to store the same length of hash as the private key size
3611 * here, it would be truncated by the internal implementation anyway. */
3612 required_hash_length = (hash_length < operation->coordinate_bytes ?
3613 hash_length : operation->coordinate_bytes);
3614
Paul Elliottba70ad42023-02-15 18:23:53 +00003615 if (required_hash_length > sizeof(operation->hash)) {
3616 /* Shouldn't happen, but better safe than sorry. */
3617 return PSA_ERROR_CORRUPTION_DETECTED;
3618 }
3619
Paul Elliott0290a762023-02-09 14:30:24 +00003620 memcpy(operation->hash, hash, required_hash_length);
3621 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003622
3623 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003624
3625#else
Paul Elliott068fe072023-01-16 13:59:15 +00003626 (void) operation;
3627 (void) key_buffer;
3628 (void) key_buffer_size;
3629 (void) alg;
3630 (void) hash;
3631 (void) hash_length;
3632 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003633 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003634
Paul Elliott068fe072023-01-16 13:59:15 +00003635 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003636#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3637 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3638 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003639}
3640
3641psa_status_t mbedtls_psa_sign_hash_complete(
3642 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3643 uint8_t *signature, size_t signature_size,
3644 size_t *signature_length)
3645{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3647 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3648 defined(MBEDTLS_ECP_RESTARTABLE)
3649
Paul Elliotta1c94092023-02-15 16:38:04 +00003650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003651 mbedtls_mpi r;
3652 mbedtls_mpi s;
3653
Paul Elliott46845252023-02-03 14:59:11 +00003654 mbedtls_mpi_init(&r);
3655 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003656
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003657 /* Ensure max_ops is set to the current value (or default). */
3658 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3659
Paul Elliotta1c94092023-02-15 16:38:04 +00003660 if (signature_size < 2 * operation->coordinate_bytes) {
3661 status = PSA_ERROR_BUFFER_TOO_SMALL;
3662 goto exit;
3663 }
3664
Paul Elliott588f8ed2022-12-02 18:10:26 +00003665 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003666
Paul Elliott588f8ed2022-12-02 18:10:26 +00003667#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3668 status = mbedtls_to_psa_error(
3669 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003670 &r,
3671 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003672 &operation->ctx->d,
3673 operation->hash,
3674 operation->hash_length,
3675 operation->md_alg,
3676 mbedtls_psa_get_random,
3677 MBEDTLS_PSA_RANDOM_STATE,
3678 &operation->restart_ctx));
3679#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003680 status = PSA_ERROR_NOT_SUPPORTED;
3681 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003682#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3683 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003684 status = mbedtls_to_psa_error(
3685 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003686 &r,
3687 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688 &operation->ctx->d,
3689 operation->hash,
3690 operation->hash_length,
3691 mbedtls_psa_get_random,
3692 MBEDTLS_PSA_RANDOM_STATE,
3693 mbedtls_psa_get_random,
3694 MBEDTLS_PSA_RANDOM_STATE,
3695 &operation->restart_ctx));
3696 }
3697
Paul Elliottf8e5b562023-02-19 18:43:45 +00003698 /* Hide the fact that the restart context only holds a delta of number of
3699 * ops done during the last operation, not an absolute value. */
3700 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3701
Paul Elliott724bd252023-02-08 12:35:08 +00003702 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003703 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003704 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003705 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003706 operation->coordinate_bytes)
3707 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003708
3709 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003710 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003711 }
3712
3713 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003714 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003716 operation->coordinate_bytes,
3717 operation->coordinate_bytes)
3718 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003719
3720 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003721 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722 }
3723
Paul Elliott1bc59df2023-02-05 13:41:57 +00003724 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725
Paul Elliott724bd252023-02-08 12:35:08 +00003726 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003727 }
Paul Elliott724bd252023-02-08 12:35:08 +00003728
3729exit:
3730
3731 mbedtls_mpi_free(&r);
3732 mbedtls_mpi_free(&s);
3733 return status;
3734
Paul Elliott588f8ed2022-12-02 18:10:26 +00003735 #else
3736
3737 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003738 (void) signature;
3739 (void) signature_size;
3740 (void) signature_length;
3741
3742 return PSA_ERROR_NOT_SUPPORTED;
3743
3744#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3745 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3746 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3747}
3748
3749psa_status_t mbedtls_psa_sign_hash_abort(
3750 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3751{
3752
3753#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3754 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3755 defined(MBEDTLS_ECP_RESTARTABLE)
3756
3757 if (operation->ctx) {
3758 mbedtls_ecdsa_free(operation->ctx);
3759 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003760 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003761 }
3762
3763 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3764
Paul Elliott93d9ca82023-02-15 18:14:21 +00003765 operation->num_ops = 0;
3766
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767 return PSA_SUCCESS;
3768
3769#else
3770
3771 (void) operation;
3772
3773 return PSA_ERROR_NOT_SUPPORTED;
3774
3775#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3776 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3777 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3778}
3779
3780psa_status_t mbedtls_psa_verify_hash_start(
3781 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3782 const psa_key_attributes_t *attributes,
3783 const uint8_t *key_buffer, size_t key_buffer_size,
3784 psa_algorithm_t alg,
3785 const uint8_t *hash, size_t hash_length,
3786 const uint8_t *signature, size_t signature_length)
3787{
3788 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003789 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003790 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003791
Paul Elliott068fe072023-01-16 13:59:15 +00003792 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3793 return PSA_ERROR_NOT_SUPPORTED;
3794 }
3795
3796 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003797 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003798 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003799
3800#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003801 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3802 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003803
Paul Elliotta1c94092023-02-15 16:38:04 +00003804 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3805 mbedtls_mpi_init(&operation->r);
3806 mbedtls_mpi_init(&operation->s);
3807
Paul Elliott93d9ca82023-02-15 18:14:21 +00003808 /* Ensure num_ops is zero'ed in case of context re-use. */
3809 operation->num_ops = 0;
3810
Paul Elliott068fe072023-01-16 13:59:15 +00003811 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3812 attributes->core.bits,
3813 key_buffer,
3814 key_buffer_size,
3815 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003816
Paul Elliott068fe072023-01-16 13:59:15 +00003817 if (status != PSA_SUCCESS) {
3818 return status;
3819 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003820
Paul Elliottc569fc22023-02-10 13:02:54 +00003821 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003822
Paul Elliott1bc59df2023-02-05 13:41:57 +00003823 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003824 return PSA_ERROR_INVALID_SIGNATURE;
3825 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826
Paul Elliott068fe072023-01-16 13:59:15 +00003827 status = mbedtls_to_psa_error(
3828 mbedtls_mpi_read_binary(&operation->r,
3829 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003830 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 Elliott068fe072023-01-16 13:59:15 +00003836 status = mbedtls_to_psa_error(
3837 mbedtls_mpi_read_binary(&operation->s,
3838 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003839 coordinate_bytes,
3840 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003841
Paul Elliott068fe072023-01-16 13:59:15 +00003842 if (status != PSA_SUCCESS) {
3843 return status;
3844 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003845
Paul Elliott2c9843f2023-02-15 17:32:42 +00003846 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003847
Paul Elliott2c9843f2023-02-15 17:32:42 +00003848 if (status != PSA_SUCCESS) {
3849 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003850 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851
Paul Elliott0290a762023-02-09 14:30:24 +00003852 /* We only need to store the same length of hash as the private key size
3853 * here, it would be truncated by the internal implementation anyway. */
3854 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3855 coordinate_bytes);
3856
Paul Elliottba70ad42023-02-15 18:23:53 +00003857 if (required_hash_length > sizeof(operation->hash)) {
3858 /* Shouldn't happen, but better safe than sorry. */
3859 return PSA_ERROR_CORRUPTION_DETECTED;
3860 }
3861
Paul Elliott0290a762023-02-09 14:30:24 +00003862 memcpy(operation->hash, hash, required_hash_length);
3863 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003864
3865 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003866#else
Paul Elliott068fe072023-01-16 13:59:15 +00003867 (void) operation;
3868 (void) key_buffer;
3869 (void) key_buffer_size;
3870 (void) alg;
3871 (void) hash;
3872 (void) hash_length;
3873 (void) signature;
3874 (void) signature_length;
3875 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003876 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003877 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003878
Paul Elliott068fe072023-01-16 13:59:15 +00003879 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003880#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3881 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3882 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003883}
3884
3885psa_status_t mbedtls_psa_verify_hash_complete(
3886 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3887{
3888
3889#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3890 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3891 defined(MBEDTLS_ECP_RESTARTABLE)
3892
Paul Elliottf8e5b562023-02-19 18:43:45 +00003893 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3894
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003895 /* Ensure max_ops is set to the current value (or default). */
3896 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3897
Paul Elliottf8e5b562023-02-19 18:43:45 +00003898 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003899 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3900 operation->hash,
3901 operation->hash_length,
3902 &operation->ctx->Q,
3903 &operation->r,
3904 &operation->s,
3905 &operation->restart_ctx));
3906
Paul Elliottf8e5b562023-02-19 18:43:45 +00003907 /* Hide the fact that the restart context only holds a delta of number of
3908 * ops done during the last operation, not an absolute value. */
3909 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3910
3911 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003912#else
3913 (void) operation;
3914
3915 return PSA_ERROR_NOT_SUPPORTED;
3916
3917#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3918 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3919 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3920}
3921
3922psa_status_t mbedtls_psa_verify_hash_abort(
3923 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3924{
3925
3926#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3927 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3928 defined(MBEDTLS_ECP_RESTARTABLE)
3929
3930 if (operation->ctx) {
3931 mbedtls_ecdsa_free(operation->ctx);
3932 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003933 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003934 }
3935
3936 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3937
Paul Elliott93d9ca82023-02-15 18:14:21 +00003938 operation->num_ops = 0;
3939
Paul Elliott588f8ed2022-12-02 18:10:26 +00003940 mbedtls_mpi_free(&operation->r);
3941 mbedtls_mpi_free(&operation->s);
3942
3943 return PSA_SUCCESS;
3944
3945#else
3946 (void) operation;
3947
3948 return PSA_ERROR_NOT_SUPPORTED;
3949
3950#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3951 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3952 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3953}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003954
mohammad1603503973b2018-03-12 15:59:30 +02003955/****************************************************************/
3956/* Symmetric cryptography */
3957/****************************************************************/
3958
Gilles Peskine449bd832023-01-11 14:50:10 +01003959static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3960 mbedtls_svc_key_id_t key,
3961 psa_algorithm_t alg,
3962 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003963{
3964 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3965 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003966 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3968 PSA_KEY_USAGE_ENCRYPT :
3969 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003970 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02003971
3972 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003974 status = PSA_ERROR_BAD_STATE;
3975 goto exit;
3976 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003977
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003979 status = PSA_ERROR_INVALID_ARGUMENT;
3980 goto exit;
3981 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003982
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3984 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003985 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003987
Ronald Cron49fafa92021-03-10 08:34:23 +01003988 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003989 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003990 * so we only set it (in the driver wrapper) after resources have been
3991 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003992 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003994 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003996 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 }
3998 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003999
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004000 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004002 };
4003
Ronald Cronab99ac22020-10-01 16:38:28 +02004004 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 if (cipher_operation == MBEDTLS_ENCRYPT) {
4006 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4007 &attributes,
4008 slot->key.data,
4009 slot->key.bytes,
4010 alg);
4011 } else {
4012 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4013 &attributes,
4014 slot->key.data,
4015 slot->key.bytes,
4016 alg);
4017 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004018
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004019exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004020 if (status != PSA_SUCCESS) {
4021 psa_cipher_abort(operation);
4022 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004023
Ryan Everett1b70a072024-01-04 10:32:49 +00004024 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004025
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004027}
4028
Gilles Peskine449bd832023-01-11 14:50:10 +01004029psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4030 mbedtls_svc_key_id_t key,
4031 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004032{
Gilles Peskine449bd832023-01-11 14:50:10 +01004033 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004034}
4035
Gilles Peskine449bd832023-01-11 14:50:10 +01004036psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4037 mbedtls_svc_key_id_t key,
4038 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004039{
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004041}
4042
Gilles Peskine449bd832023-01-11 14:50:10 +01004043psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4044 uint8_t *iv,
4045 size_t iv_size,
4046 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004047{
Ronald Cron6d051732020-10-01 14:10:20 +02004048 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004049 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004050 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004051
Gilles Peskine449bd832023-01-11 14:50:10 +01004052 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004053 status = PSA_ERROR_BAD_STATE;
4054 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004055 }
4056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004058 status = PSA_ERROR_BAD_STATE;
4059 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004060 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004061
Ronald Cron2fb90522021-07-05 12:31:44 +02004062 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004064 status = PSA_ERROR_BUFFER_TOO_SMALL;
4065 goto exit;
4066 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004067
Gilles Peskine449bd832023-01-11 14:50:10 +01004068 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004069 status = PSA_ERROR_GENERIC_ERROR;
4070 goto exit;
4071 }
4072
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 status = psa_generate_random(local_iv, default_iv_length);
4074 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004075 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 }
Ronald Cron5618a392021-03-26 09:52:26 +01004077
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 status = psa_driver_wrapper_cipher_set_iv(operation,
4079 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004080
4081exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 if (status == PSA_SUCCESS) {
4083 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004084 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004085 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004087 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004089 }
Ronald Cron6d051732020-10-01 14:10:20 +02004090
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004092}
4093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4095 const uint8_t *iv,
4096 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004097{
Ronald Cron6d051732020-10-01 14:10:20 +02004098 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004099
Gilles Peskine449bd832023-01-11 14:50:10 +01004100 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004101 status = PSA_ERROR_BAD_STATE;
4102 goto exit;
4103 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004104
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004106 status = PSA_ERROR_BAD_STATE;
4107 goto exit;
4108 }
Ronald Crona0d68172021-03-26 10:15:08 +01004109
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004111 status = PSA_ERROR_INVALID_ARGUMENT;
4112 goto exit;
4113 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004114
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 status = psa_driver_wrapper_cipher_set_iv(operation,
4116 iv,
4117 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004118
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004119exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004121 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 } else {
4123 psa_cipher_abort(operation);
4124 }
4125 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004126}
4127
Gilles Peskine449bd832023-01-11 14:50:10 +01004128psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4129 const uint8_t *input,
4130 size_t input_length,
4131 uint8_t *output,
4132 size_t output_size,
4133 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004134{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004135 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004136
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004138 status = PSA_ERROR_BAD_STATE;
4139 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004140 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004141
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004143 status = PSA_ERROR_BAD_STATE;
4144 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004145 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004146
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 status = psa_driver_wrapper_cipher_update(operation,
4148 input,
4149 input_length,
4150 output,
4151 output_size,
4152 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004153
4154exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 if (status != PSA_SUCCESS) {
4156 psa_cipher_abort(operation);
4157 }
Ronald Cron6d051732020-10-01 14:10:20 +02004158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004160}
4161
Gilles Peskine449bd832023-01-11 14:50:10 +01004162psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4163 uint8_t *output,
4164 size_t output_size,
4165 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004166{
David Saadab4ecc272019-02-14 13:48:10 +02004167 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004168
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004170 status = PSA_ERROR_BAD_STATE;
4171 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004172 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004173
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004175 status = PSA_ERROR_BAD_STATE;
4176 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004177 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004178
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 status = psa_driver_wrapper_cipher_finish(operation,
4180 output,
4181 output_size,
4182 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004183
4184exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 if (status == PSA_SUCCESS) {
4186 return psa_cipher_abort(operation);
4187 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004188 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004190
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004192 }
mohammad1603503973b2018-03-12 15:59:30 +02004193}
4194
Gilles Peskine449bd832023-01-11 14:50:10 +01004195psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004196{
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004198 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004199 * in use. It's ok to call abort on such an object, and there's
4200 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004202 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004203
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004205
Ronald Cron49fafa92021-03-10 08:34:23 +01004206 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004207 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004208 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004209
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004211}
4212
Gilles Peskine449bd832023-01-11 14:50:10 +01004213psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4214 psa_algorithm_t alg,
4215 const uint8_t *input,
4216 size_t input_length,
4217 uint8_t *output,
4218 size_t output_size,
4219 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004220{
4221 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004222 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004223 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004224 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4225 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004226 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004229 status = PSA_ERROR_INVALID_ARGUMENT;
4230 goto exit;
4231 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004232
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4234 PSA_KEY_USAGE_ENCRYPT,
4235 alg);
4236 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004237 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004239
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004240 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004242 };
4243
Gilles Peskine449bd832023-01-11 14:50:10 +01004244 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4245 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004246 status = PSA_ERROR_GENERIC_ERROR;
4247 goto exit;
4248 }
4249
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 if (default_iv_length > 0) {
4251 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004252 status = PSA_ERROR_BUFFER_TOO_SMALL;
4253 goto exit;
4254 }
4255
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 status = psa_generate_random(local_iv, default_iv_length);
4257 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004258 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004260 }
4261
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004262 status = psa_driver_wrapper_cipher_encrypt(
4263 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004264 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004265 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004267
4268exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004269 unlock_status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004271 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004272 }
Ronald Cron23919522021-07-08 19:00:07 +02004273
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 if (status == PSA_SUCCESS) {
4275 if (default_iv_length > 0) {
4276 memcpy(output, local_iv, default_iv_length);
4277 }
4278 *output_length += default_iv_length;
4279 } else {
4280 *output_length = 0;
4281 }
4282
4283 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004284}
4285
Gilles Peskine449bd832023-01-11 14:50:10 +01004286psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4287 psa_algorithm_t alg,
4288 const uint8_t *input,
4289 size_t input_length,
4290 uint8_t *output,
4291 size_t output_size,
4292 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004293{
4294 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004295 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004296 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004297 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004298
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004300 status = PSA_ERROR_INVALID_ARGUMENT;
4301 goto exit;
4302 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004303
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4305 PSA_KEY_USAGE_DECRYPT,
4306 alg);
4307 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004308 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004310
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004311 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004313 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004314
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4316 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004317 status = PSA_ERROR_INVALID_ARGUMENT;
4318 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004320 status = PSA_ERROR_INVALID_ARGUMENT;
4321 goto exit;
4322 }
4323
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004324 status = psa_driver_wrapper_cipher_decrypt(
4325 &attributes, slot->key.data, slot->key.bytes,
4326 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004328
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004329exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004330 unlock_status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004332 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004334
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004336 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004337 }
Ronald Cron23919522021-07-08 19:00:07 +02004338
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004340}
4341
4342
mohammad16035955c982018-04-26 00:53:03 +03004343/****************************************************************/
4344/* AEAD */
4345/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004346
Paul Elliottbaff51c2021-09-28 17:44:45 +01004347/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004348static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004349{
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004351}
4352
4353/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004354static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4355 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004356{
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004360#if defined(PSA_WANT_ALG_GCM)
4361 case PSA_ALG_GCM:
4362 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 * arbitrarily large nonces. Please note that we do not generally
4364 * recommend the usage of nonces of greater length than
4365 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4366 * size, which can then lead to collisions if you encrypt a very
4367 * large number of messages.*/
4368 if (nonce_length != 0) {
4369 return PSA_SUCCESS;
4370 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004371 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004372#endif /* PSA_WANT_ALG_GCM */
4373#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004374 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 if (nonce_length >= 7 && nonce_length <= 13) {
4376 return PSA_SUCCESS;
4377 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004378 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004379#endif /* PSA_WANT_ALG_CCM */
4380#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004381 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 if (nonce_length == 12) {
4383 return PSA_SUCCESS;
4384 } else if (nonce_length == 8) {
4385 return PSA_ERROR_NOT_SUPPORTED;
4386 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004387 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004388#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004389 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004390 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004392 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004393
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004395}
4396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004398{
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4400 return PSA_ERROR_INVALID_ARGUMENT;
4401 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004404}
4405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4407 psa_algorithm_t alg,
4408 const uint8_t *nonce,
4409 size_t nonce_length,
4410 const uint8_t *additional_data,
4411 size_t additional_data_length,
4412 const uint8_t *plaintext,
4413 size_t plaintext_length,
4414 uint8_t *ciphertext,
4415 size_t ciphertext_size,
4416 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004417{
Ronald Cron215633c2021-03-16 17:15:37 +01004418 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4419 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004420
mohammad1603f08a5502018-06-03 15:05:47 +03004421 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004422
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 status = psa_aead_check_algorithm(alg);
4424 if (status != PSA_SUCCESS) {
4425 return status;
4426 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004427
Ronald Cron9a986162021-03-26 12:40:07 +01004428 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4430 if (status != PSA_SUCCESS) {
4431 return status;
4432 }
mohammad16035955c982018-04-26 00:53:03 +03004433
Ronald Cron215633c2021-03-16 17:15:37 +01004434 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004436 };
mohammad16035955c982018-04-26 00:53:03 +03004437
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 status = psa_aead_check_nonce_length(alg, nonce_length);
4439 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004440 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004442
Ronald Cronde822812021-03-17 16:08:20 +01004443 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004444 &attributes, slot->key.data, slot->key.bytes,
4445 alg,
4446 nonce, nonce_length,
4447 additional_data, additional_data_length,
4448 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004450
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4452 memset(ciphertext, 0, ciphertext_size);
4453 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004454
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004455exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004456 psa_unregister_read(slot);
mohammad16035955c982018-04-26 00:53:03 +03004457
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004459}
4460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4462 psa_algorithm_t alg,
4463 const uint8_t *nonce,
4464 size_t nonce_length,
4465 const uint8_t *additional_data,
4466 size_t additional_data_length,
4467 const uint8_t *ciphertext,
4468 size_t ciphertext_length,
4469 uint8_t *plaintext,
4470 size_t plaintext_size,
4471 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004472{
Ronald Cron215633c2021-03-16 17:15:37 +01004473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4474 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004475
Gilles Peskineee652a32018-06-01 19:23:52 +02004476 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004477
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 status = psa_aead_check_algorithm(alg);
4479 if (status != PSA_SUCCESS) {
4480 return status;
4481 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004482
Ronald Cron9a986162021-03-26 12:40:07 +01004483 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4485 if (status != PSA_SUCCESS) {
4486 return status;
4487 }
mohammad16035955c982018-04-26 00:53:03 +03004488
Ronald Cron215633c2021-03-16 17:15:37 +01004489 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004491 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004492
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 status = psa_aead_check_nonce_length(alg, nonce_length);
4494 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004495 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004497
Ronald Cronde822812021-03-17 16:08:20 +01004498 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004499 &attributes, slot->key.data, slot->key.bytes,
4500 alg,
4501 nonce, nonce_length,
4502 additional_data, additional_data_length,
4503 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004505
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 if (status != PSA_SUCCESS && plaintext_size != 0) {
4507 memset(plaintext, 0, plaintext_size);
4508 }
mohammad160360a64d02018-06-03 17:20:42 +03004509
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004510exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004511 psa_unregister_read(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 return status;
mohammad16035955c982018-04-26 00:53:03 +03004514}
4515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4517{
4518 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004521#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004523 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4525 return PSA_ERROR_INVALID_ARGUMENT;
4526 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004527 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004528#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004529
Przemek Stekiel86679c72022-10-06 17:06:56 +02004530#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004532 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4534 return PSA_ERROR_INVALID_ARGUMENT;
4535 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004536 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004537#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004538
Przemek Stekiel86679c72022-10-06 17:06:56 +02004539#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004541 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 if (tag_len != 16) {
4543 return PSA_ERROR_INVALID_ARGUMENT;
4544 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004545 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004546#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004547
4548 default:
4549 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004551 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004553}
4554
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004555/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004556static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4557 int is_encrypt,
4558 mbedtls_svc_key_id_t key,
4559 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004560{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004561 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4562 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4563 psa_key_slot_t *slot = NULL;
4564 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004565 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 status = psa_aead_check_algorithm(alg);
4568 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004569 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004573 status = PSA_ERROR_BAD_STATE;
4574 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004575 }
4576
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 if (operation->nonce_set || operation->lengths_set ||
4578 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004579 status = PSA_ERROR_BAD_STATE;
4580 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004581 }
4582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004584 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004586 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4590 alg);
4591 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004592 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004594
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004595 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004596 .core = slot->attr
4597 };
4598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004600 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004602
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 if (is_encrypt) {
4604 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4605 &attributes,
4606 slot->key.data,
4607 slot->key.bytes,
4608 alg);
4609 } else {
4610 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4611 &attributes,
4612 slot->key.data,
4613 slot->key.bytes,
4614 alg);
4615 }
4616 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004617 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004619
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004621
4622exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004623 unlock_status = psa_unregister_read(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004624
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004626 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004628 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 } else {
4630 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004631 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004634}
4635
Paul Elliott302ff6b2021-04-20 18:10:30 +01004636/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004637psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4638 mbedtls_svc_key_id_t key,
4639 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004640{
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004642}
4643
4644/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004645psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4646 mbedtls_svc_key_id_t key,
4647 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004648{
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004650}
4651
4652/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004653psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4654 uint8_t *nonce,
4655 size_t nonce_size,
4656 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004657{
4658 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004659 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004660 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004661
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004662 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004663
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004665 status = PSA_ERROR_BAD_STATE;
4666 goto exit;
4667 }
4668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004670 status = PSA_ERROR_BAD_STATE;
4671 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004672 }
4673
Paul Elliotte193ea82021-10-01 13:00:16 +01004674 /* For CCM, this size may not be correct according to the PSA
4675 * specification. The PSA Crypto 1.0.1 specification states:
4676 *
4677 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4678 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4679 *
4680 * However this restriction that L has to be the smallest integer is not
4681 * applied in practice, and it is not implementable here since the
4682 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4684 operation->alg);
4685 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004686 status = PSA_ERROR_BUFFER_TOO_SMALL;
4687 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004688 }
4689
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 status = psa_generate_random(local_nonce, required_nonce_size);
4691 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004692 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004696
Paul Elliott39dc6b82021-05-11 19:16:09 +01004697exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if (status == PSA_SUCCESS) {
4699 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004700 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 } else {
4702 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004703 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004706}
4707
4708/* Set the nonce for a multipart authenticated encryption or decryption
4709 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004710psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4711 const uint8_t *nonce,
4712 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004713{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004714 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004717 status = PSA_ERROR_BAD_STATE;
4718 goto exit;
4719 }
4720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004722 status = PSA_ERROR_BAD_STATE;
4723 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004724 }
4725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4727 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004728 status = PSA_ERROR_INVALID_ARGUMENT;
4729 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004730 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4733 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004734
Paul Elliott39dc6b82021-05-11 19:16:09 +01004735exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004737 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 } else {
4739 psa_aead_abort(operation);
4740 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004741
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004743}
4744
4745/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004746psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4747 size_t ad_length,
4748 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004749{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004750 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004753 status = PSA_ERROR_BAD_STATE;
4754 goto exit;
4755 }
4756
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 if (operation->lengths_set || operation->ad_started ||
4758 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004759 status = PSA_ERROR_BAD_STATE;
4760 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004761 }
4762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004764#if defined(PSA_WANT_ALG_GCM)
4765 case PSA_ALG_GCM:
4766 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 * bits. Without the guard this code will generate warnings on 32bit
4768 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004769#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 if (((uint64_t) ad_length) >> 61 != 0 ||
4771 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004772 status = PSA_ERROR_INVALID_ARGUMENT;
4773 goto exit;
4774 }
Paul Elliott325d3742021-09-27 17:56:28 +01004775#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004776 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004777#endif /* PSA_WANT_ALG_GCM */
4778#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004779 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004781 status = PSA_ERROR_INVALID_ARGUMENT;
4782 goto exit;
4783 }
4784 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004785#endif /* PSA_WANT_ALG_CCM */
4786#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004787 case PSA_ALG_CHACHA20_POLY1305:
4788 /* No length restrictions for ChaChaPoly. */
4789 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004790#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004791 default:
4792 break;
4793 }
Paul Elliott325d3742021-09-27 17:56:28 +01004794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4796 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004797
Paul Elliott39dc6b82021-05-11 19:16:09 +01004798exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004800 operation->ad_remaining = ad_length;
4801 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004802 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 } else {
4804 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004805 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004808}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004809
4810/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004811psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4812 const uint8_t *input,
4813 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004814{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004815 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004818 status = PSA_ERROR_BAD_STATE;
4819 goto exit;
4820 }
4821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004823 status = PSA_ERROR_BAD_STATE;
4824 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004825 }
4826
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 if (operation->lengths_set) {
4828 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004829 status = PSA_ERROR_INVALID_ARGUMENT;
4830 goto exit;
4831 }
4832
4833 operation->ad_remaining -= input_length;
4834 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004835#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004837 status = PSA_ERROR_BAD_STATE;
4838 goto exit;
4839 }
4840#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 status = psa_driver_wrapper_aead_update_ad(operation, input,
4843 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004844
Paul Elliott39dc6b82021-05-11 19:16:09 +01004845exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004847 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 } else {
4849 psa_aead_abort(operation);
4850 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004853}
4854
4855/* Encrypt or decrypt a message fragment in an active multipart AEAD
4856 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004857psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4858 const uint8_t *input,
4859 size_t input_length,
4860 uint8_t *output,
4861 size_t output_size,
4862 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004863{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004864 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004865
4866 *output_length = 0;
4867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004869 status = PSA_ERROR_BAD_STATE;
4870 goto exit;
4871 }
4872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004874 status = PSA_ERROR_BAD_STATE;
4875 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004876 }
4877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004879 /* Additional data length was supplied, but not all the additional
4880 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004882 status = PSA_ERROR_INVALID_ARGUMENT;
4883 goto exit;
4884 }
4885
4886 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004888 status = PSA_ERROR_INVALID_ARGUMENT;
4889 goto exit;
4890 }
4891
4892 operation->body_remaining -= input_length;
4893 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004894#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004896 status = PSA_ERROR_BAD_STATE;
4897 goto exit;
4898 }
4899#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004900
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4902 output, output_size,
4903 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004904
Paul Elliott39dc6b82021-05-11 19:16:09 +01004905exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004907 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 } else {
4909 psa_aead_abort(operation);
4910 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004911
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004913}
4914
Gilles Peskine449bd832023-01-11 14:50:10 +01004915static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004916{
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 if (operation->id == 0 || !operation->nonce_set) {
4918 return PSA_ERROR_BAD_STATE;
4919 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004920
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4922 operation->body_remaining != 0)) {
4923 return PSA_ERROR_INVALID_ARGUMENT;
4924 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004927}
4928
Paul Elliott302ff6b2021-04-20 18:10:30 +01004929/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004930psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4931 uint8_t *ciphertext,
4932 size_t ciphertext_size,
4933 size_t *ciphertext_length,
4934 uint8_t *tag,
4935 size_t tag_size,
4936 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004937{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004938 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4939
Paul Elliott302ff6b2021-04-20 18:10:30 +01004940 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004941 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004942
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 status = psa_aead_final_checks(operation);
4944 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004945 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004947
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004949 status = PSA_ERROR_BAD_STATE;
4950 goto exit;
4951 }
4952
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4954 ciphertext_size,
4955 ciphertext_length,
4956 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004957
Paul Elliott39dc6b82021-05-11 19:16:09 +01004958exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004959
4960
Paul Elliottf47b0952021-05-21 18:02:33 +01004961 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004962 * the zero tag size, make sure the tag is set to something implausible.
4963 * Even if the operation succeeds, make sure we clear the rest of the
4964 * buffer to prevent potential leakage of anything previously placed in
4965 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004966 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004967
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004971}
4972
4973/* Finish authenticating and decrypting a message in a multipart AEAD
4974 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004975psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4976 uint8_t *plaintext,
4977 size_t plaintext_size,
4978 size_t *plaintext_length,
4979 const uint8_t *tag,
4980 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004981{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004982 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4983
Paul Elliott302ff6b2021-04-20 18:10:30 +01004984 *plaintext_length = 0;
4985
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 status = psa_aead_final_checks(operation);
4987 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004988 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004992 status = PSA_ERROR_BAD_STATE;
4993 goto exit;
4994 }
4995
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4997 plaintext_size,
4998 plaintext_length,
4999 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005000
5001exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005003
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005005}
5006
5007/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005008psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005009{
5010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5011
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005013 /* The object has (apparently) been initialized but it is not (yet)
5014 * in use. It's ok to call abort on such an object, and there's
5015 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005017 }
5018
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005020
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005022
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005024}
5025
Gilles Peskinee59236f2018-01-27 23:32:46 +01005026/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005027/* Generators */
5028/****************************************************************/
5029
Przemek Stekiel69c46792022-06-10 12:59:51 +02005030#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005031 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005032 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305033 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305034 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005035#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005036#endif /* At least one builtin KDF */
5037
Przemek Stekiel69c46792022-06-10 12:59:51 +02005038#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005039 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5040 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5041static psa_status_t psa_key_derivation_start_hmac(
5042 psa_mac_operation_t *operation,
5043 psa_algorithm_t hash_alg,
5044 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005046{
5047 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5048 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5050 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5051 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005052
Steven Cooreman72f736a2021-05-07 14:14:37 +02005053 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005055
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 status = psa_driver_wrapper_mac_sign_setup(operation,
5057 &attributes,
5058 hmac_key, hmac_key_length,
5059 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005060
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 psa_reset_key_attributes(&attributes);
5062 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005063}
5064#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005065
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005066#define HKDF_STATE_INIT 0 /* no input yet */
5067#define HKDF_STATE_STARTED 1 /* got salt */
5068#define HKDF_STATE_KEYED 2 /* got key */
5069#define HKDF_STATE_OUTPUT 3 /* output started */
5070
Gilles Peskinecbe66502019-05-16 16:59:18 +02005071static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005073{
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5075 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5076 } else {
5077 return operation->alg;
5078 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005079}
5080
Gilles Peskine449bd832023-01-11 14:50:10 +01005081psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005082{
5083 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5085 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005086 /* The object has (apparently) been initialized but it is not
5087 * in use. It's ok to call abort on such an object, and there's
5088 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005090#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5092 mbedtls_free(operation->ctx.hkdf.info);
5093 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5094 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005095#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005096#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5097 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5099 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5100 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5101 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005102 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005104 }
5105
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005107 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005109 }
5110
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005112 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005114 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005115#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005117 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005119 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005120#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005121 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005122
5123 /* We leave the fields Ai and output_block to be erased safely by the
5124 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005126#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5127 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005128#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5130 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5131 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5132 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005133#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305134#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305135 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305136 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005137 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305138 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305139 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305140
5141 status = PSA_SUCCESS;
5142 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305143#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005144 {
5145 status = PSA_ERROR_BAD_STATE;
5146 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 mbedtls_platform_zeroize(operation, sizeof(*operation));
5148 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005149}
5150
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005153{
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005157 }
5158
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005159 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005161}
5162
Gilles Peskine449bd832023-01-11 14:50:10 +01005163psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5164 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005165{
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 if (operation->alg == 0) {
5167 return PSA_ERROR_BAD_STATE;
5168 }
5169 if (capacity > operation->capacity) {
5170 return PSA_ERROR_INVALID_ARGUMENT;
5171 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005172 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005174}
5175
Przemek Stekiel69c46792022-06-10 12:59:51 +02005176#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005177/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005178static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5179 psa_algorithm_t kdf_alg,
5180 uint8_t *output,
5181 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005182{
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5184 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005185 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005186 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005187#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005189#else
5190 const uint8_t last_block = 0xff;
5191#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005192
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 if (hkdf->state < HKDF_STATE_KEYED ||
5194 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005195#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005197#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 )) {
5199 return PSA_ERROR_BAD_STATE;
5200 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005201 hkdf->state = HKDF_STATE_OUTPUT;
5202
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005204 /* Copy what remains of the current block */
5205 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005207 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 }
5209 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005210 output += n;
5211 output_length -= n;
5212 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005214 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005216 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005217 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005218 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005219 * object was corrupted or if this function is called directly
5220 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 if (hkdf->block_number == last_block) {
5222 return PSA_ERROR_BAD_STATE;
5223 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005224
5225 /* We need a new block */
5226 ++hkdf->block_number;
5227 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005228
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5230 hash_alg,
5231 hkdf->prk,
5232 hash_length);
5233 if (status != PSA_SUCCESS) {
5234 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005235 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005236
5237 if (hkdf->block_number != 1) {
5238 status = psa_mac_update(&hkdf->hmac,
5239 hkdf->output_block,
5240 hash_length);
5241 if (status != PSA_SUCCESS) {
5242 return status;
5243 }
5244 }
5245 status = psa_mac_update(&hkdf->hmac,
5246 hkdf->info,
5247 hkdf->info_length);
5248 if (status != PSA_SUCCESS) {
5249 return status;
5250 }
5251 status = psa_mac_update(&hkdf->hmac,
5252 &hkdf->block_number, 1);
5253 if (status != PSA_SUCCESS) {
5254 return status;
5255 }
5256 status = psa_mac_sign_finish(&hkdf->hmac,
5257 hkdf->output_block,
5258 sizeof(hkdf->output_block),
5259 &hmac_output_length);
5260 if (status != PSA_SUCCESS) {
5261 return status;
5262 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005263 }
5264
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005266}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005267#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005268
John Durkop07cc04a2020-11-16 22:08:34 -08005269#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5270 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005271static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5272 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005274{
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5276 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005277 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5278 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005279 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005280
Janos Follath7742fee2019-06-17 12:58:10 +01005281 /* We can't be wanting more output after block 0xff, otherwise
5282 * the capacity check in psa_key_derivation_output_bytes() would have
5283 * prevented this call. It could happen only if the operation
5284 * object was corrupted or if this function is called directly
5285 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 if (tls12_prf->block_number == 0xff) {
5287 return PSA_ERROR_CORRUPTION_DETECTED;
5288 }
Janos Follath7742fee2019-06-17 12:58:10 +01005289
5290 /* We need a new block */
5291 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005292 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005293
5294 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5295 *
5296 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5297 *
5298 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5299 * HMAC_hash(secret, A(2) + seed) +
5300 * HMAC_hash(secret, A(3) + seed) + ...
5301 *
5302 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005303 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005304 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005305 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005306 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005307 * is currently extracted as `output_block` and where i is
5308 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005309 */
5310
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 status = psa_key_derivation_start_hmac(&hmac,
5312 hash_alg,
5313 tls12_prf->secret,
5314 tls12_prf->secret_length);
5315 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005316 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005318
5319 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005321 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5322 * the variable seed and in this instance means it in the context of the
5323 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 status = psa_mac_update(&hmac,
5325 tls12_prf->label,
5326 tls12_prf->label_length);
5327 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005328 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 }
5330 status = psa_mac_update(&hmac,
5331 tls12_prf->seed,
5332 tls12_prf->seed_length);
5333 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005334 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 }
5336 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005337 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5339 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005340 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005342 }
5343
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 status = psa_mac_sign_finish(&hmac,
5345 tls12_prf->Ai, hash_length,
5346 &hmac_output_length);
5347 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005348 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 }
5350 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005351 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005353
5354 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 status = psa_key_derivation_start_hmac(&hmac,
5356 hash_alg,
5357 tls12_prf->secret,
5358 tls12_prf->secret_length);
5359 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005360 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 }
5362 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5363 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005364 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 }
5366 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5367 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005368 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 }
5370 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5371 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005372 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 }
5374 status = psa_mac_sign_finish(&hmac,
5375 tls12_prf->output_block, hash_length,
5376 &hmac_output_length);
5377 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005378 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005380
Janos Follath7742fee2019-06-17 12:58:10 +01005381
5382cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 cleanup_status = psa_mac_abort(&hmac);
5384 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005385 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005387
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005389}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005390
Janos Follath844eb0e2019-06-19 12:10:49 +01005391static psa_status_t psa_key_derivation_tls12_prf_read(
5392 psa_tls12_prf_key_derivation_t *tls12_prf,
5393 psa_algorithm_t alg,
5394 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005396{
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5398 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005399 psa_status_t status;
5400 uint8_t offset, length;
5401
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005403 case PSA_TLS12_PRF_STATE_LABEL_SET:
5404 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5405 break;
5406 case PSA_TLS12_PRF_STATE_OUTPUT:
5407 break;
5408 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005410 }
5411
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005413 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 if (tls12_prf->left_in_block == 0) {
5415 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5416 alg);
5417 if (status != PSA_SUCCESS) {
5418 return status;
5419 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005420
5421 continue;
5422 }
5423
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005425 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005427 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005429
5430 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005432 output += length;
5433 output_length -= length;
5434 tls12_prf->left_in_block -= length;
5435 }
5436
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005438}
John Durkop07cc04a2020-11-16 22:08:34 -08005439#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5440 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005441
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005442#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5443static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5444 psa_tls12_ecjpake_to_pms_t *ecjpake,
5445 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005447{
5448 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005449 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 if (output_length != 32) {
5452 return PSA_ERROR_INVALID_ARGUMENT;
5453 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005454
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5456 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5457 &output_size);
5458 if (status != PSA_SUCCESS) {
5459 return status;
5460 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005461
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 if (output_size != output_length) {
5463 return PSA_ERROR_GENERIC_ERROR;
5464 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005465
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005467}
5468#endif
5469
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305470#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305471static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5472 psa_pbkdf2_key_derivation_t *pbkdf2,
5473 psa_algorithm_t prf_alg,
5474 uint8_t prf_output_length,
5475 psa_key_attributes_t *attributes)
5476{
5477 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305478 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305479 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305480 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305481 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305482 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305483 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305484
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305485 mac_operation.is_sign = 1;
5486 mac_operation.mac_size = prf_output_length;
5487 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305488
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305489 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5490 attributes,
5491 pbkdf2->password,
5492 pbkdf2->password_length,
5493 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305494 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305495 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305496 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305497 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5498 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305499 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305500 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305501 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305502 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305503 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305504 }
5505 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5506 &mac_output_length);
5507 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305508 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305509 }
5510
5511 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305512 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305513 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305514 }
5515
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305516 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305517
5518 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305519 /* We are passing prf_output_length as mac_size because the driver
5520 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305521 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305522 status = psa_driver_wrapper_mac_compute(attributes,
5523 pbkdf2->password,
5524 pbkdf2->password_length,
5525 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305526 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305527 &mac_output_length);
5528 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305529 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305530 }
5531
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305532 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305533 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305534
5535cleanup:
5536 /* Zeroise buffers to clear sensitive data from memory. */
5537 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5538 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305539}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305540
5541static psa_status_t psa_key_derivation_pbkdf2_read(
5542 psa_pbkdf2_key_derivation_t *pbkdf2,
5543 psa_algorithm_t kdf_alg,
5544 uint8_t *output,
5545 size_t output_length)
5546{
5547 psa_status_t status;
5548 psa_algorithm_t prf_alg;
5549 uint8_t prf_output_length;
5550 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5551 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5552 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5553
5554 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5555 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5556 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5557 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305558 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5559 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305560 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305561 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305562 } else {
5563 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305564 }
5565
5566 switch (pbkdf2->state) {
5567 case PSA_PBKDF2_STATE_PASSWORD_SET:
5568 /* Initially we need a new block so bytes_used is equal to block size*/
5569 pbkdf2->bytes_used = prf_output_length;
5570 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5571 break;
5572 case PSA_PBKDF2_STATE_OUTPUT:
5573 break;
5574 default:
5575 return PSA_ERROR_BAD_STATE;
5576 }
5577
5578 while (output_length != 0) {
5579 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5580 if (n > output_length) {
5581 n = (uint8_t) output_length;
5582 }
5583 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5584 output += n;
5585 output_length -= n;
5586 pbkdf2->bytes_used += n;
5587
5588 if (output_length == 0) {
5589 break;
5590 }
5591
5592 /* We need a new block */
5593 pbkdf2->bytes_used = 0;
5594 pbkdf2->block_number++;
5595
5596 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5597 prf_output_length,
5598 &attributes);
5599 if (status != PSA_SUCCESS) {
5600 return status;
5601 }
5602 }
5603
5604 return PSA_SUCCESS;
5605}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305606#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305607
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005608psa_status_t psa_key_derivation_output_bytes(
5609 psa_key_derivation_operation_t *operation,
5610 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005612{
5613 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005614 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005615
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005617 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005619 }
5620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005622 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005623 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005624 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005625 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005626 goto exit;
5627 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005629 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005630 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005631 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5632 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005633 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005634 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005636 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005637 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005638
Przemek Stekiel69c46792022-06-10 12:59:51 +02005639#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5641 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5642 output, output_length);
5643 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005644#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005645#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5646 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5648 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5649 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5650 kdf_alg, output,
5651 output_length);
5652 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005653#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5654 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005655#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005657 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005658 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5659 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005660#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305661#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305662 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305663 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5664 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305665 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305666#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005667
Gilles Peskineeab56e42018-07-12 17:12:33 +02005668 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005669 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005671 }
5672
5673exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005675 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005676 * This allows us to differentiate between exhausted operations and
5677 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5678 * operations. */
5679 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005681 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005683 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005685}
5686
David Brown7807bf72021-02-09 16:28:23 -07005687#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005688static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005689{
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 if (data_size >= 8) {
5691 mbedtls_des_key_set_parity(data);
5692 }
5693 if (data_size >= 16) {
5694 mbedtls_des_key_set_parity(data + 8);
5695 }
5696 if (data_size >= 24) {
5697 mbedtls_des_key_set_parity(data + 16);
5698 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005699}
David Brown7807bf72021-02-09 16:28:23 -07005700#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005701
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005702/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 * ECC keys on a Weierstrass elliptic curve require the generation
5704 * of a private key which is an integer
5705 * in the range [1, N - 1], where N is the boundary of the private key domain:
5706 * N is the prime p for Diffie-Hellman, or the order of the
5707 * curve’s base point for ECC.
5708 *
5709 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5710 * This function generates the private key using the following process:
5711 *
5712 * 1. Draw a byte string of length ceiling(m/8) bytes.
5713 * 2. If m is not a multiple of 8, set the most significant
5714 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5715 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5716 * 4. If k > N - 2, discard the result and return to step 1.
5717 * 5. Output k + 1 as the private key.
5718 *
5719 * This method allows compliance to NIST standards, specifically the methods titled
5720 * Key-Pair Generation by Testing Candidates in the following publications:
5721 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5722 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5723 * Diffie-Hellman keys.
5724 *
5725 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5726 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5727 *
5728 * Note: Function allocates memory for *data buffer, so given *data should be
5729 * always NULL.
5730 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005731#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5732#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005733static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005734 psa_key_slot_t *slot,
5735 size_t bits,
5736 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005737 uint8_t **data
5738 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005739{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005740 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005741 mbedtls_mpi k;
5742 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005743 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005744 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005745 size_t m;
5746 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005747
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 mbedtls_mpi_init(&k);
5749 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005750
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005751 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005753 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01005754 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005757 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005758 goto cleanup;
5759 }
5760
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005761 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005765
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005766 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005767 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005768 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005769
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005770 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005771
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005772 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005774
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005775 /* Note: This function is always called with *data == NULL and it
5776 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 *data = mbedtls_calloc(1, m_bytes);
5778 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005779 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005780 goto cleanup;
5781 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005782
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005784 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005786 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005788
5789 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005791 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005792 * (8 * ceiling(m/8) - m) bits of the first byte in
5793 * the string to zero.
5794 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005796 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005797 }
5798
5799 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 * big-endian byte string.
5801 */
5802 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005803
5804 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 * Result of comparison is returned. When it indicates error
5806 * then this function is called again.
5807 */
5808 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005809 }
5810
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005811 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5813 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005814cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 if (ret != 0) {
5816 status = mbedtls_to_psa_error(ret);
5817 }
5818 if (status != PSA_SUCCESS) {
5819 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005820 *data = NULL;
5821 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 mbedtls_mpi_free(&k);
5823 mbedtls_mpi_free(&diff_N_2);
5824 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005825}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005826
5827/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5828 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5829 *
5830 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5831 * draw a 32-byte string and process it as specified in
5832 * Elliptic Curves for Security [RFC7748] §5.
5833 *
5834 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5835 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5836 *
5837 * Note: Function allocates memory for *data buffer, so given *data should be
5838 * always NULL.
5839 */
5840
5841static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5842 size_t bits,
5843 psa_key_derivation_operation_t *operation,
5844 uint8_t **data
5845 )
5846{
5847 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005848 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005849
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005851 case 255:
5852 output_length = 32;
5853 break;
5854 case 448:
5855 output_length = 56;
5856 break;
5857 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005859 break;
5860 }
5861
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 if (*data == NULL) {
5865 return PSA_ERROR_INSUFFICIENT_MEMORY;
5866 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005867
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005869
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005871 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005873
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005875 case 255:
5876 (*data)[0] &= 248;
5877 (*data)[31] &= 127;
5878 (*data)[31] |= 64;
5879 break;
5880 case 448:
5881 (*data)[0] &= 252;
5882 (*data)[55] |= 128;
5883 break;
5884 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005886 break;
5887 }
5888
5889 return status;
5890}
Valerio Setti86587ab2023-06-27 13:09:30 +02005891#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5892static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5893 psa_key_slot_t *slot, size_t bits,
5894 psa_key_derivation_operation_t *operation, uint8_t **data)
5895{
5896 (void) slot;
5897 (void) bits;
5898 (void) operation;
5899 (void) data;
5900 return PSA_ERROR_NOT_SUPPORTED;
5901}
5902
5903static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5904 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5905{
5906 (void) bits;
5907 (void) operation;
5908 (void) data;
5909 return PSA_ERROR_NOT_SUPPORTED;
5910}
5911#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5912#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005913
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005914static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005915 psa_key_slot_t *slot,
5916 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005918{
5919 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305921 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005922 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005923 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005924
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5926 return PSA_ERROR_INVALID_ARGUMENT;
5927 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005928
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005929#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005930 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5932 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5933 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005934 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5936 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005937 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 }
5939 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005940 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5942 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005943 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005945 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005946 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005947#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005948 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 if (key_type_is_raw_bytes(slot->attr.type)) {
5950 if (bits % 8 != 0) {
5951 return PSA_ERROR_INVALID_ARGUMENT;
5952 }
5953 data = mbedtls_calloc(1, bytes);
5954 if (data == NULL) {
5955 return PSA_ERROR_INSUFFICIENT_MEMORY;
5956 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 status = psa_key_derivation_output_bytes(operation, data, bytes);
5959 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005960 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 }
David Brown12ca5032021-02-11 11:02:00 -07005962#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5964 psa_des_set_key_parity(data, bytes);
5965 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005966#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 } else {
5968 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005969 }
Ronald Crondd04d422020-11-28 15:14:42 +01005970
Ronald Cronbf33c932020-11-28 18:06:53 +01005971 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005972 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005974 };
5975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5977 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5978 &storage_size);
5979 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305980 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 }
Archanad8a83dc2021-06-14 10:04:16 +05305982 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 status = psa_allocate_buffer_to_slot(slot, storage_size);
5984 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305985 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 }
Archanad8a83dc2021-06-14 10:04:16 +05305987
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 status = psa_driver_wrapper_import_key(&attributes,
5989 data, bytes,
5990 slot->key.data,
5991 slot->key.bytes,
5992 &slot->key.bytes, &bits);
5993 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005994 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005996
5997exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 mbedtls_free(data);
5999 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006000}
6001
Gilles Peskine449bd832023-01-11 14:50:10 +01006002psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6003 psa_key_derivation_operation_t *operation,
6004 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006005{
6006 psa_status_t status;
6007 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006008 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006009
Ronald Cron81709fc2020-11-14 12:10:32 +01006010 *key = MBEDTLS_SVC_KEY_ID_INIT;
6011
Gilles Peskine0f84d622019-09-12 19:03:13 +02006012 /* Reject any attempt to create a zero-length key so that we don't
6013 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006014 if (psa_get_key_bits(attributes) == 0) {
6015 return PSA_ERROR_INVALID_ARGUMENT;
6016 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006017
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 if (operation->alg == PSA_ALG_NONE) {
6019 return PSA_ERROR_BAD_STATE;
6020 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 if (!operation->can_output_key) {
6023 return PSA_ERROR_NOT_PERMITTED;
6024 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006025
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6027 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006028#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006030 /* Deriving a key in a secure element is not implemented yet. */
6031 status = PSA_ERROR_NOT_SUPPORTED;
6032 }
6033#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 if (status == PSA_SUCCESS) {
6035 status = psa_generate_derived_key_internal(slot,
6036 attributes->core.bits,
6037 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006038 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 if (status == PSA_SUCCESS) {
6040 status = psa_finish_key_creation(slot, driver, key);
6041 }
6042 if (status != PSA_SUCCESS) {
6043 psa_fail_key_creation(slot, driver);
6044 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006045
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006047}
6048
Gilles Peskineeab56e42018-07-12 17:12:33 +02006049
6050
6051/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006052/* Key derivation */
6053/****************************************************************/
6054
Steven Cooreman932ffb72021-02-15 12:14:32 +01006055#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006056static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006057{
6058#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6060 return 1;
6061 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006062#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006063#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6065 return 1;
6066 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006067#endif
6068#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6070 return 1;
6071 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006072#endif
6073#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6075 return 1;
6076 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006077#endif
6078#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6080 return 1;
6081 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006082#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006083#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6085 return 1;
6086 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006087#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306088#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6089 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6090 return 1;
6091 }
6092#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306093#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6094 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6095 return 1;
6096 }
6097#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006099}
6100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006102{
6103 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 psa_status_t status = psa_hash_setup(&operation, alg);
6105 psa_hash_abort(&operation);
6106 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006107}
6108
Gilles Peskine969c5d62019-01-16 15:53:06 +01006109static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006110 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006112{
Janos Follath5fe19732019-06-20 15:09:30 +01006113 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6114 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006116
Gilles Peskine969c5d62019-01-16 15:53:06 +01006117 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 if (!is_kdf_alg_supported(kdf_alg)) {
6119 return PSA_ERROR_NOT_SUPPORTED;
6120 }
John Durkop07cc04a2020-11-16 22:08:34 -08006121
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006122 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306123 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6125 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306126 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6127 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6128 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306129 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306130 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 if (hash_size == 0) {
6132 return PSA_ERROR_NOT_SUPPORTED;
6133 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006134
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006135 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6136 * we might fail later, which is somewhat unfriendly and potentially
6137 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 psa_status_t status = psa_hash_try_support(hash_alg);
6139 if (status != PSA_SUCCESS) {
6140 return status;
6141 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006142 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006143
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6145 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6146 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6147 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006148 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006149#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006150 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6152 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006153 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006155#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6156 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 operation->capacity = 255 * hash_size;
6158 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006159}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006160
Gilles Peskine449bd832023-01-11 14:50:10 +01006161static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006162{
6163#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 if (alg == PSA_ALG_ECDH) {
6165 return PSA_SUCCESS;
6166 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006167#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006168#if defined(PSA_WANT_ALG_FFDH)
6169 if (alg == PSA_ALG_FFDH) {
6170 return PSA_SUCCESS;
6171 }
6172#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006173 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006175}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006176
6177static int psa_key_derivation_allows_free_form_secret_input(
6178 psa_algorithm_t kdf_alg)
6179{
6180#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6181 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6182 return 0;
6183 }
6184#endif
6185 (void) kdf_alg;
6186 return 1;
6187}
John Durkop07cc04a2020-11-16 22:08:34 -08006188#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006189
Gilles Peskine449bd832023-01-11 14:50:10 +01006190psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6191 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006192{
6193 psa_status_t status;
6194
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 if (operation->alg != 0) {
6196 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006197 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006198
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6200 return PSA_ERROR_INVALID_ARGUMENT;
6201 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6202#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6203 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6204 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6205 status = psa_key_agreement_try_support(ka_alg);
6206 if (status != PSA_SUCCESS) {
6207 return status;
6208 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006209 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6210 return PSA_ERROR_INVALID_ARGUMENT;
6211 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6213#else
6214 return PSA_ERROR_NOT_SUPPORTED;
6215#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6216 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6217#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6218 status = psa_key_derivation_setup_kdf(operation, alg);
6219#else
6220 return PSA_ERROR_NOT_SUPPORTED;
6221#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6222 } else {
6223 return PSA_ERROR_INVALID_ARGUMENT;
6224 }
6225
6226 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006227 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 }
6229 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006230}
6231
Przemek Stekiel69c46792022-06-10 12:59:51 +02006232#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006233static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6234 psa_algorithm_t kdf_alg,
6235 psa_key_derivation_step_t step,
6236 const uint8_t *data,
6237 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006238{
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006240 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006242 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006243#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6245 return PSA_ERROR_INVALID_ARGUMENT;
6246 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006247#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 if (hkdf->state != HKDF_STATE_INIT) {
6249 return PSA_ERROR_BAD_STATE;
6250 } else {
6251 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6252 hash_alg,
6253 data, data_length);
6254 if (status != PSA_SUCCESS) {
6255 return status;
6256 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006257 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006259 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006260 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006261#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006263 /* We shouldn't be in different state as HKDF_EXPAND only allows
6264 * two inputs: SECRET (this case) and INFO which does not modify
6265 * the state. It could happen only if the hkdf
6266 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 if (hkdf->state != HKDF_STATE_INIT) {
6268 return PSA_ERROR_BAD_STATE;
6269 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006270
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006271 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6273 return PSA_ERROR_INVALID_ARGUMENT;
6274 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 memcpy(hkdf->prk, data, data_length);
6277 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006278#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006279 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006280 /* HKDF: If no salt was provided, use an empty salt.
6281 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006283#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6285 return PSA_ERROR_BAD_STATE;
6286 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006287#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6289 hash_alg,
6290 NULL, 0);
6291 if (status != PSA_SUCCESS) {
6292 return status;
6293 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006294 hkdf->state = HKDF_STATE_STARTED;
6295 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 if (hkdf->state != HKDF_STATE_STARTED) {
6297 return PSA_ERROR_BAD_STATE;
6298 }
6299 status = psa_mac_update(&hkdf->hmac,
6300 data, data_length);
6301 if (status != PSA_SUCCESS) {
6302 return status;
6303 }
6304 status = psa_mac_sign_finish(&hkdf->hmac,
6305 hkdf->prk,
6306 sizeof(hkdf->prk),
6307 &data_length);
6308 if (status != PSA_SUCCESS) {
6309 return status;
6310 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006311 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006312
Gilles Peskine2b522db2019-04-12 15:11:49 +02006313 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006314 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006315#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006317 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006319 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006321#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006322 {
6323 /* Block 0 is empty, and the next block will be
6324 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006326 }
6327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006329 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006330#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6332 return PSA_ERROR_INVALID_ARGUMENT;
6333 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006334#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006335#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6337 hkdf->state == HKDF_STATE_INIT) {
6338 return PSA_ERROR_BAD_STATE;
6339 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006340#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 if (hkdf->state == HKDF_STATE_OUTPUT) {
6342 return PSA_ERROR_BAD_STATE;
6343 }
6344 if (hkdf->info_set) {
6345 return PSA_ERROR_BAD_STATE;
6346 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006347 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 if (data_length != 0) {
6349 hkdf->info = mbedtls_calloc(1, data_length);
6350 if (hkdf->info == NULL) {
6351 return PSA_ERROR_INSUFFICIENT_MEMORY;
6352 }
6353 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006354 }
6355 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006357 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006359 }
6360}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006361#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006362
John Durkop07cc04a2020-11-16 22:08:34 -08006363#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6364 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006365static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6366 const uint8_t *data,
6367 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006368{
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6370 return PSA_ERROR_BAD_STATE;
6371 }
Janos Follathf08e2652019-06-13 09:05:41 +01006372
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 if (data_length != 0) {
6374 prf->seed = mbedtls_calloc(1, data_length);
6375 if (prf->seed == NULL) {
6376 return PSA_ERROR_INSUFFICIENT_MEMORY;
6377 }
Janos Follathf08e2652019-06-13 09:05:41 +01006378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006380 prf->seed_length = data_length;
6381 }
Janos Follathf08e2652019-06-13 09:05:41 +01006382
Gilles Peskine43f958b2020-12-13 14:55:14 +01006383 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006386}
6387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6389 const uint8_t *data,
6390 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006391{
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6393 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6394 return PSA_ERROR_BAD_STATE;
6395 }
Janos Follath81550542019-06-13 14:26:34 +01006396
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 if (data_length != 0) {
6398 prf->secret = mbedtls_calloc(1, data_length);
6399 if (prf->secret == NULL) {
6400 return PSA_ERROR_INSUFFICIENT_MEMORY;
6401 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006402
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006404 prf->secret_length = data_length;
6405 }
Janos Follath81550542019-06-13 14:26:34 +01006406
Gilles Peskine43f958b2020-12-13 14:55:14 +01006407 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006408
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006410}
6411
Gilles Peskine449bd832023-01-11 14:50:10 +01006412static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6413 const uint8_t *data,
6414 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006415{
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6417 return PSA_ERROR_BAD_STATE;
6418 }
Janos Follath63028dd2019-06-13 09:15:47 +01006419
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 if (data_length != 0) {
6421 prf->label = mbedtls_calloc(1, data_length);
6422 if (prf->label == NULL) {
6423 return PSA_ERROR_INSUFFICIENT_MEMORY;
6424 }
Janos Follath63028dd2019-06-13 09:15:47 +01006425
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006427 prf->label_length = data_length;
6428 }
Janos Follath63028dd2019-06-13 09:15:47 +01006429
Gilles Peskine43f958b2020-12-13 14:55:14 +01006430 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006431
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006433}
6434
Gilles Peskine449bd832023-01-11 14:50:10 +01006435static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6436 psa_key_derivation_step_t step,
6437 const uint8_t *data,
6438 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006439{
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006441 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006443 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006445 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006447 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006449 }
6450}
John Durkop07cc04a2020-11-16 22:08:34 -08006451#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6452 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6453
6454#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6455static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6456 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006457 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006459{
6460 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6462 4 + data_length + prf->other_secret_length :
6463 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006464
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6466 return PSA_ERROR_INVALID_ARGUMENT;
6467 }
John Durkop07cc04a2020-11-16 22:08:34 -08006468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 uint8_t *pms = mbedtls_calloc(1, pms_len);
6470 if (pms == NULL) {
6471 return PSA_ERROR_INSUFFICIENT_MEMORY;
6472 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006473 uint8_t *cur = pms;
6474
6475 /* pure-PSK:
6476 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006477 *
6478 * The premaster secret is formed as follows: if the PSK is N octets
6479 * long, concatenate a uint16 with the value N, N zero octets, a second
6480 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006481 *
6482 * mixed-PSK:
6483 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6484 * follows: concatenate a uint16 with the length of the other secret,
6485 * the other secret itself, uint16 with the length of PSK, and the
6486 * PSK itself.
6487 * For details please check:
6488 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6489 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6490 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006491 */
6492
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6494 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6495 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6496 if (prf->other_secret_length != 0) {
6497 memcpy(cur, prf->other_secret, prf->other_secret_length);
6498 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006499 cur += prf->other_secret_length;
6500 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 } else {
6502 *cur++ = MBEDTLS_BYTE_1(data_length);
6503 *cur++ = MBEDTLS_BYTE_0(data_length);
6504 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006505 cur += data_length;
6506 }
6507
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 *cur++ = MBEDTLS_BYTE_1(data_length);
6509 *cur++ = MBEDTLS_BYTE_0(data_length);
6510 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006511 cur += data_length;
6512
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006513 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006514
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006515 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006517}
Janos Follath6660f0e2019-06-17 08:44:03 +01006518
Przemek Stekiele47201b2022-04-19 13:53:28 +02006519static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6520 psa_tls12_prf_key_derivation_t *prf,
6521 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006523{
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6525 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006526 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006527
6528 if (data_length != 0) {
6529 prf->other_secret = mbedtls_calloc(1, data_length);
6530 if (prf->other_secret == NULL) {
6531 return PSA_ERROR_INSUFFICIENT_MEMORY;
6532 }
6533
6534 memcpy(prf->other_secret, data, data_length);
6535 prf->other_secret_length = data_length;
6536 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006537 prf->other_secret_length = 0;
6538 }
6539
6540 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6541
Gilles Peskine449bd832023-01-11 14:50:10 +01006542 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006543}
6544
Janos Follath6660f0e2019-06-17 08:44:03 +01006545static psa_status_t psa_tls12_prf_psk_to_ms_input(
6546 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006547 psa_key_derivation_step_t step,
6548 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006550{
Gilles Peskine449bd832023-01-11 14:50:10 +01006551 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006552 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 return psa_tls12_prf_psk_to_ms_set_key(prf,
6554 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006555 break;
6556 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6558 data,
6559 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006560 break;
6561 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006563 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006564
Przemek Stekiele47201b2022-04-19 13:53:28 +02006565 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006566}
John Durkop07cc04a2020-11-16 22:08:34 -08006567#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006568
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006569#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6570static psa_status_t psa_tls12_ecjpake_to_pms_input(
6571 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006572 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006573 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006575{
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6577 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6578 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006579 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006580
6581 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 if (data[0] != 0x04) {
6583 return PSA_ERROR_INVALID_ARGUMENT;
6584 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006585
6586 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006588
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006590}
6591#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306592
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306593#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306594static psa_status_t psa_pbkdf2_set_input_cost(
6595 psa_pbkdf2_key_derivation_t *pbkdf2,
6596 psa_key_derivation_step_t step,
6597 uint64_t data)
6598{
6599 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6600 return PSA_ERROR_INVALID_ARGUMENT;
6601 }
6602
6603 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6604 return PSA_ERROR_BAD_STATE;
6605 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306606
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306607 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306608 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306609 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306610
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306611 if (data == 0) {
6612 return PSA_ERROR_INVALID_ARGUMENT;
6613 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306614
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306615 pbkdf2->input_cost = data;
6616 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6617
6618 return PSA_SUCCESS;
6619}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306620
6621static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6622 const uint8_t *data,
6623 size_t data_length)
6624{
Gilles Peskineead17662023-08-20 22:02:51 +02006625 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6626 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6627 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6628 /* Appending to existing salt. No state change. */
6629 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306630 return PSA_ERROR_BAD_STATE;
6631 }
6632
Gilles Peskineca57d782023-07-20 19:08:06 +02006633 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006634 /* Appending an empty string, nothing to do. */
6635 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306636 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306637
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306638 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6639 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306640 return PSA_ERROR_INSUFFICIENT_MEMORY;
6641 }
6642
Gilles Peskineead17662023-08-20 22:02:51 +02006643 if (pbkdf2->salt_length != 0) {
6644 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6645 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306646 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306647 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306648 mbedtls_free(pbkdf2->salt);
6649 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306650 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306651 return PSA_SUCCESS;
6652}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306653
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306654#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306655static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306656 const uint8_t *input,
6657 size_t input_len,
6658 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306659 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306660{
6661 psa_status_t status = PSA_SUCCESS;
6662 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6663 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306664 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306665 } else {
6666 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306667 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306668 }
6669 return status;
6670}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306671#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306672
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306673#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306674static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6675 size_t input_len,
6676 uint8_t *output,
6677 size_t *output_len)
6678{
6679 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306680 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306682 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306683 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6684 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6685 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306686 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306687 * mac_size as the driver function sets mac_output_length = mac_size
6688 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306689 status = psa_driver_wrapper_mac_compute(&attributes,
6690 zeros, sizeof(zeros),
6691 PSA_ALG_CMAC, input, input_len,
6692 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306693 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6694 128U,
6695 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306696 output_len);
6697 } else {
6698 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306699 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306700 }
6701 return status;
6702}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306703#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306704
6705static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306706 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306707 const uint8_t *data,
6708 size_t data_length)
6709{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306710 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306711 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6712 return PSA_ERROR_BAD_STATE;
6713 }
6714
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306715#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306716 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6717 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6718 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6719 pbkdf2->password,
6720 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306721 } else
6722#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6723#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6724 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306725 status = psa_pbkdf2_cmac_set_password(data, data_length,
6726 pbkdf2->password,
6727 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306728 } else
6729#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6730 {
6731 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306732 }
6733
6734 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6735
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306736 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306737}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306738
6739static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306740 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306741 psa_key_derivation_step_t step,
6742 const uint8_t *data,
6743 size_t data_length)
6744{
6745 switch (step) {
6746 case PSA_KEY_DERIVATION_INPUT_SALT:
6747 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6748 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306749 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306750 default:
6751 return PSA_ERROR_INVALID_ARGUMENT;
6752 }
6753}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306754#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306755
Gilles Peskineb8965192019-09-24 16:21:10 +02006756/** Check whether the given key type is acceptable for the given
6757 * input step of a key derivation.
6758 *
6759 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6760 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6761 * Both secret and non-secret inputs can alternatively have the type
6762 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6763 * that the input was passed as a buffer rather than via a key object.
6764 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006765static int psa_key_derivation_check_input_type(
6766 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006768{
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006770 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006771 if (key_type == PSA_KEY_TYPE_DERIVE) {
6772 return PSA_SUCCESS;
6773 }
6774 if (key_type == PSA_KEY_TYPE_NONE) {
6775 return PSA_SUCCESS;
6776 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006777 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006778 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 if (key_type == PSA_KEY_TYPE_DERIVE) {
6780 return PSA_SUCCESS;
6781 }
6782 if (key_type == PSA_KEY_TYPE_NONE) {
6783 return PSA_SUCCESS;
6784 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006785 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006786 case PSA_KEY_DERIVATION_INPUT_LABEL:
6787 case PSA_KEY_DERIVATION_INPUT_SALT:
6788 case PSA_KEY_DERIVATION_INPUT_INFO:
6789 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6791 return PSA_SUCCESS;
6792 }
6793 if (key_type == PSA_KEY_TYPE_NONE) {
6794 return PSA_SUCCESS;
6795 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006796 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306797 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6798 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6799 return PSA_SUCCESS;
6800 }
6801 if (key_type == PSA_KEY_TYPE_DERIVE) {
6802 return PSA_SUCCESS;
6803 }
6804 if (key_type == PSA_KEY_TYPE_NONE) {
6805 return PSA_SUCCESS;
6806 }
6807 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006808 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006809 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006810}
6811
Janos Follathb80a94e2019-06-12 15:54:46 +01006812static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006813 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006814 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006815 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006816 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006817 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006818{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006819 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006821
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 status = psa_key_derivation_check_input_type(step, key_type);
6823 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006824 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006826
Przemek Stekiel69c46792022-06-10 12:59:51 +02006827#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006828 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6829 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6830 step, data, data_length);
6831 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006832#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006833#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6835 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6836 step, data, data_length);
6837 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006838#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6839#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6841 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6842 step, data, data_length);
6843 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006844#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006845#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006847 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6849 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006850#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306851#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306852 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306853 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6854 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306855 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306856#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006857 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006858 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006859 (void) data;
6860 (void) data_length;
6861 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006863 }
6864
Gilles Peskine224b0d62019-09-23 18:13:17 +02006865exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 if (status != PSA_SUCCESS) {
6867 psa_key_derivation_abort(operation);
6868 }
6869 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006870}
6871
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306872static psa_status_t psa_key_derivation_input_integer_internal(
6873 psa_key_derivation_operation_t *operation,
6874 psa_key_derivation_step_t step,
6875 uint64_t value)
6876{
6877 psa_status_t status;
6878 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6879
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306880#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306881 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306882 status = psa_pbkdf2_set_input_cost(
6883 &operation->ctx.pbkdf2, step, value);
6884 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306885#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306886 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306887 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306888 (void) value;
6889 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306890 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306891 }
6892
6893 if (status != PSA_SUCCESS) {
6894 psa_key_derivation_abort(operation);
6895 }
6896 return status;
6897}
6898
Janos Follath51f4a0f2019-06-14 11:35:55 +01006899psa_status_t psa_key_derivation_input_bytes(
6900 psa_key_derivation_operation_t *operation,
6901 psa_key_derivation_step_t step,
6902 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006904{
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 return psa_key_derivation_input_internal(operation, step,
6906 PSA_KEY_TYPE_NONE,
6907 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006908}
6909
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306910psa_status_t psa_key_derivation_input_integer(
6911 psa_key_derivation_operation_t *operation,
6912 psa_key_derivation_step_t step,
6913 uint64_t value)
6914{
6915 return psa_key_derivation_input_integer_internal(operation, step, value);
6916}
6917
Janos Follath51f4a0f2019-06-14 11:35:55 +01006918psa_status_t psa_key_derivation_input_key(
6919 psa_key_derivation_operation_t *operation,
6920 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006922{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006923 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006924 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006925 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006926
Ronald Cron5c522922020-11-14 16:35:34 +01006927 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6929 if (status != PSA_SUCCESS) {
6930 psa_key_derivation_abort(operation);
6931 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006932 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006933
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306934 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6935 * permission to output to a key object. */
6936 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6937 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006938 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 status = psa_key_derivation_input_internal(operation,
6942 step, slot->attr.type,
6943 slot->key.data,
6944 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006945
Ryan Everett1b70a072024-01-04 10:32:49 +00006946 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006947
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006949}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006950
6951
6952
6953/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006954/* Key agreement */
6955/****************************************************************/
6956
Gilles Peskine449bd832023-01-11 14:50:10 +01006957psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6958 const uint8_t *key_buffer,
6959 size_t key_buffer_size,
6960 psa_algorithm_t alg,
6961 const uint8_t *peer_key,
6962 size_t peer_key_length,
6963 uint8_t *shared_secret,
6964 size_t shared_secret_size,
6965 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006966{
Gilles Peskine449bd832023-01-11 14:50:10 +01006967 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006968#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006969 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6971 key_buffer_size, alg,
6972 peer_key, peer_key_length,
6973 shared_secret,
6974 shared_secret_size,
6975 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006976#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006977
6978#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6979 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006980 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006981 peer_key,
6982 peer_key_length,
6983 key_buffer,
6984 key_buffer_size,
6985 shared_secret,
6986 shared_secret_size,
6987 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006988#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6989
Gilles Peskine01d718c2018-09-18 12:01:02 +02006990 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006991 (void) attributes;
6992 (void) key_buffer;
6993 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006994 (void) peer_key;
6995 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006996 (void) shared_secret;
6997 (void) shared_secret_size;
6998 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006999 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007000 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007001}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007002
Aditya Deshpande17845b82022-10-13 17:21:01 +01007003/** Internal function for raw key agreement
7004 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007005 * to the driver's implementation if a driver is present.
7006 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007007 * (psa_key_agreement_raw_builtin).
7008 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007009static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7010 psa_key_slot_t *private_key,
7011 const uint8_t *peer_key,
7012 size_t peer_key_length,
7013 uint8_t *shared_secret,
7014 size_t shared_secret_size,
7015 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007016{
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7018 return PSA_ERROR_NOT_SUPPORTED;
7019 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007020
7021 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007022 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007023 };
7024
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 return psa_driver_wrapper_key_agreement(&attributes,
7026 private_key->key.data,
7027 private_key->key.bytes, alg,
7028 peer_key, peer_key_length,
7029 shared_secret,
7030 shared_secret_size,
7031 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007032}
7033
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007034/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007035 * to potentially free embedded data structures and wipe confidential data.
7036 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007037static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7038 psa_key_derivation_step_t step,
7039 psa_key_slot_t *private_key,
7040 const uint8_t *peer_key,
7041 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007042{
7043 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007044 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007045 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007046 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007047
7048 /* Step 1: run the secret agreement algorithm to generate the shared
7049 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 status = psa_key_agreement_raw_internal(ka_alg,
7051 private_key,
7052 peer_key, peer_key_length,
7053 shared_secret,
7054 sizeof(shared_secret),
7055 &shared_secret_length);
7056 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007057 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007059
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007060 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007061 * the shared secret. A shared secret is permitted wherever a key
7062 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 status = psa_key_derivation_input_internal(operation, step,
7064 PSA_KEY_TYPE_DERIVE,
7065 shared_secret,
7066 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007067exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007068 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7069 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007070}
7071
Gilles Peskine449bd832023-01-11 14:50:10 +01007072psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7073 psa_key_derivation_step_t step,
7074 mbedtls_svc_key_id_t private_key,
7075 const uint8_t *peer_key,
7076 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007077{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007078 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007079 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007080 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007081
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7083 return PSA_ERROR_INVALID_ARGUMENT;
7084 }
Ronald Cron5c522922020-11-14 16:35:34 +01007085 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7087 if (status != PSA_SUCCESS) {
7088 return status;
7089 }
7090 status = psa_key_agreement_internal(operation, step,
7091 slot,
7092 peer_key, peer_key_length);
7093 if (status != PSA_SUCCESS) {
7094 psa_key_derivation_abort(operation);
7095 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007096 /* If a private key has been added as SECRET, we allow the derived
7097 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007099 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007101 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007102
Ryan Everett1b70a072024-01-04 10:32:49 +00007103 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007104
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007106}
7107
Gilles Peskine449bd832023-01-11 14:50:10 +01007108psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7109 mbedtls_svc_key_id_t private_key,
7110 const uint8_t *peer_key,
7111 size_t peer_key_length,
7112 uint8_t *output,
7113 size_t output_size,
7114 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007115{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007116 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007117 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007118 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007119 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007120
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007122 status = PSA_ERROR_INVALID_ARGUMENT;
7123 goto exit;
7124 }
Ronald Cron5c522922020-11-14 16:35:34 +01007125 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007126 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7127 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007128 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007129 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007130
Gilles Peskine3e561302022-04-14 00:17:15 +02007131 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7132 * for the output size. The PSA specification only guarantees that this
7133 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7134 * but it might be nice to allow smaller buffers if the output fits.
7135 * At the time of writing this comment, with only ECDH implemented,
7136 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7137 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7138 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007139 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7141 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007142 status = PSA_ERROR_BUFFER_TOO_SMALL;
7143 goto exit;
7144 }
7145
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 status = psa_key_agreement_raw_internal(alg, slot,
7147 peer_key, peer_key_length,
7148 output, output_size,
7149 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007150
7151exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007153 /* If an error happens and is not handled properly, the output
7154 * may be used as a key to protect sensitive data. Arrange for such
7155 * a key to be random, which is likely to result in decryption or
7156 * verification errors. This is better than filling the buffer with
7157 * some constant data such as zeros, which would result in the data
7158 * being protected with a reproducible, easily knowable key.
7159 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007160 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007161 *output_length = output_size;
7162 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007163
Ryan Everett1b70a072024-01-04 10:32:49 +00007164 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007165
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007167}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007168
7169
Gilles Peskine30524eb2020-11-13 17:02:26 +01007170
Gilles Peskine86a440b2018-11-12 18:39:40 +01007171/****************************************************************/
7172/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007173/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007174
Gilles Peskine672a7712023-04-28 21:00:28 +02007175#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7176#include "entropy_poll.h"
7177#endif
7178
Gilles Peskine30524eb2020-11-13 17:02:26 +01007179/** Initialize the PSA random generator.
7180 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007181static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007182{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007183#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007185#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7186
Gilles Peskine30524eb2020-11-13 17:02:26 +01007187 /* Set default configuration if
7188 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007190 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 }
7192 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007193 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007195
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007197#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7198 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7199 /* The PSA entropy injection feature depends on using NV seed as an entropy
7200 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 mbedtls_entropy_add_source(&rng->entropy,
7202 mbedtls_nv_seed_poll, NULL,
7203 MBEDTLS_ENTROPY_BLOCK_SIZE,
7204 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007205#endif
7206
Gilles Peskine449bd832023-01-11 14:50:10 +01007207 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007208#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007209}
7210
7211/** Deinitialize the PSA random generator.
7212 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007213static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007214{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007215#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007216 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007217#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti83e0de82023-11-24 12:13:05 +01007218 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7219 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007220#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007221}
7222
7223/** Seed the PSA random generator.
7224 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007225static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007226{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007227#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7228 /* Do nothing: the external RNG seeds itself. */
7229 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007231#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007232 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007233 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7234 drbg_seed, sizeof(drbg_seed) - 1);
7235 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007236#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007237}
7238
Gilles Peskine449bd832023-01-11 14:50:10 +01007239psa_status_t psa_generate_random(uint8_t *output,
7240 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007241{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007242 GUARD_MODULE_INITIALIZED;
7243
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007244#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7245
7246 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007247 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7248 output, output_size,
7249 &output_length);
7250 if (status != PSA_SUCCESS) {
7251 return status;
7252 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007253 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007254 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 if (output_length != output_size) {
7256 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7257 }
7258 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007259
7260#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7261
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007263 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7265 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7266 output_size);
7267 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7268 output, request_size);
7269 if (ret != 0) {
7270 return mbedtls_to_psa_error(ret);
7271 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007272 output_size -= request_size;
7273 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007274 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007276#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007277}
7278
Gilles Peskine8814fc42020-12-14 15:33:44 +01007279/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007280 *
7281 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7282 * `psa_generate_random(...)`. The state parameter is ignored since the
7283 * PSA API doesn't support passing an explicit state.
7284 *
7285 * In the non-external case, psa_generate_random() calls an
7286 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7287 * and semantics as mbedtls_psa_get_random(). As an optimization,
7288 * instead of doing this back-and-forth between the PSA API and the
7289 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7290 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007292#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7293int mbedtls_psa_get_random(void *p_rng,
7294 unsigned char *output,
7295 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007296{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007297 /* This function takes a pointer to the RNG state because that's what
7298 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7299 * its own state internally and doesn't let the caller access that state.
7300 * So we just ignore the state parameter, and in practice we'll pass
7301 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007302 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007303 psa_status_t status = psa_generate_random(output, output_size);
7304 if (status == PSA_SUCCESS) {
7305 return 0;
7306 } else {
7307 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7308 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007309}
7310#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7311
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007312#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007313psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7314 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007315{
Gilles Peskine449bd832023-01-11 14:50:10 +01007316 if (global_data.initialized) {
7317 return PSA_ERROR_NOT_PERMITTED;
7318 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007319
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7321 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7322 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7323 return PSA_ERROR_INVALID_ARGUMENT;
7324 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007325
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007327}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007328#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007329
Ronald Cron3772afe2021-02-08 16:10:05 +01007330/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007331 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007332 * \param type The key type
7333 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007334 *
7335 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007336 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007337 * \retval #PSA_ERROR_INVALID_ARGUMENT
7338 * The size in bits of the key is not valid.
7339 * \retval #PSA_ERROR_NOT_SUPPORTED
7340 * The type and/or the size in bits of the key or the combination of
7341 * the two is not supported.
7342 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007343static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007345{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007346 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007347
Gilles Peskine449bd832023-01-11 14:50:10 +01007348 if (key_type_is_raw_bytes(type)) {
7349 status = psa_validate_unstructured_key_bit_size(type, bits);
7350 if (status != PSA_SUCCESS) {
7351 return status;
7352 }
7353 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007354#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007355 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7356 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7357 return PSA_ERROR_NOT_SUPPORTED;
7358 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007359 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007360 return PSA_ERROR_NOT_SUPPORTED;
7361 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007362
Ronald Cron01b2aba2020-10-05 09:42:02 +02007363 /* Accept only byte-aligned keys, for the same reasons as
7364 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 if (bits % 8 != 0) {
7366 return PSA_ERROR_NOT_SUPPORTED;
7367 }
7368 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007369#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007370
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007371#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007372 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007373 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007374 return PSA_SUCCESS;
7375 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007376#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007377
Valerio Settia55f0422023-07-10 15:34:41 +02007378#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007379 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007380 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007381 return PSA_ERROR_NOT_SUPPORTED;
7382 }
7383 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007384#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007385 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007386 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007387 }
7388
Gilles Peskine449bd832023-01-11 14:50:10 +01007389 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007390}
7391
Ronald Cron55ed0592020-10-05 10:30:40 +02007392psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007393 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007394 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007395{
7396 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007397 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007398
Gilles Peskine449bd832023-01-11 14:50:10 +01007399 if ((attributes->domain_parameters == NULL) &&
7400 (attributes->domain_parameters_size != 0)) {
7401 return PSA_ERROR_INVALID_ARGUMENT;
7402 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007403
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 if (key_type_is_raw_bytes(type)) {
7405 status = psa_generate_random(key_buffer, key_buffer_size);
7406 if (status != PSA_SUCCESS) {
7407 return status;
7408 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007409
David Brown12ca5032021-02-11 11:02:00 -07007410#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007411 if (type == PSA_KEY_TYPE_DES) {
7412 psa_des_set_key_parity(key_buffer, key_buffer_size);
7413 }
David Brown8107e312021-02-12 08:08:46 -07007414#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007415 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007416
Valerio Setti76df8c12023-07-11 14:11:28 +02007417#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007418 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7419 return mbedtls_psa_rsa_generate_key(attributes,
7420 key_buffer,
7421 key_buffer_size,
7422 key_buffer_length);
7423 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007424#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007425
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007426#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7428 return mbedtls_psa_ecp_generate_key(attributes,
7429 key_buffer,
7430 key_buffer_size,
7431 key_buffer_length);
7432 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007433#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007434
Valerio Settia55f0422023-07-10 15:34:41 +02007435#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007436 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7437 return mbedtls_psa_ffdh_generate_key(attributes,
7438 key_buffer,
7439 key_buffer_size,
7440 key_buffer_length);
7441 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007442#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007443 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 (void) key_buffer_length;
7445 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007446 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007447
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007449}
Darryl Green0c6575a2018-11-07 16:05:30 +00007450
Gilles Peskine449bd832023-01-11 14:50:10 +01007451psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7452 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007453{
7454 psa_status_t status;
7455 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007456 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007457 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007458
Ronald Cron81709fc2020-11-14 12:10:32 +01007459 *key = MBEDTLS_SVC_KEY_ID_INIT;
7460
Gilles Peskine0f84d622019-09-12 19:03:13 +02007461 /* Reject any attempt to create a zero-length key so that we don't
7462 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007463 if (psa_get_key_bits(attributes) == 0) {
7464 return PSA_ERROR_INVALID_ARGUMENT;
7465 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007466
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007467 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7469 return PSA_ERROR_INVALID_ARGUMENT;
7470 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007471
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7473 &slot, &driver);
7474 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007475 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 }
Gilles Peskine11792082019-08-06 18:36:36 +02007477
Ronald Cron977c2472020-10-13 08:32:21 +02007478 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307479 * storage ( thus not in the case of generating a key in a secure element
7480 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7481 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 if (slot->key.data == NULL) {
7483 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7484 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007485 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 attributes->core.type, attributes->core.bits);
7487 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007488 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007490
7491 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007492 attributes->core.type,
7493 attributes->core.bits);
7494 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007495 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 attributes, &key_buffer_size);
7497 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007498 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007499 }
Ronald Cron977c2472020-10-13 08:32:21 +02007500 }
Ronald Cron977c2472020-10-13 08:32:21 +02007501
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7503 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007504 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007505 }
Ronald Cron977c2472020-10-13 08:32:21 +02007506 }
7507
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 status = psa_driver_wrapper_generate_key(attributes,
7509 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007510
Gilles Peskine449bd832023-01-11 14:50:10 +01007511 if (status != PSA_SUCCESS) {
7512 psa_remove_key_data_from_memory(slot);
7513 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007514
Gilles Peskine11792082019-08-06 18:36:36 +02007515exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007516 if (status == PSA_SUCCESS) {
7517 status = psa_finish_key_creation(slot, driver, key);
7518 }
7519 if (status != PSA_SUCCESS) {
7520 psa_fail_key_creation(slot, driver);
7521 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007522
Gilles Peskine449bd832023-01-11 14:50:10 +01007523 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007524}
7525
Gilles Peskinee59236f2018-01-27 23:32:46 +01007526/****************************************************************/
7527/* Module setup */
7528/****************************************************************/
7529
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007530#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007531psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 void (* entropy_init)(mbedtls_entropy_context *ctx),
7533 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007534{
Gilles Peskine449bd832023-01-11 14:50:10 +01007535 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7536 return PSA_ERROR_BAD_STATE;
7537 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007538 global_data.rng.entropy_init = entropy_init;
7539 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007540 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007541}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007542#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007543
Gilles Peskine449bd832023-01-11 14:50:10 +01007544void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007545{
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 psa_wipe_all_key_slots();
Valerio Setti83e0de82023-11-24 12:13:05 +01007547 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7548 mbedtls_psa_random_free(&global_data.rng);
7549 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007550 /* Wipe all remaining data, including configuration.
7551 * In particular, this sets all state indicator to the value
7552 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007554
7555 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007556 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007557}
7558
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007559#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7560/** Recover a transaction that was interrupted by a power failure.
7561 *
7562 * This function is called during initialization, before psa_crypto_init()
7563 * returns. If this function returns a failure status, the initialization
7564 * fails.
7565 */
7566static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007568{
Gilles Peskine449bd832023-01-11 14:50:10 +01007569 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007570 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7571 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007572 /* TODO - fall through to the failure case until this
7573 * is implemented.
7574 * https://github.com/ARMmbed/mbed-crypto/issues/218
7575 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007576 default:
7577 /* We found an unsupported transaction in the storage.
7578 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007580 }
7581}
7582#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7583
Gilles Peskine449bd832023-01-11 14:50:10 +01007584psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007585{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007586 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007587
Gilles Peskinec6b69072018-11-20 21:42:52 +01007588 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 if (global_data.initialized != 0) {
7590 return PSA_SUCCESS;
7591 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007592
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007593 /* Init drivers */
7594 status = psa_driver_wrapper_init();
7595 if (status != PSA_SUCCESS) {
7596 goto exit;
7597 }
7598 global_data.drivers_initialized = 1;
7599
Valerio Setti402cfba2023-11-13 10:24:32 +01007600 status = psa_initialize_key_slots();
7601 if (status != PSA_SUCCESS) {
7602 goto exit;
7603 }
7604
Gilles Peskine30524eb2020-11-13 17:02:26 +01007605 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007606 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007607 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007608 status = mbedtls_psa_random_seed(&global_data.rng);
7609 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007610 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007612 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007613
Gilles Peskine4b734222019-07-24 15:56:31 +02007614#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 status = psa_crypto_load_transaction();
7616 if (status == PSA_SUCCESS) {
7617 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7618 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007619 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 }
7621 status = psa_crypto_stop_transaction();
7622 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007623 /* There's no transaction to complete. It's all good. */
7624 status = PSA_SUCCESS;
7625 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007626#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007627
Gilles Peskinec6b69072018-11-20 21:42:52 +01007628 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007629 global_data.initialized = 1;
7630
7631exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007632 if (status != PSA_SUCCESS) {
7633 mbedtls_psa_crypto_free();
7634 }
7635 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007636}
7637
Yanray Wangffc3c482023-07-11 12:01:04 +08007638#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007639psa_status_t psa_crypto_driver_pake_get_password_len(
7640 const psa_crypto_driver_pake_inputs_t *inputs,
7641 size_t *password_len)
7642{
7643 if (inputs->password_len == 0) {
7644 return PSA_ERROR_BAD_STATE;
7645 }
7646
7647 *password_len = inputs->password_len;
7648
7649 return PSA_SUCCESS;
7650}
7651
7652psa_status_t psa_crypto_driver_pake_get_password(
7653 const psa_crypto_driver_pake_inputs_t *inputs,
7654 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7655{
7656 if (inputs->password_len == 0) {
7657 return PSA_ERROR_BAD_STATE;
7658 }
7659
7660 if (buffer_size < inputs->password_len) {
7661 return PSA_ERROR_BUFFER_TOO_SMALL;
7662 }
7663
7664 memcpy(buffer, inputs->password, inputs->password_len);
7665 *buffer_length = inputs->password_len;
7666
7667 return PSA_SUCCESS;
7668}
7669
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007670psa_status_t psa_crypto_driver_pake_get_user_len(
7671 const psa_crypto_driver_pake_inputs_t *inputs,
7672 size_t *user_len)
7673{
7674 if (inputs->user_len == 0) {
7675 return PSA_ERROR_BAD_STATE;
7676 }
7677
7678 *user_len = inputs->user_len;
7679
7680 return PSA_SUCCESS;
7681}
7682
7683psa_status_t psa_crypto_driver_pake_get_user(
7684 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007685 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007686{
7687 if (inputs->user_len == 0) {
7688 return PSA_ERROR_BAD_STATE;
7689 }
7690
Przemek Stekielc0e62502023-03-14 11:49:36 +01007691 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007692 return PSA_ERROR_BUFFER_TOO_SMALL;
7693 }
7694
Przemek Stekielc0e62502023-03-14 11:49:36 +01007695 memcpy(user_id, inputs->user, inputs->user_len);
7696 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007697
7698 return PSA_SUCCESS;
7699}
7700
7701psa_status_t psa_crypto_driver_pake_get_peer_len(
7702 const psa_crypto_driver_pake_inputs_t *inputs,
7703 size_t *peer_len)
7704{
7705 if (inputs->peer_len == 0) {
7706 return PSA_ERROR_BAD_STATE;
7707 }
7708
7709 *peer_len = inputs->peer_len;
7710
7711 return PSA_SUCCESS;
7712}
7713
7714psa_status_t psa_crypto_driver_pake_get_peer(
7715 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007716 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007717{
7718 if (inputs->peer_len == 0) {
7719 return PSA_ERROR_BAD_STATE;
7720 }
7721
Przemek Stekielc0e62502023-03-14 11:49:36 +01007722 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007723 return PSA_ERROR_BUFFER_TOO_SMALL;
7724 }
7725
Przemek Stekielc0e62502023-03-14 11:49:36 +01007726 memcpy(peer_id, inputs->peer, inputs->peer_len);
7727 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007728
7729 return PSA_SUCCESS;
7730}
7731
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007732psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7733 const psa_crypto_driver_pake_inputs_t *inputs,
7734 psa_pake_cipher_suite_t *cipher_suite)
7735{
7736 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7737 return PSA_ERROR_BAD_STATE;
7738 }
7739
7740 *cipher_suite = inputs->cipher_suite;
7741
7742 return PSA_SUCCESS;
7743}
7744
Neil Armstronga7d08c32022-06-01 18:21:20 +02007745psa_status_t psa_pake_setup(
7746 psa_pake_operation_t *operation,
7747 const psa_pake_cipher_suite_t *cipher_suite)
7748{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007749 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007750
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007751 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007752 status = PSA_ERROR_BAD_STATE;
7753 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007754 }
7755
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007756 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007757 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007758 status = PSA_ERROR_INVALID_ARGUMENT;
7759 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007760 }
7761
Przemek Stekiel51eac532022-12-07 11:04:51 +01007762 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7763
Przemek Stekiele12ed362022-12-21 12:54:46 +01007764 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007765 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7766 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007767 operation->data.inputs.cipher_suite = *cipher_suite;
7768
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007769#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007770 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007771 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007772 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007773
David Horstmann16f01512023-06-14 17:21:07 +01007774 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007775 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007776 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007777#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007778 {
7779 status = PSA_ERROR_NOT_SUPPORTED;
7780 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007781 }
7782
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007783 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7784
Przemek Stekiel51eac532022-12-07 11:04:51 +01007785 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007786exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007787 psa_pake_abort(operation);
7788 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007789}
7790
7791psa_status_t psa_pake_set_password_key(
7792 psa_pake_operation_t *operation,
7793 mbedtls_svc_key_id_t password)
7794{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007795 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007796 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7797 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007798 psa_key_attributes_t attributes;
7799 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007800
Przemek Stekiel51eac532022-12-07 11:04:51 +01007801 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007802 status = PSA_ERROR_BAD_STATE;
7803 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007804 }
7805
Przemek Stekiel6c764412022-11-22 14:05:12 +01007806 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7807 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007808 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007809 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007810 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007811 }
7812
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007813 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007814 .core = slot->attr
7815 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007816
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007817 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007818
Przemek Stekiel51eac532022-12-07 11:04:51 +01007819 if (type != PSA_KEY_TYPE_PASSWORD &&
7820 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7821 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007822 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007823 }
7824
Przemek Stekiel51eac532022-12-07 11:04:51 +01007825 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7826 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007827 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007828 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007829 }
7830
7831 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7832 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007833 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007834exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007835 if (status != PSA_SUCCESS) {
7836 psa_pake_abort(operation);
7837 }
Ryan Everett1b70a072024-01-04 10:32:49 +00007838 unlock_status = psa_unregister_read(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007839 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007840}
7841
7842psa_status_t psa_pake_set_user(
7843 psa_pake_operation_t *operation,
7844 const uint8_t *user_id,
7845 size_t user_id_len)
7846{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007847 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007848
7849 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007850 status = PSA_ERROR_BAD_STATE;
7851 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007852 }
7853
Przemek Stekiel51eac532022-12-07 11:04:51 +01007854 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007855 status = PSA_ERROR_INVALID_ARGUMENT;
7856 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007857 }
7858
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007859 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007860 status = PSA_ERROR_BAD_STATE;
7861 goto exit;
7862 }
7863
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007864 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7865 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007866 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7867 goto exit;
7868 }
7869
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007870 memcpy(operation->data.inputs.user, user_id, user_id_len);
7871 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007872
7873 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007874exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007875 psa_pake_abort(operation);
7876 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007877}
7878
7879psa_status_t psa_pake_set_peer(
7880 psa_pake_operation_t *operation,
7881 const uint8_t *peer_id,
7882 size_t peer_id_len)
7883{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007884 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007885
7886 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007887 status = PSA_ERROR_BAD_STATE;
7888 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007889 }
7890
Przemek Stekiel51eac532022-12-07 11:04:51 +01007891 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007892 status = PSA_ERROR_INVALID_ARGUMENT;
7893 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007894 }
7895
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007896 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007897 status = PSA_ERROR_BAD_STATE;
7898 goto exit;
7899 }
7900
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007901 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7902 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007903 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7904 goto exit;
7905 }
7906
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007907 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7908 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007909
7910 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007911exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007912 psa_pake_abort(operation);
7913 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007914}
7915
7916psa_status_t psa_pake_set_role(
7917 psa_pake_operation_t *operation,
7918 psa_pake_role_t role)
7919{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007920 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007921
Przemek Stekiel51eac532022-12-07 11:04:51 +01007922 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007923 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007924 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007925 }
7926
Przemek Stekiel09104b82023-03-06 14:11:51 +01007927 switch (operation->alg) {
7928#if defined(PSA_WANT_ALG_JPAKE)
7929 case PSA_ALG_JPAKE:
7930 if (role == PSA_PAKE_ROLE_NONE) {
7931 return PSA_SUCCESS;
7932 }
7933 status = PSA_ERROR_INVALID_ARGUMENT;
7934 break;
7935#endif
7936 default:
7937 (void) role;
7938 status = PSA_ERROR_NOT_SUPPORTED;
7939 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007940 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007941exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007942 psa_pake_abort(operation);
7943 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007944}
7945
David Horstmanne7f21e62023-05-12 18:17:21 +01007946/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007947#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007948static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007949 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007950{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007951 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007952 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007953 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007954
David Horstmann024e5c52023-06-14 15:48:21 +01007955 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007956 is_x1 = (stage->outputs < 1);
7957 } else {
7958 is_x1 = (stage->inputs < 1);
7959 }
7960
David Horstmann74a3d8c2023-06-14 18:28:19 +01007961 key_share_step = is_x1 ?
7962 PSA_JPAKE_X1_STEP_KEY_SHARE :
7963 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007964 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007965 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7966 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7967 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7968 } else {
7969 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007970 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01007971 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01007972}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007973#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007974
Przemek Stekiel2797d372022-12-22 11:19:22 +01007975static psa_status_t psa_pake_complete_inputs(
7976 psa_pake_operation_t *operation)
7977{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007978 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007979 /* Create copy of the inputs on stack as inputs share memory
7980 with the driver context which will be setup by the driver. */
7981 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007982
Przemek Stekielfde11282023-03-13 16:06:09 +01007983 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007984 return PSA_ERROR_BAD_STATE;
7985 }
7986
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007987 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007988 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7989 return PSA_ERROR_BAD_STATE;
7990 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007991 }
7992
Przemek Stekiel18620a32023-01-17 16:34:52 +01007993 /* Clear driver context */
7994 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7995
7996 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007997
7998 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01007999 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008000
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008001 /* User and peer are translated to role. */
8002 mbedtls_free(inputs.user);
8003 mbedtls_free(inputs.peer);
8004
Przemek Stekiel2797d372022-12-22 11:19:22 +01008005 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008006#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008007 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008008 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008009 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008010#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008011 {
8012 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008013 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008014 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008015 return status;
8016}
8017
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008018#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008019static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008020 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008021 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008022 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008023{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008024 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8025 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8026 step != PSA_PAKE_STEP_ZK_PROOF) {
8027 return PSA_ERROR_INVALID_ARGUMENT;
8028 }
8029
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008030 psa_jpake_computation_stage_t *computation_stage =
8031 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008032
David Horstmann5da95602023-06-08 15:37:12 +01008033 if (computation_stage->round != PSA_JPAKE_FIRST &&
8034 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008035 return PSA_ERROR_BAD_STATE;
8036 }
8037
David Horstmanne7f21e62023-05-12 18:17:21 +01008038 /* Check that the step we are given is the one we were expecting */
8039 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008040 return PSA_ERROR_BAD_STATE;
8041 }
8042
David Horstmanne7f21e62023-05-12 18:17:21 +01008043 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8044 computation_stage->inputs == 0 &&
8045 computation_stage->outputs == 0) {
8046 /* Start of the round, so function decides whether we are inputting
8047 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008048 computation_stage->io_mode = io_mode;
8049 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008050 /* Middle of the round so the mode we are in must match the function
8051 * called by the user */
8052 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008053 }
8054
8055 return PSA_SUCCESS;
8056}
8057
David Horstmanne7f21e62023-05-12 18:17:21 +01008058static psa_status_t psa_jpake_epilogue(
8059 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008060 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008061{
David Horstmanne7f21e62023-05-12 18:17:21 +01008062 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008063 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008064
David Horstmanne7f21e62023-05-12 18:17:21 +01008065 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8066 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008067 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008068 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008069 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008070 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008071 }
8072 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008073 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008074 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008075 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008076 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008077 }
8078 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008079 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8080 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008081 /* End of a round, move to the next round */
8082 stage->inputs = 0;
8083 stage->outputs = 0;
8084 stage->round++;
8085 }
8086 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008087 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008088 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008089 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008090 return PSA_SUCCESS;
8091}
David Horstmanne7f21e62023-05-12 18:17:21 +01008092
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008093#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008094
Neil Armstronga7d08c32022-06-01 18:21:20 +02008095psa_status_t psa_pake_output(
8096 psa_pake_operation_t *operation,
8097 psa_pake_step_t step,
8098 uint8_t *output,
8099 size_t output_size,
8100 size_t *output_length)
8101{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008102 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008103 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008104 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008105
8106 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008107 status = psa_pake_complete_inputs(operation);
8108 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008109 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008110 }
8111 }
8112
8113 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008114 status = PSA_ERROR_BAD_STATE;
8115 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008116 }
8117
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008118 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008119 status = PSA_ERROR_INVALID_ARGUMENT;
8120 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008121 }
8122
Przemek Stekiele12ed362022-12-21 12:54:46 +01008123 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008124#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008125 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008126 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008127 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008128 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008129 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008130 driver_step = convert_jpake_computation_stage_to_driver_step(
8131 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008132 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008133#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008134 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008135 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008136 status = PSA_ERROR_NOT_SUPPORTED;
8137 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008138 }
8139
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008140 status = psa_driver_wrapper_pake_output(operation, driver_step,
8141 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008142
8143 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008144 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008145 }
8146
8147 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008148#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008149 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008150 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008151 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008152 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008153 }
8154 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008155#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008156 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008157 status = PSA_ERROR_NOT_SUPPORTED;
8158 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008159 }
8160
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008161 return PSA_SUCCESS;
8162exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008163 psa_pake_abort(operation);
8164 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008165}
8166
8167psa_status_t psa_pake_input(
8168 psa_pake_operation_t *operation,
8169 psa_pake_step_t step,
8170 const uint8_t *input,
8171 size_t input_length)
8172{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008173 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008174 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008175 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8176 operation->primitive,
8177 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008178
8179 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008180 status = psa_pake_complete_inputs(operation);
8181 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008182 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008183 }
8184 }
8185
8186 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008187 status = PSA_ERROR_BAD_STATE;
8188 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008189 }
8190
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008191 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008192 status = PSA_ERROR_INVALID_ARGUMENT;
8193 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008194 }
8195
Przemek Stekiele12ed362022-12-21 12:54:46 +01008196 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008197#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008198 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008199 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008200 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008201 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008202 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008203 driver_step = convert_jpake_computation_stage_to_driver_step(
8204 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008205 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008206#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008207 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008208 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008209 status = PSA_ERROR_NOT_SUPPORTED;
8210 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008211 }
8212
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008213 status = psa_driver_wrapper_pake_input(operation, driver_step,
8214 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008215
8216 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008217 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008218 }
8219
8220 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008221#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008222 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008223 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008224 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008225 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008226 }
8227 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008228#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008229 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008230 status = PSA_ERROR_NOT_SUPPORTED;
8231 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008232 }
8233
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008234 return PSA_SUCCESS;
8235exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008236 psa_pake_abort(operation);
8237 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008238}
8239
8240psa_status_t psa_pake_get_implicit_key(
8241 psa_pake_operation_t *operation,
8242 psa_key_derivation_operation_t *output)
8243{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008244 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008245 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008246 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008247 size_t shared_key_len = 0;
8248
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008249 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008250 status = PSA_ERROR_BAD_STATE;
8251 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008252 }
8253
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008254#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008255 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008256 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008257 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008258 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008259 status = PSA_ERROR_BAD_STATE;
8260 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008261 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008262 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008263#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008264 {
8265 status = PSA_ERROR_NOT_SUPPORTED;
8266 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008267 }
8268
Przemek Stekiel0c781802022-11-29 14:53:13 +01008269 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8270 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008271 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008272 &shared_key_len);
8273
8274 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008275 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008276 }
8277
8278 status = psa_key_derivation_input_bytes(output,
8279 PSA_KEY_DERIVATION_INPUT_SECRET,
8280 shared_key,
8281 shared_key_len);
8282
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008283 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008284exit:
8285 abort_status = psa_pake_abort(operation);
8286 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008287}
8288
8289psa_status_t psa_pake_abort(
8290 psa_pake_operation_t *operation)
8291{
Przemek Stekield69dca92023-01-31 19:59:20 +01008292 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008293
Przemek Stekield69dca92023-01-31 19:59:20 +01008294 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008295 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008296 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008297
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008298 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8299 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008300 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008301 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008302 }
8303 if (operation->data.inputs.user != NULL) {
8304 mbedtls_free(operation->data.inputs.user);
8305 }
8306 if (operation->data.inputs.peer != NULL) {
8307 mbedtls_free(operation->data.inputs.peer);
8308 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008309 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008310 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008311
Przemek Stekield69dca92023-01-31 19:59:20 +01008312 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008313}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008314#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008315
Gilles Peskinee59236f2018-01-27 23:32:46 +01008316#endif /* MBEDTLS_PSA_CRYPTO_C */