blob: 27ea3b84c8285b685b235bed2063286fcd5723c3 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinee59236f2018-01-27 23:32:46 +01007 */
8
Gilles Peskinedb09ef62020-06-03 01:43:33 +02009#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010010#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010011
12#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030013
John Durkop07cc04a2020-11-16 22:08:34 -080014#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
15#include "check_crypto_config.h"
16#endif
17
Gilles Peskinee59236f2018-01-27 23:32:46 +010018#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010019#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010020
Ronald Crond6d28882020-12-14 14:56:02 +010021#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010022#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010023#include "psa_crypto_invasive.h"
Xiaokang Qiane9c39c42023-08-09 08:50:19 +000024#include "psa_crypto_driver_wrappers.h"
Xiaokang Qianfe9666b2023-09-11 10:36:20 +000025#include "psa_crypto_driver_wrappers_no_static.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010027#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010028#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010029#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010031#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020033#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020034#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskineb2b64d32020-12-14 16:43:58 +010040#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010041
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include <stdlib.h>
43#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010046#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020047#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000048#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020049#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010050#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020051#include "mbedtls/chacha20.h"
52#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/cipher.h"
54#include "mbedtls/ccm.h"
55#include "mbedtls/cmac.h"
Dave Rodgman78701152023-08-29 14:20:18 +010056#include "mbedtls/constant_time.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020058#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010059#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010060#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/error.h"
62#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/md5.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010064#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000065#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010066#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000067#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/sha1.h"
71#include "mbedtls/sha256.h"
72#include "mbedtls/sha512.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010073#include "mbedtls/psa_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020075#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
76 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
77 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020078#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020079#endif
80
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010081/****************************************************************/
82/* Global data, support functions and library management */
83/****************************************************************/
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020086{
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020088}
89
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010090/* Values for psa_global_data_t::rng_state */
91#define RNG_NOT_INITIALIZED 0
92#define RNG_INITIALIZED 1
93#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010094
Gilles Peskine449bd832023-01-11 14:50:10 +010095typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +010096 uint8_t initialized;
97 uint8_t rng_state;
98 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +010099 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100100} psa_global_data_t;
101
102static psa_global_data_t global_data;
103
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100104#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
105mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
106 &global_data.rng.drbg;
107#endif
108
itayzafrir0adf0fc2018-09-06 16:24:41 +0300109#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (global_data.initialized == 0) \
111 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300112
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100113int psa_can_do_hash(psa_algorithm_t hash_alg)
114{
115 (void) hash_alg;
116 return global_data.drivers_initialized;
117}
Valerio Settic6f004f2023-12-12 11:27:36 +0100118
Valerio Setti1fff4f22023-12-28 14:19:34 +0100119int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
Valerio Settic6f004f2023-12-12 11:27:36 +0100120{
Valerio Setti1fff4f22023-12-28 14:19:34 +0100121 (void) key_type;
Valerio Settic6f004f2023-12-12 11:27:36 +0100122 (void) cipher_alg;
123 return global_data.drivers_initialized;
124}
125
126
Valerio Settia55f0422023-07-10 15:34:41 +0200127#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200128 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200129 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200130static int psa_is_dh_key_size_valid(size_t bits)
131{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200132 if (bits != 2048 && bits != 3072 && bits != 4096 &&
133 bits != 6144 && bits != 8192) {
134 return 0;
135 }
136
137 return 1;
138}
Valerio Settia55f0422023-07-10 15:34:41 +0200139#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200140 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200141 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100144{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100145 /* Mbed TLS error codes can combine a high-level error code and a
146 * low-level error code. The low-level error usually reflects the
147 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 int low_level_ret = -(-ret & 0x007f);
149 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100150 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100152
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100153#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100154 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
155 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100157 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
158 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100159#endif
160
161#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200162 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
163 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
164 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
165 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
166 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200168 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200170 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100172#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200173
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100174#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000175 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100178#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100179
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100180#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100185#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100186
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100187#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100190#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200191
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100192#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200193 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200195 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100197#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200198
Dave Rodgman509b5672023-08-16 19:26:23 +0100199#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100200 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100204 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100212 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100214#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
217 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100218 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
219 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100222 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
223 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100227#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100228
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100229#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100232#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
Gilles Peskinee59236f2018-01-27 23:32:46 +0100234 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
235 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
236 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100238
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100239#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100240 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200242 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100244 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100246#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100247
Gilles Peskine82e57d12020-11-13 21:31:17 +0100248#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100250 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
251 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100252 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100254 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
255 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100257 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100259#endif
260
Dave Rodgmandea266f2023-08-31 11:52:43 +0100261#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100262 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100264 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100268#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100271#endif
272#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100273
Dave Rodgman509b5672023-08-16 19:26:23 +0100274#if defined(MBEDTLS_BIGNUM_C)
275#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100276 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100278#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100279 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100281 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100283 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100285 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100287 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100289 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100291 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100293#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100294
Dave Rodgman509b5672023-08-16 19:26:23 +0100295#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100296 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100298 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
299 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100301#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
302 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100303 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100305#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
307 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100311 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
312 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100314 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100316 case MBEDTLS_ERR_PK_INVALID_ALG:
317 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
318 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100320 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200322 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100324#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100325
Gilles Peskineff2d2002019-05-06 15:26:23 +0200326 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200328 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200330
Dave Rodgman509b5672023-08-16 19:26:23 +0100331#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100332 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100334 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100336 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100338 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100340 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
341 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100343 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100345 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100349#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200350
Dave Rodgman1dab4452023-09-01 09:59:51 +0100351#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300352 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300353 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300355 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300357 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300359 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
360 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300362 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100364 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100366
Dave Rodgman509b5672023-08-16 19:26:23 +0100367#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000368 case MBEDTLS_ERR_ECP_IN_PROGRESS:
369 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100370#endif
371#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000372
Janos Follatha13b9052019-11-22 12:48:59 +0000373 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300375
Gilles Peskinee59236f2018-01-27 23:32:46 +0100376 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100378 }
379}
380
Paul Elliottdc42ca82023-02-24 18:11:59 +0000381/**
382 * \brief For output buffers which contain "tags"
383 * (outputs that may be checked for validity like
384 * hashes, MACs and signatures), fill the unused
385 * part of the output buffer (the whole buffer on
386 * error, the trailing part on success) with
387 * something that isn't a valid tag (barring an
388 * attack on the tag and deliberately-crafted
389 * input), in case the caller doesn't check the
390 * return status properly.
391 *
392 * \param output_buffer Pointer to buffer to wipe. May not be NULL
393 * unless \p output_buffer_size is zero.
394 * \param status Status of function called to generate
395 * output_buffer originally
396 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
397 * could be NULL.
398 * \param output_buffer_length Length of data written to output_buffer, must be
399 * less than \p output_buffer_size
400 */
401static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000402 size_t output_buffer_size, size_t output_buffer_length)
403{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000404 size_t offset = 0;
405
406 if (output_buffer_size == 0) {
407 /* If output_buffer_size is 0 then we have nothing to do. We must not
408 call memset because output_buffer may be NULL in this case */
409 return;
410 }
411
412 if (status == PSA_SUCCESS) {
413 offset = output_buffer_length;
414 }
415
416 memset(output_buffer + offset, '!', output_buffer_size - offset);
417}
418
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
421 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200422{
423 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200425 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200426 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200427 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200428 case PSA_KEY_TYPE_PASSWORD:
429 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200430 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100431#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200432 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (bits != 128 && bits != 192 && bits != 256) {
434 return PSA_ERROR_INVALID_ARGUMENT;
435 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200436 break;
437#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200438#if defined(PSA_WANT_KEY_TYPE_ARIA)
439 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (bits != 128 && bits != 192 && bits != 256) {
441 return PSA_ERROR_INVALID_ARGUMENT;
442 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200443 break;
444#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100445#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (bits != 128 && bits != 192 && bits != 256) {
448 return PSA_ERROR_INVALID_ARGUMENT;
449 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200450 break;
451#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100452#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200453 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (bits != 64 && bits != 128 && bits != 192) {
455 return PSA_ERROR_INVALID_ARGUMENT;
456 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200457 break;
458#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100459#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200460 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (bits != 256) {
462 return PSA_ERROR_INVALID_ARGUMENT;
463 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200464 break;
465#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200468 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (bits % 8 != 0) {
470 return PSA_ERROR_INVALID_ARGUMENT;
471 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200474}
475
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100476/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100477 *
Steven Cooremancd640932021-03-08 12:00:27 +0100478 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
479 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100480 *
481 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100482 * \param[in] key_type The key type of the key to be used with the
483 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100484 *
485 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100486 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100487 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100488 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100489 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100490MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100491 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100493{
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (PSA_ALG_IS_HMAC(algorithm)) {
495 if (key_type == PSA_KEY_TYPE_HMAC) {
496 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100497 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100498 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
501 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
502 * key. */
503 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
504 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
505 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
506 * the block length (larger than 1) for block ciphers. */
507 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
508 return PSA_SUCCESS;
509 }
510 }
511 }
512
513 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100514}
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
517 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200518{
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (slot->key.data != NULL) {
520 return PSA_ERROR_ALREADY_EXISTS;
521 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 slot->key.data = mbedtls_calloc(1, buffer_length);
524 if (slot->key.data == NULL) {
525 return PSA_ERROR_INSUFFICIENT_MEMORY;
526 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200527
Ronald Cronea0f8a62020-11-25 17:52:23 +0100528 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200530}
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
533 const uint8_t *data,
534 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200535{
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 psa_status_t status = psa_allocate_buffer_to_slot(slot,
537 data_length);
538 if (status != PSA_SUCCESS) {
539 return status;
540 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 memcpy(slot->key.data, data, data_length);
543 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200544}
545
Ronald Crona0fe59f2020-11-29 11:14:53 +0100546psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100547 const psa_key_attributes_t *attributes,
548 const uint8_t *data, size_t data_length,
549 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100552 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
553 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200555 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (data_length == 0) {
557 return PSA_ERROR_NOT_SUPPORTED;
558 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (key_type_is_raw_bytes(type)) {
561 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
564 *bits);
565 if (status != PSA_SUCCESS) {
566 return status;
567 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200568
Ronald Crondd04d422020-11-28 15:14:42 +0100569 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100571 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 return PSA_SUCCESS;
575 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200576#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200577 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100578 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200579 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100580 return PSA_ERROR_INVALID_ARGUMENT;
581 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200582 return mbedtls_psa_ffdh_import_key(attributes,
583 data, data_length,
584 key_buffer, key_buffer_size,
585 key_buffer_length,
586 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100587 }
Valerio Settia55f0422023-07-10 15:34:41 +0200588#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200589 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200590#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
592 if (PSA_KEY_TYPE_IS_ECC(type)) {
593 return mbedtls_psa_ecp_import_key(attributes,
594 data, data_length,
595 key_buffer, key_buffer_size,
596 key_buffer_length,
597 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100598 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200599#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800600 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200601#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
602 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
604 if (PSA_KEY_TYPE_IS_RSA(type)) {
605 return mbedtls_psa_rsa_import_key(attributes,
606 data, data_length,
607 key_buffer, key_buffer_size,
608 key_buffer_length,
609 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200610 }
Valerio Settife478902023-07-25 12:27:19 +0200611#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
612 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800613 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100614 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100617}
618
Gilles Peskinef603c712019-01-19 13:40:11 +0100619/** Calculate the intersection of two algorithm usage policies.
620 *
621 * Return 0 (which allows no operation) on incompatibility.
622 */
623static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100624 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100625 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100627{
Gilles Peskine549ea862019-05-22 11:45:59 +0200628 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (alg1 == alg2) {
630 return alg1;
631 }
Steven Cooreman03488022021-02-18 12:07:20 +0100632 /* If the policies are from the same hash-and-sign family, check
633 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
635 PSA_ALG_IS_SIGN_HASH(alg2) &&
636 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
637 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
638 return alg2;
639 }
640 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
641 return alg1;
642 }
Steven Cooreman03488022021-02-18 12:07:20 +0100643 }
644 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100645 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100646 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
648 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
649 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
650 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
651 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100652 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100653
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100654 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
656 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
657 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
658 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100659 }
660 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
662 (alg1_len <= alg2_len)) {
663 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100664 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
666 (alg2_len <= alg1_len)) {
667 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100668 }
669 }
670 /* If the policies are from the same MAC family, check whether one
671 * of them is a minimum-MAC-length policy. Calculate the most
672 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
674 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
675 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100676 /* Validate the combination of key type and algorithm. Since the base
677 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
679 return 0;
680 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100681
Steven Cooreman31a876d2021-03-03 20:47:40 +0100682 /* Get the (exact or at-least) output lengths for both sides of the
683 * requested intersection. None of the currently supported algorithms
684 * have an output length dependent on the actual key size, so setting it
685 * to a bogus value of 0 is currently OK.
686 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100687 * Note that for at-least-this-length wildcard algorithms, the output
688 * length is set to the shortest allowed length, which allows us to
689 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
691 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100692 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100693
Steven Cooremana1d83222021-02-25 10:20:29 +0100694 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
696 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
697 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100698 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100699
700 /* If only one is an at-least-this-length policy, the intersection would
701 * be the other (fixed-length) policy as long as said fixed length is
702 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
704 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100705 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
707 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100708 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100709
Steven Cooremancd640932021-03-08 12:00:27 +0100710 /* If none of them are wildcards, check whether they define the same tag
711 * length. This is still possible here when one is default-length and
712 * the other specific-length. Ensure to always return the
713 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (alg1_len == alg2_len) {
715 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
716 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100717 }
718 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100720}
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722static int psa_key_algorithm_permits(psa_key_type_t key_type,
723 psa_algorithm_t policy_alg,
724 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200725{
Gilles Peskine549ea862019-05-22 11:45:59 +0200726 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (requested_alg == policy_alg) {
728 return 1;
729 }
Steven Cooreman03488022021-02-18 12:07:20 +0100730 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
731 * and requested_alg is the same hash-and-sign family with any hash,
732 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
734 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
735 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
736 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100737 }
738 /* If policy_alg is a wildcard AEAD algorithm of the same base as
739 * the requested algorithm, check the requested tag length to be
740 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (PSA_ALG_IS_AEAD(policy_alg) &&
742 PSA_ALG_IS_AEAD(requested_alg) &&
743 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
744 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
745 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
746 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
747 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100748 }
Steven Cooremancd640932021-03-08 12:00:27 +0100749 /* If policy_alg is a MAC algorithm of the same base as the requested
750 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (PSA_ALG_IS_MAC(policy_alg) &&
752 PSA_ALG_IS_MAC(requested_alg) &&
753 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
754 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100755 /* Validate the combination of key type and algorithm. Since the policy
756 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
758 return 0;
759 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100760
Steven Cooreman31a876d2021-03-03 20:47:40 +0100761 /* Get both the requested output length for the algorithm which is to be
762 * verified, and the default output length for the base algorithm.
763 * Note that none of the currently supported algorithms have an output
764 * length dependent on actual key size, so setting it to a bogus value
765 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100766 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100768 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 key_type, 0,
770 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100771
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100772 /* If the policy is default-length, only allow an algorithm with
773 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
775 return requested_output_length == default_output_length;
776 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100777
778 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100779 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
781 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
782 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100783 }
784
Steven Cooremancd640932021-03-08 12:00:27 +0100785 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
786 * check for the requested MAC length to be equal to or longer than the
787 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
789 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
790 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100791 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200792 }
Steven Cooremance48e852020-10-05 16:02:45 +0200793 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200794 * a key derivation with that key agreement should also be allowed. This
795 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
797 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
798 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
799 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200800 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100801 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200803}
804
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100805/** Test whether a policy permits an algorithm.
806 *
807 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100808 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100809 * \note This function requires providing the key type for which the policy is
810 * being validated, since some algorithm policy definitions (e.g. MAC)
811 * have different properties depending on what kind of cipher it is
812 * combined with.
813 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100814 * \retval PSA_SUCCESS When \p alg is a specific algorithm
815 * allowed by the \p policy.
816 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
817 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
818 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100819 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
821 psa_key_type_t key_type,
822 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100823{
Steven Cooremand788fab2021-02-25 11:29:17 +0100824 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (alg == 0) {
826 return PSA_ERROR_INVALID_ARGUMENT;
827 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100828
Steven Cooreman7e39f052021-02-23 14:18:32 +0100829 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if (PSA_ALG_IS_WILDCARD(alg)) {
831 return PSA_ERROR_INVALID_ARGUMENT;
832 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
835 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
836 return PSA_SUCCESS;
837 } else {
838 return PSA_ERROR_NOT_PERMITTED;
839 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100840}
841
Gilles Peskinef603c712019-01-19 13:40:11 +0100842/** Restrict a key policy based on a constraint.
843 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100844 * \note This function requires providing the key type for which the policy is
845 * being restricted, since some algorithm policy definitions (e.g. MAC)
846 * have different properties depending on what kind of cipher it is
847 * combined with.
848 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100849 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100850 * \param[in,out] policy The policy to restrict.
851 * \param[in] constraint The policy constraint to apply.
852 *
853 * \retval #PSA_SUCCESS
854 * \c *policy contains the intersection of the original value of
855 * \c *policy and \c *constraint.
856 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100857 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100858 * \c *policy is unchanged.
859 */
860static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100861 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100862 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100864{
865 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 psa_key_policy_algorithm_intersection(key_type, policy->alg,
867 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200868 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
870 constraint->alg2);
871 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
872 return PSA_ERROR_INVALID_ARGUMENT;
873 }
874 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
875 return PSA_ERROR_INVALID_ARGUMENT;
876 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100877 policy->usage &= constraint->usage;
878 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200879 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100881}
882
Przemek Stekiel061f6942022-11-30 10:51:35 +0100883/** Get the description of a key given its identifier and policy constraints
884 * and lock it.
885 *
886 * The key must have allow all the usage flags set in \p usage. If \p alg is
887 * nonzero, the key must allow operations with this algorithm. If \p alg is
888 * zero, the algorithm is not checked.
889 *
890 * In case of a persistent key, the function loads the description of the key
891 * into a key slot if not already done.
892 *
Ryan Everett098c6652024-01-03 13:03:36 +0000893 * On success, the returned key slot has been registered for reading.
894 * It is the responsibility of the caller to call psa_unregister_read(slot)
895 * when they have finished reading the contents of the slot.
Przemek Stekiel061f6942022-11-30 10:51:35 +0100896 */
897static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100898 mbedtls_svc_key_id_t key,
899 psa_key_slot_t **p_slot,
900 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100902{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100904 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 status = psa_get_and_lock_key_slot(key, p_slot);
907 if (status != PSA_SUCCESS) {
908 return status;
909 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200910 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100911
912 /* Enforce that usage policy for the key slot contains all the flags
913 * required by the usage parameter. There is one exception: public
914 * keys can always be exported, so we treat public key objects as
915 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100921 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200922 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100923 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100924
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800925 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if (alg != 0) {
927 status = psa_key_policy_permits(&slot->attr.policy,
928 slot->attr.type,
929 alg);
930 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100931 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100933 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200936
937error:
938 *p_slot = NULL;
Ryan Everett098c6652024-01-03 13:03:36 +0000939 psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100942}
Darryl Green940d72c2018-07-13 13:18:51 +0100943
Ronald Cron5c522922020-11-14 16:35:34 +0100944/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200945 *
946 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200947 * available, as opposed to a key in a secure element and/or to be used
948 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200949 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200950 * This is a temporary function that may be used instead of
951 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
952 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200953 *
Ryan Everett098c6652024-01-03 13:03:36 +0000954 * On success, the returned key slot has been registered for reading.
955 * It is the responsibility of the caller to call psa_unregister_read(slot)
956 * when they have finished reading the contents of the slot.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200957 */
Ronald Cron5c522922020-11-14 16:35:34 +0100958static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
959 mbedtls_svc_key_id_t key,
960 psa_key_slot_t **p_slot,
961 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200963{
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
965 usage, alg);
966 if (status != PSA_SUCCESS) {
967 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200968 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everett098c6652024-01-03 13:03:36 +0000971 psa_unregister_read(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 *p_slot = NULL;
973 return PSA_ERROR_NOT_SUPPORTED;
974 }
975
976 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200977}
Gilles Peskine28f8f302019-07-24 13:30:31 +0200978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +0000980{
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +0100982 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 }
Ronald Cronea0f8a62020-11-25 17:52:23 +0100984
Ronald Cronea0f8a62020-11-25 17:52:23 +0100985 slot->key.data = NULL;
986 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +0000989}
990
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100991/** Completely wipe a slot in memory, including its policy.
992 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100993psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100994{
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +0100996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 /*
998 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +0000999 * do our best to report an unexpected amount of registered readers or
1000 * an unexpected state.
1001 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1002 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1004 * function is called as part of the execution of a test suite, the
1005 * execution of the test suite is stopped in error if the assertion fails.
1006 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001007 switch (slot->state) {
1008 case PSA_SLOT_FULL:
1009 /* In this state psa_wipe_key_slot() must only be called if the
1010 * caller is the last reader. */
1011 case PSA_SLOT_PENDING_DELETION:
1012 /* In this state psa_wipe_key_slot() must only be called if the
1013 * caller is the last reader. */
1014 if (slot->registered_readers != 1) {
1015 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1);
1016 status = PSA_ERROR_CORRUPTION_DETECTED;
1017 }
1018 break;
1019 case PSA_SLOT_FILLING:
1020 /* In this state registered_readers must be 0. */
1021 if (slot->registered_readers != 0) {
1022 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0);
1023 status = PSA_ERROR_CORRUPTION_DETECTED;
1024 }
1025 break;
1026 case PSA_SLOT_EMPTY:
1027 /* The slot is already empty, it cannot be wiped. */
1028 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1029 status = PSA_ERROR_CORRUPTION_DETECTED;
1030 break;
1031 default:
1032 /* The slot's state is invalid. */
1033 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001034 }
1035
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001036 /* Multipart operations may still be using the key. This is safe
1037 * because all multipart operation objects are independent from
1038 * the key slot: if they need to access the key after the setup
1039 * phase, they have a copy of the key. Note that this means that
1040 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001041 /* At this point, key material and other type-specific content has
1042 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001043 * zeroize because the metadata is not particularly sensitive.
1044 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 memset(slot, 0, sizeof(*slot));
1046 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001047}
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001050{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001051 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001052 psa_status_t status; /* status of the last operation */
1053 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001054#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1055 psa_se_drv_table_entry_t *driver;
1056#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (mbedtls_svc_key_id_is_null(key)) {
1059 return PSA_SUCCESS;
1060 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001061
Ronald Cronf2911112020-10-29 17:51:10 +01001062 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001063 * Get the description of the key in a key slot, and register to read it.
1064 * In the case of a persistent key, this will load the key description
1065 * from persistent memory if not done yet.
1066 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001067 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001068 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 status = psa_get_and_lock_key_slot(key, &slot);
1070 if (status != PSA_SUCCESS) {
1071 return status;
1072 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001073
Ryan Everettc053d962024-01-25 17:56:32 +00001074#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001075 /* We cannot unlock between setting the state to PENDING_DELETION
1076 * and destroying the key in storage, as otherwise another thread
1077 * could load the key into a new slot and the key will not be
1078 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001079 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1080 &mbedtls_threading_key_slot_mutex));
1081#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001082 /* Set the key slot containing the key description's state to
1083 * PENDING_DELETION. This stops new operations from registering
1084 * to read the slot. Current readers can safely continue to access
1085 * the key within the slot; the last registered reader will
1086 * automatically wipe the slot when they call psa_unregister_read().
1087 * If the key is persistent, we can now delete the copy of the key
1088 * from memory. If the key is opaque, we require the driver to
1089 * deal with the deletion. */
Ryan Everettc053d962024-01-25 17:56:32 +00001090 status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1091 PSA_SLOT_PENDING_DELETION);
1092
1093 if (status != PSA_SUCCESS) {
1094 goto exit;
1095 }
Ronald Cronf2911112020-10-29 17:51:10 +01001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001098 /* Refuse the destruction of a read-only key (which may or may not work
1099 * if we attempt it, depending on whether the key is merely read-only
1100 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001101 * Just do the best we can, which is to wipe the copy in memory
1102 * (done in this function's cleanup code). */
1103 overall_status = PSA_ERROR_NOT_PERMITTED;
1104 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001105 }
1106
Gilles Peskine354f7672019-07-12 23:46:38 +02001107#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1109 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001110 /* For a key in a secure element, we need to do three things:
1111 * remove the key file in internal storage, destroy the
1112 * key inside the secure element, and update the driver's
1113 * persistent data. Start a transaction that will encompass these
1114 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001116 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001118 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 status = psa_crypto_save_transaction();
1120 if (status != PSA_SUCCESS) {
1121 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001122 /* We should still try to destroy the key in the secure
1123 * element and the key metadata in storage. This is especially
1124 * important if the error is that the storage is full.
1125 * But how to do it exactly without risking an inconsistent
1126 * state after a reset?
1127 * https://github.com/ARMmbed/mbed-crypto/issues/215
1128 */
1129 overall_status = status;
1130 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001131 }
1132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 status = psa_destroy_se_key(driver,
1134 psa_key_slot_get_slot_number(slot));
1135 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001136 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001138 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001139#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1140
Darryl Greend49a4992018-06-18 17:27:26 +01001141#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001143 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001144 * The slot will still hold a copy of the key until the last reader
1145 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 status = psa_destroy_persistent_key(slot->attr.id);
1147 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001148 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 }
Darryl Greend49a4992018-06-18 17:27:26 +01001150 }
1151#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001152
Gilles Peskinefc762652019-07-22 19:30:34 +02001153#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if (driver != NULL) {
1155 status = psa_save_se_persistent_data(driver);
1156 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001157 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 }
1159 status = psa_crypto_stop_transaction();
1160 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001161 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001163 }
1164#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1165
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001166exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001167 /* Unregister from reading the slot. If we are the last active reader
1168 * then this will wipe the slot. */
1169 status = psa_unregister_read(slot);
Ryan Everett7fee4f72024-02-09 14:11:27 +00001170 /* Prioritize CORRUPTION_DETECTED from unregistering over
1171 * a storage error. */
1172 if (status != PSA_SUCCESS) {
1173 overall_status = status;
1174 }
Ryan Everettc053d962024-01-25 17:56:32 +00001175
1176#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001177 /* Don't overwrite existing errors if the unlock fails. */
1178 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001179 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1180 &mbedtls_threading_key_slot_mutex));
1181#endif
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001184}
1185
Valerio Settib2bcedb2023-07-10 11:24:00 +02001186#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001187 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001188static psa_status_t psa_get_rsa_public_exponent(
1189 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001191{
1192 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001193 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001194 uint8_t *buffer = NULL;
1195 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1199 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001200 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 }
1202 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001203 /* It's the default value, which is reported as an empty string,
1204 * so there's nothing to do. */
1205 goto exit;
1206 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 buflen = mbedtls_mpi_size(&mpi);
1209 buffer = mbedtls_calloc(1, buflen);
1210 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001211 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1212 goto exit;
1213 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1215 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001216 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001218 attributes->domain_parameters = buffer;
1219 attributes->domain_parameters_size = buflen;
1220
1221exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 mbedtls_mpi_free(&mpi);
1223 if (ret != 0) {
1224 mbedtls_free(buffer);
1225 }
1226 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001227}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001228#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001229 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001230
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001231/** Retrieve all the publicly-accessible attributes of a key.
1232 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001233psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1234 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001235{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001236 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001237 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001238 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1243 if (status != PSA_SUCCESS) {
1244 return status;
1245 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001246
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001247 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1249 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001250
1251#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1253 psa_set_key_slot_number(attributes,
1254 psa_key_slot_get_slot_number(slot));
1255 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001256#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001259#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1260 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001261 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001262 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001263 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Pengyu Lvf75893b2023-12-08 17:21:39 +08001264 /* TODO: This is a temporary situation where domain parameters are deprecated,
1265 * but we need it for namely generating an RSA key with a non-default exponent.
1266 * This would be improved after https://github.com/Mbed-TLS/mbedtls/issues/6494.
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001267 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001269 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001270
Ronald Cron90857082020-11-25 14:58:33 +01001271 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 slot->attr.type,
1273 slot->key.data,
1274 slot->key.bytes,
1275 &rsa);
1276 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001277 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 status = psa_get_rsa_public_exponent(rsa,
1281 attributes);
1282 mbedtls_rsa_free(rsa);
1283 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001284 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001285 break;
Pengyu Lve9efbc22023-12-08 16:59:08 +08001286#else
1287 case PSA_KEY_TYPE_RSA_KEY_PAIR:
1288 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1289 attributes->domain_parameters = NULL;
1290 attributes->domain_parameters_size = SIZE_MAX;
1291 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001292#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1293 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001294 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001295 default:
1296 /* Nothing else to do. */
1297 break;
1298 }
1299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if (status != PSA_SUCCESS) {
1301 psa_reset_key_attributes(attributes);
1302 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001303
Ryan Everett1b70a072024-01-04 10:32:49 +00001304 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001307}
1308
Gilles Peskinec8000c02019-08-02 20:15:51 +02001309#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1310psa_status_t psa_get_key_slot_number(
1311 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001313{
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001315 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 return PSA_SUCCESS;
1317 } else {
1318 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001319 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001320}
1321#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1324 size_t key_buffer_size,
1325 uint8_t *data,
1326 size_t data_size,
1327 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001328{
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 if (key_buffer_size > data_size) {
1330 return PSA_ERROR_BUFFER_TOO_SMALL;
1331 }
1332 memcpy(data, key_buffer, key_buffer_size);
1333 memset(data + key_buffer_size, 0,
1334 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001335 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001337}
1338
Ronald Cron7285cda2020-11-26 14:46:37 +01001339psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001340 const psa_key_attributes_t *attributes,
1341 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001343{
Ronald Crond18b5f82020-11-25 16:46:05 +01001344 psa_key_type_t type = attributes->core.type;
1345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (key_type_is_raw_bytes(type) ||
1347 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001348 PSA_KEY_TYPE_IS_ECC(type) ||
1349 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 return psa_export_key_buffer_internal(
1351 key_buffer, key_buffer_size,
1352 data, data_size, data_length);
1353 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001354 /* This shouldn't happen in the reference implementation, but
1355 it is valid for a special-purpose implementation to omit
1356 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001358 }
1359}
1360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1362 uint8_t *data,
1363 size_t data_size,
1364 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001365{
1366 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1367 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1368 psa_key_slot_t *slot;
1369
Ronald Crone907e552021-01-18 13:22:38 +01001370 /* Reject a zero-length output buffer now, since this can never be a
1371 * valid key representation. This way we know that data must be a valid
1372 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 if (data_size == 0) {
1374 return PSA_ERROR_BUFFER_TOO_SMALL;
1375 }
Ronald Crone907e552021-01-18 13:22:38 +01001376
Ronald Cron9486f9d2020-11-26 13:36:23 +01001377 /* Set the key to empty now, so that even when there are errors, we always
1378 * set data_length to a value between 0 and data_size. On error, setting
1379 * the key to empty is a good choice because an empty key representation is
1380 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001381 *data_length = 0;
1382
Ronald Cron9486f9d2020-11-26 13:36:23 +01001383 /* Export requires the EXPORT flag. There is an exception for public keys,
1384 * which don't require any flag, but
1385 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1386 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1388 PSA_KEY_USAGE_EXPORT, 0);
1389 if (status != PSA_SUCCESS) {
1390 return status;
1391 }
mohammad160306e79202018-03-28 13:17:44 +03001392
Ronald Crond18b5f82020-11-25 16:46:05 +01001393 psa_key_attributes_t attributes = {
1394 .core = slot->attr
1395 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 status = psa_driver_wrapper_export_key(&attributes,
1397 slot->key.data, slot->key.bytes,
1398 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001399
Ryan Everett1b70a072024-01-04 10:32:49 +00001400 unlock_status = psa_unregister_read(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001403}
1404
Ronald Cron7285cda2020-11-26 14:46:37 +01001405psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001406 const psa_key_attributes_t *attributes,
1407 const uint8_t *key_buffer,
1408 size_t key_buffer_size,
1409 uint8_t *data,
1410 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001412{
Ronald Crond18b5f82020-11-25 16:46:05 +01001413 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001414
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001415 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001416 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1417 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001418 /* Exporting public -> public */
1419 return psa_export_key_buffer_internal(
1420 key_buffer, key_buffer_size,
1421 data, data_size, data_length);
1422 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001423#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001424 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1425 return mbedtls_psa_rsa_export_public_key(attributes,
1426 key_buffer,
1427 key_buffer_size,
1428 data,
1429 data_size,
1430 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001431#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001432 /* We don't know how to convert a private RSA key to public. */
1433 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001434#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001435 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001436 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001437#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001438 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1439 return mbedtls_psa_ecp_export_public_key(attributes,
1440 key_buffer,
1441 key_buffer_size,
1442 data,
1443 data_size,
1444 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001445#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001446 /* We don't know how to convert a private ECC key to public */
1447 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001448#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001449 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001450 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001451#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001452 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001453 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001454 key_buffer,
1455 key_buffer_size,
1456 data, data_size,
1457 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001458#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001459 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001460#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001461 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001463 (void) key_buffer;
1464 (void) key_buffer_size;
1465 (void) data;
1466 (void) data_size;
1467 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001469 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001470}
Ronald Crond18b5f82020-11-25 16:46:05 +01001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1473 uint8_t *data,
1474 size_t data_size,
1475 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001476{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001478 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001479 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001480 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001481
Ronald Crone907e552021-01-18 13:22:38 +01001482 /* Reject a zero-length output buffer now, since this can never be a
1483 * valid key representation. This way we know that data must be a valid
1484 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 if (data_size == 0) {
1486 return PSA_ERROR_BUFFER_TOO_SMALL;
1487 }
Ronald Crone907e552021-01-18 13:22:38 +01001488
Darryl Greendd8fb772018-11-07 16:00:44 +00001489 /* Set the key to empty now, so that even when there are errors, we always
1490 * set data_length to a value between 0 and data_size. On error, setting
1491 * the key to empty is a good choice because an empty key representation is
1492 * unlikely to be accepted anywhere. */
1493 *data_length = 0;
1494
1495 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1497 if (status != PSA_SUCCESS) {
1498 return status;
1499 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1502 status = PSA_ERROR_INVALID_ARGUMENT;
1503 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001504 }
1505
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001506 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001507 .core = slot->attr
1508 };
Ronald Cron67227982020-11-26 15:16:05 +01001509 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001510 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001512
1513exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00001514 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001517}
1518
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001519MBEDTLS_STATIC_ASSERT(
1520 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1521 "One or more key attribute flag is listed as both external-only and dual-use")
1522MBEDTLS_STATIC_ASSERT(
1523 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1524 "One or more key attribute flag is listed as both internal-only and dual-use")
1525MBEDTLS_STATIC_ASSERT(
1526 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1527 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001528
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001529/** Validate that a key policy is internally well-formed.
1530 *
1531 * This function only rejects invalid policies. It does not validate the
1532 * consistency of the policy with respect to other attributes of the key
1533 * such as the key type.
1534 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001535static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001536{
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1538 PSA_KEY_USAGE_COPY |
1539 PSA_KEY_USAGE_ENCRYPT |
1540 PSA_KEY_USAGE_DECRYPT |
1541 PSA_KEY_USAGE_SIGN_MESSAGE |
1542 PSA_KEY_USAGE_VERIFY_MESSAGE |
1543 PSA_KEY_USAGE_SIGN_HASH |
1544 PSA_KEY_USAGE_VERIFY_HASH |
1545 PSA_KEY_USAGE_VERIFY_DERIVATION |
1546 PSA_KEY_USAGE_DERIVE)) != 0) {
1547 return PSA_ERROR_INVALID_ARGUMENT;
1548 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001551}
1552
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001553/** Validate the internal consistency of key attributes.
1554 *
1555 * This function only rejects invalid attribute values. If does not
1556 * validate the consistency of the attributes with any key data that may
1557 * be involved in the creation of the key.
1558 *
1559 * Call this function early in the key creation process.
1560 *
1561 * \param[in] attributes Key attributes for the new key.
1562 * \param[out] p_drv On any return, the driver for the key, if any.
1563 * NULL for a transparent key.
1564 *
1565 */
1566static psa_status_t psa_validate_key_attributes(
1567 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001569{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001570 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1572 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 status = psa_validate_key_location(lifetime, p_drv);
1575 if (status != PSA_SUCCESS) {
1576 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001577 }
1578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 status = psa_validate_key_persistence(lifetime);
1580 if (status != PSA_SUCCESS) {
1581 return status;
1582 }
1583
1584 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1585 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1586 return PSA_ERROR_INVALID_ARGUMENT;
1587 }
1588 } else {
1589 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1590 return PSA_ERROR_INVALID_ARGUMENT;
1591 }
1592 }
1593
1594 status = psa_validate_key_policy(&attributes->core.policy);
1595 if (status != PSA_SUCCESS) {
1596 return status;
1597 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001598
1599 /* Refuse to create overly large keys.
1600 * Note that this doesn't trigger on import if the attributes don't
1601 * explicitly specify a size (so psa_get_key_bits returns 0), so
1602 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1604 return PSA_ERROR_NOT_SUPPORTED;
1605 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001606
Gilles Peskine91e8c332019-08-02 19:19:39 +02001607 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1609 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1610 return PSA_ERROR_INVALID_ARGUMENT;
1611 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001612
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001614}
1615
Gilles Peskine4747d192019-04-17 15:05:45 +02001616/** Prepare a key slot to receive key material.
1617 *
1618 * This function allocates a key slot and sets its metadata.
1619 *
1620 * If this function fails, call psa_fail_key_creation().
1621 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001622 * This function is intended to be used as follows:
1623 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001624 * it with the specified attributes, and in case of a volatile key assign it
1625 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001626 * -# Populate the slot with the key material.
1627 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1628 * In case of failure at any step, stop the sequence and call
1629 * psa_fail_key_creation().
1630 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001631 * On success, the key slot's state is PSA_SLOT_FILLING.
1632 * It is the responsibility of the caller to change the slot's state to
1633 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001634 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001635 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001636 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001637 * \param[out] p_slot On success, a pointer to the prepared slot.
1638 * \param[out] p_drv On any return, the driver for the key, if any.
1639 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001640 *
1641 * \retval #PSA_SUCCESS
1642 * The key slot is ready to receive key material.
1643 * \return If this function fails, the key slot is an invalid state.
1644 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001645 */
1646static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001647 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001648 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001649 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001651{
1652 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001653 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001654 psa_key_slot_t *slot;
1655
Gilles Peskinedf179142019-07-15 22:02:14 +02001656 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001657 *p_drv = NULL;
1658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 status = psa_validate_key_attributes(attributes, p_drv);
1660 if (status != PSA_SUCCESS) {
1661 return status;
1662 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001663
Ryan Everettb69118e2024-01-02 15:54:32 +00001664 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (status != PSA_SUCCESS) {
1666 return status;
1667 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001668 slot = *p_slot;
1669
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001670 /* We're storing the declared bit-size of the key. It's up to each
1671 * creation mechanism to verify that this information is correct.
1672 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001673 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001674 * is optional (import, copy). In case of a volatile key, assign it the
1675 * volatile key identifier associated to the slot returned to contain its
1676 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001677
1678 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001680#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1681 slot->attr.id = volatile_key_id;
1682#else
1683 slot->attr.id.key_id = volatile_key_id;
1684#endif
1685 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001686
Gilles Peskine91e8c332019-08-02 19:19:39 +02001687 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001688 * external-only flags, query `attributes`. Thanks to the check
1689 * in psa_validate_key_attributes(), this leaves the dual-use
Ryan Everettb69118e2024-01-02 15:54:32 +00001690 * flags and any internal flag that psa_reserve_free_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001691 * may have set. */
1692 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001693
Gilles Peskinecbaff462019-07-12 23:46:04 +02001694#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001695 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001696 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001697 * create the key file in internal storage, create the
1698 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001699 * persistent data. This is done by starting a transaction that will
1700 * encompass these three actions.
1701 * For registering a volatile key, we just need to find an appropriate
1702 * slot number inside the SE. Since the key is designated volatile, creating
1703 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001704 /* The first thing to do is to find a slot number for the new key.
1705 * We save the slot number in persistent storage as part of the
1706 * transaction data. It will be needed to recover if the power
1707 * fails during the key creation process, to clean up on the secure
1708 * element side after restarting. Obtaining a slot number from the
1709 * secure element driver updates its persistent state, but we do not yet
1710 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001711 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001713 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1715 &slot_number);
1716 if (status != PSA_SUCCESS) {
1717 return status;
1718 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1721 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001722 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001723 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001724 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 status = psa_crypto_save_transaction();
1726 if (status != PSA_SUCCESS) {
1727 (void) psa_crypto_stop_transaction();
1728 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001729 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001730 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001731
1732 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001734 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001735
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001737 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001739 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001740#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001743}
Gilles Peskine4747d192019-04-17 15:05:45 +02001744
1745/** Finalize the creation of a key once its key material has been set.
1746 *
1747 * This entails writing the key to persistent storage.
1748 *
1749 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001750 * See the documentation of psa_start_key_creation() for the intended use
1751 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001752 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001753 * If the finalization succeeds, the function sets the key slot's state to
1754 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1755 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001756 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001757 * \param[in,out] slot Pointer to the slot with key material.
1758 * \param[in] driver The secure element driver for the key,
1759 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001760 * \param[out] key On success, identifier of the key. Note that the
1761 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001762 *
1763 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001764 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001765 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1766 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1767 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1768 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1769 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1770 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001771 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001772 * \return If this function fails, the key slot is an invalid state.
1773 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001774 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001775static psa_status_t psa_finish_key_creation(
1776 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001777 psa_se_drv_table_entry_t *driver,
1778 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001779{
1780 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001781 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001782 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001783
1784#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001786#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001788 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001789 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001791
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001792 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1793 sizeof(data.slot_number),
1794 "Slot number size does not match psa_se_key_data_storage_t");
1795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1797 status = psa_save_persistent_key(&slot->attr,
1798 (uint8_t *) &data,
1799 sizeof(data));
1800 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001801#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1802 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001803 /* Key material is saved in export representation in the slot, so
1804 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 status = psa_save_persistent_key(&slot->attr,
1806 slot->key.data,
1807 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001808 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001809 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001810#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1811
Gilles Peskinecbaff462019-07-12 23:46:04 +02001812#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001813 /* Finish the transaction for a key creation. This does not
1814 * happen when registering an existing key. Detect this case
1815 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001816 * creation of a persistent key in a secure element requires a transaction,
1817 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 if (driver != NULL &&
1819 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1820 status = psa_save_se_persistent_data(driver);
1821 if (status != PSA_SUCCESS) {
1822 psa_destroy_persistent_key(slot->attr.id);
1823 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001824 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001826 }
1827#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001830 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001831 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1832 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001834 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001836 }
Ronald Cron50972942020-11-14 11:28:25 +01001837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001839}
1840
1841/** Abort the creation of a key.
1842 *
1843 * You may call this function after calling psa_start_key_creation(),
1844 * or after psa_finish_key_creation() fails. In other circumstances, this
1845 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001846 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001847 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001848 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001849 * \param[in,out] slot Pointer to the slot with key material.
1850 * \param[in] driver The secure element driver for the key,
1851 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001852 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001853static void psa_fail_key_creation(psa_key_slot_t *slot,
1854 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001855{
Gilles Peskine011e4282019-06-26 18:34:38 +02001856 (void) driver;
1857
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001859 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001861
1862#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001863 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001864 * element, and the failure happened later (when saving metadata
1865 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001866 * element.
1867 * https://github.com/ARMmbed/mbed-crypto/issues/217
1868 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001869
Gilles Peskined7729582019-08-05 15:55:54 +02001870 /* Abort the ongoing transaction if any (there may not be one if
1871 * the creation process failed before starting one, or if the
1872 * key creation is a registration of a key in a secure element).
1873 * Earlier functions must already have done what it takes to undo any
1874 * partial creation. All that's left is to update the transaction data
1875 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001877#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001880}
1881
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001882/** Validate optional attributes during key creation.
1883 *
1884 * Some key attributes are optional during key creation. If they are
1885 * specified in the attributes structure, check that they are consistent
1886 * with the data in the slot.
1887 *
1888 * This function should be called near the end of key creation, after
1889 * the slot in memory is fully populated but before saving persistent data.
1890 */
1891static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001892 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001894{
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 if (attributes->core.type != 0) {
1896 if (attributes->core.type != slot->attr.type) {
1897 return PSA_ERROR_INVALID_ARGUMENT;
1898 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001899 }
1900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001902#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1903 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1905 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001906 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001907 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001908 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001909
Ronald Cron90857082020-11-25 14:58:33 +01001910 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 slot->attr.type,
1912 slot->key.data,
1913 slot->key.bytes,
1914 &rsa);
1915 if (status != PSA_SUCCESS) {
1916 return status;
1917 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 mbedtls_mpi_init(&actual);
1920 mbedtls_mpi_init(&required);
1921 ret = mbedtls_rsa_export(rsa,
1922 NULL, NULL, NULL, NULL, &actual);
1923 mbedtls_rsa_free(rsa);
1924 mbedtls_free(rsa);
1925 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001926 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 }
1928 ret = mbedtls_mpi_read_binary(&required,
1929 attributes->domain_parameters,
1930 attributes->domain_parameters_size);
1931 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001932 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 }
1934 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001935 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 }
1937rsa_exit:
1938 mbedtls_mpi_free(&actual);
1939 mbedtls_mpi_free(&required);
1940 if (ret != 0) {
1941 return mbedtls_to_psa_error(ret);
1942 }
1943 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02001944#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
1945 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001946 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001947 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001949 }
1950 }
1951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 if (attributes->core.bits != 0) {
1953 if (attributes->core.bits != slot->attr.bits) {
1954 return PSA_ERROR_INVALID_ARGUMENT;
1955 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001956 }
1957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001959}
1960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1962 const uint8_t *data,
1963 size_t data_length,
1964 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001965{
1966 psa_status_t status;
1967 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001968 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001969 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301970 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001971
Ronald Cron81709fc2020-11-14 12:10:32 +01001972 *key = MBEDTLS_SVC_KEY_ID_INIT;
1973
Gilles Peskine0f84d622019-09-12 19:03:13 +02001974 /* Reject zero-length symmetric keys (including raw data key objects).
1975 * This also rejects any key which might be encoded as an empty string,
1976 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 if (data_length == 0) {
1978 return PSA_ERROR_INVALID_ARGUMENT;
1979 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001980
Archana449608b2021-09-08 15:36:05 +05301981 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 if (data_length > SIZE_MAX / 8) {
1983 return PSA_ERROR_NOT_SUPPORTED;
1984 }
Archana6ed4bda2021-08-04 10:47:15 +05301985
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1987 &slot, &driver);
1988 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001989 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001991
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001992 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301993 * storage ( thus not in the case of importing a key in a secure element
1994 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1995 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 if (slot->key.data == NULL) {
1997 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301998 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 attributes, data, data_length, &storage_size);
2000 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302001 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 }
Archanad8a83dc2021-06-14 10:04:16 +05302003 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 status = psa_allocate_buffer_to_slot(slot, storage_size);
2005 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002006 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002008 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002009
2010 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 status = psa_driver_wrapper_import_key(attributes,
2012 data, data_length,
2013 slot->key.data,
2014 slot->key.bytes,
2015 &slot->key.bytes, &bits);
2016 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002017 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002021 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002023 status = PSA_ERROR_INVALID_ARGUMENT;
2024 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002025 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002026
Archana6ed4bda2021-08-04 10:47:15 +05302027 /* Enforce a size limit, and in particular ensure that the bit
2028 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302030 status = PSA_ERROR_NOT_SUPPORTED;
2031 goto exit;
2032 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 status = psa_validate_optional_attributes(slot, attributes);
2034 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002035 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002039exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 if (status != PSA_SUCCESS) {
2041 psa_fail_key_creation(slot, driver);
2042 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002045}
2046
Gilles Peskined7729582019-08-05 15:55:54 +02002047#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2048psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002050{
2051 psa_status_t status;
2052 psa_key_slot_t *slot = NULL;
2053 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002054 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002055
2056 /* Leaving attributes unspecified is not currently supported.
2057 * It could make sense to query the key type and size from the
2058 * secure element, but not all secure elements support this
2059 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2061 return PSA_ERROR_NOT_SUPPORTED;
2062 }
2063 if (psa_get_key_bits(attributes) == 0) {
2064 return PSA_ERROR_NOT_SUPPORTED;
2065 }
Gilles Peskined7729582019-08-05 15:55:54 +02002066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2068 &slot, &driver);
2069 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002070 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 }
Gilles Peskined7729582019-08-05 15:55:54 +02002072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002074
2075exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 if (status != PSA_SUCCESS) {
2077 psa_fail_key_creation(slot, driver);
2078 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002079
Gilles Peskined7729582019-08-05 15:55:54 +02002080 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 psa_close_key(key);
2082 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002083}
2084#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2085
Gilles Peskine449bd832023-01-11 14:50:10 +01002086psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2087 const psa_key_attributes_t *specified_attributes,
2088 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002089{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002090 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002091 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002092 psa_key_slot_t *source_slot = NULL;
2093 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002094 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002095 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302096 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002097
Ronald Cron81709fc2020-11-14 12:10:32 +01002098 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2099
Archana8a180362021-07-05 02:18:48 +05302100 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2102 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 status = psa_validate_optional_attributes(source_slot,
2107 specified_attributes);
2108 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002109 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002111
Archana9d17bf42021-09-10 06:22:44 +05302112 /* The target key type and number of bits have been validated by
2113 * psa_validate_optional_attributes() to be either equal to zero or
2114 * equal to the ones of the source key. So it is safe to inherit
2115 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302116 * */
Archana9d17bf42021-09-10 06:22:44 +05302117 actual_attributes.core.bits = source_slot->attr.bits;
2118 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302119
2120
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 status = psa_restrict_key_policy(source_slot->attr.type,
2122 &actual_attributes.core.policy,
2123 &source_slot->attr.policy);
2124 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002125 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2129 &target_slot, &driver);
2130 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002131 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 }
2133 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2134 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302135 /*
Archana9d17bf42021-09-10 06:22:44 +05302136 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302137 * the source key would need to be exported as plaintext and re-imported
2138 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302139 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302140 * appropriate API invocations from the application, if needed.
2141 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002142 status = PSA_ERROR_NOT_SUPPORTED;
2143 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002144 }
Archana8a180362021-07-05 02:18:48 +05302145 /*
2146 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302147 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302148 * - For opaque keys this translates to an invocation of the drivers'
2149 * copy_key entry point through the dispatch layer.
2150 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2152 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2153 &storage_size);
2154 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302155 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 }
Archana374fe5b2021-09-08 15:50:28 +05302157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2159 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302160 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 }
Archana374fe5b2021-09-08 15:50:28 +05302162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 status = psa_driver_wrapper_copy_key(&actual_attributes,
2164 source_slot->key.data,
2165 source_slot->key.bytes,
2166 target_slot->key.data,
2167 target_slot->key.bytes,
2168 &target_slot->key.bytes);
2169 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302170 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 }
2172 } else {
2173 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302174 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 source_slot->key.bytes);
2176 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302177 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 }
Archana8a180362021-07-05 02:18:48 +05302179 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002181exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 if (status != PSA_SUCCESS) {
2183 psa_fail_key_creation(target_slot, driver);
2184 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002185
Ryan Everett1b70a072024-01-04 10:32:49 +00002186 unlock_status = psa_unregister_read(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002189}
2190
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002191
2192
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002193/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002194/* Message digests */
2195/****************************************************************/
2196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002198{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002199 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (operation->id == 0) {
2201 return PSA_SUCCESS;
2202 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002205 operation->id = 0;
2206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002208}
2209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2211 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002212{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002214
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002215 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002217 status = PSA_ERROR_BAD_STATE;
2218 goto exit;
2219 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002222 status = PSA_ERROR_INVALID_ARGUMENT;
2223 goto exit;
2224 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002225
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002226 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2227 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002231
2232exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 if (status != PSA_SUCCESS) {
2234 psa_hash_abort(operation);
2235 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002236
2237 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238}
2239
Gilles Peskine449bd832023-01-11 14:50:10 +01002240psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2241 const uint8_t *input,
2242 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002243{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002244 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002247 status = PSA_ERROR_BAD_STATE;
2248 goto exit;
2249 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002250
Steven Cooremanc8288352021-03-04 14:02:19 +01002251 /* Don't require hash implementations to behave correctly on a
2252 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 if (input_length == 0) {
2254 return PSA_SUCCESS;
2255 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002258
2259exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 if (status != PSA_SUCCESS) {
2261 psa_hash_abort(operation);
2262 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002265}
2266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2268 uint8_t *hash,
2269 size_t hash_size,
2270 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002272 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (operation->id == 0) {
2274 return PSA_ERROR_BAD_STATE;
2275 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002276
Steven Cooreman1e582352021-02-18 17:24:37 +01002277 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 operation, hash, hash_size, hash_length);
2279 psa_hash_abort(operation);
2280 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002281}
2282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2284 const uint8_t *hash,
2285 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286{
Ronald Cron69a63422021-10-18 09:47:58 +02002287 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002289 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 operation,
2291 actual_hash, sizeof(actual_hash),
2292 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002295 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002299 status = PSA_ERROR_INVALID_SIGNATURE;
2300 goto exit;
2301 }
2302
Dave Rodgman78701152023-08-29 14:20:18 +01002303 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002304 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002306
2307exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2309 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002310 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002312
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314}
2315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316psa_status_t psa_hash_compute(psa_algorithm_t alg,
2317 const uint8_t *input, size_t input_length,
2318 uint8_t *hash, size_t hash_size,
2319 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002320{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002321 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 if (!PSA_ALG_IS_HASH(alg)) {
2323 return PSA_ERROR_INVALID_ARGUMENT;
2324 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2327 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002328}
2329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330psa_status_t psa_hash_compare(psa_algorithm_t alg,
2331 const uint8_t *input, size_t input_length,
2332 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002333{
Ronald Cron69a63422021-10-18 09:47:58 +02002334 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002335 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (!PSA_ALG_IS_HASH(alg)) {
2338 return PSA_ERROR_INVALID_ARGUMENT;
2339 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002340
Steven Cooreman1e582352021-02-18 17:24:37 +01002341 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 alg, input, input_length,
2343 actual_hash, sizeof(actual_hash),
2344 &actual_hash_length);
2345 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002346 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 }
2348 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002349 status = PSA_ERROR_INVALID_SIGNATURE;
2350 goto exit;
2351 }
Dave Rodgman78701152023-08-29 14:20:18 +01002352 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002353 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002355
2356exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2358 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002359}
2360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2362 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002363{
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 if (source_operation->id == 0 ||
2365 target_operation->id != 0) {
2366 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002367 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2370 target_operation);
2371 if (status != PSA_SUCCESS) {
2372 psa_hash_abort(target_operation);
2373 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002376}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002377
2378
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002379/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002380/* MAC */
2381/****************************************************************/
2382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002384{
Steven Cooremane6804192021-03-19 18:28:56 +01002385 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 if (operation->id == 0) {
2387 return PSA_SUCCESS;
2388 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002391 operation->mac_size = 0;
2392 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002393 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002396}
2397
Ronald Cron2dff3b22021-06-17 16:33:22 +02002398static psa_status_t psa_mac_finalize_alg_and_key_validation(
2399 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002400 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002402{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002403 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 psa_key_type_t key_type = psa_get_key_type(attributes);
2405 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (!PSA_ALG_IS_MAC(alg)) {
2408 return PSA_ERROR_INVALID_ARGUMENT;
2409 }
Steven Cooremane6804192021-03-19 18:28:56 +01002410
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002411 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 status = psa_mac_key_can_do(alg, key_type);
2413 if (status != PSA_SUCCESS) {
2414 return status;
2415 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002416
Steven Cooremand1a68f12021-05-10 11:13:41 +02002417 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002421 /* A very short MAC is too short for security since it can be
2422 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2423 * so we make this our minimum, even though 32 bits is still
2424 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002426 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2429 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002430 /* It's impossible to "truncate" to a larger length than the full length
2431 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002433 }
2434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002436 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2437 * that is disabled in the compile-time configuration. The result can
2438 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2439 * configuration into account. In this case, force a return of
2440 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2441 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2442 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2443 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2444 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002446 }
2447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002449}
2450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2452 mbedtls_svc_key_id_t key,
2453 psa_algorithm_t alg,
2454 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002455{
2456 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2457 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002458 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002459 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002460
2461 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002463 status = PSA_ERROR_BAD_STATE;
2464 goto exit;
2465 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002466
2467 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 key,
2469 &slot,
2470 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2471 alg);
2472 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002473 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002475
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002476 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002477 .core = slot->attr
2478 };
2479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2481 &operation->mac_size);
2482 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485
2486 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002487 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 if (is_sign) {
2489 status = psa_driver_wrapper_mac_sign_setup(operation,
2490 &attributes,
2491 slot->key.data,
2492 slot->key.bytes,
2493 alg);
2494 } else {
2495 status = psa_driver_wrapper_mac_verify_setup(operation,
2496 &attributes,
2497 slot->key.data,
2498 slot->key.bytes,
2499 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002501
Gilles Peskinefbfac682018-07-08 20:51:54 +02002502exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if (status != PSA_SUCCESS) {
2504 psa_mac_abort(operation);
2505 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002506
Ryan Everett1b70a072024-01-04 10:32:49 +00002507 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002510}
2511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2513 mbedtls_svc_key_id_t key,
2514 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002515{
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002517}
2518
Gilles Peskine449bd832023-01-11 14:50:10 +01002519psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2520 mbedtls_svc_key_id_t key,
2521 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002522{
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002524}
2525
Gilles Peskine449bd832023-01-11 14:50:10 +01002526psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2527 const uint8_t *input,
2528 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002529{
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (operation->id == 0) {
2531 return PSA_ERROR_BAD_STATE;
2532 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002533
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002534 /* Don't require hash implementations to behave correctly on a
2535 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (input_length == 0) {
2537 return PSA_SUCCESS;
2538 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2541 input, input_length);
2542 if (status != PSA_SUCCESS) {
2543 psa_mac_abort(operation);
2544 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002547}
2548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2550 uint8_t *mac,
2551 size_t mac_size,
2552 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002553{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002554 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2555 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002558 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002559 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002560 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002563 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002564 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002565 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002566
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002567 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2568 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002570 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002571 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002572 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002573
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002575 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002576 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002577 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 status = psa_driver_wrapper_mac_sign_finish(operation,
2580 mac, operation->mac_size,
2581 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002582
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002583exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002584 /* In case of success, set the potential excess room in the output buffer
2585 * to an invalid value, to avoid potentially leaking a longer MAC.
2586 * In case of error, set the output length and content to a safe default,
2587 * such that in case the caller misses an error check, the output would be
2588 * an unachievable MAC.
2589 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002591 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002592 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002593 }
mohammad16036df908f2018-04-02 08:34:15 -07002594
Paul Elliottdc42ca82023-02-24 18:11:59 +00002595 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002600}
2601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2603 const uint8_t *mac,
2604 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002606 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2607 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002610 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002611 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002612 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002615 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002616 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002617 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002618
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002620 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002621 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002622 }
2623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 status = psa_driver_wrapper_mac_verify_finish(operation,
2625 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002626
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002627exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002631}
2632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2634 psa_algorithm_t alg,
2635 const uint8_t *input,
2636 size_t input_length,
2637 uint8_t *mac,
2638 size_t mac_size,
2639 size_t *mac_length,
2640 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002641{
2642 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002643 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2644 psa_key_slot_t *slot;
2645 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002646 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002647
Ronald Cron51131b52021-06-17 17:17:20 +02002648 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 key,
2650 &slot,
2651 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2652 alg);
2653 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002654 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002656
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002657 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002658 .core = slot->attr
2659 };
2660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2662 &operation_mac_size);
2663 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002664 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002668 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002669 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002670 }
2671
2672 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 &attributes,
2674 slot->key.data, slot->key.bytes,
2675 alg,
2676 input, input_length,
2677 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002678
2679exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002680 /* In case of success, set the potential excess room in the output buffer
2681 * to an invalid value, to avoid potentially leaking a longer MAC.
2682 * In case of error, set the output length and content to a safe default,
2683 * such that in case the caller misses an error check, the output would be
2684 * an unachievable MAC.
2685 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002687 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002688 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002689 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002690
2691 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002692
Ryan Everett1b70a072024-01-04 10:32:49 +00002693 unlock_status = psa_unregister_read(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002696}
2697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002699 psa_algorithm_t alg,
2700 const uint8_t *input,
2701 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 uint8_t *mac,
2703 size_t mac_size,
2704 size_t *mac_length)
2705{
2706 return psa_mac_compute_internal(key, alg,
2707 input, input_length,
2708 mac, mac_size, mac_length, 1);
2709}
2710
2711psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2712 psa_algorithm_t alg,
2713 const uint8_t *input,
2714 size_t input_length,
2715 const uint8_t *mac,
2716 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002717{
2718 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002719 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2720 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 status = psa_mac_compute_internal(key, alg,
2723 input, input_length,
2724 actual_mac, sizeof(actual_mac),
2725 &actual_mac_length, 0);
2726 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002727 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002731 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002732 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002733 }
Dave Rodgman78701152023-08-29 14:20:18 +01002734 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002735 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002736 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002737 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002738
2739exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002743}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002744
Gilles Peskinee59236f2018-01-27 23:32:46 +01002745/****************************************************************/
2746/* Asymmetric cryptography */
2747/****************************************************************/
2748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2750 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002751{
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 if (input_is_message) {
2753 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2754 return PSA_ERROR_INVALID_ARGUMENT;
2755 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002756
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2758 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2759 return PSA_ERROR_INVALID_ARGUMENT;
2760 }
2761 }
2762 } else {
2763 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2764 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002765 }
2766 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002767
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002769}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2772 int input_is_message,
2773 psa_algorithm_t alg,
2774 const uint8_t *input,
2775 size_t input_length,
2776 uint8_t *signature,
2777 size_t signature_size,
2778 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002779{
2780 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2781 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2782 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002783 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002784
2785 *signature_length = 0;
2786
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 status = psa_sign_verify_check_alg(input_is_message, alg);
2788 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002789 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002791
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002792 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002793 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002794 * buffer can in principle be empty since it doesn't actually have
2795 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 if (signature_size == 0) {
2797 return PSA_ERROR_BUFFER_TOO_SMALL;
2798 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002799
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002800 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 key, &slot,
2802 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2803 PSA_KEY_USAGE_SIGN_HASH,
2804 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002805
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002807 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002811 status = PSA_ERROR_INVALID_ARGUMENT;
2812 goto exit;
2813 }
2814
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002815 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002817 };
2818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002820 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002821 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002822 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 signature, signature_size, signature_length);
2824 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002825
2826 status = psa_driver_wrapper_sign_hash(
2827 &attributes, slot->key.data, slot->key.bytes,
2828 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002830 }
2831
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002832
2833exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002834 psa_wipe_tag_output_buffer(signature, status, signature_size,
2835 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002836
Ryan Everett1b70a072024-01-04 10:32:49 +00002837 unlock_status = psa_unregister_read(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002840}
2841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2843 int input_is_message,
2844 psa_algorithm_t alg,
2845 const uint8_t *input,
2846 size_t input_length,
2847 const uint8_t *signature,
2848 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002849{
2850 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2851 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2852 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002853
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 status = psa_sign_verify_check_alg(input_is_message, alg);
2855 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002856 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002858
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002859 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 key, &slot,
2861 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2862 PSA_KEY_USAGE_VERIFY_HASH,
2863 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 if (status != PSA_SUCCESS) {
2866 return status;
2867 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002868
2869 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002871 };
2872
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002874 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002875 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002876 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 signature, signature_length);
2878 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002879 status = psa_driver_wrapper_verify_hash(
2880 &attributes, slot->key.data, slot->key.bytes,
2881 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002883 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002884
Ryan Everett1b70a072024-01-04 10:32:49 +00002885 unlock_status = psa_unregister_read(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002886
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002888
2889}
2890
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002891psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002892 const psa_key_attributes_t *attributes,
2893 const uint8_t *key_buffer,
2894 size_t key_buffer_size,
2895 psa_algorithm_t alg,
2896 const uint8_t *input,
2897 size_t input_length,
2898 uint8_t *signature,
2899 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002901{
2902 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002903
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002905 size_t hash_length;
2906 uint8_t hash[PSA_HASH_MAX_SIZE];
2907
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002908 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 PSA_ALG_SIGN_GET_HASH(alg),
2910 input, input_length,
2911 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002914 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002916
2917 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 attributes, key_buffer, key_buffer_size,
2919 alg, hash, hash_length,
2920 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002921 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002924}
2925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2927 psa_algorithm_t alg,
2928 const uint8_t *input,
2929 size_t input_length,
2930 uint8_t *signature,
2931 size_t signature_size,
2932 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002933{
2934 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002935 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002937}
2938
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002939psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002940 const psa_key_attributes_t *attributes,
2941 const uint8_t *key_buffer,
2942 size_t key_buffer_size,
2943 psa_algorithm_t alg,
2944 const uint8_t *input,
2945 size_t input_length,
2946 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002948{
2949 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002950
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002952 size_t hash_length;
2953 uint8_t hash[PSA_HASH_MAX_SIZE];
2954
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002955 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 PSA_ALG_SIGN_GET_HASH(alg),
2957 input, input_length,
2958 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002959
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002961 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002963
2964 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 attributes, key_buffer, key_buffer_size,
2966 alg, hash, hash_length,
2967 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002968 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002971}
2972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2974 psa_algorithm_t alg,
2975 const uint8_t *input,
2976 size_t input_length,
2977 const uint8_t *signature,
2978 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002979{
2980 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002981 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002983}
2984
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002985psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002986 const psa_key_attributes_t *attributes,
2987 const uint8_t *key_buffer, size_t key_buffer_size,
2988 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002990{
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2992 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2993 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002994#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2996 return mbedtls_psa_rsa_sign_hash(
2997 attributes,
2998 key_buffer, key_buffer_size,
2999 alg, hash, hash_length,
3000 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003001#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3002 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 } else {
3004 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003005 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3007 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003008#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3010 return mbedtls_psa_ecdsa_sign_hash(
3011 attributes,
3012 key_buffer, key_buffer_size,
3013 alg, hash, hash_length,
3014 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003015#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3016 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 } else {
3018 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003019 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003020 }
Ronald Cron4501c982021-03-19 20:09:32 +01003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 (void) key_buffer;
3023 (void) key_buffer_size;
3024 (void) hash;
3025 (void) hash_length;
3026 (void) signature;
3027 (void) signature_size;
3028 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003031}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3034 psa_algorithm_t alg,
3035 const uint8_t *hash,
3036 size_t hash_length,
3037 uint8_t *signature,
3038 size_t signature_size,
3039 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003040{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003041 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003042 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003044}
3045
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003046psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003047 const psa_key_attributes_t *attributes,
3048 const uint8_t *key_buffer, size_t key_buffer_size,
3049 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003051{
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3053 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3054 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003055#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3057 return mbedtls_psa_rsa_verify_hash(
3058 attributes,
3059 key_buffer, key_buffer_size,
3060 alg, hash, hash_length,
3061 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003062#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3063 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 } else {
3065 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003066 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3068 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003069#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3071 return mbedtls_psa_ecdsa_verify_hash(
3072 attributes,
3073 key_buffer, key_buffer_size,
3074 alg, hash, hash_length,
3075 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003076#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3077 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 } else {
3079 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003080 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003081 }
Ronald Cron4501c982021-03-19 20:09:32 +01003082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 (void) key_buffer;
3084 (void) key_buffer_size;
3085 (void) hash;
3086 (void) hash_length;
3087 (void) signature;
3088 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003091}
3092
Gilles Peskine449bd832023-01-11 14:50:10 +01003093psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3094 psa_algorithm_t alg,
3095 const uint8_t *hash,
3096 size_t hash_length,
3097 const uint8_t *signature,
3098 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003099{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003100 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003101 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003103}
3104
Gilles Peskine449bd832023-01-11 14:50:10 +01003105psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3106 psa_algorithm_t alg,
3107 const uint8_t *input,
3108 size_t input_length,
3109 const uint8_t *salt,
3110 size_t salt_length,
3111 uint8_t *output,
3112 size_t output_size,
3113 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003114{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003115 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003116 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003117 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003118 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003119
Darryl Green5cc689a2018-07-24 15:34:10 +01003120 (void) input;
3121 (void) input_length;
3122 (void) salt;
3123 (void) output;
3124 (void) output_size;
3125
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003126 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003127
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3129 return PSA_ERROR_INVALID_ARGUMENT;
3130 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003131
Valerio Setti5bb454a2024-01-15 10:43:16 +01003132 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3134 if (status != PSA_SUCCESS) {
3135 return status;
3136 }
3137 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3138 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003139 status = PSA_ERROR_INVALID_ARGUMENT;
3140 goto exit;
3141 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003142
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003143 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003145 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003146
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003147 status = psa_driver_wrapper_asymmetric_encrypt(
3148 &attributes, slot->key.data, slot->key.bytes,
3149 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003151exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00003152 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003153
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003155}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003156
Gilles Peskine449bd832023-01-11 14:50:10 +01003157psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3158 psa_algorithm_t alg,
3159 const uint8_t *input,
3160 size_t input_length,
3161 const uint8_t *salt,
3162 size_t salt_length,
3163 uint8_t *output,
3164 size_t output_size,
3165 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003167 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003168 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003169 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003170 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003171
Darryl Green5cc689a2018-07-24 15:34:10 +01003172 (void) input;
3173 (void) input_length;
3174 (void) salt;
3175 (void) output;
3176 (void) output_size;
3177
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003178 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003179
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3181 return PSA_ERROR_INVALID_ARGUMENT;
3182 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003183
Valerio Setti5bb454a2024-01-15 10:43:16 +01003184 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3186 if (status != PSA_SUCCESS) {
3187 return status;
3188 }
3189 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003190 status = PSA_ERROR_INVALID_ARGUMENT;
3191 goto exit;
3192 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003193
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003194 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003196 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003197
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003198 status = psa_driver_wrapper_asymmetric_decrypt(
3199 &attributes, slot->key.data, slot->key.bytes,
3200 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003202
3203exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00003204 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003205
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003207}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003208
Paul Elliott9fe12f62022-11-30 19:16:02 +00003209/****************************************************************/
3210/* Asymmetric interruptible cryptography */
3211/****************************************************************/
3212
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003213static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3214
Paul Elliott9fe12f62022-11-30 19:16:02 +00003215void psa_interruptible_set_max_ops(uint32_t max_ops)
3216{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003217 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003218}
3219
3220uint32_t psa_interruptible_get_max_ops(void)
3221{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003222 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003223}
3224
Paul Elliott9fe12f62022-11-30 19:16:02 +00003225uint32_t psa_sign_hash_get_num_ops(
3226 const psa_sign_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
3231uint32_t psa_verify_hash_get_num_ops(
3232 const psa_verify_hash_interruptible_operation_t *operation)
3233{
Paul Elliott296ede92022-12-15 17:00:30 +00003234 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003235}
3236
Paul Elliott59ad9452022-12-18 15:09:02 +00003237static psa_status_t psa_sign_hash_abort_internal(
3238 psa_sign_hash_interruptible_operation_t *operation)
3239{
3240 if (operation->id == 0) {
3241 /* The object has (apparently) been initialized but it is not (yet)
3242 * in use. It's ok to call abort on such an object, and there's
3243 * nothing to do. */
3244 return PSA_SUCCESS;
3245 }
3246
3247 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3248
3249 status = psa_driver_wrapper_sign_hash_abort(operation);
3250
3251 operation->id = 0;
3252
Paul Elliotte6145dc2023-02-07 12:51:21 +00003253 /* Do not clear either the error_occurred or num_ops elements here as they
3254 * only want to be cleared by the application calling abort, not by abort
3255 * being called at completion of an operation. */
3256
Paul Elliott59ad9452022-12-18 15:09:02 +00003257 return status;
3258}
3259
Paul Elliott9fe12f62022-11-30 19:16:02 +00003260psa_status_t psa_sign_hash_start(
3261 psa_sign_hash_interruptible_operation_t *operation,
3262 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3263 const uint8_t *hash, size_t hash_length)
3264{
3265 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3266 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3267 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003268 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003269
Paul Elliottc9774412023-02-06 15:14:07 +00003270 /* Check that start has not been previously called, or operation has not
3271 * previously errored. */
3272 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003273 return PSA_ERROR_BAD_STATE;
3274 }
3275
Paul Elliott9fe12f62022-11-30 19:16:02 +00003276 status = psa_sign_verify_check_alg(0, alg);
3277 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003278 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003279 return status;
3280 }
3281
3282 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3283 PSA_KEY_USAGE_SIGN_HASH,
3284 alg);
3285
3286 if (status != PSA_SUCCESS) {
3287 goto exit;
3288 }
3289
3290 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3291 status = PSA_ERROR_INVALID_ARGUMENT;
3292 goto exit;
3293 }
3294
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003295 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003296 .core = slot->attr
3297 };
3298
Paul Elliott296ede92022-12-15 17:00:30 +00003299 /* Ensure ops count gets reset, in case of operation re-use. */
3300 operation->num_ops = 0;
3301
Paul Elliott9fe12f62022-11-30 19:16:02 +00003302 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3303 slot->key.data,
3304 slot->key.bytes, alg,
3305 hash, hash_length);
3306exit:
3307
3308 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003309 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003310 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003311 }
3312
Ryan Everett1b70a072024-01-04 10:32:49 +00003313 unlock_status = psa_unregister_read(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003314
Paul Elliottc9774412023-02-06 15:14:07 +00003315 if (unlock_status != PSA_SUCCESS) {
3316 operation->error_occurred = 1;
3317 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003318
Paul Elliottc9774412023-02-06 15:14:07 +00003319 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003320}
3321
3322
3323psa_status_t psa_sign_hash_complete(
3324 psa_sign_hash_interruptible_operation_t *operation,
3325 uint8_t *signature, size_t signature_size,
3326 size_t *signature_length)
3327{
3328 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3329
3330 *signature_length = 0;
3331
Paul Elliottc9774412023-02-06 15:14:07 +00003332 /* Check that start has been called first, and that operation has not
3333 * previously errored. */
3334 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003335 status = PSA_ERROR_BAD_STATE;
3336 goto exit;
3337 }
3338
Paul Elliott096abc42023-02-03 18:33:23 +00003339 /* Immediately reject a zero-length signature buffer. This guarantees that
3340 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003341 if (signature_size == 0) {
3342 status = PSA_ERROR_BUFFER_TOO_SMALL;
3343 goto exit;
3344 }
3345
3346 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3347 signature_size,
3348 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003349
Paul Elliott296ede92022-12-15 17:00:30 +00003350 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003351 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003352
Paul Elliottebe225c2023-02-10 14:32:53 +00003353exit:
3354
Paul Elliott59200a22023-02-21 15:07:40 +00003355 psa_wipe_tag_output_buffer(signature, status, signature_size,
3356 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003357
Paul Elliott53bb3122023-02-10 14:22:22 +00003358 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003359 if (status != PSA_SUCCESS) {
3360 operation->error_occurred = 1;
3361 }
3362
Paul Elliott59ad9452022-12-18 15:09:02 +00003363 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003364 }
3365
3366 return status;
3367}
3368
3369psa_status_t psa_sign_hash_abort(
3370 psa_sign_hash_interruptible_operation_t *operation)
3371{
Paul Elliott59ad9452022-12-18 15:09:02 +00003372 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3373
3374 status = psa_sign_hash_abort_internal(operation);
3375
3376 /* We clear the number of ops done here, so that it is not cleared when
3377 * the operation fails or succeeds, only on manual abort. */
3378 operation->num_ops = 0;
3379
Paul Elliottc9774412023-02-06 15:14:07 +00003380 /* Likewise, failure state. */
3381 operation->error_occurred = 0;
3382
Paul Elliott59ad9452022-12-18 15:09:02 +00003383 return status;
3384}
3385
3386static psa_status_t psa_verify_hash_abort_internal(
3387 psa_verify_hash_interruptible_operation_t *operation)
3388{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003389 if (operation->id == 0) {
3390 /* The object has (apparently) been initialized but it is not (yet)
3391 * in use. It's ok to call abort on such an object, and there's
3392 * nothing to do. */
3393 return PSA_SUCCESS;
3394 }
3395
Paul Elliott59ad9452022-12-18 15:09:02 +00003396 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3397
3398 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003399
3400 operation->id = 0;
3401
Paul Elliotte6145dc2023-02-07 12:51:21 +00003402 /* Do not clear either the error_occurred or num_ops elements here as they
3403 * only want to be cleared by the application calling abort, not by abort
3404 * being called at completion of an operation. */
3405
Paul Elliott59ad9452022-12-18 15:09:02 +00003406 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003407}
3408
3409psa_status_t psa_verify_hash_start(
3410 psa_verify_hash_interruptible_operation_t *operation,
3411 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3412 const uint8_t *hash, size_t hash_length,
3413 const uint8_t *signature, size_t signature_length)
3414{
3415 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3416 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3417 psa_key_slot_t *slot;
3418
Paul Elliottc9774412023-02-06 15:14:07 +00003419 /* Check that start has not been previously called, or operation has not
3420 * previously errored. */
3421 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003422 return PSA_ERROR_BAD_STATE;
3423 }
3424
3425 status = psa_sign_verify_check_alg(0, alg);
3426 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003427 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003428 return status;
3429 }
3430
3431 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3432 PSA_KEY_USAGE_VERIFY_HASH,
3433 alg);
3434
3435 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003436 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003437 return status;
3438 }
3439
3440 psa_key_attributes_t attributes = {
3441 .core = slot->attr
3442 };
3443
Paul Elliott296ede92022-12-15 17:00:30 +00003444 /* Ensure ops count gets reset, in case of operation re-use. */
3445 operation->num_ops = 0;
3446
Paul Elliott9fe12f62022-11-30 19:16:02 +00003447 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3448 slot->key.data,
3449 slot->key.bytes,
3450 alg, hash, hash_length,
3451 signature, signature_length);
3452
3453 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003454 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003455 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003456 }
3457
Ryan Everett1b70a072024-01-04 10:32:49 +00003458 unlock_status = psa_unregister_read(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003459
Paul Elliottc9774412023-02-06 15:14:07 +00003460 if (unlock_status != PSA_SUCCESS) {
3461 operation->error_occurred = 1;
3462 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003463
Paul Elliottc9774412023-02-06 15:14:07 +00003464 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003465}
3466
3467psa_status_t psa_verify_hash_complete(
3468 psa_verify_hash_interruptible_operation_t *operation)
3469{
3470 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3471
Paul Elliottc9774412023-02-06 15:14:07 +00003472 /* Check that start has been called first, and that operation has not
3473 * previously errored. */
3474 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003475 status = PSA_ERROR_BAD_STATE;
3476 goto exit;
3477 }
3478
3479 status = psa_driver_wrapper_verify_hash_complete(operation);
3480
Paul Elliott296ede92022-12-15 17:00:30 +00003481 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003482 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003483 operation);
3484
Paul Elliottebe225c2023-02-10 14:32:53 +00003485exit:
3486
Paul Elliott9fe12f62022-11-30 19:16:02 +00003487 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003488 if (status != PSA_SUCCESS) {
3489 operation->error_occurred = 1;
3490 }
3491
Paul Elliott59ad9452022-12-18 15:09:02 +00003492 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003493 }
3494
3495 return status;
3496}
3497
3498psa_status_t psa_verify_hash_abort(
3499 psa_verify_hash_interruptible_operation_t *operation)
3500{
Paul Elliott59ad9452022-12-18 15:09:02 +00003501 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003502
Paul Elliott59ad9452022-12-18 15:09:02 +00003503 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003504
Paul Elliott59ad9452022-12-18 15:09:02 +00003505 /* We clear the number of ops done here, so that it is not cleared when
3506 * the operation fails or succeeds, only on manual abort. */
3507 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003508
Paul Elliottc9774412023-02-06 15:14:07 +00003509 /* Likewise, failure state. */
3510 operation->error_occurred = 0;
3511
Paul Elliott59ad9452022-12-18 15:09:02 +00003512 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003513}
3514
Paul Elliott588f8ed2022-12-02 18:10:26 +00003515/****************************************************************/
3516/* Asymmetric interruptible cryptography internal */
3517/* implementations */
3518/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003519
Paul Elliott588f8ed2022-12-02 18:10:26 +00003520void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3521{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003522
Paul Elliott588f8ed2022-12-02 18:10:26 +00003523#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3524 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3525 defined(MBEDTLS_ECP_RESTARTABLE)
3526
3527 /* Internal implementation uses zero to indicate infinite number max ops,
3528 * therefore avoid this value, and set to minimum possible. */
3529 if (max_ops == 0) {
3530 max_ops = 1;
3531 }
3532
Paul Elliott588f8ed2022-12-02 18:10:26 +00003533 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003534#else
3535 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003536#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3537 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3538 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3539}
3540
Paul Elliott588f8ed2022-12-02 18:10:26 +00003541uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003542 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003543{
3544#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3545 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3546 defined(MBEDTLS_ECP_RESTARTABLE)
3547
Paul Elliott93d9ca82023-02-15 18:14:21 +00003548 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003549#else
3550 (void) operation;
3551 return 0;
3552#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3553 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3554 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3555}
3556
3557uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003558 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003559{
3560 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3561 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3562 defined(MBEDTLS_ECP_RESTARTABLE)
3563
Paul Elliott93d9ca82023-02-15 18:14:21 +00003564 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003565#else
3566 (void) operation;
3567 return 0;
3568#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3569 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3570 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3571}
3572
3573psa_status_t mbedtls_psa_sign_hash_start(
3574 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3575 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3576 size_t key_buffer_size, psa_algorithm_t alg,
3577 const uint8_t *hash, size_t hash_length)
3578{
3579 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003580 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003581
Paul Elliott068fe072023-01-16 13:59:15 +00003582 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3583 return PSA_ERROR_NOT_SUPPORTED;
3584 }
3585
3586 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003587 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003588 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003589
3590#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003591 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3592 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003593
Paul Elliotta1c94092023-02-15 16:38:04 +00003594 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3595
Paul Elliott93d9ca82023-02-15 18:14:21 +00003596 /* Ensure num_ops is zero'ed in case of context re-use. */
3597 operation->num_ops = 0;
3598
Paul Elliott068fe072023-01-16 13:59:15 +00003599 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3600 attributes->core.bits,
3601 key_buffer,
3602 key_buffer_size,
3603 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003604
Paul Elliott068fe072023-01-16 13:59:15 +00003605 if (status != PSA_SUCCESS) {
3606 return status;
3607 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003608
Paul Elliott1bc59df2023-02-05 13:41:57 +00003609 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003610 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003611
Paul Elliott068fe072023-01-16 13:59:15 +00003612 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003613 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003614 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003615
Paul Elliott0290a762023-02-09 14:30:24 +00003616 /* We only need to store the same length of hash as the private key size
3617 * here, it would be truncated by the internal implementation anyway. */
3618 required_hash_length = (hash_length < operation->coordinate_bytes ?
3619 hash_length : operation->coordinate_bytes);
3620
Paul Elliottba70ad42023-02-15 18:23:53 +00003621 if (required_hash_length > sizeof(operation->hash)) {
3622 /* Shouldn't happen, but better safe than sorry. */
3623 return PSA_ERROR_CORRUPTION_DETECTED;
3624 }
3625
Paul Elliott0290a762023-02-09 14:30:24 +00003626 memcpy(operation->hash, hash, required_hash_length);
3627 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003628
3629 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003630
3631#else
Paul Elliott068fe072023-01-16 13:59:15 +00003632 (void) operation;
3633 (void) key_buffer;
3634 (void) key_buffer_size;
3635 (void) alg;
3636 (void) hash;
3637 (void) hash_length;
3638 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003639 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640
Paul Elliott068fe072023-01-16 13:59:15 +00003641 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003642#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3643 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3644 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003645}
3646
3647psa_status_t mbedtls_psa_sign_hash_complete(
3648 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3649 uint8_t *signature, size_t signature_size,
3650 size_t *signature_length)
3651{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003652#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3653 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3654 defined(MBEDTLS_ECP_RESTARTABLE)
3655
Paul Elliotta1c94092023-02-15 16:38:04 +00003656 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003657 mbedtls_mpi r;
3658 mbedtls_mpi s;
3659
Paul Elliott46845252023-02-03 14:59:11 +00003660 mbedtls_mpi_init(&r);
3661 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003662
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003663 /* Ensure max_ops is set to the current value (or default). */
3664 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3665
Paul Elliotta1c94092023-02-15 16:38:04 +00003666 if (signature_size < 2 * operation->coordinate_bytes) {
3667 status = PSA_ERROR_BUFFER_TOO_SMALL;
3668 goto exit;
3669 }
3670
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003672
Paul Elliott588f8ed2022-12-02 18:10:26 +00003673#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3674 status = mbedtls_to_psa_error(
3675 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003676 &r,
3677 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003678 &operation->ctx->d,
3679 operation->hash,
3680 operation->hash_length,
3681 operation->md_alg,
3682 mbedtls_psa_get_random,
3683 MBEDTLS_PSA_RANDOM_STATE,
3684 &operation->restart_ctx));
3685#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003686 status = PSA_ERROR_NOT_SUPPORTED;
3687 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3689 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003690 status = mbedtls_to_psa_error(
3691 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003692 &r,
3693 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003694 &operation->ctx->d,
3695 operation->hash,
3696 operation->hash_length,
3697 mbedtls_psa_get_random,
3698 MBEDTLS_PSA_RANDOM_STATE,
3699 mbedtls_psa_get_random,
3700 MBEDTLS_PSA_RANDOM_STATE,
3701 &operation->restart_ctx));
3702 }
3703
Paul Elliottf8e5b562023-02-19 18:43:45 +00003704 /* Hide the fact that the restart context only holds a delta of number of
3705 * ops done during the last operation, not an absolute value. */
3706 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3707
Paul Elliott724bd252023-02-08 12:35:08 +00003708 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003709 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003710 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003711 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003712 operation->coordinate_bytes)
3713 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003714
3715 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003716 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003717 }
3718
3719 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003720 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003721 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003722 operation->coordinate_bytes,
3723 operation->coordinate_bytes)
3724 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725
3726 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003727 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003728 }
3729
Paul Elliott1bc59df2023-02-05 13:41:57 +00003730 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731
Paul Elliott724bd252023-02-08 12:35:08 +00003732 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003733 }
Paul Elliott724bd252023-02-08 12:35:08 +00003734
3735exit:
3736
3737 mbedtls_mpi_free(&r);
3738 mbedtls_mpi_free(&s);
3739 return status;
3740
Paul Elliott588f8ed2022-12-02 18:10:26 +00003741 #else
3742
3743 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003744 (void) signature;
3745 (void) signature_size;
3746 (void) signature_length;
3747
3748 return PSA_ERROR_NOT_SUPPORTED;
3749
3750#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3751 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3752 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3753}
3754
3755psa_status_t mbedtls_psa_sign_hash_abort(
3756 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3757{
3758
3759#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3760 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3761 defined(MBEDTLS_ECP_RESTARTABLE)
3762
3763 if (operation->ctx) {
3764 mbedtls_ecdsa_free(operation->ctx);
3765 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003766 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767 }
3768
3769 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3770
Paul Elliott93d9ca82023-02-15 18:14:21 +00003771 operation->num_ops = 0;
3772
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773 return PSA_SUCCESS;
3774
3775#else
3776
3777 (void) operation;
3778
3779 return PSA_ERROR_NOT_SUPPORTED;
3780
3781#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3782 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3783 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3784}
3785
3786psa_status_t mbedtls_psa_verify_hash_start(
3787 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3788 const psa_key_attributes_t *attributes,
3789 const uint8_t *key_buffer, size_t key_buffer_size,
3790 psa_algorithm_t alg,
3791 const uint8_t *hash, size_t hash_length,
3792 const uint8_t *signature, size_t signature_length)
3793{
3794 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003795 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003796 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003797
Paul Elliott068fe072023-01-16 13:59:15 +00003798 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3799 return PSA_ERROR_NOT_SUPPORTED;
3800 }
3801
3802 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003803 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003804 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805
3806#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003807 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3808 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003809
Paul Elliotta1c94092023-02-15 16:38:04 +00003810 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3811 mbedtls_mpi_init(&operation->r);
3812 mbedtls_mpi_init(&operation->s);
3813
Paul Elliott93d9ca82023-02-15 18:14:21 +00003814 /* Ensure num_ops is zero'ed in case of context re-use. */
3815 operation->num_ops = 0;
3816
Paul Elliott068fe072023-01-16 13:59:15 +00003817 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3818 attributes->core.bits,
3819 key_buffer,
3820 key_buffer_size,
3821 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003822
Paul Elliott068fe072023-01-16 13:59:15 +00003823 if (status != PSA_SUCCESS) {
3824 return status;
3825 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826
Paul Elliottc569fc22023-02-10 13:02:54 +00003827 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003828
Paul Elliott1bc59df2023-02-05 13:41:57 +00003829 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003830 return PSA_ERROR_INVALID_SIGNATURE;
3831 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003832
Paul Elliott068fe072023-01-16 13:59:15 +00003833 status = mbedtls_to_psa_error(
3834 mbedtls_mpi_read_binary(&operation->r,
3835 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003836 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003837
Paul Elliott068fe072023-01-16 13:59:15 +00003838 if (status != PSA_SUCCESS) {
3839 return status;
3840 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003841
Paul Elliott068fe072023-01-16 13:59:15 +00003842 status = mbedtls_to_psa_error(
3843 mbedtls_mpi_read_binary(&operation->s,
3844 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003845 coordinate_bytes,
3846 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003847
Paul Elliott068fe072023-01-16 13:59:15 +00003848 if (status != PSA_SUCCESS) {
3849 return status;
3850 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851
Paul Elliott2c9843f2023-02-15 17:32:42 +00003852 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853
Paul Elliott2c9843f2023-02-15 17:32:42 +00003854 if (status != PSA_SUCCESS) {
3855 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003856 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003857
Paul Elliott0290a762023-02-09 14:30:24 +00003858 /* We only need to store the same length of hash as the private key size
3859 * here, it would be truncated by the internal implementation anyway. */
3860 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3861 coordinate_bytes);
3862
Paul Elliottba70ad42023-02-15 18:23:53 +00003863 if (required_hash_length > sizeof(operation->hash)) {
3864 /* Shouldn't happen, but better safe than sorry. */
3865 return PSA_ERROR_CORRUPTION_DETECTED;
3866 }
3867
Paul Elliott0290a762023-02-09 14:30:24 +00003868 memcpy(operation->hash, hash, required_hash_length);
3869 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003870
3871 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003872#else
Paul Elliott068fe072023-01-16 13:59:15 +00003873 (void) operation;
3874 (void) key_buffer;
3875 (void) key_buffer_size;
3876 (void) alg;
3877 (void) hash;
3878 (void) hash_length;
3879 (void) signature;
3880 (void) signature_length;
3881 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003882 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003883 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884
Paul Elliott068fe072023-01-16 13:59:15 +00003885 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003886#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3887 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3888 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003889}
3890
3891psa_status_t mbedtls_psa_verify_hash_complete(
3892 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3893{
3894
3895#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3896 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3897 defined(MBEDTLS_ECP_RESTARTABLE)
3898
Paul Elliottf8e5b562023-02-19 18:43:45 +00003899 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3900
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003901 /* Ensure max_ops is set to the current value (or default). */
3902 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3903
Paul Elliottf8e5b562023-02-19 18:43:45 +00003904 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003905 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3906 operation->hash,
3907 operation->hash_length,
3908 &operation->ctx->Q,
3909 &operation->r,
3910 &operation->s,
3911 &operation->restart_ctx));
3912
Paul Elliottf8e5b562023-02-19 18:43:45 +00003913 /* Hide the fact that the restart context only holds a delta of number of
3914 * ops done during the last operation, not an absolute value. */
3915 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3916
3917 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003918#else
3919 (void) operation;
3920
3921 return PSA_ERROR_NOT_SUPPORTED;
3922
3923#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3924 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3925 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3926}
3927
3928psa_status_t mbedtls_psa_verify_hash_abort(
3929 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3930{
3931
3932#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3933 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3934 defined(MBEDTLS_ECP_RESTARTABLE)
3935
3936 if (operation->ctx) {
3937 mbedtls_ecdsa_free(operation->ctx);
3938 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003939 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003940 }
3941
3942 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3943
Paul Elliott93d9ca82023-02-15 18:14:21 +00003944 operation->num_ops = 0;
3945
Paul Elliott588f8ed2022-12-02 18:10:26 +00003946 mbedtls_mpi_free(&operation->r);
3947 mbedtls_mpi_free(&operation->s);
3948
3949 return PSA_SUCCESS;
3950
3951#else
3952 (void) operation;
3953
3954 return PSA_ERROR_NOT_SUPPORTED;
3955
3956#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3957 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3958 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3959}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003960
mohammad1603503973b2018-03-12 15:59:30 +02003961/****************************************************************/
3962/* Symmetric cryptography */
3963/****************************************************************/
3964
Gilles Peskine449bd832023-01-11 14:50:10 +01003965static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3966 mbedtls_svc_key_id_t key,
3967 psa_algorithm_t alg,
3968 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003969{
3970 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3971 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003972 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3974 PSA_KEY_USAGE_ENCRYPT :
3975 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003976 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02003977
3978 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003980 status = PSA_ERROR_BAD_STATE;
3981 goto exit;
3982 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003983
Gilles Peskine449bd832023-01-11 14:50:10 +01003984 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003985 status = PSA_ERROR_INVALID_ARGUMENT;
3986 goto exit;
3987 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003988
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3990 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003991 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003993
Ronald Cron49fafa92021-03-10 08:34:23 +01003994 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003995 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003996 * so we only set it (in the driver wrapper) after resources have been
3997 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003998 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003999 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004000 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004002 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 }
4004 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004005
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004006 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004008 };
4009
Ronald Cronab99ac22020-10-01 16:38:28 +02004010 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004011 if (cipher_operation == MBEDTLS_ENCRYPT) {
4012 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4013 &attributes,
4014 slot->key.data,
4015 slot->key.bytes,
4016 alg);
4017 } else {
4018 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4019 &attributes,
4020 slot->key.data,
4021 slot->key.bytes,
4022 alg);
4023 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004024
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004025exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 if (status != PSA_SUCCESS) {
4027 psa_cipher_abort(operation);
4028 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004029
Ryan Everett1b70a072024-01-04 10:32:49 +00004030 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004033}
4034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4036 mbedtls_svc_key_id_t key,
4037 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004038{
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004040}
4041
Gilles Peskine449bd832023-01-11 14:50:10 +01004042psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4043 mbedtls_svc_key_id_t key,
4044 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004045{
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004047}
4048
Gilles Peskine449bd832023-01-11 14:50:10 +01004049psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4050 uint8_t *iv,
4051 size_t iv_size,
4052 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004053{
Ronald Cron6d051732020-10-01 14:10:20 +02004054 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004055 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004056 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004057
Gilles Peskine449bd832023-01-11 14:50:10 +01004058 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004059 status = PSA_ERROR_BAD_STATE;
4060 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004061 }
4062
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004064 status = PSA_ERROR_BAD_STATE;
4065 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004066 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004067
Ronald Cron2fb90522021-07-05 12:31:44 +02004068 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004070 status = PSA_ERROR_BUFFER_TOO_SMALL;
4071 goto exit;
4072 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004073
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004075 status = PSA_ERROR_GENERIC_ERROR;
4076 goto exit;
4077 }
4078
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 status = psa_generate_random(local_iv, default_iv_length);
4080 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004081 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 }
Ronald Cron5618a392021-03-26 09:52:26 +01004083
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 status = psa_driver_wrapper_cipher_set_iv(operation,
4085 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004086
4087exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 if (status == PSA_SUCCESS) {
4089 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004090 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004091 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004093 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004095 }
Ronald Cron6d051732020-10-01 14:10:20 +02004096
Gilles Peskine449bd832023-01-11 14:50:10 +01004097 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004098}
4099
Gilles Peskine449bd832023-01-11 14:50:10 +01004100psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4101 const uint8_t *iv,
4102 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004103{
Ronald Cron6d051732020-10-01 14:10:20 +02004104 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004105
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004107 status = PSA_ERROR_BAD_STATE;
4108 goto exit;
4109 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004112 status = PSA_ERROR_BAD_STATE;
4113 goto exit;
4114 }
Ronald Crona0d68172021-03-26 10:15:08 +01004115
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004117 status = PSA_ERROR_INVALID_ARGUMENT;
4118 goto exit;
4119 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004120
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 status = psa_driver_wrapper_cipher_set_iv(operation,
4122 iv,
4123 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004124
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004125exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004127 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 } else {
4129 psa_cipher_abort(operation);
4130 }
4131 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004132}
4133
Gilles Peskine449bd832023-01-11 14:50:10 +01004134psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4135 const uint8_t *input,
4136 size_t input_length,
4137 uint8_t *output,
4138 size_t output_size,
4139 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004140{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004141 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004144 status = PSA_ERROR_BAD_STATE;
4145 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004146 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004147
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004149 status = PSA_ERROR_BAD_STATE;
4150 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004151 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 status = psa_driver_wrapper_cipher_update(operation,
4154 input,
4155 input_length,
4156 output,
4157 output_size,
4158 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004159
4160exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 if (status != PSA_SUCCESS) {
4162 psa_cipher_abort(operation);
4163 }
Ronald Cron6d051732020-10-01 14:10:20 +02004164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004166}
4167
Gilles Peskine449bd832023-01-11 14:50:10 +01004168psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4169 uint8_t *output,
4170 size_t output_size,
4171 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004172{
David Saadab4ecc272019-02-14 13:48:10 +02004173 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004176 status = PSA_ERROR_BAD_STATE;
4177 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004178 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004181 status = PSA_ERROR_BAD_STATE;
4182 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004183 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004184
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 status = psa_driver_wrapper_cipher_finish(operation,
4186 output,
4187 output_size,
4188 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004189
4190exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 if (status == PSA_SUCCESS) {
4192 return psa_cipher_abort(operation);
4193 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004194 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004196
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004198 }
mohammad1603503973b2018-03-12 15:59:30 +02004199}
4200
Gilles Peskine449bd832023-01-11 14:50:10 +01004201psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004202{
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004204 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004205 * in use. It's ok to call abort on such an object, and there's
4206 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004208 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004209
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004211
Ronald Cron49fafa92021-03-10 08:34:23 +01004212 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004213 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004214 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004215
Gilles Peskine449bd832023-01-11 14:50:10 +01004216 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004217}
4218
Gilles Peskine449bd832023-01-11 14:50:10 +01004219psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4220 psa_algorithm_t alg,
4221 const uint8_t *input,
4222 size_t input_length,
4223 uint8_t *output,
4224 size_t output_size,
4225 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004226{
4227 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004228 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004229 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004230 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4231 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004232 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004235 status = PSA_ERROR_INVALID_ARGUMENT;
4236 goto exit;
4237 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004238
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4240 PSA_KEY_USAGE_ENCRYPT,
4241 alg);
4242 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004243 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004244 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004245
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004246 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004247 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004248 };
4249
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4251 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004252 status = PSA_ERROR_GENERIC_ERROR;
4253 goto exit;
4254 }
4255
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 if (default_iv_length > 0) {
4257 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004258 status = PSA_ERROR_BUFFER_TOO_SMALL;
4259 goto exit;
4260 }
4261
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 status = psa_generate_random(local_iv, default_iv_length);
4263 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004264 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004266 }
4267
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004268 status = psa_driver_wrapper_cipher_encrypt(
4269 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004270 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004271 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004273
4274exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004275 unlock_status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004277 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004278 }
Ronald Cron23919522021-07-08 19:00:07 +02004279
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 if (status == PSA_SUCCESS) {
4281 if (default_iv_length > 0) {
4282 memcpy(output, local_iv, default_iv_length);
4283 }
4284 *output_length += default_iv_length;
4285 } else {
4286 *output_length = 0;
4287 }
4288
4289 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004290}
4291
Gilles Peskine449bd832023-01-11 14:50:10 +01004292psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4293 psa_algorithm_t alg,
4294 const uint8_t *input,
4295 size_t input_length,
4296 uint8_t *output,
4297 size_t output_size,
4298 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004299{
4300 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004301 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004302 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004303 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004304
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004306 status = PSA_ERROR_INVALID_ARGUMENT;
4307 goto exit;
4308 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004309
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4311 PSA_KEY_USAGE_DECRYPT,
4312 alg);
4313 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004314 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004316
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004317 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004319 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4322 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004323 status = PSA_ERROR_INVALID_ARGUMENT;
4324 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004326 status = PSA_ERROR_INVALID_ARGUMENT;
4327 goto exit;
4328 }
4329
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004330 status = psa_driver_wrapper_cipher_decrypt(
4331 &attributes, slot->key.data, slot->key.bytes,
4332 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004334
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004335exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004336 unlock_status = psa_unregister_read(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004337 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004338 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004340
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004342 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004343 }
Ronald Cron23919522021-07-08 19:00:07 +02004344
Gilles Peskine449bd832023-01-11 14:50:10 +01004345 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004346}
4347
4348
mohammad16035955c982018-04-26 00:53:03 +03004349/****************************************************************/
4350/* AEAD */
4351/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004352
Paul Elliottbaff51c2021-09-28 17:44:45 +01004353/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004354static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004355{
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004357}
4358
4359/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004360static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4361 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004362{
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004364
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004366#if defined(PSA_WANT_ALG_GCM)
4367 case PSA_ALG_GCM:
4368 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 * arbitrarily large nonces. Please note that we do not generally
4370 * recommend the usage of nonces of greater length than
4371 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4372 * size, which can then lead to collisions if you encrypt a very
4373 * large number of messages.*/
4374 if (nonce_length != 0) {
4375 return PSA_SUCCESS;
4376 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004377 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004378#endif /* PSA_WANT_ALG_GCM */
4379#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004380 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 if (nonce_length >= 7 && nonce_length <= 13) {
4382 return PSA_SUCCESS;
4383 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004384 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004385#endif /* PSA_WANT_ALG_CCM */
4386#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004387 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 if (nonce_length == 12) {
4389 return PSA_SUCCESS;
4390 } else if (nonce_length == 8) {
4391 return PSA_ERROR_NOT_SUPPORTED;
4392 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004393 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004394#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004395 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004396 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004398 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004399
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004401}
4402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004404{
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4406 return PSA_ERROR_INVALID_ARGUMENT;
4407 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004410}
4411
Gilles Peskine449bd832023-01-11 14:50:10 +01004412psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4413 psa_algorithm_t alg,
4414 const uint8_t *nonce,
4415 size_t nonce_length,
4416 const uint8_t *additional_data,
4417 size_t additional_data_length,
4418 const uint8_t *plaintext,
4419 size_t plaintext_length,
4420 uint8_t *ciphertext,
4421 size_t ciphertext_size,
4422 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004423{
Ronald Cron215633c2021-03-16 17:15:37 +01004424 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4425 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004426
mohammad1603f08a5502018-06-03 15:05:47 +03004427 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004428
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 status = psa_aead_check_algorithm(alg);
4430 if (status != PSA_SUCCESS) {
4431 return status;
4432 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004433
Ronald Cron9a986162021-03-26 12:40:07 +01004434 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4436 if (status != PSA_SUCCESS) {
4437 return status;
4438 }
mohammad16035955c982018-04-26 00:53:03 +03004439
Ronald Cron215633c2021-03-16 17:15:37 +01004440 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004442 };
mohammad16035955c982018-04-26 00:53:03 +03004443
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 status = psa_aead_check_nonce_length(alg, nonce_length);
4445 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004446 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004448
Ronald Cronde822812021-03-17 16:08:20 +01004449 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004450 &attributes, slot->key.data, slot->key.bytes,
4451 alg,
4452 nonce, nonce_length,
4453 additional_data, additional_data_length,
4454 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004456
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4458 memset(ciphertext, 0, ciphertext_size);
4459 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004460
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004461exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004462 psa_unregister_read(slot);
mohammad16035955c982018-04-26 00:53:03 +03004463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004465}
4466
Gilles Peskine449bd832023-01-11 14:50:10 +01004467psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4468 psa_algorithm_t alg,
4469 const uint8_t *nonce,
4470 size_t nonce_length,
4471 const uint8_t *additional_data,
4472 size_t additional_data_length,
4473 const uint8_t *ciphertext,
4474 size_t ciphertext_length,
4475 uint8_t *plaintext,
4476 size_t plaintext_size,
4477 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004478{
Ronald Cron215633c2021-03-16 17:15:37 +01004479 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4480 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004481
Gilles Peskineee652a32018-06-01 19:23:52 +02004482 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004483
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 status = psa_aead_check_algorithm(alg);
4485 if (status != PSA_SUCCESS) {
4486 return status;
4487 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004488
Ronald Cron9a986162021-03-26 12:40:07 +01004489 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4491 if (status != PSA_SUCCESS) {
4492 return status;
4493 }
mohammad16035955c982018-04-26 00:53:03 +03004494
Ronald Cron215633c2021-03-16 17:15:37 +01004495 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004497 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004498
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 status = psa_aead_check_nonce_length(alg, nonce_length);
4500 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004501 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004503
Ronald Cronde822812021-03-17 16:08:20 +01004504 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004505 &attributes, slot->key.data, slot->key.bytes,
4506 alg,
4507 nonce, nonce_length,
4508 additional_data, additional_data_length,
4509 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004511
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 if (status != PSA_SUCCESS && plaintext_size != 0) {
4513 memset(plaintext, 0, plaintext_size);
4514 }
mohammad160360a64d02018-06-03 17:20:42 +03004515
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004516exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004517 psa_unregister_read(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004518
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 return status;
mohammad16035955c982018-04-26 00:53:03 +03004520}
4521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4523{
4524 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004527#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004529 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4531 return PSA_ERROR_INVALID_ARGUMENT;
4532 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004533 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004534#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004535
Przemek Stekiel86679c72022-10-06 17:06:56 +02004536#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004538 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4540 return PSA_ERROR_INVALID_ARGUMENT;
4541 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004542 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004543#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004544
Przemek Stekiel86679c72022-10-06 17:06:56 +02004545#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004547 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 if (tag_len != 16) {
4549 return PSA_ERROR_INVALID_ARGUMENT;
4550 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004551 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004552#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004553
4554 default:
4555 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004557 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004559}
4560
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004561/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004562static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4563 int is_encrypt,
4564 mbedtls_svc_key_id_t key,
4565 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004566{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004567 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4568 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4569 psa_key_slot_t *slot = NULL;
4570 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004571 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 status = psa_aead_check_algorithm(alg);
4574 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004575 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004577
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 if (operation->id != 0) {
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 (operation->nonce_set || operation->lengths_set ||
4584 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004585 status = PSA_ERROR_BAD_STATE;
4586 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004587 }
4588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004590 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004592 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004594
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4596 alg);
4597 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004598 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004600
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004601 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004602 .core = slot->attr
4603 };
4604
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004606 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004608
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 if (is_encrypt) {
4610 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4611 &attributes,
4612 slot->key.data,
4613 slot->key.bytes,
4614 alg);
4615 } else {
4616 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4617 &attributes,
4618 slot->key.data,
4619 slot->key.bytes,
4620 alg);
4621 }
4622 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004623 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004625
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004627
4628exit:
Ryan Everett1b70a072024-01-04 10:32:49 +00004629 unlock_status = psa_unregister_read(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004632 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004634 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 } else {
4636 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004637 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004640}
4641
Paul Elliott302ff6b2021-04-20 18:10:30 +01004642/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004643psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4644 mbedtls_svc_key_id_t key,
4645 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004646{
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004648}
4649
4650/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004651psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4652 mbedtls_svc_key_id_t key,
4653 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004654{
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004656}
4657
4658/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004659psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4660 uint8_t *nonce,
4661 size_t nonce_size,
4662 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004663{
4664 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004665 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004666 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004667
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004668 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004669
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004671 status = PSA_ERROR_BAD_STATE;
4672 goto exit;
4673 }
4674
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004676 status = PSA_ERROR_BAD_STATE;
4677 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004678 }
4679
Paul Elliotte193ea82021-10-01 13:00:16 +01004680 /* For CCM, this size may not be correct according to the PSA
4681 * specification. The PSA Crypto 1.0.1 specification states:
4682 *
4683 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4684 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4685 *
4686 * However this restriction that L has to be the smallest integer is not
4687 * applied in practice, and it is not implementable here since the
4688 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4690 operation->alg);
4691 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004692 status = PSA_ERROR_BUFFER_TOO_SMALL;
4693 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004694 }
4695
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 status = psa_generate_random(local_nonce, required_nonce_size);
4697 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004698 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004700
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004702
Paul Elliott39dc6b82021-05-11 19:16:09 +01004703exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 if (status == PSA_SUCCESS) {
4705 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004706 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 } else {
4708 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004709 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004710
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004712}
4713
4714/* Set the nonce for a multipart authenticated encryption or decryption
4715 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004716psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4717 const uint8_t *nonce,
4718 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004719{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004720 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004723 status = PSA_ERROR_BAD_STATE;
4724 goto exit;
4725 }
4726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004728 status = PSA_ERROR_BAD_STATE;
4729 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004730 }
4731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4733 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004734 status = PSA_ERROR_INVALID_ARGUMENT;
4735 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004736 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004737
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4739 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004740
Paul Elliott39dc6b82021-05-11 19:16:09 +01004741exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004743 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 } else {
4745 psa_aead_abort(operation);
4746 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004747
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004749}
4750
4751/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004752psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4753 size_t ad_length,
4754 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004755{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004756 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4757
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004759 status = PSA_ERROR_BAD_STATE;
4760 goto exit;
4761 }
4762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 if (operation->lengths_set || operation->ad_started ||
4764 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004765 status = PSA_ERROR_BAD_STATE;
4766 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004767 }
4768
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004770#if defined(PSA_WANT_ALG_GCM)
4771 case PSA_ALG_GCM:
4772 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 * bits. Without the guard this code will generate warnings on 32bit
4774 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004775#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 if (((uint64_t) ad_length) >> 61 != 0 ||
4777 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004778 status = PSA_ERROR_INVALID_ARGUMENT;
4779 goto exit;
4780 }
Paul Elliott325d3742021-09-27 17:56:28 +01004781#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004782 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004783#endif /* PSA_WANT_ALG_GCM */
4784#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004785 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004787 status = PSA_ERROR_INVALID_ARGUMENT;
4788 goto exit;
4789 }
4790 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004791#endif /* PSA_WANT_ALG_CCM */
4792#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004793 case PSA_ALG_CHACHA20_POLY1305:
4794 /* No length restrictions for ChaChaPoly. */
4795 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004796#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004797 default:
4798 break;
4799 }
Paul Elliott325d3742021-09-27 17:56:28 +01004800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4802 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004803
Paul Elliott39dc6b82021-05-11 19:16:09 +01004804exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004806 operation->ad_remaining = ad_length;
4807 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004808 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 } else {
4810 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004811 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004812
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004814}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004815
4816/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004817psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4818 const uint8_t *input,
4819 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004820{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004821 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4822
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004824 status = PSA_ERROR_BAD_STATE;
4825 goto exit;
4826 }
4827
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004829 status = PSA_ERROR_BAD_STATE;
4830 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004831 }
4832
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 if (operation->lengths_set) {
4834 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004835 status = PSA_ERROR_INVALID_ARGUMENT;
4836 goto exit;
4837 }
4838
4839 operation->ad_remaining -= input_length;
4840 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004841#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004843 status = PSA_ERROR_BAD_STATE;
4844 goto exit;
4845 }
4846#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004847
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 status = psa_driver_wrapper_aead_update_ad(operation, input,
4849 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004850
Paul Elliott39dc6b82021-05-11 19:16:09 +01004851exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004853 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 } else {
4855 psa_aead_abort(operation);
4856 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004857
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004859}
4860
4861/* Encrypt or decrypt a message fragment in an active multipart AEAD
4862 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004863psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4864 const uint8_t *input,
4865 size_t input_length,
4866 uint8_t *output,
4867 size_t output_size,
4868 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004869{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004870 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004871
4872 *output_length = 0;
4873
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004875 status = PSA_ERROR_BAD_STATE;
4876 goto exit;
4877 }
4878
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004880 status = PSA_ERROR_BAD_STATE;
4881 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004882 }
4883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004885 /* Additional data length was supplied, but not all the additional
4886 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004888 status = PSA_ERROR_INVALID_ARGUMENT;
4889 goto exit;
4890 }
4891
4892 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004894 status = PSA_ERROR_INVALID_ARGUMENT;
4895 goto exit;
4896 }
4897
4898 operation->body_remaining -= input_length;
4899 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004900#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004902 status = PSA_ERROR_BAD_STATE;
4903 goto exit;
4904 }
4905#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004906
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4908 output, output_size,
4909 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004910
Paul Elliott39dc6b82021-05-11 19:16:09 +01004911exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004913 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 } else {
4915 psa_aead_abort(operation);
4916 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004917
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004919}
4920
Gilles Peskine449bd832023-01-11 14:50:10 +01004921static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004922{
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 if (operation->id == 0 || !operation->nonce_set) {
4924 return PSA_ERROR_BAD_STATE;
4925 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004926
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4928 operation->body_remaining != 0)) {
4929 return PSA_ERROR_INVALID_ARGUMENT;
4930 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004931
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004933}
4934
Paul Elliott302ff6b2021-04-20 18:10:30 +01004935/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004936psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4937 uint8_t *ciphertext,
4938 size_t ciphertext_size,
4939 size_t *ciphertext_length,
4940 uint8_t *tag,
4941 size_t tag_size,
4942 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004943{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004944 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4945
Paul Elliott302ff6b2021-04-20 18:10:30 +01004946 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004947 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004948
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 status = psa_aead_final_checks(operation);
4950 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004951 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004953
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004955 status = PSA_ERROR_BAD_STATE;
4956 goto exit;
4957 }
4958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4960 ciphertext_size,
4961 ciphertext_length,
4962 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004963
Paul Elliott39dc6b82021-05-11 19:16:09 +01004964exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004965
4966
Paul Elliottf47b0952021-05-21 18:02:33 +01004967 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004968 * the zero tag size, make sure the tag is set to something implausible.
4969 * Even if the operation succeeds, make sure we clear the rest of the
4970 * buffer to prevent potential leakage of anything previously placed in
4971 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004972 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004973
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004975
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004977}
4978
4979/* Finish authenticating and decrypting a message in a multipart AEAD
4980 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004981psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4982 uint8_t *plaintext,
4983 size_t plaintext_size,
4984 size_t *plaintext_length,
4985 const uint8_t *tag,
4986 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004987{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004988 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4989
Paul Elliott302ff6b2021-04-20 18:10:30 +01004990 *plaintext_length = 0;
4991
Gilles Peskine449bd832023-01-11 14:50:10 +01004992 status = psa_aead_final_checks(operation);
4993 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004994 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004996
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004998 status = PSA_ERROR_BAD_STATE;
4999 goto exit;
5000 }
5001
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5003 plaintext_size,
5004 plaintext_length,
5005 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005006
5007exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005009
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005011}
5012
5013/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005014psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005015{
5016 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5017
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005019 /* The object has (apparently) been initialized but it is not (yet)
5020 * in use. It's ok to call abort on such an object, and there's
5021 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005023 }
5024
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005026
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005028
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005030}
5031
Gilles Peskinee59236f2018-01-27 23:32:46 +01005032/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005033/* Generators */
5034/****************************************************************/
5035
Przemek Stekiel69c46792022-06-10 12:59:51 +02005036#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005037 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005038 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305039 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305040 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005041#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005042#endif /* At least one builtin KDF */
5043
Przemek Stekiel69c46792022-06-10 12:59:51 +02005044#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005045 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5046 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5047static psa_status_t psa_key_derivation_start_hmac(
5048 psa_mac_operation_t *operation,
5049 psa_algorithm_t hash_alg,
5050 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005052{
5053 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5054 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5056 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5057 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005058
Steven Cooreman72f736a2021-05-07 14:14:37 +02005059 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005061
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 status = psa_driver_wrapper_mac_sign_setup(operation,
5063 &attributes,
5064 hmac_key, hmac_key_length,
5065 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005066
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 psa_reset_key_attributes(&attributes);
5068 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005069}
5070#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005071
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005072#define HKDF_STATE_INIT 0 /* no input yet */
5073#define HKDF_STATE_STARTED 1 /* got salt */
5074#define HKDF_STATE_KEYED 2 /* got key */
5075#define HKDF_STATE_OUTPUT 3 /* output started */
5076
Gilles Peskinecbe66502019-05-16 16:59:18 +02005077static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005079{
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5081 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5082 } else {
5083 return operation->alg;
5084 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005085}
5086
Gilles Peskine449bd832023-01-11 14:50:10 +01005087psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005088{
5089 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5091 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005092 /* The object has (apparently) been initialized but it is not
5093 * in use. It's ok to call abort on such an object, and there's
5094 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005096#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5098 mbedtls_free(operation->ctx.hkdf.info);
5099 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5100 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005101#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005102#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5103 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5105 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5106 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5107 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005108 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005110 }
5111
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005113 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005115 }
5116
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005118 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005120 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005121#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005123 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005125 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005126#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005127 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005128
5129 /* We leave the fields Ai and output_block to be erased safely by the
5130 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005132#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5133 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005134#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5136 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5137 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5138 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005139#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305140#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305141 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305142 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005143 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305144 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305145 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305146
5147 status = PSA_SUCCESS;
5148 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305149#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005150 {
5151 status = PSA_ERROR_BAD_STATE;
5152 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 mbedtls_platform_zeroize(operation, sizeof(*operation));
5154 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005155}
5156
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005157psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005159{
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005161 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005163 }
5164
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005165 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005167}
5168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5170 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005171{
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 if (operation->alg == 0) {
5173 return PSA_ERROR_BAD_STATE;
5174 }
5175 if (capacity > operation->capacity) {
5176 return PSA_ERROR_INVALID_ARGUMENT;
5177 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005178 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005180}
5181
Przemek Stekiel69c46792022-06-10 12:59:51 +02005182#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005183/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005184static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5185 psa_algorithm_t kdf_alg,
5186 uint8_t *output,
5187 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005188{
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5190 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005191 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005192 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005193#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005195#else
5196 const uint8_t last_block = 0xff;
5197#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005198
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 if (hkdf->state < HKDF_STATE_KEYED ||
5200 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005201#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005203#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 )) {
5205 return PSA_ERROR_BAD_STATE;
5206 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005207 hkdf->state = HKDF_STATE_OUTPUT;
5208
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005210 /* Copy what remains of the current block */
5211 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005213 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 }
5215 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005216 output += n;
5217 output_length -= n;
5218 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005220 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005222 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005223 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005224 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005225 * object was corrupted or if this function is called directly
5226 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 if (hkdf->block_number == last_block) {
5228 return PSA_ERROR_BAD_STATE;
5229 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005230
5231 /* We need a new block */
5232 ++hkdf->block_number;
5233 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005234
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5236 hash_alg,
5237 hkdf->prk,
5238 hash_length);
5239 if (status != PSA_SUCCESS) {
5240 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005241 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005242
5243 if (hkdf->block_number != 1) {
5244 status = psa_mac_update(&hkdf->hmac,
5245 hkdf->output_block,
5246 hash_length);
5247 if (status != PSA_SUCCESS) {
5248 return status;
5249 }
5250 }
5251 status = psa_mac_update(&hkdf->hmac,
5252 hkdf->info,
5253 hkdf->info_length);
5254 if (status != PSA_SUCCESS) {
5255 return status;
5256 }
5257 status = psa_mac_update(&hkdf->hmac,
5258 &hkdf->block_number, 1);
5259 if (status != PSA_SUCCESS) {
5260 return status;
5261 }
5262 status = psa_mac_sign_finish(&hkdf->hmac,
5263 hkdf->output_block,
5264 sizeof(hkdf->output_block),
5265 &hmac_output_length);
5266 if (status != PSA_SUCCESS) {
5267 return status;
5268 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005269 }
5270
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005272}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005273#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005274
John Durkop07cc04a2020-11-16 22:08:34 -08005275#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5276 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005277static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5278 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005280{
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5282 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005283 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5284 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005285 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005286
Janos Follath7742fee2019-06-17 12:58:10 +01005287 /* We can't be wanting more output after block 0xff, otherwise
5288 * the capacity check in psa_key_derivation_output_bytes() would have
5289 * prevented this call. It could happen only if the operation
5290 * object was corrupted or if this function is called directly
5291 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 if (tls12_prf->block_number == 0xff) {
5293 return PSA_ERROR_CORRUPTION_DETECTED;
5294 }
Janos Follath7742fee2019-06-17 12:58:10 +01005295
5296 /* We need a new block */
5297 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005298 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005299
5300 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5301 *
5302 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5303 *
5304 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5305 * HMAC_hash(secret, A(2) + seed) +
5306 * HMAC_hash(secret, A(3) + seed) + ...
5307 *
5308 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005309 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005310 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005311 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005312 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005313 * is currently extracted as `output_block` and where i is
5314 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005315 */
5316
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 status = psa_key_derivation_start_hmac(&hmac,
5318 hash_alg,
5319 tls12_prf->secret,
5320 tls12_prf->secret_length);
5321 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005322 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005324
5325 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005327 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5328 * the variable seed and in this instance means it in the context of the
5329 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 status = psa_mac_update(&hmac,
5331 tls12_prf->label,
5332 tls12_prf->label_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 status = psa_mac_update(&hmac,
5337 tls12_prf->seed,
5338 tls12_prf->seed_length);
5339 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005340 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 }
5342 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005343 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5345 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005346 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005348 }
5349
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 status = psa_mac_sign_finish(&hmac,
5351 tls12_prf->Ai, hash_length,
5352 &hmac_output_length);
5353 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005354 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 }
5356 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005357 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005359
5360 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 status = psa_key_derivation_start_hmac(&hmac,
5362 hash_alg,
5363 tls12_prf->secret,
5364 tls12_prf->secret_length);
5365 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005366 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 }
5368 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5369 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005370 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 }
5372 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5373 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005374 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 }
5376 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5377 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005378 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 }
5380 status = psa_mac_sign_finish(&hmac,
5381 tls12_prf->output_block, hash_length,
5382 &hmac_output_length);
5383 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005384 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005386
Janos Follath7742fee2019-06-17 12:58:10 +01005387
5388cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 cleanup_status = psa_mac_abort(&hmac);
5390 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005391 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005393
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005395}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005396
Janos Follath844eb0e2019-06-19 12:10:49 +01005397static psa_status_t psa_key_derivation_tls12_prf_read(
5398 psa_tls12_prf_key_derivation_t *tls12_prf,
5399 psa_algorithm_t alg,
5400 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005402{
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5404 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005405 psa_status_t status;
5406 uint8_t offset, length;
5407
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005409 case PSA_TLS12_PRF_STATE_LABEL_SET:
5410 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5411 break;
5412 case PSA_TLS12_PRF_STATE_OUTPUT:
5413 break;
5414 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005416 }
5417
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005419 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 if (tls12_prf->left_in_block == 0) {
5421 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5422 alg);
5423 if (status != PSA_SUCCESS) {
5424 return status;
5425 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005426
5427 continue;
5428 }
5429
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005431 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005433 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005435
5436 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005438 output += length;
5439 output_length -= length;
5440 tls12_prf->left_in_block -= length;
5441 }
5442
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005444}
John Durkop07cc04a2020-11-16 22:08:34 -08005445#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5446 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005447
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005448#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5449static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5450 psa_tls12_ecjpake_to_pms_t *ecjpake,
5451 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005453{
5454 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005455 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005456
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 if (output_length != 32) {
5458 return PSA_ERROR_INVALID_ARGUMENT;
5459 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005460
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5462 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5463 &output_size);
5464 if (status != PSA_SUCCESS) {
5465 return status;
5466 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005467
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 if (output_size != output_length) {
5469 return PSA_ERROR_GENERIC_ERROR;
5470 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005471
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005473}
5474#endif
5475
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305476#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305477static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5478 psa_pbkdf2_key_derivation_t *pbkdf2,
5479 psa_algorithm_t prf_alg,
5480 uint8_t prf_output_length,
5481 psa_key_attributes_t *attributes)
5482{
5483 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305484 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305485 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305486 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305487 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305488 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305489 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305490
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305491 mac_operation.is_sign = 1;
5492 mac_operation.mac_size = prf_output_length;
5493 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305494
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305495 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5496 attributes,
5497 pbkdf2->password,
5498 pbkdf2->password_length,
5499 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305500 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305501 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305502 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305503 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5504 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305505 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305506 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305507 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305508 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305509 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305510 }
5511 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5512 &mac_output_length);
5513 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305514 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305515 }
5516
5517 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305518 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305519 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305520 }
5521
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305522 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305523
5524 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305525 /* We are passing prf_output_length as mac_size because the driver
5526 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305527 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305528 status = psa_driver_wrapper_mac_compute(attributes,
5529 pbkdf2->password,
5530 pbkdf2->password_length,
5531 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305532 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305533 &mac_output_length);
5534 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305535 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305536 }
5537
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305538 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305539 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305540
5541cleanup:
5542 /* Zeroise buffers to clear sensitive data from memory. */
5543 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5544 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305545}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305546
5547static psa_status_t psa_key_derivation_pbkdf2_read(
5548 psa_pbkdf2_key_derivation_t *pbkdf2,
5549 psa_algorithm_t kdf_alg,
5550 uint8_t *output,
5551 size_t output_length)
5552{
5553 psa_status_t status;
5554 psa_algorithm_t prf_alg;
5555 uint8_t prf_output_length;
5556 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5557 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5558 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5559
5560 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5561 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5562 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5563 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305564 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5565 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305566 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305567 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305568 } else {
5569 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305570 }
5571
5572 switch (pbkdf2->state) {
5573 case PSA_PBKDF2_STATE_PASSWORD_SET:
5574 /* Initially we need a new block so bytes_used is equal to block size*/
5575 pbkdf2->bytes_used = prf_output_length;
5576 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5577 break;
5578 case PSA_PBKDF2_STATE_OUTPUT:
5579 break;
5580 default:
5581 return PSA_ERROR_BAD_STATE;
5582 }
5583
5584 while (output_length != 0) {
5585 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5586 if (n > output_length) {
5587 n = (uint8_t) output_length;
5588 }
5589 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5590 output += n;
5591 output_length -= n;
5592 pbkdf2->bytes_used += n;
5593
5594 if (output_length == 0) {
5595 break;
5596 }
5597
5598 /* We need a new block */
5599 pbkdf2->bytes_used = 0;
5600 pbkdf2->block_number++;
5601
5602 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5603 prf_output_length,
5604 &attributes);
5605 if (status != PSA_SUCCESS) {
5606 return status;
5607 }
5608 }
5609
5610 return PSA_SUCCESS;
5611}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305612#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305613
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005614psa_status_t psa_key_derivation_output_bytes(
5615 psa_key_derivation_operation_t *operation,
5616 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005618{
5619 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005621
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005623 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005625 }
5626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005628 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005629 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005630 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005631 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005632 goto exit;
5633 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005635 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005636 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005637 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5638 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005639 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005640 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005642 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005643 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005644
Przemek Stekiel69c46792022-06-10 12:59:51 +02005645#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5647 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5648 output, output_length);
5649 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005650#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005651#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5652 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5654 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5655 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5656 kdf_alg, output,
5657 output_length);
5658 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005659#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5660 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005661#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005663 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5665 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005666#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305667#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305668 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305669 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5670 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305671 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305672#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005673
Gilles Peskineeab56e42018-07-12 17:12:33 +02005674 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005675 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005677 }
5678
5679exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005681 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005682 * This allows us to differentiate between exhausted operations and
5683 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5684 * operations. */
5685 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005687 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005689 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005691}
5692
David Brown7807bf72021-02-09 16:28:23 -07005693#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005694static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005695{
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 if (data_size >= 8) {
5697 mbedtls_des_key_set_parity(data);
5698 }
5699 if (data_size >= 16) {
5700 mbedtls_des_key_set_parity(data + 8);
5701 }
5702 if (data_size >= 24) {
5703 mbedtls_des_key_set_parity(data + 16);
5704 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005705}
David Brown7807bf72021-02-09 16:28:23 -07005706#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005707
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005708/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 * ECC keys on a Weierstrass elliptic curve require the generation
5710 * of a private key which is an integer
5711 * in the range [1, N - 1], where N is the boundary of the private key domain:
5712 * N is the prime p for Diffie-Hellman, or the order of the
5713 * curve’s base point for ECC.
5714 *
5715 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5716 * This function generates the private key using the following process:
5717 *
5718 * 1. Draw a byte string of length ceiling(m/8) bytes.
5719 * 2. If m is not a multiple of 8, set the most significant
5720 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5721 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5722 * 4. If k > N - 2, discard the result and return to step 1.
5723 * 5. Output k + 1 as the private key.
5724 *
5725 * This method allows compliance to NIST standards, specifically the methods titled
5726 * Key-Pair Generation by Testing Candidates in the following publications:
5727 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5728 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5729 * Diffie-Hellman keys.
5730 *
5731 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5732 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5733 *
5734 * Note: Function allocates memory for *data buffer, so given *data should be
5735 * always NULL.
5736 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005737#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5738#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005739static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005740 psa_key_slot_t *slot,
5741 size_t bits,
5742 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005743 uint8_t **data
5744 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005745{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005746 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005747 mbedtls_mpi k;
5748 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005750 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005751 size_t m;
5752 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 mbedtls_mpi_init(&k);
5755 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005756
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005757 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005759 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01005760 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005761
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005763 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005764 goto cleanup;
5765 }
5766
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005767 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005771
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005772 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005773 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005774 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005775
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005776 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005777
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005778 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005780
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005781 /* Note: This function is always called with *data == NULL and it
5782 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 *data = mbedtls_calloc(1, m_bytes);
5784 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005785 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005786 goto cleanup;
5787 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005790 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005792 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005794
5795 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005797 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005798 * (8 * ceiling(m/8) - m) bits of the first byte in
5799 * the string to zero.
5800 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005802 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005803 }
5804
5805 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 * big-endian byte string.
5807 */
5808 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005809
5810 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 * Result of comparison is returned. When it indicates error
5812 * then this function is called again.
5813 */
5814 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005815 }
5816
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005817 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5819 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005820cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 if (ret != 0) {
5822 status = mbedtls_to_psa_error(ret);
5823 }
5824 if (status != PSA_SUCCESS) {
5825 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005826 *data = NULL;
5827 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 mbedtls_mpi_free(&k);
5829 mbedtls_mpi_free(&diff_N_2);
5830 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005831}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005832
5833/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5834 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5835 *
5836 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5837 * draw a 32-byte string and process it as specified in
5838 * Elliptic Curves for Security [RFC7748] §5.
5839 *
5840 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5841 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5842 *
5843 * Note: Function allocates memory for *data buffer, so given *data should be
5844 * always NULL.
5845 */
5846
5847static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5848 size_t bits,
5849 psa_key_derivation_operation_t *operation,
5850 uint8_t **data
5851 )
5852{
5853 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005854 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005857 case 255:
5858 output_length = 32;
5859 break;
5860 case 448:
5861 output_length = 56;
5862 break;
5863 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005865 break;
5866 }
5867
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005869
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 if (*data == NULL) {
5871 return PSA_ERROR_INSUFFICIENT_MEMORY;
5872 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005873
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005875
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005877 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005879
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005881 case 255:
5882 (*data)[0] &= 248;
5883 (*data)[31] &= 127;
5884 (*data)[31] |= 64;
5885 break;
5886 case 448:
5887 (*data)[0] &= 252;
5888 (*data)[55] |= 128;
5889 break;
5890 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005892 break;
5893 }
5894
5895 return status;
5896}
Valerio Setti86587ab2023-06-27 13:09:30 +02005897#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5898static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5899 psa_key_slot_t *slot, size_t bits,
5900 psa_key_derivation_operation_t *operation, uint8_t **data)
5901{
5902 (void) slot;
5903 (void) bits;
5904 (void) operation;
5905 (void) data;
5906 return PSA_ERROR_NOT_SUPPORTED;
5907}
5908
5909static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5910 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5911{
5912 (void) bits;
5913 (void) operation;
5914 (void) data;
5915 return PSA_ERROR_NOT_SUPPORTED;
5916}
5917#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5918#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005919
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005920static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005921 psa_key_slot_t *slot,
5922 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005924{
5925 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305927 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005928 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005929 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005930
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5932 return PSA_ERROR_INVALID_ARGUMENT;
5933 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005934
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005935#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005936 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5938 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5939 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005940 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5942 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005943 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 }
5945 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005946 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5948 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005949 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005951 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005952 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005953#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005954 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 if (key_type_is_raw_bytes(slot->attr.type)) {
5956 if (bits % 8 != 0) {
5957 return PSA_ERROR_INVALID_ARGUMENT;
5958 }
5959 data = mbedtls_calloc(1, bytes);
5960 if (data == NULL) {
5961 return PSA_ERROR_INSUFFICIENT_MEMORY;
5962 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005963
Gilles Peskine449bd832023-01-11 14:50:10 +01005964 status = psa_key_derivation_output_bytes(operation, data, bytes);
5965 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005966 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 }
David Brown12ca5032021-02-11 11:02:00 -07005968#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005969 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5970 psa_des_set_key_parity(data, bytes);
5971 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005972#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 } else {
5974 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005975 }
Ronald Crondd04d422020-11-28 15:14:42 +01005976
Ronald Cronbf33c932020-11-28 18:06:53 +01005977 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005978 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005980 };
5981
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5983 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5984 &storage_size);
5985 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305986 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 }
Archanad8a83dc2021-06-14 10:04:16 +05305988 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 status = psa_allocate_buffer_to_slot(slot, storage_size);
5990 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305991 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 }
Archanad8a83dc2021-06-14 10:04:16 +05305993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 status = psa_driver_wrapper_import_key(&attributes,
5995 data, bytes,
5996 slot->key.data,
5997 slot->key.bytes,
5998 &slot->key.bytes, &bits);
5999 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006000 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006002
6003exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 mbedtls_free(data);
6005 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006006}
6007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6009 psa_key_derivation_operation_t *operation,
6010 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006011{
6012 psa_status_t status;
6013 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006014 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006015
Ronald Cron81709fc2020-11-14 12:10:32 +01006016 *key = MBEDTLS_SVC_KEY_ID_INIT;
6017
Gilles Peskine0f84d622019-09-12 19:03:13 +02006018 /* Reject any attempt to create a zero-length key so that we don't
6019 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 if (psa_get_key_bits(attributes) == 0) {
6021 return PSA_ERROR_INVALID_ARGUMENT;
6022 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006023
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 if (operation->alg == PSA_ALG_NONE) {
6025 return PSA_ERROR_BAD_STATE;
6026 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 if (!operation->can_output_key) {
6029 return PSA_ERROR_NOT_PERMITTED;
6030 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006031
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6033 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006034#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006036 /* Deriving a key in a secure element is not implemented yet. */
6037 status = PSA_ERROR_NOT_SUPPORTED;
6038 }
6039#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 if (status == PSA_SUCCESS) {
6041 status = psa_generate_derived_key_internal(slot,
6042 attributes->core.bits,
6043 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006044 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 if (status == PSA_SUCCESS) {
6046 status = psa_finish_key_creation(slot, driver, key);
6047 }
6048 if (status != PSA_SUCCESS) {
6049 psa_fail_key_creation(slot, driver);
6050 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006051
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006053}
6054
Gilles Peskineeab56e42018-07-12 17:12:33 +02006055
6056
6057/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006058/* Key derivation */
6059/****************************************************************/
6060
Steven Cooreman932ffb72021-02-15 12:14:32 +01006061#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006062static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006063{
6064#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6066 return 1;
6067 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006068#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006069#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6071 return 1;
6072 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006073#endif
6074#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6076 return 1;
6077 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006078#endif
6079#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6081 return 1;
6082 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006083#endif
6084#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6086 return 1;
6087 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006088#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006089#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6091 return 1;
6092 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006093#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306094#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6095 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6096 return 1;
6097 }
6098#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306099#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6100 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6101 return 1;
6102 }
6103#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006105}
6106
Gilles Peskine449bd832023-01-11 14:50:10 +01006107static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006108{
6109 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 psa_status_t status = psa_hash_setup(&operation, alg);
6111 psa_hash_abort(&operation);
6112 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006113}
6114
Gilles Peskine969c5d62019-01-16 15:53:06 +01006115static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006116 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006118{
Janos Follath5fe19732019-06-20 15:09:30 +01006119 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6120 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006122
Gilles Peskine969c5d62019-01-16 15:53:06 +01006123 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 if (!is_kdf_alg_supported(kdf_alg)) {
6125 return PSA_ERROR_NOT_SUPPORTED;
6126 }
John Durkop07cc04a2020-11-16 22:08:34 -08006127
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006128 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306129 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6131 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306132 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6133 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6134 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306135 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306136 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 if (hash_size == 0) {
6138 return PSA_ERROR_NOT_SUPPORTED;
6139 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006140
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006141 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6142 * we might fail later, which is somewhat unfriendly and potentially
6143 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 psa_status_t status = psa_hash_try_support(hash_alg);
6145 if (status != PSA_SUCCESS) {
6146 return status;
6147 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006148 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6151 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6152 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6153 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006154 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006155#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006156 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6158 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006159 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006161#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6162 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 operation->capacity = 255 * hash_size;
6164 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006165}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006166
Gilles Peskine449bd832023-01-11 14:50:10 +01006167static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006168{
6169#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 if (alg == PSA_ALG_ECDH) {
6171 return PSA_SUCCESS;
6172 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006173#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006174#if defined(PSA_WANT_ALG_FFDH)
6175 if (alg == PSA_ALG_FFDH) {
6176 return PSA_SUCCESS;
6177 }
6178#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006179 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006181}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006182
6183static int psa_key_derivation_allows_free_form_secret_input(
6184 psa_algorithm_t kdf_alg)
6185{
6186#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6187 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6188 return 0;
6189 }
6190#endif
6191 (void) kdf_alg;
6192 return 1;
6193}
John Durkop07cc04a2020-11-16 22:08:34 -08006194#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006195
Gilles Peskine449bd832023-01-11 14:50:10 +01006196psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6197 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006198{
6199 psa_status_t status;
6200
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (operation->alg != 0) {
6202 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006203 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006204
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6206 return PSA_ERROR_INVALID_ARGUMENT;
6207 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6208#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6209 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6210 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6211 status = psa_key_agreement_try_support(ka_alg);
6212 if (status != PSA_SUCCESS) {
6213 return status;
6214 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006215 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6216 return PSA_ERROR_INVALID_ARGUMENT;
6217 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6219#else
6220 return PSA_ERROR_NOT_SUPPORTED;
6221#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6222 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6223#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6224 status = psa_key_derivation_setup_kdf(operation, alg);
6225#else
6226 return PSA_ERROR_NOT_SUPPORTED;
6227#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6228 } else {
6229 return PSA_ERROR_INVALID_ARGUMENT;
6230 }
6231
6232 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006233 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 }
6235 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006236}
6237
Przemek Stekiel69c46792022-06-10 12:59:51 +02006238#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006239static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6240 psa_algorithm_t kdf_alg,
6241 psa_key_derivation_step_t step,
6242 const uint8_t *data,
6243 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006244{
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006246 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006248 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006249#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6251 return PSA_ERROR_INVALID_ARGUMENT;
6252 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006253#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 if (hkdf->state != HKDF_STATE_INIT) {
6255 return PSA_ERROR_BAD_STATE;
6256 } else {
6257 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6258 hash_alg,
6259 data, data_length);
6260 if (status != PSA_SUCCESS) {
6261 return status;
6262 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006263 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006265 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006266 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006267#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006269 /* We shouldn't be in different state as HKDF_EXPAND only allows
6270 * two inputs: SECRET (this case) and INFO which does not modify
6271 * the state. It could happen only if the hkdf
6272 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 if (hkdf->state != HKDF_STATE_INIT) {
6274 return PSA_ERROR_BAD_STATE;
6275 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006276
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006277 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6279 return PSA_ERROR_INVALID_ARGUMENT;
6280 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 memcpy(hkdf->prk, data, data_length);
6283 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006284#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006285 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006286 /* HKDF: If no salt was provided, use an empty salt.
6287 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006289#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6291 return PSA_ERROR_BAD_STATE;
6292 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006293#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6295 hash_alg,
6296 NULL, 0);
6297 if (status != PSA_SUCCESS) {
6298 return status;
6299 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006300 hkdf->state = HKDF_STATE_STARTED;
6301 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 if (hkdf->state != HKDF_STATE_STARTED) {
6303 return PSA_ERROR_BAD_STATE;
6304 }
6305 status = psa_mac_update(&hkdf->hmac,
6306 data, data_length);
6307 if (status != PSA_SUCCESS) {
6308 return status;
6309 }
6310 status = psa_mac_sign_finish(&hkdf->hmac,
6311 hkdf->prk,
6312 sizeof(hkdf->prk),
6313 &data_length);
6314 if (status != PSA_SUCCESS) {
6315 return status;
6316 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006317 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006318
Gilles Peskine2b522db2019-04-12 15:11:49 +02006319 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006320 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006321#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006323 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006325 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006327#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006328 {
6329 /* Block 0 is empty, and the next block will be
6330 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006332 }
6333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006335 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006336#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6338 return PSA_ERROR_INVALID_ARGUMENT;
6339 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006340#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006341#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6343 hkdf->state == HKDF_STATE_INIT) {
6344 return PSA_ERROR_BAD_STATE;
6345 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006346#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 if (hkdf->state == HKDF_STATE_OUTPUT) {
6348 return PSA_ERROR_BAD_STATE;
6349 }
6350 if (hkdf->info_set) {
6351 return PSA_ERROR_BAD_STATE;
6352 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006353 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 if (data_length != 0) {
6355 hkdf->info = mbedtls_calloc(1, data_length);
6356 if (hkdf->info == NULL) {
6357 return PSA_ERROR_INSUFFICIENT_MEMORY;
6358 }
6359 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006360 }
6361 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006363 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006365 }
6366}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006367#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006368
John Durkop07cc04a2020-11-16 22:08:34 -08006369#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6370 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006371static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6372 const uint8_t *data,
6373 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006374{
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6376 return PSA_ERROR_BAD_STATE;
6377 }
Janos Follathf08e2652019-06-13 09:05:41 +01006378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 if (data_length != 0) {
6380 prf->seed = mbedtls_calloc(1, data_length);
6381 if (prf->seed == NULL) {
6382 return PSA_ERROR_INSUFFICIENT_MEMORY;
6383 }
Janos Follathf08e2652019-06-13 09:05:41 +01006384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006386 prf->seed_length = data_length;
6387 }
Janos Follathf08e2652019-06-13 09:05:41 +01006388
Gilles Peskine43f958b2020-12-13 14:55:14 +01006389 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006390
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006392}
6393
Gilles Peskine449bd832023-01-11 14:50:10 +01006394static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6395 const uint8_t *data,
6396 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006397{
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6399 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6400 return PSA_ERROR_BAD_STATE;
6401 }
Janos Follath81550542019-06-13 14:26:34 +01006402
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 if (data_length != 0) {
6404 prf->secret = mbedtls_calloc(1, data_length);
6405 if (prf->secret == NULL) {
6406 return PSA_ERROR_INSUFFICIENT_MEMORY;
6407 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006408
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006410 prf->secret_length = data_length;
6411 }
Janos Follath81550542019-06-13 14:26:34 +01006412
Gilles Peskine43f958b2020-12-13 14:55:14 +01006413 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006414
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006416}
6417
Gilles Peskine449bd832023-01-11 14:50:10 +01006418static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6419 const uint8_t *data,
6420 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006421{
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6423 return PSA_ERROR_BAD_STATE;
6424 }
Janos Follath63028dd2019-06-13 09:15:47 +01006425
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 if (data_length != 0) {
6427 prf->label = mbedtls_calloc(1, data_length);
6428 if (prf->label == NULL) {
6429 return PSA_ERROR_INSUFFICIENT_MEMORY;
6430 }
Janos Follath63028dd2019-06-13 09:15:47 +01006431
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006433 prf->label_length = data_length;
6434 }
Janos Follath63028dd2019-06-13 09:15:47 +01006435
Gilles Peskine43f958b2020-12-13 14:55:14 +01006436 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006437
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006439}
6440
Gilles Peskine449bd832023-01-11 14:50:10 +01006441static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6442 psa_key_derivation_step_t step,
6443 const uint8_t *data,
6444 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006445{
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006447 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006449 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006451 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006453 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006455 }
6456}
John Durkop07cc04a2020-11-16 22:08:34 -08006457#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6458 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6459
6460#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6461static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6462 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006463 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006465{
6466 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6468 4 + data_length + prf->other_secret_length :
6469 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006470
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6472 return PSA_ERROR_INVALID_ARGUMENT;
6473 }
John Durkop07cc04a2020-11-16 22:08:34 -08006474
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 uint8_t *pms = mbedtls_calloc(1, pms_len);
6476 if (pms == NULL) {
6477 return PSA_ERROR_INSUFFICIENT_MEMORY;
6478 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006479 uint8_t *cur = pms;
6480
6481 /* pure-PSK:
6482 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006483 *
6484 * The premaster secret is formed as follows: if the PSK is N octets
6485 * long, concatenate a uint16 with the value N, N zero octets, a second
6486 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006487 *
6488 * mixed-PSK:
6489 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6490 * follows: concatenate a uint16 with the length of the other secret,
6491 * the other secret itself, uint16 with the length of PSK, and the
6492 * PSK itself.
6493 * For details please check:
6494 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6495 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6496 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006497 */
6498
Gilles Peskine449bd832023-01-11 14:50:10 +01006499 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6500 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6501 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6502 if (prf->other_secret_length != 0) {
6503 memcpy(cur, prf->other_secret, prf->other_secret_length);
6504 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006505 cur += prf->other_secret_length;
6506 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 } else {
6508 *cur++ = MBEDTLS_BYTE_1(data_length);
6509 *cur++ = MBEDTLS_BYTE_0(data_length);
6510 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006511 cur += data_length;
6512 }
6513
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 *cur++ = MBEDTLS_BYTE_1(data_length);
6515 *cur++ = MBEDTLS_BYTE_0(data_length);
6516 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006517 cur += data_length;
6518
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006519 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006520
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006521 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006523}
Janos Follath6660f0e2019-06-17 08:44:03 +01006524
Przemek Stekiele47201b2022-04-19 13:53:28 +02006525static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6526 psa_tls12_prf_key_derivation_t *prf,
6527 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006528 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006529{
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6531 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006532 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006533
6534 if (data_length != 0) {
6535 prf->other_secret = mbedtls_calloc(1, data_length);
6536 if (prf->other_secret == NULL) {
6537 return PSA_ERROR_INSUFFICIENT_MEMORY;
6538 }
6539
6540 memcpy(prf->other_secret, data, data_length);
6541 prf->other_secret_length = data_length;
6542 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006543 prf->other_secret_length = 0;
6544 }
6545
6546 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6547
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006549}
6550
Janos Follath6660f0e2019-06-17 08:44:03 +01006551static psa_status_t psa_tls12_prf_psk_to_ms_input(
6552 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006553 psa_key_derivation_step_t step,
6554 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006556{
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006558 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 return psa_tls12_prf_psk_to_ms_set_key(prf,
6560 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006561 break;
6562 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6564 data,
6565 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006566 break;
6567 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006569 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006570
Przemek Stekiele47201b2022-04-19 13:53:28 +02006571 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006572}
John Durkop07cc04a2020-11-16 22:08:34 -08006573#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006574
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006575#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6576static psa_status_t psa_tls12_ecjpake_to_pms_input(
6577 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006578 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006579 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006581{
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6583 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6584 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006585 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006586
6587 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 if (data[0] != 0x04) {
6589 return PSA_ERROR_INVALID_ARGUMENT;
6590 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006591
6592 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006594
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006596}
6597#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306598
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306599#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306600static psa_status_t psa_pbkdf2_set_input_cost(
6601 psa_pbkdf2_key_derivation_t *pbkdf2,
6602 psa_key_derivation_step_t step,
6603 uint64_t data)
6604{
6605 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6606 return PSA_ERROR_INVALID_ARGUMENT;
6607 }
6608
6609 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6610 return PSA_ERROR_BAD_STATE;
6611 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306612
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306613 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306614 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306615 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306616
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306617 if (data == 0) {
6618 return PSA_ERROR_INVALID_ARGUMENT;
6619 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306620
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306621 pbkdf2->input_cost = data;
6622 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6623
6624 return PSA_SUCCESS;
6625}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306626
6627static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6628 const uint8_t *data,
6629 size_t data_length)
6630{
Gilles Peskineead17662023-08-20 22:02:51 +02006631 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6632 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6633 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6634 /* Appending to existing salt. No state change. */
6635 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306636 return PSA_ERROR_BAD_STATE;
6637 }
6638
Gilles Peskineca57d782023-07-20 19:08:06 +02006639 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006640 /* Appending an empty string, nothing to do. */
6641 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306642 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306643
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306644 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6645 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306646 return PSA_ERROR_INSUFFICIENT_MEMORY;
6647 }
6648
Gilles Peskineead17662023-08-20 22:02:51 +02006649 if (pbkdf2->salt_length != 0) {
6650 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6651 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306652 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306653 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306654 mbedtls_free(pbkdf2->salt);
6655 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306656 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306657 return PSA_SUCCESS;
6658}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306659
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306660#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306661static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306662 const uint8_t *input,
6663 size_t input_len,
6664 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306665 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306666{
6667 psa_status_t status = PSA_SUCCESS;
6668 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6669 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306670 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306671 } else {
6672 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306673 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306674 }
6675 return status;
6676}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306677#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306678
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306679#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306680static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6681 size_t input_len,
6682 uint8_t *output,
6683 size_t *output_len)
6684{
6685 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306686 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306687 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306688 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306689 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6690 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6691 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306692 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306693 * mac_size as the driver function sets mac_output_length = mac_size
6694 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306695 status = psa_driver_wrapper_mac_compute(&attributes,
6696 zeros, sizeof(zeros),
6697 PSA_ALG_CMAC, input, input_len,
6698 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306699 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6700 128U,
6701 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306702 output_len);
6703 } else {
6704 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306705 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306706 }
6707 return status;
6708}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306709#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306710
6711static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306712 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306713 const uint8_t *data,
6714 size_t data_length)
6715{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306716 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306717 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6718 return PSA_ERROR_BAD_STATE;
6719 }
6720
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306721#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306722 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6723 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6724 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6725 pbkdf2->password,
6726 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306727 } else
6728#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6729#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6730 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306731 status = psa_pbkdf2_cmac_set_password(data, data_length,
6732 pbkdf2->password,
6733 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306734 } else
6735#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6736 {
6737 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306738 }
6739
6740 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6741
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306742 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306743}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306744
6745static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306746 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306747 psa_key_derivation_step_t step,
6748 const uint8_t *data,
6749 size_t data_length)
6750{
6751 switch (step) {
6752 case PSA_KEY_DERIVATION_INPUT_SALT:
6753 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6754 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306755 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306756 default:
6757 return PSA_ERROR_INVALID_ARGUMENT;
6758 }
6759}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306760#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306761
Gilles Peskineb8965192019-09-24 16:21:10 +02006762/** Check whether the given key type is acceptable for the given
6763 * input step of a key derivation.
6764 *
6765 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6766 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6767 * Both secret and non-secret inputs can alternatively have the type
6768 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6769 * that the input was passed as a buffer rather than via a key object.
6770 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006771static int psa_key_derivation_check_input_type(
6772 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006774{
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006776 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006777 if (key_type == PSA_KEY_TYPE_DERIVE) {
6778 return PSA_SUCCESS;
6779 }
6780 if (key_type == PSA_KEY_TYPE_NONE) {
6781 return PSA_SUCCESS;
6782 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006783 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006784 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 if (key_type == PSA_KEY_TYPE_DERIVE) {
6786 return PSA_SUCCESS;
6787 }
6788 if (key_type == PSA_KEY_TYPE_NONE) {
6789 return PSA_SUCCESS;
6790 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006791 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006792 case PSA_KEY_DERIVATION_INPUT_LABEL:
6793 case PSA_KEY_DERIVATION_INPUT_SALT:
6794 case PSA_KEY_DERIVATION_INPUT_INFO:
6795 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006796 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6797 return PSA_SUCCESS;
6798 }
6799 if (key_type == PSA_KEY_TYPE_NONE) {
6800 return PSA_SUCCESS;
6801 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006802 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306803 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6804 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6805 return PSA_SUCCESS;
6806 }
6807 if (key_type == PSA_KEY_TYPE_DERIVE) {
6808 return PSA_SUCCESS;
6809 }
6810 if (key_type == PSA_KEY_TYPE_NONE) {
6811 return PSA_SUCCESS;
6812 }
6813 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006814 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006815 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006816}
6817
Janos Follathb80a94e2019-06-12 15:54:46 +01006818static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006819 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006820 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006821 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006822 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006824{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006825 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006827
Gilles Peskine449bd832023-01-11 14:50:10 +01006828 status = psa_key_derivation_check_input_type(step, key_type);
6829 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006830 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006832
Przemek Stekiel69c46792022-06-10 12:59:51 +02006833#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6835 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6836 step, data, data_length);
6837 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006838#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006839#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6841 status = psa_tls12_prf_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_PRF */
6845#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6847 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6848 step, data, data_length);
6849 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006850#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006851#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006853 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6855 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006856#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306857#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306858 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306859 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6860 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306861 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306862#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006863 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006864 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006865 (void) data;
6866 (void) data_length;
6867 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006869 }
6870
Gilles Peskine224b0d62019-09-23 18:13:17 +02006871exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 if (status != PSA_SUCCESS) {
6873 psa_key_derivation_abort(operation);
6874 }
6875 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006876}
6877
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306878static psa_status_t psa_key_derivation_input_integer_internal(
6879 psa_key_derivation_operation_t *operation,
6880 psa_key_derivation_step_t step,
6881 uint64_t value)
6882{
6883 psa_status_t status;
6884 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6885
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306886#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306887 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306888 status = psa_pbkdf2_set_input_cost(
6889 &operation->ctx.pbkdf2, step, value);
6890 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306891#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306892 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306893 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306894 (void) value;
6895 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306896 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306897 }
6898
6899 if (status != PSA_SUCCESS) {
6900 psa_key_derivation_abort(operation);
6901 }
6902 return status;
6903}
6904
Janos Follath51f4a0f2019-06-14 11:35:55 +01006905psa_status_t psa_key_derivation_input_bytes(
6906 psa_key_derivation_operation_t *operation,
6907 psa_key_derivation_step_t step,
6908 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006910{
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 return psa_key_derivation_input_internal(operation, step,
6912 PSA_KEY_TYPE_NONE,
6913 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006914}
6915
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306916psa_status_t psa_key_derivation_input_integer(
6917 psa_key_derivation_operation_t *operation,
6918 psa_key_derivation_step_t step,
6919 uint64_t value)
6920{
6921 return psa_key_derivation_input_integer_internal(operation, step, value);
6922}
6923
Janos Follath51f4a0f2019-06-14 11:35:55 +01006924psa_status_t psa_key_derivation_input_key(
6925 psa_key_derivation_operation_t *operation,
6926 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006928{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006929 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006930 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006931 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006932
Ronald Cron5c522922020-11-14 16:35:34 +01006933 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006934 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6935 if (status != PSA_SUCCESS) {
6936 psa_key_derivation_abort(operation);
6937 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006938 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006939
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306940 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6941 * permission to output to a key object. */
6942 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6943 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006944 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006946
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 status = psa_key_derivation_input_internal(operation,
6948 step, slot->attr.type,
6949 slot->key.data,
6950 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006951
Ryan Everett1b70a072024-01-04 10:32:49 +00006952 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006953
Gilles Peskine449bd832023-01-11 14:50:10 +01006954 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006955}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006956
6957
6958
6959/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006960/* Key agreement */
6961/****************************************************************/
6962
Gilles Peskine449bd832023-01-11 14:50:10 +01006963psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6964 const uint8_t *key_buffer,
6965 size_t key_buffer_size,
6966 psa_algorithm_t alg,
6967 const uint8_t *peer_key,
6968 size_t peer_key_length,
6969 uint8_t *shared_secret,
6970 size_t shared_secret_size,
6971 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006972{
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006974#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006975 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006976 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6977 key_buffer_size, alg,
6978 peer_key, peer_key_length,
6979 shared_secret,
6980 shared_secret_size,
6981 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006982#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006983
6984#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6985 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006986 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006987 peer_key,
6988 peer_key_length,
6989 key_buffer,
6990 key_buffer_size,
6991 shared_secret,
6992 shared_secret_size,
6993 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006994#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6995
Gilles Peskine01d718c2018-09-18 12:01:02 +02006996 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006997 (void) attributes;
6998 (void) key_buffer;
6999 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007000 (void) peer_key;
7001 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007002 (void) shared_secret;
7003 (void) shared_secret_size;
7004 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007006 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007007}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007008
Aditya Deshpande17845b82022-10-13 17:21:01 +01007009/** Internal function for raw key agreement
7010 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007011 * to the driver's implementation if a driver is present.
7012 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007013 * (psa_key_agreement_raw_builtin).
7014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007015static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7016 psa_key_slot_t *private_key,
7017 const uint8_t *peer_key,
7018 size_t peer_key_length,
7019 uint8_t *shared_secret,
7020 size_t shared_secret_size,
7021 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007022{
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7024 return PSA_ERROR_NOT_SUPPORTED;
7025 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007026
7027 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007029 };
7030
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 return psa_driver_wrapper_key_agreement(&attributes,
7032 private_key->key.data,
7033 private_key->key.bytes, alg,
7034 peer_key, peer_key_length,
7035 shared_secret,
7036 shared_secret_size,
7037 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007038}
7039
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007040/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007041 * to potentially free embedded data structures and wipe confidential data.
7042 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007043static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7044 psa_key_derivation_step_t step,
7045 psa_key_slot_t *private_key,
7046 const uint8_t *peer_key,
7047 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007048{
7049 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007050 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007051 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007053
7054 /* Step 1: run the secret agreement algorithm to generate the shared
7055 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 status = psa_key_agreement_raw_internal(ka_alg,
7057 private_key,
7058 peer_key, peer_key_length,
7059 shared_secret,
7060 sizeof(shared_secret),
7061 &shared_secret_length);
7062 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007063 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007065
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007066 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007067 * the shared secret. A shared secret is permitted wherever a key
7068 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 status = psa_key_derivation_input_internal(operation, step,
7070 PSA_KEY_TYPE_DERIVE,
7071 shared_secret,
7072 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007073exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7075 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007076}
7077
Gilles Peskine449bd832023-01-11 14:50:10 +01007078psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7079 psa_key_derivation_step_t step,
7080 mbedtls_svc_key_id_t private_key,
7081 const uint8_t *peer_key,
7082 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007083{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007084 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007085 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007086 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007087
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7089 return PSA_ERROR_INVALID_ARGUMENT;
7090 }
Ronald Cron5c522922020-11-14 16:35:34 +01007091 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7093 if (status != PSA_SUCCESS) {
7094 return status;
7095 }
7096 status = psa_key_agreement_internal(operation, step,
7097 slot,
7098 peer_key, peer_key_length);
7099 if (status != PSA_SUCCESS) {
7100 psa_key_derivation_abort(operation);
7101 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007102 /* If a private key has been added as SECRET, we allow the derived
7103 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007105 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007107 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007108
Ryan Everett1b70a072024-01-04 10:32:49 +00007109 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007110
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007112}
7113
Gilles Peskine449bd832023-01-11 14:50:10 +01007114psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7115 mbedtls_svc_key_id_t private_key,
7116 const uint8_t *peer_key,
7117 size_t peer_key_length,
7118 uint8_t *output,
7119 size_t output_size,
7120 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007121{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007122 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007123 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007124 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007125 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007126
Gilles Peskine449bd832023-01-11 14:50:10 +01007127 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007128 status = PSA_ERROR_INVALID_ARGUMENT;
7129 goto exit;
7130 }
Ronald Cron5c522922020-11-14 16:35:34 +01007131 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7133 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007134 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007136
Gilles Peskine3e561302022-04-14 00:17:15 +02007137 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7138 * for the output size. The PSA specification only guarantees that this
7139 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7140 * but it might be nice to allow smaller buffers if the output fits.
7141 * At the time of writing this comment, with only ECDH implemented,
7142 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7143 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7144 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007145 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7147 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007148 status = PSA_ERROR_BUFFER_TOO_SMALL;
7149 goto exit;
7150 }
7151
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 status = psa_key_agreement_raw_internal(alg, slot,
7153 peer_key, peer_key_length,
7154 output, output_size,
7155 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007156
7157exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007159 /* If an error happens and is not handled properly, the output
7160 * may be used as a key to protect sensitive data. Arrange for such
7161 * a key to be random, which is likely to result in decryption or
7162 * verification errors. This is better than filling the buffer with
7163 * some constant data such as zeros, which would result in the data
7164 * being protected with a reproducible, easily knowable key.
7165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007167 *output_length = output_size;
7168 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007169
Ryan Everett1b70a072024-01-04 10:32:49 +00007170 unlock_status = psa_unregister_read(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007171
Gilles Peskine449bd832023-01-11 14:50:10 +01007172 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007173}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007174
7175
Gilles Peskine30524eb2020-11-13 17:02:26 +01007176
Gilles Peskine86a440b2018-11-12 18:39:40 +01007177/****************************************************************/
7178/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007179/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007180
Gilles Peskine672a7712023-04-28 21:00:28 +02007181#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7182#include "entropy_poll.h"
7183#endif
7184
Gilles Peskine30524eb2020-11-13 17:02:26 +01007185/** Initialize the PSA random generator.
7186 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007187static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007188{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007189#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007190 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007191#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7192
Gilles Peskine30524eb2020-11-13 17:02:26 +01007193 /* Set default configuration if
7194 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007196 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007197 }
7198 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007199 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007200 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007201
Gilles Peskine449bd832023-01-11 14:50:10 +01007202 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007203#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7204 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7205 /* The PSA entropy injection feature depends on using NV seed as an entropy
7206 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007207 mbedtls_entropy_add_source(&rng->entropy,
7208 mbedtls_nv_seed_poll, NULL,
7209 MBEDTLS_ENTROPY_BLOCK_SIZE,
7210 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007211#endif
7212
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007214#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007215}
7216
7217/** Deinitialize the PSA random generator.
7218 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007219static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007220{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007221#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007222 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007223#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti83e0de82023-11-24 12:13:05 +01007224 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7225 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007226#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007227}
7228
7229/** Seed the PSA random generator.
7230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007231static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007232{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007233#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7234 /* Do nothing: the external RNG seeds itself. */
7235 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007237#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007238 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7240 drbg_seed, sizeof(drbg_seed) - 1);
7241 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007242#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007243}
7244
Gilles Peskine449bd832023-01-11 14:50:10 +01007245psa_status_t psa_generate_random(uint8_t *output,
7246 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007247{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007248 GUARD_MODULE_INITIALIZED;
7249
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007250#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7251
7252 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7254 output, output_size,
7255 &output_length);
7256 if (status != PSA_SUCCESS) {
7257 return status;
7258 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007259 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007260 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 if (output_length != output_size) {
7262 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7263 }
7264 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007265
7266#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7267
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007269 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007270 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7271 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7272 output_size);
7273 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7274 output, request_size);
7275 if (ret != 0) {
7276 return mbedtls_to_psa_error(ret);
7277 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007278 output_size -= request_size;
7279 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007280 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007282#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007283}
7284
Gilles Peskine8814fc42020-12-14 15:33:44 +01007285/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007286 *
7287 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7288 * `psa_generate_random(...)`. The state parameter is ignored since the
7289 * PSA API doesn't support passing an explicit state.
7290 *
7291 * In the non-external case, psa_generate_random() calls an
7292 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7293 * and semantics as mbedtls_psa_get_random(). As an optimization,
7294 * instead of doing this back-and-forth between the PSA API and the
7295 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7296 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007297 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007298#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7299int mbedtls_psa_get_random(void *p_rng,
7300 unsigned char *output,
7301 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007302{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007303 /* This function takes a pointer to the RNG state because that's what
7304 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7305 * its own state internally and doesn't let the caller access that state.
7306 * So we just ignore the state parameter, and in practice we'll pass
7307 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007308 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 psa_status_t status = psa_generate_random(output, output_size);
7310 if (status == PSA_SUCCESS) {
7311 return 0;
7312 } else {
7313 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7314 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007315}
7316#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7317
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007318#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007319psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7320 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007321{
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 if (global_data.initialized) {
7323 return PSA_ERROR_NOT_PERMITTED;
7324 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007325
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7327 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7328 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7329 return PSA_ERROR_INVALID_ARGUMENT;
7330 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007331
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007333}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007334#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007335
Ronald Cron3772afe2021-02-08 16:10:05 +01007336/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007337 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007338 * \param type The key type
7339 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007340 *
7341 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007342 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007343 * \retval #PSA_ERROR_INVALID_ARGUMENT
7344 * The size in bits of the key is not valid.
7345 * \retval #PSA_ERROR_NOT_SUPPORTED
7346 * The type and/or the size in bits of the key or the combination of
7347 * the two is not supported.
7348 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007349static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007350 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007351{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007352 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007353
Gilles Peskine449bd832023-01-11 14:50:10 +01007354 if (key_type_is_raw_bytes(type)) {
7355 status = psa_validate_unstructured_key_bit_size(type, bits);
7356 if (status != PSA_SUCCESS) {
7357 return status;
7358 }
7359 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007360#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007361 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7362 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7363 return PSA_ERROR_NOT_SUPPORTED;
7364 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007365 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007366 return PSA_ERROR_NOT_SUPPORTED;
7367 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007368
Ronald Cron01b2aba2020-10-05 09:42:02 +02007369 /* Accept only byte-aligned keys, for the same reasons as
7370 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007371 if (bits % 8 != 0) {
7372 return PSA_ERROR_NOT_SUPPORTED;
7373 }
7374 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007375#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007376
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007377#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007379 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007380 return PSA_SUCCESS;
7381 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007382#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007383
Valerio Settia55f0422023-07-10 15:34:41 +02007384#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007385 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007386 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007387 return PSA_ERROR_NOT_SUPPORTED;
7388 }
7389 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007390#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007391 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007393 }
7394
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007396}
7397
Ronald Cron55ed0592020-10-05 10:30:40 +02007398psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007399 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007400 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007401{
7402 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007403 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007404
Gilles Peskine449bd832023-01-11 14:50:10 +01007405 if ((attributes->domain_parameters == NULL) &&
7406 (attributes->domain_parameters_size != 0)) {
7407 return PSA_ERROR_INVALID_ARGUMENT;
7408 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007409
Gilles Peskine449bd832023-01-11 14:50:10 +01007410 if (key_type_is_raw_bytes(type)) {
7411 status = psa_generate_random(key_buffer, key_buffer_size);
7412 if (status != PSA_SUCCESS) {
7413 return status;
7414 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007415
David Brown12ca5032021-02-11 11:02:00 -07007416#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007417 if (type == PSA_KEY_TYPE_DES) {
7418 psa_des_set_key_parity(key_buffer, key_buffer_size);
7419 }
David Brown8107e312021-02-12 08:08:46 -07007420#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007421 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007422
Valerio Setti76df8c12023-07-11 14:11:28 +02007423#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007424 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7425 return mbedtls_psa_rsa_generate_key(attributes,
7426 key_buffer,
7427 key_buffer_size,
7428 key_buffer_length);
7429 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007430#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007431
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007432#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7434 return mbedtls_psa_ecp_generate_key(attributes,
7435 key_buffer,
7436 key_buffer_size,
7437 key_buffer_length);
7438 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007439#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007440
Valerio Settia55f0422023-07-10 15:34:41 +02007441#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007442 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7443 return mbedtls_psa_ffdh_generate_key(attributes,
7444 key_buffer,
7445 key_buffer_size,
7446 key_buffer_length);
7447 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007448#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007449 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 (void) key_buffer_length;
7451 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007452 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007453
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007455}
Darryl Green0c6575a2018-11-07 16:05:30 +00007456
Gilles Peskine449bd832023-01-11 14:50:10 +01007457psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7458 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007459{
7460 psa_status_t status;
7461 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007462 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007463 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007464
Ronald Cron81709fc2020-11-14 12:10:32 +01007465 *key = MBEDTLS_SVC_KEY_ID_INIT;
7466
Gilles Peskine0f84d622019-09-12 19:03:13 +02007467 /* Reject any attempt to create a zero-length key so that we don't
7468 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007469 if (psa_get_key_bits(attributes) == 0) {
7470 return PSA_ERROR_INVALID_ARGUMENT;
7471 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007472
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007473 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007474 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7475 return PSA_ERROR_INVALID_ARGUMENT;
7476 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007477
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7479 &slot, &driver);
7480 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007481 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 }
Gilles Peskine11792082019-08-06 18:36:36 +02007483
Ronald Cron977c2472020-10-13 08:32:21 +02007484 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307485 * storage ( thus not in the case of generating a key in a secure element
7486 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7487 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007488 if (slot->key.data == NULL) {
7489 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7490 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007491 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007492 attributes->core.type, attributes->core.bits);
7493 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007494 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007495 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007496
7497 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007498 attributes->core.type,
7499 attributes->core.bits);
7500 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007501 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 attributes, &key_buffer_size);
7503 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007504 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007505 }
Ronald Cron977c2472020-10-13 08:32:21 +02007506 }
Ronald Cron977c2472020-10-13 08:32:21 +02007507
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7509 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007510 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007511 }
Ronald Cron977c2472020-10-13 08:32:21 +02007512 }
7513
Gilles Peskine449bd832023-01-11 14:50:10 +01007514 status = psa_driver_wrapper_generate_key(attributes,
7515 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007516
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 if (status != PSA_SUCCESS) {
7518 psa_remove_key_data_from_memory(slot);
7519 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007520
Gilles Peskine11792082019-08-06 18:36:36 +02007521exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007522 if (status == PSA_SUCCESS) {
7523 status = psa_finish_key_creation(slot, driver, key);
7524 }
7525 if (status != PSA_SUCCESS) {
7526 psa_fail_key_creation(slot, driver);
7527 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007528
Gilles Peskine449bd832023-01-11 14:50:10 +01007529 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007530}
7531
Gilles Peskinee59236f2018-01-27 23:32:46 +01007532/****************************************************************/
7533/* Module setup */
7534/****************************************************************/
7535
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007536#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007537psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 void (* entropy_init)(mbedtls_entropy_context *ctx),
7539 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007540{
Gilles Peskine449bd832023-01-11 14:50:10 +01007541 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7542 return PSA_ERROR_BAD_STATE;
7543 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007544 global_data.rng.entropy_init = entropy_init;
7545 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007547}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007548#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007549
Gilles Peskine449bd832023-01-11 14:50:10 +01007550void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007551{
Gilles Peskine449bd832023-01-11 14:50:10 +01007552 psa_wipe_all_key_slots();
Valerio Setti83e0de82023-11-24 12:13:05 +01007553 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7554 mbedtls_psa_random_free(&global_data.rng);
7555 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007556 /* Wipe all remaining data, including configuration.
7557 * In particular, this sets all state indicator to the value
7558 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007559 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007560
7561 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007563}
7564
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007565#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7566/** Recover a transaction that was interrupted by a power failure.
7567 *
7568 * This function is called during initialization, before psa_crypto_init()
7569 * returns. If this function returns a failure status, the initialization
7570 * fails.
7571 */
7572static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007574{
Gilles Peskine449bd832023-01-11 14:50:10 +01007575 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007576 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7577 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 /* TODO - fall through to the failure case until this
7579 * is implemented.
7580 * https://github.com/ARMmbed/mbed-crypto/issues/218
7581 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007582 default:
7583 /* We found an unsupported transaction in the storage.
7584 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007585 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007586 }
7587}
7588#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7589
Gilles Peskine449bd832023-01-11 14:50:10 +01007590psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007591{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007592 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007593
Gilles Peskinec6b69072018-11-20 21:42:52 +01007594 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 if (global_data.initialized != 0) {
7596 return PSA_SUCCESS;
7597 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007598
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007599 /* Init drivers */
7600 status = psa_driver_wrapper_init();
7601 if (status != PSA_SUCCESS) {
7602 goto exit;
7603 }
7604 global_data.drivers_initialized = 1;
7605
Valerio Setti402cfba2023-11-13 10:24:32 +01007606 status = psa_initialize_key_slots();
7607 if (status != PSA_SUCCESS) {
7608 goto exit;
7609 }
7610
Gilles Peskine30524eb2020-11-13 17:02:26 +01007611 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007612 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007613 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007614 status = mbedtls_psa_random_seed(&global_data.rng);
7615 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007616 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007618 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007619
Gilles Peskine4b734222019-07-24 15:56:31 +02007620#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007621 status = psa_crypto_load_transaction();
7622 if (status == PSA_SUCCESS) {
7623 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7624 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007625 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 }
7627 status = psa_crypto_stop_transaction();
7628 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007629 /* There's no transaction to complete. It's all good. */
7630 status = PSA_SUCCESS;
7631 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007632#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007633
Gilles Peskinec6b69072018-11-20 21:42:52 +01007634 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007635 global_data.initialized = 1;
7636
7637exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007638 if (status != PSA_SUCCESS) {
7639 mbedtls_psa_crypto_free();
7640 }
7641 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007642}
7643
Yanray Wangffc3c482023-07-11 12:01:04 +08007644#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007645psa_status_t psa_crypto_driver_pake_get_password_len(
7646 const psa_crypto_driver_pake_inputs_t *inputs,
7647 size_t *password_len)
7648{
7649 if (inputs->password_len == 0) {
7650 return PSA_ERROR_BAD_STATE;
7651 }
7652
7653 *password_len = inputs->password_len;
7654
7655 return PSA_SUCCESS;
7656}
7657
7658psa_status_t psa_crypto_driver_pake_get_password(
7659 const psa_crypto_driver_pake_inputs_t *inputs,
7660 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7661{
7662 if (inputs->password_len == 0) {
7663 return PSA_ERROR_BAD_STATE;
7664 }
7665
7666 if (buffer_size < inputs->password_len) {
7667 return PSA_ERROR_BUFFER_TOO_SMALL;
7668 }
7669
7670 memcpy(buffer, inputs->password, inputs->password_len);
7671 *buffer_length = inputs->password_len;
7672
7673 return PSA_SUCCESS;
7674}
7675
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007676psa_status_t psa_crypto_driver_pake_get_user_len(
7677 const psa_crypto_driver_pake_inputs_t *inputs,
7678 size_t *user_len)
7679{
7680 if (inputs->user_len == 0) {
7681 return PSA_ERROR_BAD_STATE;
7682 }
7683
7684 *user_len = inputs->user_len;
7685
7686 return PSA_SUCCESS;
7687}
7688
7689psa_status_t psa_crypto_driver_pake_get_user(
7690 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007691 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007692{
7693 if (inputs->user_len == 0) {
7694 return PSA_ERROR_BAD_STATE;
7695 }
7696
Przemek Stekielc0e62502023-03-14 11:49:36 +01007697 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007698 return PSA_ERROR_BUFFER_TOO_SMALL;
7699 }
7700
Przemek Stekielc0e62502023-03-14 11:49:36 +01007701 memcpy(user_id, inputs->user, inputs->user_len);
7702 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007703
7704 return PSA_SUCCESS;
7705}
7706
7707psa_status_t psa_crypto_driver_pake_get_peer_len(
7708 const psa_crypto_driver_pake_inputs_t *inputs,
7709 size_t *peer_len)
7710{
7711 if (inputs->peer_len == 0) {
7712 return PSA_ERROR_BAD_STATE;
7713 }
7714
7715 *peer_len = inputs->peer_len;
7716
7717 return PSA_SUCCESS;
7718}
7719
7720psa_status_t psa_crypto_driver_pake_get_peer(
7721 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007722 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007723{
7724 if (inputs->peer_len == 0) {
7725 return PSA_ERROR_BAD_STATE;
7726 }
7727
Przemek Stekielc0e62502023-03-14 11:49:36 +01007728 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007729 return PSA_ERROR_BUFFER_TOO_SMALL;
7730 }
7731
Przemek Stekielc0e62502023-03-14 11:49:36 +01007732 memcpy(peer_id, inputs->peer, inputs->peer_len);
7733 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007734
7735 return PSA_SUCCESS;
7736}
7737
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007738psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7739 const psa_crypto_driver_pake_inputs_t *inputs,
7740 psa_pake_cipher_suite_t *cipher_suite)
7741{
7742 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7743 return PSA_ERROR_BAD_STATE;
7744 }
7745
7746 *cipher_suite = inputs->cipher_suite;
7747
7748 return PSA_SUCCESS;
7749}
7750
Neil Armstronga7d08c32022-06-01 18:21:20 +02007751psa_status_t psa_pake_setup(
7752 psa_pake_operation_t *operation,
7753 const psa_pake_cipher_suite_t *cipher_suite)
7754{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007755 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007756
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007757 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007758 status = PSA_ERROR_BAD_STATE;
7759 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007760 }
7761
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007762 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007763 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007764 status = PSA_ERROR_INVALID_ARGUMENT;
7765 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007766 }
7767
Przemek Stekiel51eac532022-12-07 11:04:51 +01007768 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7769
Przemek Stekiele12ed362022-12-21 12:54:46 +01007770 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007771 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7772 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007773 operation->data.inputs.cipher_suite = *cipher_suite;
7774
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007775#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007776 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007777 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007778 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007779
David Horstmann16f01512023-06-14 17:21:07 +01007780 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007781 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007782 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007783#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007784 {
7785 status = PSA_ERROR_NOT_SUPPORTED;
7786 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007787 }
7788
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007789 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7790
Przemek Stekiel51eac532022-12-07 11:04:51 +01007791 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007792exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007793 psa_pake_abort(operation);
7794 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007795}
7796
7797psa_status_t psa_pake_set_password_key(
7798 psa_pake_operation_t *operation,
7799 mbedtls_svc_key_id_t password)
7800{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007801 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007802 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7803 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007804 psa_key_attributes_t attributes;
7805 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007806
Przemek Stekiel51eac532022-12-07 11:04:51 +01007807 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007808 status = PSA_ERROR_BAD_STATE;
7809 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007810 }
7811
Przemek Stekiel6c764412022-11-22 14:05:12 +01007812 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7813 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007814 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007815 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007816 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007817 }
7818
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007819 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007820 .core = slot->attr
7821 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007822
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007823 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007824
Przemek Stekiel51eac532022-12-07 11:04:51 +01007825 if (type != PSA_KEY_TYPE_PASSWORD &&
7826 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7827 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007828 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007829 }
7830
Przemek Stekiel51eac532022-12-07 11:04:51 +01007831 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7832 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007833 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007834 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007835 }
7836
7837 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7838 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007839 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007840exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007841 if (status != PSA_SUCCESS) {
7842 psa_pake_abort(operation);
7843 }
Ryan Everett1b70a072024-01-04 10:32:49 +00007844 unlock_status = psa_unregister_read(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007845 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007846}
7847
7848psa_status_t psa_pake_set_user(
7849 psa_pake_operation_t *operation,
7850 const uint8_t *user_id,
7851 size_t user_id_len)
7852{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007853 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007854
7855 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007856 status = PSA_ERROR_BAD_STATE;
7857 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007858 }
7859
Przemek Stekiel51eac532022-12-07 11:04:51 +01007860 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007861 status = PSA_ERROR_INVALID_ARGUMENT;
7862 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007863 }
7864
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007865 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007866 status = PSA_ERROR_BAD_STATE;
7867 goto exit;
7868 }
7869
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007870 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7871 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007872 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7873 goto exit;
7874 }
7875
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007876 memcpy(operation->data.inputs.user, user_id, user_id_len);
7877 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007878
7879 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007880exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007881 psa_pake_abort(operation);
7882 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007883}
7884
7885psa_status_t psa_pake_set_peer(
7886 psa_pake_operation_t *operation,
7887 const uint8_t *peer_id,
7888 size_t peer_id_len)
7889{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007890 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007891
7892 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007893 status = PSA_ERROR_BAD_STATE;
7894 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007895 }
7896
Przemek Stekiel51eac532022-12-07 11:04:51 +01007897 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007898 status = PSA_ERROR_INVALID_ARGUMENT;
7899 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007900 }
7901
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007902 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007903 status = PSA_ERROR_BAD_STATE;
7904 goto exit;
7905 }
7906
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007907 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7908 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007909 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7910 goto exit;
7911 }
7912
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007913 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7914 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007915
7916 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007917exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007918 psa_pake_abort(operation);
7919 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007920}
7921
7922psa_status_t psa_pake_set_role(
7923 psa_pake_operation_t *operation,
7924 psa_pake_role_t role)
7925{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007927
Przemek Stekiel51eac532022-12-07 11:04:51 +01007928 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007929 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007930 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007931 }
7932
Przemek Stekiel09104b82023-03-06 14:11:51 +01007933 switch (operation->alg) {
7934#if defined(PSA_WANT_ALG_JPAKE)
7935 case PSA_ALG_JPAKE:
7936 if (role == PSA_PAKE_ROLE_NONE) {
7937 return PSA_SUCCESS;
7938 }
7939 status = PSA_ERROR_INVALID_ARGUMENT;
7940 break;
7941#endif
7942 default:
7943 (void) role;
7944 status = PSA_ERROR_NOT_SUPPORTED;
7945 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007946 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007947exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007948 psa_pake_abort(operation);
7949 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007950}
7951
David Horstmanne7f21e62023-05-12 18:17:21 +01007952/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007953#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007954static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007955 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007956{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007957 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007958 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007959 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007960
David Horstmann024e5c52023-06-14 15:48:21 +01007961 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007962 is_x1 = (stage->outputs < 1);
7963 } else {
7964 is_x1 = (stage->inputs < 1);
7965 }
7966
David Horstmann74a3d8c2023-06-14 18:28:19 +01007967 key_share_step = is_x1 ?
7968 PSA_JPAKE_X1_STEP_KEY_SHARE :
7969 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007970 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007971 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7972 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7973 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7974 } else {
7975 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007976 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01007977 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01007978}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007979#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007980
Przemek Stekiel2797d372022-12-22 11:19:22 +01007981static psa_status_t psa_pake_complete_inputs(
7982 psa_pake_operation_t *operation)
7983{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007984 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007985 /* Create copy of the inputs on stack as inputs share memory
7986 with the driver context which will be setup by the driver. */
7987 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007988
Przemek Stekielfde11282023-03-13 16:06:09 +01007989 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007990 return PSA_ERROR_BAD_STATE;
7991 }
7992
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007993 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007994 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7995 return PSA_ERROR_BAD_STATE;
7996 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007997 }
7998
Przemek Stekiel18620a32023-01-17 16:34:52 +01007999 /* Clear driver context */
8000 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8001
8002 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008003
8004 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008005 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008006
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008007 /* User and peer are translated to role. */
8008 mbedtls_free(inputs.user);
8009 mbedtls_free(inputs.peer);
8010
Przemek Stekiel2797d372022-12-22 11:19:22 +01008011 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008012#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008013 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008014 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008015 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008016#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008017 {
8018 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008019 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008020 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008021 return status;
8022}
8023
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008024#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008025static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008026 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008027 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008028 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008029{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008030 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8031 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8032 step != PSA_PAKE_STEP_ZK_PROOF) {
8033 return PSA_ERROR_INVALID_ARGUMENT;
8034 }
8035
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008036 psa_jpake_computation_stage_t *computation_stage =
8037 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008038
David Horstmann5da95602023-06-08 15:37:12 +01008039 if (computation_stage->round != PSA_JPAKE_FIRST &&
8040 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008041 return PSA_ERROR_BAD_STATE;
8042 }
8043
David Horstmanne7f21e62023-05-12 18:17:21 +01008044 /* Check that the step we are given is the one we were expecting */
8045 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008046 return PSA_ERROR_BAD_STATE;
8047 }
8048
David Horstmanne7f21e62023-05-12 18:17:21 +01008049 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8050 computation_stage->inputs == 0 &&
8051 computation_stage->outputs == 0) {
8052 /* Start of the round, so function decides whether we are inputting
8053 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008054 computation_stage->io_mode = io_mode;
8055 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008056 /* Middle of the round so the mode we are in must match the function
8057 * called by the user */
8058 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008059 }
8060
8061 return PSA_SUCCESS;
8062}
8063
David Horstmanne7f21e62023-05-12 18:17:21 +01008064static psa_status_t psa_jpake_epilogue(
8065 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008066 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008067{
David Horstmanne7f21e62023-05-12 18:17:21 +01008068 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008069 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008070
David Horstmanne7f21e62023-05-12 18:17:21 +01008071 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8072 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008073 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008074 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008075 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008076 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008077 }
8078 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008079 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008080 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008081 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008082 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008083 }
8084 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008085 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8086 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008087 /* End of a round, move to the next round */
8088 stage->inputs = 0;
8089 stage->outputs = 0;
8090 stage->round++;
8091 }
8092 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008093 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008094 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008095 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008096 return PSA_SUCCESS;
8097}
David Horstmanne7f21e62023-05-12 18:17:21 +01008098
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008099#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008100
Neil Armstronga7d08c32022-06-01 18:21:20 +02008101psa_status_t psa_pake_output(
8102 psa_pake_operation_t *operation,
8103 psa_pake_step_t step,
8104 uint8_t *output,
8105 size_t output_size,
8106 size_t *output_length)
8107{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008108 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008109 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008110 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008111
8112 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008113 status = psa_pake_complete_inputs(operation);
8114 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008115 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008116 }
8117 }
8118
8119 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008120 status = PSA_ERROR_BAD_STATE;
8121 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008122 }
8123
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008124 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008125 status = PSA_ERROR_INVALID_ARGUMENT;
8126 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008127 }
8128
Przemek Stekiele12ed362022-12-21 12:54:46 +01008129 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008130#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008131 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008132 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008133 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008134 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008135 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008136 driver_step = convert_jpake_computation_stage_to_driver_step(
8137 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008138 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008139#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008140 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008141 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008142 status = PSA_ERROR_NOT_SUPPORTED;
8143 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008144 }
8145
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008146 status = psa_driver_wrapper_pake_output(operation, driver_step,
8147 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008148
8149 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008150 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008151 }
8152
8153 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008154#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008155 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008156 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008157 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008158 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008159 }
8160 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008161#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008162 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008163 status = PSA_ERROR_NOT_SUPPORTED;
8164 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008165 }
8166
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008167 return PSA_SUCCESS;
8168exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008169 psa_pake_abort(operation);
8170 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008171}
8172
8173psa_status_t psa_pake_input(
8174 psa_pake_operation_t *operation,
8175 psa_pake_step_t step,
8176 const uint8_t *input,
8177 size_t input_length)
8178{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008179 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008180 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008181 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8182 operation->primitive,
8183 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008184
8185 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008186 status = psa_pake_complete_inputs(operation);
8187 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008188 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008189 }
8190 }
8191
8192 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008193 status = PSA_ERROR_BAD_STATE;
8194 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008195 }
8196
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008197 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008198 status = PSA_ERROR_INVALID_ARGUMENT;
8199 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008200 }
8201
Przemek Stekiele12ed362022-12-21 12:54:46 +01008202 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008203#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008204 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008205 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008206 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008207 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008208 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008209 driver_step = convert_jpake_computation_stage_to_driver_step(
8210 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008211 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008212#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008213 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008214 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008215 status = PSA_ERROR_NOT_SUPPORTED;
8216 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008217 }
8218
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008219 status = psa_driver_wrapper_pake_input(operation, driver_step,
8220 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008221
8222 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008223 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008224 }
8225
8226 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008227#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008228 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008229 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008230 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008231 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008232 }
8233 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008234#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008235 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008236 status = PSA_ERROR_NOT_SUPPORTED;
8237 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008238 }
8239
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008240 return PSA_SUCCESS;
8241exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008242 psa_pake_abort(operation);
8243 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008244}
8245
8246psa_status_t psa_pake_get_implicit_key(
8247 psa_pake_operation_t *operation,
8248 psa_key_derivation_operation_t *output)
8249{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008250 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008251 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008252 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008253 size_t shared_key_len = 0;
8254
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008255 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008256 status = PSA_ERROR_BAD_STATE;
8257 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008258 }
8259
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008260#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008261 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008262 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008263 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008264 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008265 status = PSA_ERROR_BAD_STATE;
8266 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008267 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008268 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008269#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008270 {
8271 status = PSA_ERROR_NOT_SUPPORTED;
8272 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008273 }
8274
Przemek Stekiel0c781802022-11-29 14:53:13 +01008275 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8276 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008277 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008278 &shared_key_len);
8279
8280 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008281 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008282 }
8283
8284 status = psa_key_derivation_input_bytes(output,
8285 PSA_KEY_DERIVATION_INPUT_SECRET,
8286 shared_key,
8287 shared_key_len);
8288
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008289 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008290exit:
8291 abort_status = psa_pake_abort(operation);
8292 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008293}
8294
8295psa_status_t psa_pake_abort(
8296 psa_pake_operation_t *operation)
8297{
Przemek Stekield69dca92023-01-31 19:59:20 +01008298 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008299
Przemek Stekield69dca92023-01-31 19:59:20 +01008300 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008301 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008302 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008303
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008304 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8305 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008306 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008307 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008308 }
8309 if (operation->data.inputs.user != NULL) {
8310 mbedtls_free(operation->data.inputs.user);
8311 }
8312 if (operation->data.inputs.peer != NULL) {
8313 mbedtls_free(operation->data.inputs.peer);
8314 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008315 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008316 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008317
Przemek Stekield69dca92023-01-31 19:59:20 +01008318 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008319}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008320#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008321
Gilles Peskinee59236f2018-01-27 23:32:46 +01008322#endif /* MBEDTLS_PSA_CRYPTO_C */