blob: dd5b4465f3514831ee2c1d252024a3309e6bb4f9 [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"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020073#include "md_psa.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 Peskine7bcfc0a2018-06-18 21:49:39 +0200420
421
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100422/****************************************************************/
423/* Key management */
424/****************************************************************/
425
Valerio Settia9aab1a2023-06-19 13:39:54 +0200426#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200427psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
428 size_t *bits)
429{
430 switch (grpid) {
Valerio Settic437fae2023-09-08 14:58:03 +0200431#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200432 case MBEDTLS_ECP_DP_SECP192R1:
433 *bits = 192;
434 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100435#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200436#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200437 case MBEDTLS_ECP_DP_SECP224R1:
438 *bits = 224;
439 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100440#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200441#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200442 case MBEDTLS_ECP_DP_SECP256R1:
443 *bits = 256;
444 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100445#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200446#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200447 case MBEDTLS_ECP_DP_SECP384R1:
448 *bits = 384;
449 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100450#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200451#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200452 case MBEDTLS_ECP_DP_SECP521R1:
453 *bits = 521;
454 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100455#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200456#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200457 case MBEDTLS_ECP_DP_BP256R1:
458 *bits = 256;
459 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100460#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200461#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200462 case MBEDTLS_ECP_DP_BP384R1:
463 *bits = 384;
464 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100465#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200466#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200467 case MBEDTLS_ECP_DP_BP512R1:
468 *bits = 512;
469 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100470#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200471#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200472 case MBEDTLS_ECP_DP_CURVE25519:
473 *bits = 255;
474 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100475#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200476#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200477 case MBEDTLS_ECP_DP_SECP192K1:
478 *bits = 192;
479 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100480#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200481#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200482 case MBEDTLS_ECP_DP_SECP224K1:
483 *bits = 224;
484 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100485#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200486#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200487 case MBEDTLS_ECP_DP_SECP256K1:
488 *bits = 256;
489 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100490#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200491#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200492 case MBEDTLS_ECP_DP_CURVE448:
493 *bits = 448;
494 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100495#endif
Valerio Settibc2b1d32023-06-19 12:15:13 +0200496 default:
497 *bits = 0;
498 return 0;
499 }
500}
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
503 size_t bits,
504 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200505{
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100507 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100509#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100510 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100512#endif
513#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100514 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100516#endif
517#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100518 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100520#endif
521#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100522 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100524#endif
525#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100526 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100528 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (bits_is_sloppy) {
530 return MBEDTLS_ECP_DP_SECP521R1;
531 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100532 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100533#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100534 }
535 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100536
Paul Elliott8ff510a2020-06-02 17:19:28 +0100537 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100539#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100540 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100542#endif
543#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100544 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100546#endif
547#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100548 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100550#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100551 }
552 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100553
Paul Elliott8ff510a2020-06-02 17:19:28 +0100554 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100556#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100557 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100559 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (bits_is_sloppy) {
561 return MBEDTLS_ECP_DP_CURVE25519;
562 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100563 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100564#endif
565#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100566 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100568#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100569 }
570 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100571
Paul Elliott8ff510a2020-06-02 17:19:28 +0100572 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100574#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100575 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100577#endif
578#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100579 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100581#endif
582#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100583 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100585#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100586 }
587 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200588 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100589
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100590 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200592}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200593#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
596 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200597{
598 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200600 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200601 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200602 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200603 case PSA_KEY_TYPE_PASSWORD:
604 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200605 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100606#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200607 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (bits != 128 && bits != 192 && bits != 256) {
609 return PSA_ERROR_INVALID_ARGUMENT;
610 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200611 break;
612#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200613#if defined(PSA_WANT_KEY_TYPE_ARIA)
614 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (bits != 128 && bits != 192 && bits != 256) {
616 return PSA_ERROR_INVALID_ARGUMENT;
617 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200618 break;
619#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100620#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200621 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (bits != 128 && bits != 192 && bits != 256) {
623 return PSA_ERROR_INVALID_ARGUMENT;
624 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200625 break;
626#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100627#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200628 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (bits != 64 && bits != 128 && bits != 192) {
630 return PSA_ERROR_INVALID_ARGUMENT;
631 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200632 break;
633#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100634#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200635 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 if (bits != 256) {
637 return PSA_ERROR_INVALID_ARGUMENT;
638 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200639 break;
640#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200641 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200643 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (bits % 8 != 0) {
645 return PSA_ERROR_INVALID_ARGUMENT;
646 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200649}
650
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100651/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100652 *
Steven Cooremancd640932021-03-08 12:00:27 +0100653 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
654 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100655 *
656 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100657 * \param[in] key_type The key type of the key to be used with the
658 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100659 *
660 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100661 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100662 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100663 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100664 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100665MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100666 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100668{
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if (PSA_ALG_IS_HMAC(algorithm)) {
670 if (key_type == PSA_KEY_TYPE_HMAC) {
671 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100672 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100673 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
676 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
677 * key. */
678 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
679 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
680 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
681 * the block length (larger than 1) for block ciphers. */
682 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
683 return PSA_SUCCESS;
684 }
685 }
686 }
687
688 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100689}
690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
692 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200693{
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (slot->key.data != NULL) {
695 return PSA_ERROR_ALREADY_EXISTS;
696 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 slot->key.data = mbedtls_calloc(1, buffer_length);
699 if (slot->key.data == NULL) {
700 return PSA_ERROR_INSUFFICIENT_MEMORY;
701 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200702
Ronald Cronea0f8a62020-11-25 17:52:23 +0100703 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200705}
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
708 const uint8_t *data,
709 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200710{
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 psa_status_t status = psa_allocate_buffer_to_slot(slot,
712 data_length);
713 if (status != PSA_SUCCESS) {
714 return status;
715 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 memcpy(slot->key.data, data, data_length);
718 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200719}
720
Ronald Crona0fe59f2020-11-29 11:14:53 +0100721psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100722 const psa_key_attributes_t *attributes,
723 const uint8_t *data, size_t data_length,
724 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100726{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100727 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
728 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100729
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200730 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (data_length == 0) {
732 return PSA_ERROR_NOT_SUPPORTED;
733 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (key_type_is_raw_bytes(type)) {
736 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
739 *bits);
740 if (status != PSA_SUCCESS) {
741 return status;
742 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200743
Ronald Crondd04d422020-11-28 15:14:42 +0100744 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100746 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 return PSA_SUCCESS;
750 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200751#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200752 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100753 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200754 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100755 return PSA_ERROR_INVALID_ARGUMENT;
756 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200757 return mbedtls_psa_ffdh_import_key(attributes,
758 data, data_length,
759 key_buffer, key_buffer_size,
760 key_buffer_length,
761 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100762 }
Valerio Settia55f0422023-07-10 15:34:41 +0200763#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200764 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200765#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
767 if (PSA_KEY_TYPE_IS_ECC(type)) {
768 return mbedtls_psa_ecp_import_key(attributes,
769 data, data_length,
770 key_buffer, key_buffer_size,
771 key_buffer_length,
772 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100773 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200774#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800775 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200776#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
777 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
779 if (PSA_KEY_TYPE_IS_RSA(type)) {
780 return mbedtls_psa_rsa_import_key(attributes,
781 data, data_length,
782 key_buffer, key_buffer_size,
783 key_buffer_length,
784 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200785 }
Valerio Settife478902023-07-25 12:27:19 +0200786#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
787 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800788 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100789 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100792}
793
Gilles Peskinef603c712019-01-19 13:40:11 +0100794/** Calculate the intersection of two algorithm usage policies.
795 *
796 * Return 0 (which allows no operation) on incompatibility.
797 */
798static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100799 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100800 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100802{
Gilles Peskine549ea862019-05-22 11:45:59 +0200803 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (alg1 == alg2) {
805 return alg1;
806 }
Steven Cooreman03488022021-02-18 12:07:20 +0100807 /* If the policies are from the same hash-and-sign family, check
808 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
810 PSA_ALG_IS_SIGN_HASH(alg2) &&
811 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
812 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
813 return alg2;
814 }
815 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
816 return alg1;
817 }
Steven Cooreman03488022021-02-18 12:07:20 +0100818 }
819 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100820 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100821 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
823 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
824 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
825 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
826 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100827 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100828
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100829 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
831 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
832 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
833 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100834 }
835 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
837 (alg1_len <= alg2_len)) {
838 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100839 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
841 (alg2_len <= alg1_len)) {
842 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100843 }
844 }
845 /* If the policies are from the same MAC family, check whether one
846 * of them is a minimum-MAC-length policy. Calculate the most
847 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
849 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
850 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100851 /* Validate the combination of key type and algorithm. Since the base
852 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
854 return 0;
855 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100856
Steven Cooreman31a876d2021-03-03 20:47:40 +0100857 /* Get the (exact or at-least) output lengths for both sides of the
858 * requested intersection. None of the currently supported algorithms
859 * have an output length dependent on the actual key size, so setting it
860 * to a bogus value of 0 is currently OK.
861 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100862 * Note that for at-least-this-length wildcard algorithms, the output
863 * length is set to the shortest allowed length, which allows us to
864 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
866 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100867 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100868
Steven Cooremana1d83222021-02-25 10:20:29 +0100869 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
871 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
872 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100873 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100874
875 /* If only one is an at-least-this-length policy, the intersection would
876 * be the other (fixed-length) policy as long as said fixed length is
877 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
879 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100880 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
882 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100883 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100884
Steven Cooremancd640932021-03-08 12:00:27 +0100885 /* If none of them are wildcards, check whether they define the same tag
886 * length. This is still possible here when one is default-length and
887 * the other specific-length. Ensure to always return the
888 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (alg1_len == alg2_len) {
890 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
891 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100892 }
893 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100895}
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897static int psa_key_algorithm_permits(psa_key_type_t key_type,
898 psa_algorithm_t policy_alg,
899 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200900{
Gilles Peskine549ea862019-05-22 11:45:59 +0200901 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (requested_alg == policy_alg) {
903 return 1;
904 }
Steven Cooreman03488022021-02-18 12:07:20 +0100905 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
906 * and requested_alg is the same hash-and-sign family with any hash,
907 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
909 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
910 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
911 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100912 }
913 /* If policy_alg is a wildcard AEAD algorithm of the same base as
914 * the requested algorithm, check the requested tag length to be
915 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if (PSA_ALG_IS_AEAD(policy_alg) &&
917 PSA_ALG_IS_AEAD(requested_alg) &&
918 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
919 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
920 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
921 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
922 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100923 }
Steven Cooremancd640932021-03-08 12:00:27 +0100924 /* If policy_alg is a MAC algorithm of the same base as the requested
925 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if (PSA_ALG_IS_MAC(policy_alg) &&
927 PSA_ALG_IS_MAC(requested_alg) &&
928 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
929 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100930 /* Validate the combination of key type and algorithm. Since the policy
931 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
933 return 0;
934 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100935
Steven Cooreman31a876d2021-03-03 20:47:40 +0100936 /* Get both the requested output length for the algorithm which is to be
937 * verified, and the default output length for the base algorithm.
938 * Note that none of the currently supported algorithms have an output
939 * length dependent on actual key size, so setting it to a bogus value
940 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100941 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100943 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 key_type, 0,
945 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100946
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100947 /* If the policy is default-length, only allow an algorithm with
948 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
950 return requested_output_length == default_output_length;
951 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100952
953 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100954 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
956 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
957 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100958 }
959
Steven Cooremancd640932021-03-08 12:00:27 +0100960 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
961 * check for the requested MAC length to be equal to or longer than the
962 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
964 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
965 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100966 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200967 }
Steven Cooremance48e852020-10-05 16:02:45 +0200968 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200969 * a key derivation with that key agreement should also be allowed. This
970 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
972 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
973 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
974 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200975 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100976 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200978}
979
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100980/** Test whether a policy permits an algorithm.
981 *
982 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100983 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100984 * \note This function requires providing the key type for which the policy is
985 * being validated, since some algorithm policy definitions (e.g. MAC)
986 * have different properties depending on what kind of cipher it is
987 * combined with.
988 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100989 * \retval PSA_SUCCESS When \p alg is a specific algorithm
990 * allowed by the \p policy.
991 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
992 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
993 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100994 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100995static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
996 psa_key_type_t key_type,
997 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100998{
Steven Cooremand788fab2021-02-25 11:29:17 +0100999 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (alg == 0) {
1001 return PSA_ERROR_INVALID_ARGUMENT;
1002 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001003
Steven Cooreman7e39f052021-02-23 14:18:32 +01001004 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 if (PSA_ALG_IS_WILDCARD(alg)) {
1006 return PSA_ERROR_INVALID_ARGUMENT;
1007 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1010 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1011 return PSA_SUCCESS;
1012 } else {
1013 return PSA_ERROR_NOT_PERMITTED;
1014 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001015}
1016
Gilles Peskinef603c712019-01-19 13:40:11 +01001017/** Restrict a key policy based on a constraint.
1018 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001019 * \note This function requires providing the key type for which the policy is
1020 * being restricted, since some algorithm policy definitions (e.g. MAC)
1021 * have different properties depending on what kind of cipher it is
1022 * combined with.
1023 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001024 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001025 * \param[in,out] policy The policy to restrict.
1026 * \param[in] constraint The policy constraint to apply.
1027 *
1028 * \retval #PSA_SUCCESS
1029 * \c *policy contains the intersection of the original value of
1030 * \c *policy and \c *constraint.
1031 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001032 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001033 * \c *policy is unchanged.
1034 */
1035static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001036 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001037 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001039{
1040 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1042 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001043 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1045 constraint->alg2);
1046 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1047 return PSA_ERROR_INVALID_ARGUMENT;
1048 }
1049 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1050 return PSA_ERROR_INVALID_ARGUMENT;
1051 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001052 policy->usage &= constraint->usage;
1053 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001054 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001056}
1057
Przemek Stekiel061f6942022-11-30 10:51:35 +01001058/** Get the description of a key given its identifier and policy constraints
1059 * and lock it.
1060 *
1061 * The key must have allow all the usage flags set in \p usage. If \p alg is
1062 * nonzero, the key must allow operations with this algorithm. If \p alg is
1063 * zero, the algorithm is not checked.
1064 *
1065 * In case of a persistent key, the function loads the description of the key
1066 * into a key slot if not already done.
1067 *
1068 * On success, the returned key slot is locked. It is the responsibility of
1069 * the caller to unlock the key slot when it does not access it anymore.
1070 */
1071static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001072 mbedtls_svc_key_id_t key,
1073 psa_key_slot_t **p_slot,
1074 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001076{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001077 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001078 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 status = psa_get_and_lock_key_slot(key, p_slot);
1081 if (status != PSA_SUCCESS) {
1082 return status;
1083 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001084 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001085
1086 /* Enforce that usage policy for the key slot contains all the flags
1087 * required by the usage parameter. There is one exception: public
1088 * keys can always be exported, so we treat public key objects as
1089 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001091 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001095 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001096 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001097 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001098
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001099 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 if (alg != 0) {
1101 status = psa_key_policy_permits(&slot->attr.policy,
1102 slot->attr.type,
1103 alg);
1104 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001105 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001107 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001110
1111error:
1112 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001116}
Darryl Green940d72c2018-07-13 13:18:51 +01001117
Ronald Cron5c522922020-11-14 16:35:34 +01001118/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001119 *
1120 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001121 * available, as opposed to a key in a secure element and/or to be used
1122 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001123 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001124 * This is a temporary function that may be used instead of
1125 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1126 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001127 *
Ronald Cron5c522922020-11-14 16:35:34 +01001128 * On success, the returned key slot is locked. It is the responsibility of the
1129 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001130 */
Ronald Cron5c522922020-11-14 16:35:34 +01001131static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1132 mbedtls_svc_key_id_t key,
1133 psa_key_slot_t **p_slot,
1134 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001136{
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1138 usage, alg);
1139 if (status != PSA_SUCCESS) {
1140 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001141 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1144 psa_unlock_key_slot(*p_slot);
1145 *p_slot = NULL;
1146 return PSA_ERROR_NOT_SUPPORTED;
1147 }
1148
1149 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001150}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001153{
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001155 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001157
Ronald Cronea0f8a62020-11-25 17:52:23 +01001158 slot->key.data = NULL;
1159 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001162}
1163
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001164/** Completely wipe a slot in memory, including its policy.
1165 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001166psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001167{
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 /*
1171 * As the return error code may not be handled in case of multiple errors,
1172 * do our best to report an unexpected lock counter. Assert with
1173 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1174 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1175 * function is called as part of the execution of a test suite, the
1176 * execution of the test suite is stopped in error if the assertion fails.
1177 */
1178 if (slot->lock_count != 1) {
1179 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001180 status = PSA_ERROR_CORRUPTION_DETECTED;
1181 }
1182
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001183 /* Multipart operations may still be using the key. This is safe
1184 * because all multipart operation objects are independent from
1185 * the key slot: if they need to access the key after the setup
1186 * phase, they have a copy of the key. Note that this means that
1187 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001188 /* At this point, key material and other type-specific content has
1189 * been wiped. Clear remaining metadata. We can call memset and not
1190 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 memset(slot, 0, sizeof(*slot));
1192 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001193}
1194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001196{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001197 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001198 psa_status_t status; /* status of the last operation */
1199 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001200#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1201 psa_se_drv_table_entry_t *driver;
1202#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001203
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 if (mbedtls_svc_key_id_is_null(key)) {
1205 return PSA_SUCCESS;
1206 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001207
Ronald Cronf2911112020-10-29 17:51:10 +01001208 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001209 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001210 * key, this will load the key description from persistent memory if not
1211 * done yet. We cannot avoid this loading as without it we don't know if
1212 * the key is operated by an SE or not and this information is needed by
1213 * the current implementation.
1214 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 status = psa_get_and_lock_key_slot(key, &slot);
1216 if (status != PSA_SUCCESS) {
1217 return status;
1218 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001219
Ronald Cronf2911112020-10-29 17:51:10 +01001220 /*
1221 * If the key slot containing the key description is under access by the
1222 * library (apart from the present access), the key cannot be destroyed
1223 * yet. For the time being, just return in error. Eventually (to be
1224 * implemented), the key should be destroyed when all accesses have
1225 * stopped.
1226 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if (slot->lock_count > 1) {
1228 psa_unlock_key_slot(slot);
1229 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001230 }
1231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001233 /* Refuse the destruction of a read-only key (which may or may not work
1234 * if we attempt it, depending on whether the key is merely read-only
1235 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001236 * Just do the best we can, which is to wipe the copy in memory
1237 * (done in this function's cleanup code). */
1238 overall_status = PSA_ERROR_NOT_PERMITTED;
1239 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001240 }
1241
Gilles Peskine354f7672019-07-12 23:46:38 +02001242#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1244 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001245 /* For a key in a secure element, we need to do three things:
1246 * remove the key file in internal storage, destroy the
1247 * key inside the secure element, and update the driver's
1248 * persistent data. Start a transaction that will encompass these
1249 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001251 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001253 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 status = psa_crypto_save_transaction();
1255 if (status != PSA_SUCCESS) {
1256 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001257 /* We should still try to destroy the key in the secure
1258 * element and the key metadata in storage. This is especially
1259 * important if the error is that the storage is full.
1260 * But how to do it exactly without risking an inconsistent
1261 * state after a reset?
1262 * https://github.com/ARMmbed/mbed-crypto/issues/215
1263 */
1264 overall_status = status;
1265 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001266 }
1267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 status = psa_destroy_se_key(driver,
1269 psa_key_slot_get_slot_number(slot));
1270 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001271 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001273 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001274#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1275
Darryl Greend49a4992018-06-18 17:27:26 +01001276#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1278 status = psa_destroy_persistent_key(slot->attr.id);
1279 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001280 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001282
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001283 /* TODO: other slots may have a copy of the same key. We should
1284 * invalidate them.
1285 * https://github.com/ARMmbed/mbed-crypto/issues/214
1286 */
Darryl Greend49a4992018-06-18 17:27:26 +01001287 }
1288#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001289
Gilles Peskinefc762652019-07-22 19:30:34 +02001290#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 if (driver != NULL) {
1292 status = psa_save_se_persistent_data(driver);
1293 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001294 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 }
1296 status = psa_crypto_stop_transaction();
1297 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001298 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001300 }
1301#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1302
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001303exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001305 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001307 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 }
1309 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001310}
1311
Valerio Settib2bcedb2023-07-10 11:24:00 +02001312#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001313 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001314static psa_status_t psa_get_rsa_public_exponent(
1315 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001317{
1318 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001319 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001320 uint8_t *buffer = NULL;
1321 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1325 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001326 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 }
1328 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001329 /* It's the default value, which is reported as an empty string,
1330 * so there's nothing to do. */
1331 goto exit;
1332 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 buflen = mbedtls_mpi_size(&mpi);
1335 buffer = mbedtls_calloc(1, buflen);
1336 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001337 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1338 goto exit;
1339 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1341 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001342 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001344 attributes->domain_parameters = buffer;
1345 attributes->domain_parameters_size = buflen;
1346
1347exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 mbedtls_mpi_free(&mpi);
1349 if (ret != 0) {
1350 mbedtls_free(buffer);
1351 }
1352 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001353}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001354#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001355 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001356
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001357/** Retrieve all the publicly-accessible attributes of a key.
1358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001359psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1360 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001361{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001362 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001363 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001364 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1369 if (status != PSA_SUCCESS) {
1370 return status;
1371 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001372
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001373 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1375 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001376
1377#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1379 psa_set_key_slot_number(attributes,
1380 psa_key_slot_get_slot_number(slot));
1381 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001382#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001385#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1386 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001387 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001388 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001389 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Pengyu Lvf75893b2023-12-08 17:21:39 +08001390 /* TODO: This is a temporary situation where domain parameters are deprecated,
1391 * but we need it for namely generating an RSA key with a non-default exponent.
1392 * This would be improved after https://github.com/Mbed-TLS/mbedtls/issues/6494.
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001395 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001396
Ronald Cron90857082020-11-25 14:58:33 +01001397 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 slot->attr.type,
1399 slot->key.data,
1400 slot->key.bytes,
1401 &rsa);
1402 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001403 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 status = psa_get_rsa_public_exponent(rsa,
1407 attributes);
1408 mbedtls_rsa_free(rsa);
1409 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001410 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001411 break;
Pengyu Lve9efbc22023-12-08 16:59:08 +08001412#else
1413 case PSA_KEY_TYPE_RSA_KEY_PAIR:
1414 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1415 attributes->domain_parameters = NULL;
1416 attributes->domain_parameters_size = SIZE_MAX;
1417 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001418#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1419 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001420 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001421 default:
1422 /* Nothing else to do. */
1423 break;
1424 }
1425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if (status != PSA_SUCCESS) {
1427 psa_reset_key_attributes(attributes);
1428 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001433}
1434
Gilles Peskinec8000c02019-08-02 20:15:51 +02001435#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1436psa_status_t psa_get_key_slot_number(
1437 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001439{
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001441 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 return PSA_SUCCESS;
1443 } else {
1444 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001445 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001446}
1447#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1450 size_t key_buffer_size,
1451 uint8_t *data,
1452 size_t data_size,
1453 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001454{
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (key_buffer_size > data_size) {
1456 return PSA_ERROR_BUFFER_TOO_SMALL;
1457 }
1458 memcpy(data, key_buffer, key_buffer_size);
1459 memset(data + key_buffer_size, 0,
1460 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001461 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001463}
1464
Ronald Cron7285cda2020-11-26 14:46:37 +01001465psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001466 const psa_key_attributes_t *attributes,
1467 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001469{
Ronald Crond18b5f82020-11-25 16:46:05 +01001470 psa_key_type_t type = attributes->core.type;
1471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (key_type_is_raw_bytes(type) ||
1473 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001474 PSA_KEY_TYPE_IS_ECC(type) ||
1475 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 return psa_export_key_buffer_internal(
1477 key_buffer, key_buffer_size,
1478 data, data_size, data_length);
1479 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001480 /* This shouldn't happen in the reference implementation, but
1481 it is valid for a special-purpose implementation to omit
1482 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001484 }
1485}
1486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1488 uint8_t *data,
1489 size_t data_size,
1490 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001491{
1492 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1493 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1494 psa_key_slot_t *slot;
1495
Ronald Crone907e552021-01-18 13:22:38 +01001496 /* Reject a zero-length output buffer now, since this can never be a
1497 * valid key representation. This way we know that data must be a valid
1498 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (data_size == 0) {
1500 return PSA_ERROR_BUFFER_TOO_SMALL;
1501 }
Ronald Crone907e552021-01-18 13:22:38 +01001502
Ronald Cron9486f9d2020-11-26 13:36:23 +01001503 /* Set the key to empty now, so that even when there are errors, we always
1504 * set data_length to a value between 0 and data_size. On error, setting
1505 * the key to empty is a good choice because an empty key representation is
1506 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001507 *data_length = 0;
1508
Ronald Cron9486f9d2020-11-26 13:36:23 +01001509 /* Export requires the EXPORT flag. There is an exception for public keys,
1510 * which don't require any flag, but
1511 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1512 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1514 PSA_KEY_USAGE_EXPORT, 0);
1515 if (status != PSA_SUCCESS) {
1516 return status;
1517 }
mohammad160306e79202018-03-28 13:17:44 +03001518
Ronald Crond18b5f82020-11-25 16:46:05 +01001519 psa_key_attributes_t attributes = {
1520 .core = slot->attr
1521 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 status = psa_driver_wrapper_export_key(&attributes,
1523 slot->key.data, slot->key.bytes,
1524 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001529}
1530
Ronald Cron7285cda2020-11-26 14:46:37 +01001531psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001532 const psa_key_attributes_t *attributes,
1533 const uint8_t *key_buffer,
1534 size_t key_buffer_size,
1535 uint8_t *data,
1536 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001538{
Ronald Crond18b5f82020-11-25 16:46:05 +01001539 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001540
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001541 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001542 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1543 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001544 /* Exporting public -> public */
1545 return psa_export_key_buffer_internal(
1546 key_buffer, key_buffer_size,
1547 data, data_size, data_length);
1548 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001549#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001550 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1551 return mbedtls_psa_rsa_export_public_key(attributes,
1552 key_buffer,
1553 key_buffer_size,
1554 data,
1555 data_size,
1556 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001557#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001558 /* We don't know how to convert a private RSA key to public. */
1559 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001560#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001561 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001562 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001563#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001564 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1565 return mbedtls_psa_ecp_export_public_key(attributes,
1566 key_buffer,
1567 key_buffer_size,
1568 data,
1569 data_size,
1570 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001571#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001572 /* We don't know how to convert a private ECC key to public */
1573 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001574#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001575 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001576 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001577#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001578 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001579 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001580 key_buffer,
1581 key_buffer_size,
1582 data, data_size,
1583 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001584#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001585 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001586#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001587 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001589 (void) key_buffer;
1590 (void) key_buffer_size;
1591 (void) data;
1592 (void) data_size;
1593 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001595 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001596}
Ronald Crond18b5f82020-11-25 16:46:05 +01001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1599 uint8_t *data,
1600 size_t data_size,
1601 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001602{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001603 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001604 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001605 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001606 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001607
Ronald Crone907e552021-01-18 13:22:38 +01001608 /* Reject a zero-length output buffer now, since this can never be a
1609 * valid key representation. This way we know that data must be a valid
1610 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if (data_size == 0) {
1612 return PSA_ERROR_BUFFER_TOO_SMALL;
1613 }
Ronald Crone907e552021-01-18 13:22:38 +01001614
Darryl Greendd8fb772018-11-07 16:00:44 +00001615 /* Set the key to empty now, so that even when there are errors, we always
1616 * set data_length to a value between 0 and data_size. On error, setting
1617 * the key to empty is a good choice because an empty key representation is
1618 * unlikely to be accepted anywhere. */
1619 *data_length = 0;
1620
1621 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1623 if (status != PSA_SUCCESS) {
1624 return status;
1625 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1628 status = PSA_ERROR_INVALID_ARGUMENT;
1629 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001630 }
1631
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001632 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001633 .core = slot->attr
1634 };
Ronald Cron67227982020-11-26 15:16:05 +01001635 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001636 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001638
1639exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001643}
1644
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001645MBEDTLS_STATIC_ASSERT(
1646 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1647 "One or more key attribute flag is listed as both external-only and dual-use")
1648MBEDTLS_STATIC_ASSERT(
1649 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1650 "One or more key attribute flag is listed as both internal-only and dual-use")
1651MBEDTLS_STATIC_ASSERT(
1652 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1653 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001654
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001655/** Validate that a key policy is internally well-formed.
1656 *
1657 * This function only rejects invalid policies. It does not validate the
1658 * consistency of the policy with respect to other attributes of the key
1659 * such as the key type.
1660 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001661static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001662{
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1664 PSA_KEY_USAGE_COPY |
1665 PSA_KEY_USAGE_ENCRYPT |
1666 PSA_KEY_USAGE_DECRYPT |
1667 PSA_KEY_USAGE_SIGN_MESSAGE |
1668 PSA_KEY_USAGE_VERIFY_MESSAGE |
1669 PSA_KEY_USAGE_SIGN_HASH |
1670 PSA_KEY_USAGE_VERIFY_HASH |
1671 PSA_KEY_USAGE_VERIFY_DERIVATION |
1672 PSA_KEY_USAGE_DERIVE)) != 0) {
1673 return PSA_ERROR_INVALID_ARGUMENT;
1674 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001677}
1678
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001679/** Validate the internal consistency of key attributes.
1680 *
1681 * This function only rejects invalid attribute values. If does not
1682 * validate the consistency of the attributes with any key data that may
1683 * be involved in the creation of the key.
1684 *
1685 * Call this function early in the key creation process.
1686 *
1687 * \param[in] attributes Key attributes for the new key.
1688 * \param[out] p_drv On any return, the driver for the key, if any.
1689 * NULL for a transparent key.
1690 *
1691 */
1692static psa_status_t psa_validate_key_attributes(
1693 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001695{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001696 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1698 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 status = psa_validate_key_location(lifetime, p_drv);
1701 if (status != PSA_SUCCESS) {
1702 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001703 }
1704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 status = psa_validate_key_persistence(lifetime);
1706 if (status != PSA_SUCCESS) {
1707 return status;
1708 }
1709
1710 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1711 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1712 return PSA_ERROR_INVALID_ARGUMENT;
1713 }
1714 } else {
1715 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1716 return PSA_ERROR_INVALID_ARGUMENT;
1717 }
1718 }
1719
1720 status = psa_validate_key_policy(&attributes->core.policy);
1721 if (status != PSA_SUCCESS) {
1722 return status;
1723 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001724
1725 /* Refuse to create overly large keys.
1726 * Note that this doesn't trigger on import if the attributes don't
1727 * explicitly specify a size (so psa_get_key_bits returns 0), so
1728 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1730 return PSA_ERROR_NOT_SUPPORTED;
1731 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001732
Gilles Peskine91e8c332019-08-02 19:19:39 +02001733 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1735 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1736 return PSA_ERROR_INVALID_ARGUMENT;
1737 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001740}
1741
Gilles Peskine4747d192019-04-17 15:05:45 +02001742/** Prepare a key slot to receive key material.
1743 *
1744 * This function allocates a key slot and sets its metadata.
1745 *
1746 * If this function fails, call psa_fail_key_creation().
1747 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001748 * This function is intended to be used as follows:
1749 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001750 * it with the specified attributes, and in case of a volatile key assign it
1751 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001752 * -# Populate the slot with the key material.
1753 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1754 * In case of failure at any step, stop the sequence and call
1755 * psa_fail_key_creation().
1756 *
Ronald Cron5c522922020-11-14 16:35:34 +01001757 * On success, the key slot is locked. It is the responsibility of the caller
1758 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001759 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001760 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001761 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001762 * \param[out] p_slot On success, a pointer to the prepared slot.
1763 * \param[out] p_drv On any return, the driver for the key, if any.
1764 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001765 *
1766 * \retval #PSA_SUCCESS
1767 * The key slot is ready to receive key material.
1768 * \return If this function fails, the key slot is an invalid state.
1769 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 */
1771static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001772 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001773 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001774 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001776{
1777 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001778 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001779 psa_key_slot_t *slot;
1780
Gilles Peskinedf179142019-07-15 22:02:14 +02001781 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001782 *p_drv = NULL;
1783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 status = psa_validate_key_attributes(attributes, p_drv);
1785 if (status != PSA_SUCCESS) {
1786 return status;
1787 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1790 if (status != PSA_SUCCESS) {
1791 return status;
1792 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001793 slot = *p_slot;
1794
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001795 /* We're storing the declared bit-size of the key. It's up to each
1796 * creation mechanism to verify that this information is correct.
1797 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001798 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001799 * is optional (import, copy). In case of a volatile key, assign it the
1800 * volatile key identifier associated to the slot returned to contain its
1801 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001802
1803 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001805#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1806 slot->attr.id = volatile_key_id;
1807#else
1808 slot->attr.id.key_id = volatile_key_id;
1809#endif
1810 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001811
Gilles Peskine91e8c332019-08-02 19:19:39 +02001812 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001813 * external-only flags, query `attributes`. Thanks to the check
1814 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001815 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001816 * may have set. */
1817 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001818
Gilles Peskinecbaff462019-07-12 23:46:04 +02001819#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001820 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001821 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001822 * create the key file in internal storage, create the
1823 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001824 * persistent data. This is done by starting a transaction that will
1825 * encompass these three actions.
1826 * For registering a volatile key, we just need to find an appropriate
1827 * slot number inside the SE. Since the key is designated volatile, creating
1828 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001829 /* The first thing to do is to find a slot number for the new key.
1830 * We save the slot number in persistent storage as part of the
1831 * transaction data. It will be needed to recover if the power
1832 * fails during the key creation process, to clean up on the secure
1833 * element side after restarting. Obtaining a slot number from the
1834 * secure element driver updates its persistent state, but we do not yet
1835 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001836 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001838 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1840 &slot_number);
1841 if (status != PSA_SUCCESS) {
1842 return status;
1843 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1846 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001847 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001848 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001849 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 status = psa_crypto_save_transaction();
1851 if (status != PSA_SUCCESS) {
1852 (void) psa_crypto_stop_transaction();
1853 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001854 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001855 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001856
1857 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001859 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001860
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001862 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001864 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001865#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1866
Ryan Everett975d4112023-11-16 13:37:51 +00001867 slot->status = PSA_SLOT_OCCUPIED;
1868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001870}
Gilles Peskine4747d192019-04-17 15:05:45 +02001871
1872/** Finalize the creation of a key once its key material has been set.
1873 *
1874 * This entails writing the key to persistent storage.
1875 *
1876 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001877 * See the documentation of psa_start_key_creation() for the intended use
1878 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001879 *
Ronald Cron5c522922020-11-14 16:35:34 +01001880 * If the finalization succeeds, the function unlocks the key slot (it was
1881 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1882 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001883 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001884 * \param[in,out] slot Pointer to the slot with key material.
1885 * \param[in] driver The secure element driver for the key,
1886 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001887 * \param[out] key On success, identifier of the key. Note that the
1888 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001889 *
1890 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001891 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001892 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1893 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1894 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1895 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1896 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1897 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001898 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001899 * \return If this function fails, the key slot is an invalid state.
1900 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001901 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001902static psa_status_t psa_finish_key_creation(
1903 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001904 psa_se_drv_table_entry_t *driver,
1905 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001906{
1907 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001908 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001909 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001910
1911#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001913#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001915 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001916 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001918
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001919 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1920 sizeof(data.slot_number),
1921 "Slot number size does not match psa_se_key_data_storage_t");
1922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1924 status = psa_save_persistent_key(&slot->attr,
1925 (uint8_t *) &data,
1926 sizeof(data));
1927 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001928#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1929 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001930 /* Key material is saved in export representation in the slot, so
1931 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 status = psa_save_persistent_key(&slot->attr,
1933 slot->key.data,
1934 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001935 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001936 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001937#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1938
Gilles Peskinecbaff462019-07-12 23:46:04 +02001939#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001940 /* Finish the transaction for a key creation. This does not
1941 * happen when registering an existing key. Detect this case
1942 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001943 * creation of a persistent key in a secure element requires a transaction,
1944 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 if (driver != NULL &&
1946 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1947 status = psa_save_se_persistent_data(driver);
1948 if (status != PSA_SUCCESS) {
1949 psa_destroy_persistent_key(slot->attr.id);
1950 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001951 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001953 }
1954#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001957 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 status = psa_unlock_key_slot(slot);
1959 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001960 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001962 }
Ronald Cron50972942020-11-14 11:28:25 +01001963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001965}
1966
1967/** Abort the creation of a key.
1968 *
1969 * You may call this function after calling psa_start_key_creation(),
1970 * or after psa_finish_key_creation() fails. In other circumstances, this
1971 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001972 * See the documentation of psa_start_key_creation() for the intended use
1973 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001974 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001975 * \param[in,out] slot Pointer to the slot with key material.
1976 * \param[in] driver The secure element driver for the key,
1977 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001978 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001979static void psa_fail_key_creation(psa_key_slot_t *slot,
1980 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001981{
Gilles Peskine011e4282019-06-26 18:34:38 +02001982 (void) driver;
1983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001985 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001987
1988#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001989 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001990 * element, and the failure happened later (when saving metadata
1991 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001992 * element.
1993 * https://github.com/ARMmbed/mbed-crypto/issues/217
1994 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001995
Gilles Peskined7729582019-08-05 15:55:54 +02001996 /* Abort the ongoing transaction if any (there may not be one if
1997 * the creation process failed before starting one, or if the
1998 * key creation is a registration of a key in a secure element).
1999 * Earlier functions must already have done what it takes to undo any
2000 * partial creation. All that's left is to update the transaction data
2001 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02002003#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2004
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02002006}
2007
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002008/** Validate optional attributes during key creation.
2009 *
2010 * Some key attributes are optional during key creation. If they are
2011 * specified in the attributes structure, check that they are consistent
2012 * with the data in the slot.
2013 *
2014 * This function should be called near the end of key creation, after
2015 * the slot in memory is fully populated but before saving persistent data.
2016 */
2017static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002018 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002020{
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 if (attributes->core.type != 0) {
2022 if (attributes->core.type != slot->attr.type) {
2023 return PSA_ERROR_INVALID_ARGUMENT;
2024 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002025 }
2026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002028#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2029 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2031 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002032 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002033 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002034 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002035
Ronald Cron90857082020-11-25 14:58:33 +01002036 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 slot->attr.type,
2038 slot->key.data,
2039 slot->key.bytes,
2040 &rsa);
2041 if (status != PSA_SUCCESS) {
2042 return status;
2043 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 mbedtls_mpi_init(&actual);
2046 mbedtls_mpi_init(&required);
2047 ret = mbedtls_rsa_export(rsa,
2048 NULL, NULL, NULL, NULL, &actual);
2049 mbedtls_rsa_free(rsa);
2050 mbedtls_free(rsa);
2051 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002052 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 }
2054 ret = mbedtls_mpi_read_binary(&required,
2055 attributes->domain_parameters,
2056 attributes->domain_parameters_size);
2057 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002058 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 }
2060 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002061 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 }
2063rsa_exit:
2064 mbedtls_mpi_free(&actual);
2065 mbedtls_mpi_free(&required);
2066 if (ret != 0) {
2067 return mbedtls_to_psa_error(ret);
2068 }
2069 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002070#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2071 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002072 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002073 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002075 }
2076 }
2077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (attributes->core.bits != 0) {
2079 if (attributes->core.bits != slot->attr.bits) {
2080 return PSA_ERROR_INVALID_ARGUMENT;
2081 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002082 }
2083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002085}
2086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2088 const uint8_t *data,
2089 size_t data_length,
2090 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002091{
2092 psa_status_t status;
2093 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002094 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002095 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302096 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002097
Ronald Cron81709fc2020-11-14 12:10:32 +01002098 *key = MBEDTLS_SVC_KEY_ID_INIT;
2099
Gilles Peskine0f84d622019-09-12 19:03:13 +02002100 /* Reject zero-length symmetric keys (including raw data key objects).
2101 * This also rejects any key which might be encoded as an empty string,
2102 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 if (data_length == 0) {
2104 return PSA_ERROR_INVALID_ARGUMENT;
2105 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002106
Archana449608b2021-09-08 15:36:05 +05302107 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 if (data_length > SIZE_MAX / 8) {
2109 return PSA_ERROR_NOT_SUPPORTED;
2110 }
Archana6ed4bda2021-08-04 10:47:15 +05302111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2113 &slot, &driver);
2114 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002117
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002118 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302119 * storage ( thus not in the case of importing a key in a secure element
2120 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2121 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 if (slot->key.data == NULL) {
2123 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302124 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 attributes, data, data_length, &storage_size);
2126 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 }
Archanad8a83dc2021-06-14 10:04:16 +05302129 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 status = psa_allocate_buffer_to_slot(slot, storage_size);
2131 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002132 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002134 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002135
2136 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 status = psa_driver_wrapper_import_key(attributes,
2138 data, data_length,
2139 slot->key.data,
2140 slot->key.bytes,
2141 &slot->key.bytes, &bits);
2142 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002143 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002147 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002149 status = PSA_ERROR_INVALID_ARGUMENT;
2150 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002151 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002152
Archana6ed4bda2021-08-04 10:47:15 +05302153 /* Enforce a size limit, and in particular ensure that the bit
2154 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302156 status = PSA_ERROR_NOT_SUPPORTED;
2157 goto exit;
2158 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 status = psa_validate_optional_attributes(slot, attributes);
2160 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002165exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (status != PSA_SUCCESS) {
2167 psa_fail_key_creation(slot, driver);
2168 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002171}
2172
Gilles Peskined7729582019-08-05 15:55:54 +02002173#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2174psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002176{
2177 psa_status_t status;
2178 psa_key_slot_t *slot = NULL;
2179 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002180 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002181
2182 /* Leaving attributes unspecified is not currently supported.
2183 * It could make sense to query the key type and size from the
2184 * secure element, but not all secure elements support this
2185 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2187 return PSA_ERROR_NOT_SUPPORTED;
2188 }
2189 if (psa_get_key_bits(attributes) == 0) {
2190 return PSA_ERROR_NOT_SUPPORTED;
2191 }
Gilles Peskined7729582019-08-05 15:55:54 +02002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2194 &slot, &driver);
2195 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002196 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 }
Gilles Peskined7729582019-08-05 15:55:54 +02002198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002200
2201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (status != PSA_SUCCESS) {
2203 psa_fail_key_creation(slot, driver);
2204 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002205
Gilles Peskined7729582019-08-05 15:55:54 +02002206 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 psa_close_key(key);
2208 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002209}
2210#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2213 const psa_key_attributes_t *specified_attributes,
2214 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002215{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002216 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002217 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002218 psa_key_slot_t *source_slot = NULL;
2219 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002220 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002221 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302222 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002223
Ronald Cron81709fc2020-11-14 12:10:32 +01002224 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2225
Archana8a180362021-07-05 02:18:48 +05302226 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2228 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002229 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 status = psa_validate_optional_attributes(source_slot,
2233 specified_attributes);
2234 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002235 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002237
Archana9d17bf42021-09-10 06:22:44 +05302238 /* The target key type and number of bits have been validated by
2239 * psa_validate_optional_attributes() to be either equal to zero or
2240 * equal to the ones of the source key. So it is safe to inherit
2241 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302242 * */
Archana9d17bf42021-09-10 06:22:44 +05302243 actual_attributes.core.bits = source_slot->attr.bits;
2244 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302245
2246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 status = psa_restrict_key_policy(source_slot->attr.type,
2248 &actual_attributes.core.policy,
2249 &source_slot->attr.policy);
2250 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002251 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2255 &target_slot, &driver);
2256 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002257 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 }
2259 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2260 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302261 /*
Archana9d17bf42021-09-10 06:22:44 +05302262 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302263 * the source key would need to be exported as plaintext and re-imported
2264 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302265 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302266 * appropriate API invocations from the application, if needed.
2267 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002268 status = PSA_ERROR_NOT_SUPPORTED;
2269 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002270 }
Archana8a180362021-07-05 02:18:48 +05302271 /*
2272 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302273 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302274 * - For opaque keys this translates to an invocation of the drivers'
2275 * copy_key entry point through the dispatch layer.
2276 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2278 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2279 &storage_size);
2280 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302281 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 }
Archana374fe5b2021-09-08 15:50:28 +05302283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2285 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302286 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 }
Archana374fe5b2021-09-08 15:50:28 +05302288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 status = psa_driver_wrapper_copy_key(&actual_attributes,
2290 source_slot->key.data,
2291 source_slot->key.bytes,
2292 target_slot->key.data,
2293 target_slot->key.bytes,
2294 &target_slot->key.bytes);
2295 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302296 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 }
2298 } else {
2299 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302300 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 source_slot->key.bytes);
2302 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302303 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 }
Archana8a180362021-07-05 02:18:48 +05302305 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002307exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 if (status != PSA_SUCCESS) {
2309 psa_fail_key_creation(target_slot, driver);
2310 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002315}
2316
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002317
2318
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002319/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002320/* Message digests */
2321/****************************************************************/
2322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002325 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (operation->id == 0) {
2327 return PSA_SUCCESS;
2328 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002331 operation->id = 0;
2332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334}
2335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2337 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002338{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002339 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002340
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002341 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002343 status = PSA_ERROR_BAD_STATE;
2344 goto exit;
2345 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002348 status = PSA_ERROR_INVALID_ARGUMENT;
2349 goto exit;
2350 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002351
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002352 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2353 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002357
2358exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 if (status != PSA_SUCCESS) {
2360 psa_hash_abort(operation);
2361 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002362
2363 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002364}
2365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2367 const uint8_t *input,
2368 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002369{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002373 status = PSA_ERROR_BAD_STATE;
2374 goto exit;
2375 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002376
Steven Cooremanc8288352021-03-04 14:02:19 +01002377 /* Don't require hash implementations to behave correctly on a
2378 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 if (input_length == 0) {
2380 return PSA_SUCCESS;
2381 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002384
2385exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 if (status != PSA_SUCCESS) {
2387 psa_hash_abort(operation);
2388 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002391}
2392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2394 uint8_t *hash,
2395 size_t hash_size,
2396 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002397{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002398 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 if (operation->id == 0) {
2400 return PSA_ERROR_BAD_STATE;
2401 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002402
Steven Cooreman1e582352021-02-18 17:24:37 +01002403 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 operation, hash, hash_size, hash_length);
2405 psa_hash_abort(operation);
2406 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002407}
2408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2410 const uint8_t *hash,
2411 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002412{
Ronald Cron69a63422021-10-18 09:47:58 +02002413 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002414 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002415 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 operation,
2417 actual_hash, sizeof(actual_hash),
2418 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002421 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002425 status = PSA_ERROR_INVALID_SIGNATURE;
2426 goto exit;
2427 }
2428
Dave Rodgman78701152023-08-29 14:20:18 +01002429 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002430 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002432
2433exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2435 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002436 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002440}
2441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442psa_status_t psa_hash_compute(psa_algorithm_t alg,
2443 const uint8_t *input, size_t input_length,
2444 uint8_t *hash, size_t hash_size,
2445 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002446{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002447 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 if (!PSA_ALG_IS_HASH(alg)) {
2449 return PSA_ERROR_INVALID_ARGUMENT;
2450 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2453 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002454}
2455
Gilles Peskine449bd832023-01-11 14:50:10 +01002456psa_status_t psa_hash_compare(psa_algorithm_t alg,
2457 const uint8_t *input, size_t input_length,
2458 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002459{
Ronald Cron69a63422021-10-18 09:47:58 +02002460 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002461 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 if (!PSA_ALG_IS_HASH(alg)) {
2464 return PSA_ERROR_INVALID_ARGUMENT;
2465 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002466
Steven Cooreman1e582352021-02-18 17:24:37 +01002467 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 alg, input, input_length,
2469 actual_hash, sizeof(actual_hash),
2470 &actual_hash_length);
2471 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002472 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 }
2474 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002475 status = PSA_ERROR_INVALID_SIGNATURE;
2476 goto exit;
2477 }
Dave Rodgman78701152023-08-29 14:20:18 +01002478 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002479 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002481
2482exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2484 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002485}
2486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2488 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002489{
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 if (source_operation->id == 0 ||
2491 target_operation->id != 0) {
2492 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002493 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2496 target_operation);
2497 if (status != PSA_SUCCESS) {
2498 psa_hash_abort(target_operation);
2499 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002500
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002502}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002503
2504
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002505/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002506/* MAC */
2507/****************************************************************/
2508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002510{
Steven Cooremane6804192021-03-19 18:28:56 +01002511 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 if (operation->id == 0) {
2513 return PSA_SUCCESS;
2514 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002515
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002517 operation->mac_size = 0;
2518 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002519 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002520
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002522}
2523
Ronald Cron2dff3b22021-06-17 16:33:22 +02002524static psa_status_t psa_mac_finalize_alg_and_key_validation(
2525 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002526 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002529 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 psa_key_type_t key_type = psa_get_key_type(attributes);
2531 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 if (!PSA_ALG_IS_MAC(alg)) {
2534 return PSA_ERROR_INVALID_ARGUMENT;
2535 }
Steven Cooremane6804192021-03-19 18:28:56 +01002536
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002537 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 status = psa_mac_key_can_do(alg, key_type);
2539 if (status != PSA_SUCCESS) {
2540 return status;
2541 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002542
Steven Cooremand1a68f12021-05-10 11:13:41 +02002543 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002547 /* A very short MAC is too short for security since it can be
2548 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2549 * so we make this our minimum, even though 32 bits is still
2550 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002552 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2555 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002556 /* It's impossible to "truncate" to a larger length than the full length
2557 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002559 }
2560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002562 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2563 * that is disabled in the compile-time configuration. The result can
2564 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2565 * configuration into account. In this case, force a return of
2566 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2567 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2568 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2569 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2570 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002572 }
2573
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002575}
2576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2578 mbedtls_svc_key_id_t key,
2579 psa_algorithm_t alg,
2580 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002581{
2582 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2583 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002584 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002585 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586
2587 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002589 status = PSA_ERROR_BAD_STATE;
2590 goto exit;
2591 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002592
2593 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 key,
2595 &slot,
2596 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2597 alg);
2598 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002599 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002601
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002602 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603 .core = slot->attr
2604 };
2605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2607 &operation->mac_size);
2608 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002609 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611
2612 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002613 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (is_sign) {
2615 status = psa_driver_wrapper_mac_sign_setup(operation,
2616 &attributes,
2617 slot->key.data,
2618 slot->key.bytes,
2619 alg);
2620 } else {
2621 status = psa_driver_wrapper_mac_verify_setup(operation,
2622 &attributes,
2623 slot->key.data,
2624 slot->key.bytes,
2625 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002626 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002627
Gilles Peskinefbfac682018-07-08 20:51:54 +02002628exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (status != PSA_SUCCESS) {
2630 psa_mac_abort(operation);
2631 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002636}
2637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2639 mbedtls_svc_key_id_t key,
2640 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002641{
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002643}
2644
Gilles Peskine449bd832023-01-11 14:50:10 +01002645psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2646 mbedtls_svc_key_id_t key,
2647 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002648{
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002650}
2651
Gilles Peskine449bd832023-01-11 14:50:10 +01002652psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2653 const uint8_t *input,
2654 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002655{
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 if (operation->id == 0) {
2657 return PSA_ERROR_BAD_STATE;
2658 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002659
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002660 /* Don't require hash implementations to behave correctly on a
2661 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 if (input_length == 0) {
2663 return PSA_SUCCESS;
2664 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2667 input, input_length);
2668 if (status != PSA_SUCCESS) {
2669 psa_mac_abort(operation);
2670 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002673}
2674
Gilles Peskine449bd832023-01-11 14:50:10 +01002675psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2676 uint8_t *mac,
2677 size_t mac_size,
2678 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002679{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002680 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2681 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002684 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002685 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002686 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002689 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002690 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002691 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002692
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002693 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2694 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002696 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002697 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002698 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002699
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002701 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002702 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002703 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002704
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 status = psa_driver_wrapper_mac_sign_finish(operation,
2706 mac, operation->mac_size,
2707 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002708
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002709exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002710 /* In case of success, set the potential excess room in the output buffer
2711 * to an invalid value, to avoid potentially leaking a longer MAC.
2712 * In case of error, set the output length and content to a safe default,
2713 * such that in case the caller misses an error check, the output would be
2714 * an unachievable MAC.
2715 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002717 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002718 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002719 }
mohammad16036df908f2018-04-02 08:34:15 -07002720
Paul Elliottdc42ca82023-02-24 18:11:59 +00002721 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002726}
2727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2729 const uint8_t *mac,
2730 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002731{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002732 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2733 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002736 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002737 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002738 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002741 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002742 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002743 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002744
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002746 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002747 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002748 }
2749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 status = psa_driver_wrapper_mac_verify_finish(operation,
2751 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002752
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002753exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002755
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002757}
2758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2760 psa_algorithm_t alg,
2761 const uint8_t *input,
2762 size_t input_length,
2763 uint8_t *mac,
2764 size_t mac_size,
2765 size_t *mac_length,
2766 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002767{
2768 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002769 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2770 psa_key_slot_t *slot;
2771 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002772 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002773
Ronald Cron51131b52021-06-17 17:17:20 +02002774 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 key,
2776 &slot,
2777 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2778 alg);
2779 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002780 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002782
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002783 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002784 .core = slot->attr
2785 };
2786
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2788 &operation_mac_size);
2789 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002790 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002794 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002795 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002796 }
2797
2798 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 &attributes,
2800 slot->key.data, slot->key.bytes,
2801 alg,
2802 input, input_length,
2803 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002804
2805exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002806 /* In case of success, set the potential excess room in the output buffer
2807 * to an invalid value, to avoid potentially leaking a longer MAC.
2808 * In case of error, set the output length and content to a safe default,
2809 * such that in case the caller misses an error check, the output would be
2810 * an unachievable MAC.
2811 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002813 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002814 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002815 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002816
2817 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002820
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002822}
2823
Gilles Peskine449bd832023-01-11 14:50:10 +01002824psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002825 psa_algorithm_t alg,
2826 const uint8_t *input,
2827 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 uint8_t *mac,
2829 size_t mac_size,
2830 size_t *mac_length)
2831{
2832 return psa_mac_compute_internal(key, alg,
2833 input, input_length,
2834 mac, mac_size, mac_length, 1);
2835}
2836
2837psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2838 psa_algorithm_t alg,
2839 const uint8_t *input,
2840 size_t input_length,
2841 const uint8_t *mac,
2842 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002843{
2844 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002845 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2846 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002847
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 status = psa_mac_compute_internal(key, alg,
2849 input, input_length,
2850 actual_mac, sizeof(actual_mac),
2851 &actual_mac_length, 0);
2852 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002853 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002855
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002857 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002858 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002859 }
Dave Rodgman78701152023-08-29 14:20:18 +01002860 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002861 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002862 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002863 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002864
2865exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002869}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002870
Gilles Peskinee59236f2018-01-27 23:32:46 +01002871/****************************************************************/
2872/* Asymmetric cryptography */
2873/****************************************************************/
2874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2876 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002877{
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 if (input_is_message) {
2879 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2880 return PSA_ERROR_INVALID_ARGUMENT;
2881 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2884 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2885 return PSA_ERROR_INVALID_ARGUMENT;
2886 }
2887 }
2888 } else {
2889 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2890 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002891 }
2892 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002893
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002895}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002896
Gilles Peskine449bd832023-01-11 14:50:10 +01002897static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2898 int input_is_message,
2899 psa_algorithm_t alg,
2900 const uint8_t *input,
2901 size_t input_length,
2902 uint8_t *signature,
2903 size_t signature_size,
2904 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002905{
2906 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2907 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2908 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002909 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002910
2911 *signature_length = 0;
2912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 status = psa_sign_verify_check_alg(input_is_message, alg);
2914 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002915 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002917
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002918 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002919 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002920 * buffer can in principle be empty since it doesn't actually have
2921 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 if (signature_size == 0) {
2923 return PSA_ERROR_BUFFER_TOO_SMALL;
2924 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002925
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002926 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 key, &slot,
2928 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2929 PSA_KEY_USAGE_SIGN_HASH,
2930 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002933 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002935
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002937 status = PSA_ERROR_INVALID_ARGUMENT;
2938 goto exit;
2939 }
2940
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002941 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002943 };
2944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002946 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002947 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002948 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 signature, signature_size, signature_length);
2950 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002951
2952 status = psa_driver_wrapper_sign_hash(
2953 &attributes, slot->key.data, slot->key.bytes,
2954 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002956 }
2957
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002958
2959exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002960 psa_wipe_tag_output_buffer(signature, status, signature_size,
2961 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002966}
2967
Gilles Peskine449bd832023-01-11 14:50:10 +01002968static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2969 int input_is_message,
2970 psa_algorithm_t alg,
2971 const uint8_t *input,
2972 size_t input_length,
2973 const uint8_t *signature,
2974 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002975{
2976 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2977 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2978 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 status = psa_sign_verify_check_alg(input_is_message, alg);
2981 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002982 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002984
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002985 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 key, &slot,
2987 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2988 PSA_KEY_USAGE_VERIFY_HASH,
2989 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002990
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 if (status != PSA_SUCCESS) {
2992 return status;
2993 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002994
2995 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002997 };
2998
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003000 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003001 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003002 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 signature, signature_length);
3004 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003005 status = psa_driver_wrapper_verify_hash(
3006 &attributes, slot->key.data, slot->key.bytes,
3007 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003009 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003014
3015}
3016
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003017psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003018 const psa_key_attributes_t *attributes,
3019 const uint8_t *key_buffer,
3020 size_t key_buffer_size,
3021 psa_algorithm_t alg,
3022 const uint8_t *input,
3023 size_t input_length,
3024 uint8_t *signature,
3025 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003027{
3028 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003031 size_t hash_length;
3032 uint8_t hash[PSA_HASH_MAX_SIZE];
3033
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003034 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 PSA_ALG_SIGN_GET_HASH(alg),
3036 input, input_length,
3037 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003038
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003040 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003042
3043 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 attributes, key_buffer, key_buffer_size,
3045 alg, hash, hash_length,
3046 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003047 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003050}
3051
Gilles Peskine449bd832023-01-11 14:50:10 +01003052psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3053 psa_algorithm_t alg,
3054 const uint8_t *input,
3055 size_t input_length,
3056 uint8_t *signature,
3057 size_t signature_size,
3058 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003059{
3060 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003061 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003063}
3064
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003065psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003066 const psa_key_attributes_t *attributes,
3067 const uint8_t *key_buffer,
3068 size_t key_buffer_size,
3069 psa_algorithm_t alg,
3070 const uint8_t *input,
3071 size_t input_length,
3072 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003074{
3075 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003078 size_t hash_length;
3079 uint8_t hash[PSA_HASH_MAX_SIZE];
3080
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003081 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 PSA_ALG_SIGN_GET_HASH(alg),
3083 input, input_length,
3084 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003087 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003089
3090 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 attributes, key_buffer, key_buffer_size,
3092 alg, hash, hash_length,
3093 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003094 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003095
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003097}
3098
Gilles Peskine449bd832023-01-11 14:50:10 +01003099psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3100 psa_algorithm_t alg,
3101 const uint8_t *input,
3102 size_t input_length,
3103 const uint8_t *signature,
3104 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003105{
3106 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003107 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003109}
3110
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003111psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003112 const psa_key_attributes_t *attributes,
3113 const uint8_t *key_buffer, size_t key_buffer_size,
3114 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003116{
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3118 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3119 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003120#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3122 return mbedtls_psa_rsa_sign_hash(
3123 attributes,
3124 key_buffer, key_buffer_size,
3125 alg, hash, hash_length,
3126 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003127#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3128 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 } else {
3130 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003131 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3133 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003134#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3136 return mbedtls_psa_ecdsa_sign_hash(
3137 attributes,
3138 key_buffer, key_buffer_size,
3139 alg, hash, hash_length,
3140 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003141#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3142 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 } else {
3144 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003145 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003146 }
Ronald Cron4501c982021-03-19 20:09:32 +01003147
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 (void) key_buffer;
3149 (void) key_buffer_size;
3150 (void) hash;
3151 (void) hash_length;
3152 (void) signature;
3153 (void) signature_size;
3154 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003155
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003157}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003158
Gilles Peskine449bd832023-01-11 14:50:10 +01003159psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3160 psa_algorithm_t alg,
3161 const uint8_t *hash,
3162 size_t hash_length,
3163 uint8_t *signature,
3164 size_t signature_size,
3165 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003166{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003167 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003168 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003170}
3171
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003172psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003173 const psa_key_attributes_t *attributes,
3174 const uint8_t *key_buffer, size_t key_buffer_size,
3175 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003177{
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3179 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3180 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003181#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3183 return mbedtls_psa_rsa_verify_hash(
3184 attributes,
3185 key_buffer, key_buffer_size,
3186 alg, hash, hash_length,
3187 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003188#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3189 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 } else {
3191 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003192 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3194 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003195#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3197 return mbedtls_psa_ecdsa_verify_hash(
3198 attributes,
3199 key_buffer, key_buffer_size,
3200 alg, hash, hash_length,
3201 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003202#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3203 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 } else {
3205 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003206 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003207 }
Ronald Cron4501c982021-03-19 20:09:32 +01003208
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 (void) key_buffer;
3210 (void) key_buffer_size;
3211 (void) hash;
3212 (void) hash_length;
3213 (void) signature;
3214 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003217}
3218
Gilles Peskine449bd832023-01-11 14:50:10 +01003219psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3220 psa_algorithm_t alg,
3221 const uint8_t *hash,
3222 size_t hash_length,
3223 const uint8_t *signature,
3224 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003225{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003226 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003227 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003229}
3230
Gilles Peskine449bd832023-01-11 14:50:10 +01003231psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3232 psa_algorithm_t alg,
3233 const uint8_t *input,
3234 size_t input_length,
3235 const uint8_t *salt,
3236 size_t salt_length,
3237 uint8_t *output,
3238 size_t output_size,
3239 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003240{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003241 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003242 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003243 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003244 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003245
Darryl Green5cc689a2018-07-24 15:34:10 +01003246 (void) input;
3247 (void) input_length;
3248 (void) salt;
3249 (void) output;
3250 (void) output_size;
3251
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003252 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003253
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3255 return PSA_ERROR_INVALID_ARGUMENT;
3256 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003257
Ronald Cron5c522922020-11-14 16:35:34 +01003258 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3260 if (status != PSA_SUCCESS) {
3261 return status;
3262 }
3263 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3264 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003265 status = PSA_ERROR_INVALID_ARGUMENT;
3266 goto exit;
3267 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003268
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003269 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003271 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003272
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003273 status = psa_driver_wrapper_asymmetric_encrypt(
3274 &attributes, slot->key.data, slot->key.bytes,
3275 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003277exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003279
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003281}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003282
Gilles Peskine449bd832023-01-11 14:50:10 +01003283psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3284 psa_algorithm_t alg,
3285 const uint8_t *input,
3286 size_t input_length,
3287 const uint8_t *salt,
3288 size_t salt_length,
3289 uint8_t *output,
3290 size_t output_size,
3291 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003292{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003293 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003294 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003295 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003296 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003297
Darryl Green5cc689a2018-07-24 15:34:10 +01003298 (void) input;
3299 (void) input_length;
3300 (void) salt;
3301 (void) output;
3302 (void) output_size;
3303
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003304 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003305
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3307 return PSA_ERROR_INVALID_ARGUMENT;
3308 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003309
Ronald Cron5c522922020-11-14 16:35:34 +01003310 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3312 if (status != PSA_SUCCESS) {
3313 return status;
3314 }
3315 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003316 status = PSA_ERROR_INVALID_ARGUMENT;
3317 goto exit;
3318 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003319
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003320 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003322 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003323
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003324 status = psa_driver_wrapper_asymmetric_decrypt(
3325 &attributes, slot->key.data, slot->key.bytes,
3326 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003328
3329exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003331
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003333}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003334
Paul Elliott9fe12f62022-11-30 19:16:02 +00003335/****************************************************************/
3336/* Asymmetric interruptible cryptography */
3337/****************************************************************/
3338
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003339static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3340
Paul Elliott9fe12f62022-11-30 19:16:02 +00003341void psa_interruptible_set_max_ops(uint32_t max_ops)
3342{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003343 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003344}
3345
3346uint32_t psa_interruptible_get_max_ops(void)
3347{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003348 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003349}
3350
Paul Elliott9fe12f62022-11-30 19:16:02 +00003351uint32_t psa_sign_hash_get_num_ops(
3352 const psa_sign_hash_interruptible_operation_t *operation)
3353{
Paul Elliott296ede92022-12-15 17:00:30 +00003354 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003355}
3356
3357uint32_t psa_verify_hash_get_num_ops(
3358 const psa_verify_hash_interruptible_operation_t *operation)
3359{
Paul Elliott296ede92022-12-15 17:00:30 +00003360 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003361}
3362
Paul Elliott59ad9452022-12-18 15:09:02 +00003363static psa_status_t psa_sign_hash_abort_internal(
3364 psa_sign_hash_interruptible_operation_t *operation)
3365{
3366 if (operation->id == 0) {
3367 /* The object has (apparently) been initialized but it is not (yet)
3368 * in use. It's ok to call abort on such an object, and there's
3369 * nothing to do. */
3370 return PSA_SUCCESS;
3371 }
3372
3373 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3374
3375 status = psa_driver_wrapper_sign_hash_abort(operation);
3376
3377 operation->id = 0;
3378
Paul Elliotte6145dc2023-02-07 12:51:21 +00003379 /* Do not clear either the error_occurred or num_ops elements here as they
3380 * only want to be cleared by the application calling abort, not by abort
3381 * being called at completion of an operation. */
3382
Paul Elliott59ad9452022-12-18 15:09:02 +00003383 return status;
3384}
3385
Paul Elliott9fe12f62022-11-30 19:16:02 +00003386psa_status_t psa_sign_hash_start(
3387 psa_sign_hash_interruptible_operation_t *operation,
3388 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3389 const uint8_t *hash, size_t hash_length)
3390{
3391 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3392 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3393 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003394 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003395
Paul Elliottc9774412023-02-06 15:14:07 +00003396 /* Check that start has not been previously called, or operation has not
3397 * previously errored. */
3398 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003399 return PSA_ERROR_BAD_STATE;
3400 }
3401
Paul Elliott9fe12f62022-11-30 19:16:02 +00003402 status = psa_sign_verify_check_alg(0, alg);
3403 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003404 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003405 return status;
3406 }
3407
3408 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3409 PSA_KEY_USAGE_SIGN_HASH,
3410 alg);
3411
3412 if (status != PSA_SUCCESS) {
3413 goto exit;
3414 }
3415
3416 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3417 status = PSA_ERROR_INVALID_ARGUMENT;
3418 goto exit;
3419 }
3420
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003421 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003422 .core = slot->attr
3423 };
3424
Paul Elliott296ede92022-12-15 17:00:30 +00003425 /* Ensure ops count gets reset, in case of operation re-use. */
3426 operation->num_ops = 0;
3427
Paul Elliott9fe12f62022-11-30 19:16:02 +00003428 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3429 slot->key.data,
3430 slot->key.bytes, alg,
3431 hash, hash_length);
3432exit:
3433
3434 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003435 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003436 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003437 }
3438
3439 unlock_status = psa_unlock_key_slot(slot);
3440
Paul Elliottc9774412023-02-06 15:14:07 +00003441 if (unlock_status != PSA_SUCCESS) {
3442 operation->error_occurred = 1;
3443 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003444
Paul Elliottc9774412023-02-06 15:14:07 +00003445 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003446}
3447
3448
3449psa_status_t psa_sign_hash_complete(
3450 psa_sign_hash_interruptible_operation_t *operation,
3451 uint8_t *signature, size_t signature_size,
3452 size_t *signature_length)
3453{
3454 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3455
3456 *signature_length = 0;
3457
Paul Elliottc9774412023-02-06 15:14:07 +00003458 /* Check that start has been called first, and that operation has not
3459 * previously errored. */
3460 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003461 status = PSA_ERROR_BAD_STATE;
3462 goto exit;
3463 }
3464
Paul Elliott096abc42023-02-03 18:33:23 +00003465 /* Immediately reject a zero-length signature buffer. This guarantees that
3466 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003467 if (signature_size == 0) {
3468 status = PSA_ERROR_BUFFER_TOO_SMALL;
3469 goto exit;
3470 }
3471
3472 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3473 signature_size,
3474 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003475
Paul Elliott296ede92022-12-15 17:00:30 +00003476 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003477 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003478
Paul Elliottebe225c2023-02-10 14:32:53 +00003479exit:
3480
Paul Elliott59200a22023-02-21 15:07:40 +00003481 psa_wipe_tag_output_buffer(signature, status, signature_size,
3482 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003483
Paul Elliott53bb3122023-02-10 14:22:22 +00003484 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003485 if (status != PSA_SUCCESS) {
3486 operation->error_occurred = 1;
3487 }
3488
Paul Elliott59ad9452022-12-18 15:09:02 +00003489 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003490 }
3491
3492 return status;
3493}
3494
3495psa_status_t psa_sign_hash_abort(
3496 psa_sign_hash_interruptible_operation_t *operation)
3497{
Paul Elliott59ad9452022-12-18 15:09:02 +00003498 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3499
3500 status = psa_sign_hash_abort_internal(operation);
3501
3502 /* We clear the number of ops done here, so that it is not cleared when
3503 * the operation fails or succeeds, only on manual abort. */
3504 operation->num_ops = 0;
3505
Paul Elliottc9774412023-02-06 15:14:07 +00003506 /* Likewise, failure state. */
3507 operation->error_occurred = 0;
3508
Paul Elliott59ad9452022-12-18 15:09:02 +00003509 return status;
3510}
3511
3512static psa_status_t psa_verify_hash_abort_internal(
3513 psa_verify_hash_interruptible_operation_t *operation)
3514{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003515 if (operation->id == 0) {
3516 /* The object has (apparently) been initialized but it is not (yet)
3517 * in use. It's ok to call abort on such an object, and there's
3518 * nothing to do. */
3519 return PSA_SUCCESS;
3520 }
3521
Paul Elliott59ad9452022-12-18 15:09:02 +00003522 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3523
3524 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003525
3526 operation->id = 0;
3527
Paul Elliotte6145dc2023-02-07 12:51:21 +00003528 /* Do not clear either the error_occurred or num_ops elements here as they
3529 * only want to be cleared by the application calling abort, not by abort
3530 * being called at completion of an operation. */
3531
Paul Elliott59ad9452022-12-18 15:09:02 +00003532 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003533}
3534
3535psa_status_t psa_verify_hash_start(
3536 psa_verify_hash_interruptible_operation_t *operation,
3537 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3538 const uint8_t *hash, size_t hash_length,
3539 const uint8_t *signature, size_t signature_length)
3540{
3541 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3542 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3543 psa_key_slot_t *slot;
3544
Paul Elliottc9774412023-02-06 15:14:07 +00003545 /* Check that start has not been previously called, or operation has not
3546 * previously errored. */
3547 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003548 return PSA_ERROR_BAD_STATE;
3549 }
3550
3551 status = psa_sign_verify_check_alg(0, alg);
3552 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003553 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003554 return status;
3555 }
3556
3557 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3558 PSA_KEY_USAGE_VERIFY_HASH,
3559 alg);
3560
3561 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003562 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003563 return status;
3564 }
3565
3566 psa_key_attributes_t attributes = {
3567 .core = slot->attr
3568 };
3569
Paul Elliott296ede92022-12-15 17:00:30 +00003570 /* Ensure ops count gets reset, in case of operation re-use. */
3571 operation->num_ops = 0;
3572
Paul Elliott9fe12f62022-11-30 19:16:02 +00003573 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3574 slot->key.data,
3575 slot->key.bytes,
3576 alg, hash, hash_length,
3577 signature, signature_length);
3578
3579 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003580 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003581 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003582 }
3583
3584 unlock_status = psa_unlock_key_slot(slot);
3585
Paul Elliottc9774412023-02-06 15:14:07 +00003586 if (unlock_status != PSA_SUCCESS) {
3587 operation->error_occurred = 1;
3588 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003589
Paul Elliottc9774412023-02-06 15:14:07 +00003590 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003591}
3592
3593psa_status_t psa_verify_hash_complete(
3594 psa_verify_hash_interruptible_operation_t *operation)
3595{
3596 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3597
Paul Elliottc9774412023-02-06 15:14:07 +00003598 /* Check that start has been called first, and that operation has not
3599 * previously errored. */
3600 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003601 status = PSA_ERROR_BAD_STATE;
3602 goto exit;
3603 }
3604
3605 status = psa_driver_wrapper_verify_hash_complete(operation);
3606
Paul Elliott296ede92022-12-15 17:00:30 +00003607 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003608 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003609 operation);
3610
Paul Elliottebe225c2023-02-10 14:32:53 +00003611exit:
3612
Paul Elliott9fe12f62022-11-30 19:16:02 +00003613 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003614 if (status != PSA_SUCCESS) {
3615 operation->error_occurred = 1;
3616 }
3617
Paul Elliott59ad9452022-12-18 15:09:02 +00003618 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003619 }
3620
3621 return status;
3622}
3623
3624psa_status_t psa_verify_hash_abort(
3625 psa_verify_hash_interruptible_operation_t *operation)
3626{
Paul Elliott59ad9452022-12-18 15:09:02 +00003627 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003628
Paul Elliott59ad9452022-12-18 15:09:02 +00003629 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003630
Paul Elliott59ad9452022-12-18 15:09:02 +00003631 /* We clear the number of ops done here, so that it is not cleared when
3632 * the operation fails or succeeds, only on manual abort. */
3633 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003634
Paul Elliottc9774412023-02-06 15:14:07 +00003635 /* Likewise, failure state. */
3636 operation->error_occurred = 0;
3637
Paul Elliott59ad9452022-12-18 15:09:02 +00003638 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003639}
3640
Paul Elliott588f8ed2022-12-02 18:10:26 +00003641/****************************************************************/
3642/* Asymmetric interruptible cryptography internal */
3643/* implementations */
3644/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003645
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3647{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003648
Paul Elliott588f8ed2022-12-02 18:10:26 +00003649#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3650 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3651 defined(MBEDTLS_ECP_RESTARTABLE)
3652
3653 /* Internal implementation uses zero to indicate infinite number max ops,
3654 * therefore avoid this value, and set to minimum possible. */
3655 if (max_ops == 0) {
3656 max_ops = 1;
3657 }
3658
Paul Elliott588f8ed2022-12-02 18:10:26 +00003659 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003660#else
3661 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003662#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3663 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3664 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3665}
3666
Paul Elliott588f8ed2022-12-02 18:10:26 +00003667uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003668 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003669{
3670#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3671 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3672 defined(MBEDTLS_ECP_RESTARTABLE)
3673
Paul Elliott93d9ca82023-02-15 18:14:21 +00003674 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003675#else
3676 (void) operation;
3677 return 0;
3678#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3679 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3680 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3681}
3682
3683uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003684 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003685{
3686 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3687 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3688 defined(MBEDTLS_ECP_RESTARTABLE)
3689
Paul Elliott93d9ca82023-02-15 18:14:21 +00003690 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003691#else
3692 (void) operation;
3693 return 0;
3694#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3695 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3696 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3697}
3698
3699psa_status_t mbedtls_psa_sign_hash_start(
3700 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3701 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3702 size_t key_buffer_size, psa_algorithm_t alg,
3703 const uint8_t *hash, size_t hash_length)
3704{
3705 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003706 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003707
Paul Elliott068fe072023-01-16 13:59:15 +00003708 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3709 return PSA_ERROR_NOT_SUPPORTED;
3710 }
3711
3712 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003713 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003714 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715
3716#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003717 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3718 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003719
Paul Elliotta1c94092023-02-15 16:38:04 +00003720 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3721
Paul Elliott93d9ca82023-02-15 18:14:21 +00003722 /* Ensure num_ops is zero'ed in case of context re-use. */
3723 operation->num_ops = 0;
3724
Paul Elliott068fe072023-01-16 13:59:15 +00003725 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3726 attributes->core.bits,
3727 key_buffer,
3728 key_buffer_size,
3729 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003730
Paul Elliott068fe072023-01-16 13:59:15 +00003731 if (status != PSA_SUCCESS) {
3732 return status;
3733 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003734
Paul Elliott1bc59df2023-02-05 13:41:57 +00003735 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003736 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003737
Paul Elliott068fe072023-01-16 13:59:15 +00003738 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003739 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003740 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003741
Paul Elliott0290a762023-02-09 14:30:24 +00003742 /* We only need to store the same length of hash as the private key size
3743 * here, it would be truncated by the internal implementation anyway. */
3744 required_hash_length = (hash_length < operation->coordinate_bytes ?
3745 hash_length : operation->coordinate_bytes);
3746
Paul Elliottba70ad42023-02-15 18:23:53 +00003747 if (required_hash_length > sizeof(operation->hash)) {
3748 /* Shouldn't happen, but better safe than sorry. */
3749 return PSA_ERROR_CORRUPTION_DETECTED;
3750 }
3751
Paul Elliott0290a762023-02-09 14:30:24 +00003752 memcpy(operation->hash, hash, required_hash_length);
3753 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003754
3755 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003756
3757#else
Paul Elliott068fe072023-01-16 13:59:15 +00003758 (void) operation;
3759 (void) key_buffer;
3760 (void) key_buffer_size;
3761 (void) alg;
3762 (void) hash;
3763 (void) hash_length;
3764 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003765 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003766
Paul Elliott068fe072023-01-16 13:59:15 +00003767 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003768#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3769 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3770 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003771}
3772
3773psa_status_t mbedtls_psa_sign_hash_complete(
3774 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3775 uint8_t *signature, size_t signature_size,
3776 size_t *signature_length)
3777{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003778#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3779 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3780 defined(MBEDTLS_ECP_RESTARTABLE)
3781
Paul Elliotta1c94092023-02-15 16:38:04 +00003782 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003783 mbedtls_mpi r;
3784 mbedtls_mpi s;
3785
Paul Elliott46845252023-02-03 14:59:11 +00003786 mbedtls_mpi_init(&r);
3787 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003788
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003789 /* Ensure max_ops is set to the current value (or default). */
3790 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3791
Paul Elliotta1c94092023-02-15 16:38:04 +00003792 if (signature_size < 2 * operation->coordinate_bytes) {
3793 status = PSA_ERROR_BUFFER_TOO_SMALL;
3794 goto exit;
3795 }
3796
Paul Elliott588f8ed2022-12-02 18:10:26 +00003797 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003798
Paul Elliott588f8ed2022-12-02 18:10:26 +00003799#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3800 status = mbedtls_to_psa_error(
3801 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003802 &r,
3803 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003804 &operation->ctx->d,
3805 operation->hash,
3806 operation->hash_length,
3807 operation->md_alg,
3808 mbedtls_psa_get_random,
3809 MBEDTLS_PSA_RANDOM_STATE,
3810 &operation->restart_ctx));
3811#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003812 status = PSA_ERROR_NOT_SUPPORTED;
3813 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003814#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3815 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003816 status = mbedtls_to_psa_error(
3817 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003818 &r,
3819 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003820 &operation->ctx->d,
3821 operation->hash,
3822 operation->hash_length,
3823 mbedtls_psa_get_random,
3824 MBEDTLS_PSA_RANDOM_STATE,
3825 mbedtls_psa_get_random,
3826 MBEDTLS_PSA_RANDOM_STATE,
3827 &operation->restart_ctx));
3828 }
3829
Paul Elliottf8e5b562023-02-19 18:43:45 +00003830 /* Hide the fact that the restart context only holds a delta of number of
3831 * ops done during the last operation, not an absolute value. */
3832 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3833
Paul Elliott724bd252023-02-08 12:35:08 +00003834 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003835 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003836 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003837 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003838 operation->coordinate_bytes)
3839 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003840
3841 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003842 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003843 }
3844
3845 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003846 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003847 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003848 operation->coordinate_bytes,
3849 operation->coordinate_bytes)
3850 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851
3852 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003853 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003854 }
3855
Paul Elliott1bc59df2023-02-05 13:41:57 +00003856 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003857
Paul Elliott724bd252023-02-08 12:35:08 +00003858 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003859 }
Paul Elliott724bd252023-02-08 12:35:08 +00003860
3861exit:
3862
3863 mbedtls_mpi_free(&r);
3864 mbedtls_mpi_free(&s);
3865 return status;
3866
Paul Elliott588f8ed2022-12-02 18:10:26 +00003867 #else
3868
3869 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003870 (void) signature;
3871 (void) signature_size;
3872 (void) signature_length;
3873
3874 return PSA_ERROR_NOT_SUPPORTED;
3875
3876#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3877 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3878 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3879}
3880
3881psa_status_t mbedtls_psa_sign_hash_abort(
3882 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3883{
3884
3885#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3886 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3887 defined(MBEDTLS_ECP_RESTARTABLE)
3888
3889 if (operation->ctx) {
3890 mbedtls_ecdsa_free(operation->ctx);
3891 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003892 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003893 }
3894
3895 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3896
Paul Elliott93d9ca82023-02-15 18:14:21 +00003897 operation->num_ops = 0;
3898
Paul Elliott588f8ed2022-12-02 18:10:26 +00003899 return PSA_SUCCESS;
3900
3901#else
3902
3903 (void) operation;
3904
3905 return PSA_ERROR_NOT_SUPPORTED;
3906
3907#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3908 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3909 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3910}
3911
3912psa_status_t mbedtls_psa_verify_hash_start(
3913 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3914 const psa_key_attributes_t *attributes,
3915 const uint8_t *key_buffer, size_t key_buffer_size,
3916 psa_algorithm_t alg,
3917 const uint8_t *hash, size_t hash_length,
3918 const uint8_t *signature, size_t signature_length)
3919{
3920 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003921 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003922 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003923
Paul Elliott068fe072023-01-16 13:59:15 +00003924 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3925 return PSA_ERROR_NOT_SUPPORTED;
3926 }
3927
3928 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003929 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003930 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003931
3932#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003933 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3934 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003935
Paul Elliotta1c94092023-02-15 16:38:04 +00003936 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3937 mbedtls_mpi_init(&operation->r);
3938 mbedtls_mpi_init(&operation->s);
3939
Paul Elliott93d9ca82023-02-15 18:14:21 +00003940 /* Ensure num_ops is zero'ed in case of context re-use. */
3941 operation->num_ops = 0;
3942
Paul Elliott068fe072023-01-16 13:59:15 +00003943 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3944 attributes->core.bits,
3945 key_buffer,
3946 key_buffer_size,
3947 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003948
Paul Elliott068fe072023-01-16 13:59:15 +00003949 if (status != PSA_SUCCESS) {
3950 return status;
3951 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003952
Paul Elliottc569fc22023-02-10 13:02:54 +00003953 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003954
Paul Elliott1bc59df2023-02-05 13:41:57 +00003955 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003956 return PSA_ERROR_INVALID_SIGNATURE;
3957 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003958
Paul Elliott068fe072023-01-16 13:59:15 +00003959 status = mbedtls_to_psa_error(
3960 mbedtls_mpi_read_binary(&operation->r,
3961 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003962 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003963
Paul Elliott068fe072023-01-16 13:59:15 +00003964 if (status != PSA_SUCCESS) {
3965 return status;
3966 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003967
Paul Elliott068fe072023-01-16 13:59:15 +00003968 status = mbedtls_to_psa_error(
3969 mbedtls_mpi_read_binary(&operation->s,
3970 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003971 coordinate_bytes,
3972 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003973
Paul Elliott068fe072023-01-16 13:59:15 +00003974 if (status != PSA_SUCCESS) {
3975 return status;
3976 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003977
Paul Elliott2c9843f2023-02-15 17:32:42 +00003978 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003979
Paul Elliott2c9843f2023-02-15 17:32:42 +00003980 if (status != PSA_SUCCESS) {
3981 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003982 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003983
Paul Elliott0290a762023-02-09 14:30:24 +00003984 /* We only need to store the same length of hash as the private key size
3985 * here, it would be truncated by the internal implementation anyway. */
3986 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3987 coordinate_bytes);
3988
Paul Elliottba70ad42023-02-15 18:23:53 +00003989 if (required_hash_length > sizeof(operation->hash)) {
3990 /* Shouldn't happen, but better safe than sorry. */
3991 return PSA_ERROR_CORRUPTION_DETECTED;
3992 }
3993
Paul Elliott0290a762023-02-09 14:30:24 +00003994 memcpy(operation->hash, hash, required_hash_length);
3995 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003996
3997 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003998#else
Paul Elliott068fe072023-01-16 13:59:15 +00003999 (void) operation;
4000 (void) key_buffer;
4001 (void) key_buffer_size;
4002 (void) alg;
4003 (void) hash;
4004 (void) hash_length;
4005 (void) signature;
4006 (void) signature_length;
4007 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004008 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004009 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004010
Paul Elliott068fe072023-01-16 13:59:15 +00004011 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004012#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4013 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4014 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004015}
4016
4017psa_status_t mbedtls_psa_verify_hash_complete(
4018 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4019{
4020
4021#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4022 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4023 defined(MBEDTLS_ECP_RESTARTABLE)
4024
Paul Elliottf8e5b562023-02-19 18:43:45 +00004025 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4026
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004027 /* Ensure max_ops is set to the current value (or default). */
4028 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4029
Paul Elliottf8e5b562023-02-19 18:43:45 +00004030 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004031 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4032 operation->hash,
4033 operation->hash_length,
4034 &operation->ctx->Q,
4035 &operation->r,
4036 &operation->s,
4037 &operation->restart_ctx));
4038
Paul Elliottf8e5b562023-02-19 18:43:45 +00004039 /* Hide the fact that the restart context only holds a delta of number of
4040 * ops done during the last operation, not an absolute value. */
4041 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4042
4043 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004044#else
4045 (void) operation;
4046
4047 return PSA_ERROR_NOT_SUPPORTED;
4048
4049#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4050 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4051 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4052}
4053
4054psa_status_t mbedtls_psa_verify_hash_abort(
4055 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4056{
4057
4058#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4059 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4060 defined(MBEDTLS_ECP_RESTARTABLE)
4061
4062 if (operation->ctx) {
4063 mbedtls_ecdsa_free(operation->ctx);
4064 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004065 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004066 }
4067
4068 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4069
Paul Elliott93d9ca82023-02-15 18:14:21 +00004070 operation->num_ops = 0;
4071
Paul Elliott588f8ed2022-12-02 18:10:26 +00004072 mbedtls_mpi_free(&operation->r);
4073 mbedtls_mpi_free(&operation->s);
4074
4075 return PSA_SUCCESS;
4076
4077#else
4078 (void) operation;
4079
4080 return PSA_ERROR_NOT_SUPPORTED;
4081
4082#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4083 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4084 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4085}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004086
mohammad1603503973b2018-03-12 15:59:30 +02004087/****************************************************************/
4088/* Symmetric cryptography */
4089/****************************************************************/
4090
Gilles Peskine449bd832023-01-11 14:50:10 +01004091static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4092 mbedtls_svc_key_id_t key,
4093 psa_algorithm_t alg,
4094 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004095{
4096 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4097 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004098 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4100 PSA_KEY_USAGE_ENCRYPT :
4101 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004102 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004103
4104 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004106 status = PSA_ERROR_BAD_STATE;
4107 goto exit;
4108 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004109
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004111 status = PSA_ERROR_INVALID_ARGUMENT;
4112 goto exit;
4113 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004114
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4116 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004117 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004119
Ronald Cron49fafa92021-03-10 08:34:23 +01004120 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004121 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004122 * so we only set it (in the driver wrapper) after resources have been
4123 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004124 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004126 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004128 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 }
4130 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004131
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004132 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004133 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004134 };
4135
Ronald Cronab99ac22020-10-01 16:38:28 +02004136 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 if (cipher_operation == MBEDTLS_ENCRYPT) {
4138 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4139 &attributes,
4140 slot->key.data,
4141 slot->key.bytes,
4142 alg);
4143 } else {
4144 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4145 &attributes,
4146 slot->key.data,
4147 slot->key.bytes,
4148 alg);
4149 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004150
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004151exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 if (status != PSA_SUCCESS) {
4153 psa_cipher_abort(operation);
4154 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004155
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004157
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004159}
4160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4162 mbedtls_svc_key_id_t key,
4163 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004164{
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004166}
4167
Gilles Peskine449bd832023-01-11 14:50:10 +01004168psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4169 mbedtls_svc_key_id_t key,
4170 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004171{
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004173}
4174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4176 uint8_t *iv,
4177 size_t iv_size,
4178 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004179{
Ronald Cron6d051732020-10-01 14:10:20 +02004180 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004181 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004182 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004183
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004185 status = PSA_ERROR_BAD_STATE;
4186 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004187 }
4188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004190 status = PSA_ERROR_BAD_STATE;
4191 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004192 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004193
Ronald Cron2fb90522021-07-05 12:31:44 +02004194 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004196 status = PSA_ERROR_BUFFER_TOO_SMALL;
4197 goto exit;
4198 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004201 status = PSA_ERROR_GENERIC_ERROR;
4202 goto exit;
4203 }
4204
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 status = psa_generate_random(local_iv, default_iv_length);
4206 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004207 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 }
Ronald Cron5618a392021-03-26 09:52:26 +01004209
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 status = psa_driver_wrapper_cipher_set_iv(operation,
4211 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004212
4213exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 if (status == PSA_SUCCESS) {
4215 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004216 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004217 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004219 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004221 }
Ronald Cron6d051732020-10-01 14:10:20 +02004222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004224}
4225
Gilles Peskine449bd832023-01-11 14:50:10 +01004226psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4227 const uint8_t *iv,
4228 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004229{
Ronald Cron6d051732020-10-01 14:10:20 +02004230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004231
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004233 status = PSA_ERROR_BAD_STATE;
4234 goto exit;
4235 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004236
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004238 status = PSA_ERROR_BAD_STATE;
4239 goto exit;
4240 }
Ronald Crona0d68172021-03-26 10:15:08 +01004241
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004243 status = PSA_ERROR_INVALID_ARGUMENT;
4244 goto exit;
4245 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004246
Gilles Peskine449bd832023-01-11 14:50:10 +01004247 status = psa_driver_wrapper_cipher_set_iv(operation,
4248 iv,
4249 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004250
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004251exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004253 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 } else {
4255 psa_cipher_abort(operation);
4256 }
4257 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004258}
4259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4261 const uint8_t *input,
4262 size_t input_length,
4263 uint8_t *output,
4264 size_t output_size,
4265 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004266{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004267 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004268
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004270 status = PSA_ERROR_BAD_STATE;
4271 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004272 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004273
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004275 status = PSA_ERROR_BAD_STATE;
4276 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004277 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 status = psa_driver_wrapper_cipher_update(operation,
4280 input,
4281 input_length,
4282 output,
4283 output_size,
4284 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004285
4286exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 if (status != PSA_SUCCESS) {
4288 psa_cipher_abort(operation);
4289 }
Ronald Cron6d051732020-10-01 14:10:20 +02004290
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004292}
4293
Gilles Peskine449bd832023-01-11 14:50:10 +01004294psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4295 uint8_t *output,
4296 size_t output_size,
4297 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004298{
David Saadab4ecc272019-02-14 13:48:10 +02004299 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004300
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004302 status = PSA_ERROR_BAD_STATE;
4303 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004304 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004305
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004307 status = PSA_ERROR_BAD_STATE;
4308 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004309 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004310
Gilles Peskine449bd832023-01-11 14:50:10 +01004311 status = psa_driver_wrapper_cipher_finish(operation,
4312 output,
4313 output_size,
4314 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004315
4316exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 if (status == PSA_SUCCESS) {
4318 return psa_cipher_abort(operation);
4319 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004320 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004322
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004324 }
mohammad1603503973b2018-03-12 15:59:30 +02004325}
4326
Gilles Peskine449bd832023-01-11 14:50:10 +01004327psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004328{
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004330 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004331 * in use. It's ok to call abort on such an object, and there's
4332 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004334 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004335
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004337
Ronald Cron49fafa92021-03-10 08:34:23 +01004338 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004339 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004340 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004341
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004343}
4344
Gilles Peskine449bd832023-01-11 14:50:10 +01004345psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4346 psa_algorithm_t alg,
4347 const uint8_t *input,
4348 size_t input_length,
4349 uint8_t *output,
4350 size_t output_size,
4351 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004352{
4353 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004354 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004355 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004356 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4357 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004358 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004361 status = PSA_ERROR_INVALID_ARGUMENT;
4362 goto exit;
4363 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004364
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4366 PSA_KEY_USAGE_ENCRYPT,
4367 alg);
4368 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004369 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004371
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004372 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004374 };
4375
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4377 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004378 status = PSA_ERROR_GENERIC_ERROR;
4379 goto exit;
4380 }
4381
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 if (default_iv_length > 0) {
4383 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004384 status = PSA_ERROR_BUFFER_TOO_SMALL;
4385 goto exit;
4386 }
4387
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 status = psa_generate_random(local_iv, default_iv_length);
4389 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004390 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004392 }
4393
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004394 status = psa_driver_wrapper_cipher_encrypt(
4395 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004396 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004397 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004399
4400exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 unlock_status = psa_unlock_key_slot(slot);
4402 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004403 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004404 }
Ronald Cron23919522021-07-08 19:00:07 +02004405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 if (status == PSA_SUCCESS) {
4407 if (default_iv_length > 0) {
4408 memcpy(output, local_iv, default_iv_length);
4409 }
4410 *output_length += default_iv_length;
4411 } else {
4412 *output_length = 0;
4413 }
4414
4415 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004416}
4417
Gilles Peskine449bd832023-01-11 14:50:10 +01004418psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4419 psa_algorithm_t alg,
4420 const uint8_t *input,
4421 size_t input_length,
4422 uint8_t *output,
4423 size_t output_size,
4424 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004425{
4426 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004427 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004428 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004429 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004430
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004432 status = PSA_ERROR_INVALID_ARGUMENT;
4433 goto exit;
4434 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004435
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4437 PSA_KEY_USAGE_DECRYPT,
4438 alg);
4439 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004440 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004442
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004443 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004445 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004446
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4448 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004449 status = PSA_ERROR_INVALID_ARGUMENT;
4450 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004452 status = PSA_ERROR_INVALID_ARGUMENT;
4453 goto exit;
4454 }
4455
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004456 status = psa_driver_wrapper_cipher_decrypt(
4457 &attributes, slot->key.data, slot->key.bytes,
4458 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004460
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004461exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 unlock_status = psa_unlock_key_slot(slot);
4463 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004464 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004466
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004468 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 }
Ronald Cron23919522021-07-08 19:00:07 +02004470
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004472}
4473
4474
mohammad16035955c982018-04-26 00:53:03 +03004475/****************************************************************/
4476/* AEAD */
4477/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004478
Paul Elliottbaff51c2021-09-28 17:44:45 +01004479/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004480static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004481{
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004483}
4484
4485/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004486static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4487 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004488{
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004490
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004492#if defined(PSA_WANT_ALG_GCM)
4493 case PSA_ALG_GCM:
4494 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 * arbitrarily large nonces. Please note that we do not generally
4496 * recommend the usage of nonces of greater length than
4497 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4498 * size, which can then lead to collisions if you encrypt a very
4499 * large number of messages.*/
4500 if (nonce_length != 0) {
4501 return PSA_SUCCESS;
4502 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004503 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004504#endif /* PSA_WANT_ALG_GCM */
4505#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004506 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 if (nonce_length >= 7 && nonce_length <= 13) {
4508 return PSA_SUCCESS;
4509 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004510 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004511#endif /* PSA_WANT_ALG_CCM */
4512#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004513 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 if (nonce_length == 12) {
4515 return PSA_SUCCESS;
4516 } else if (nonce_length == 8) {
4517 return PSA_ERROR_NOT_SUPPORTED;
4518 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004519 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004520#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004521 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004522 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004524 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004527}
4528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004530{
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4532 return PSA_ERROR_INVALID_ARGUMENT;
4533 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004536}
4537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4539 psa_algorithm_t alg,
4540 const uint8_t *nonce,
4541 size_t nonce_length,
4542 const uint8_t *additional_data,
4543 size_t additional_data_length,
4544 const uint8_t *plaintext,
4545 size_t plaintext_length,
4546 uint8_t *ciphertext,
4547 size_t ciphertext_size,
4548 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004549{
Ronald Cron215633c2021-03-16 17:15:37 +01004550 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4551 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004552
mohammad1603f08a5502018-06-03 15:05:47 +03004553 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 status = psa_aead_check_algorithm(alg);
4556 if (status != PSA_SUCCESS) {
4557 return status;
4558 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004559
Ronald Cron9a986162021-03-26 12:40:07 +01004560 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4562 if (status != PSA_SUCCESS) {
4563 return status;
4564 }
mohammad16035955c982018-04-26 00:53:03 +03004565
Ronald Cron215633c2021-03-16 17:15:37 +01004566 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004568 };
mohammad16035955c982018-04-26 00:53:03 +03004569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 status = psa_aead_check_nonce_length(alg, nonce_length);
4571 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004572 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004574
Ronald Cronde822812021-03-17 16:08:20 +01004575 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004576 &attributes, slot->key.data, slot->key.bytes,
4577 alg,
4578 nonce, nonce_length,
4579 additional_data, additional_data_length,
4580 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4584 memset(ciphertext, 0, ciphertext_size);
4585 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004586
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004587exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004589
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004591}
4592
Gilles Peskine449bd832023-01-11 14:50:10 +01004593psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4594 psa_algorithm_t alg,
4595 const uint8_t *nonce,
4596 size_t nonce_length,
4597 const uint8_t *additional_data,
4598 size_t additional_data_length,
4599 const uint8_t *ciphertext,
4600 size_t ciphertext_length,
4601 uint8_t *plaintext,
4602 size_t plaintext_size,
4603 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004604{
Ronald Cron215633c2021-03-16 17:15:37 +01004605 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4606 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004607
Gilles Peskineee652a32018-06-01 19:23:52 +02004608 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004609
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 status = psa_aead_check_algorithm(alg);
4611 if (status != PSA_SUCCESS) {
4612 return status;
4613 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004614
Ronald Cron9a986162021-03-26 12:40:07 +01004615 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4617 if (status != PSA_SUCCESS) {
4618 return status;
4619 }
mohammad16035955c982018-04-26 00:53:03 +03004620
Ronald Cron215633c2021-03-16 17:15:37 +01004621 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004623 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004624
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 status = psa_aead_check_nonce_length(alg, nonce_length);
4626 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004627 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004629
Ronald Cronde822812021-03-17 16:08:20 +01004630 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004631 &attributes, slot->key.data, slot->key.bytes,
4632 alg,
4633 nonce, nonce_length,
4634 additional_data, additional_data_length,
4635 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004637
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 if (status != PSA_SUCCESS && plaintext_size != 0) {
4639 memset(plaintext, 0, plaintext_size);
4640 }
mohammad160360a64d02018-06-03 17:20:42 +03004641
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004642exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004644
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 return status;
mohammad16035955c982018-04-26 00:53:03 +03004646}
4647
Gilles Peskine449bd832023-01-11 14:50:10 +01004648static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4649{
4650 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004653#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004655 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4657 return PSA_ERROR_INVALID_ARGUMENT;
4658 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004659 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004660#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004661
Przemek Stekiel86679c72022-10-06 17:06:56 +02004662#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004664 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4666 return PSA_ERROR_INVALID_ARGUMENT;
4667 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004668 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004669#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004670
Przemek Stekiel86679c72022-10-06 17:06:56 +02004671#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004673 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 if (tag_len != 16) {
4675 return PSA_ERROR_INVALID_ARGUMENT;
4676 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004677 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004678#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004679
4680 default:
4681 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004683 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004685}
4686
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004687/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004688static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4689 int is_encrypt,
4690 mbedtls_svc_key_id_t key,
4691 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004692{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004693 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4694 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4695 psa_key_slot_t *slot = NULL;
4696 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004697 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004698
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 status = psa_aead_check_algorithm(alg);
4700 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004701 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004705 status = PSA_ERROR_BAD_STATE;
4706 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004707 }
4708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 if (operation->nonce_set || operation->lengths_set ||
4710 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004711 status = PSA_ERROR_BAD_STATE;
4712 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004713 }
4714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004716 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004718 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4722 alg);
4723 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004724 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004726
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004727 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004728 .core = slot->attr
4729 };
4730
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004732 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004734
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 if (is_encrypt) {
4736 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4737 &attributes,
4738 slot->key.data,
4739 slot->key.bytes,
4740 alg);
4741 } else {
4742 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4743 &attributes,
4744 slot->key.data,
4745 slot->key.bytes,
4746 alg);
4747 }
4748 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004749 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004753
4754exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004756
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004758 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004760 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 } else {
4762 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004763 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004764
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004766}
4767
Paul Elliott302ff6b2021-04-20 18:10:30 +01004768/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004769psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4770 mbedtls_svc_key_id_t key,
4771 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004772{
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004774}
4775
4776/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004777psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4778 mbedtls_svc_key_id_t key,
4779 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004780{
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004782}
4783
4784/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004785psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4786 uint8_t *nonce,
4787 size_t nonce_size,
4788 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004789{
4790 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004791 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004792 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004793
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004794 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004795
Gilles Peskine449bd832023-01-11 14:50:10 +01004796 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004797 status = PSA_ERROR_BAD_STATE;
4798 goto exit;
4799 }
4800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004802 status = PSA_ERROR_BAD_STATE;
4803 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004804 }
4805
Paul Elliotte193ea82021-10-01 13:00:16 +01004806 /* For CCM, this size may not be correct according to the PSA
4807 * specification. The PSA Crypto 1.0.1 specification states:
4808 *
4809 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4810 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4811 *
4812 * However this restriction that L has to be the smallest integer is not
4813 * applied in practice, and it is not implementable here since the
4814 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4816 operation->alg);
4817 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004818 status = PSA_ERROR_BUFFER_TOO_SMALL;
4819 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004820 }
4821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 status = psa_generate_random(local_nonce, required_nonce_size);
4823 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004824 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004826
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004828
Paul Elliott39dc6b82021-05-11 19:16:09 +01004829exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 if (status == PSA_SUCCESS) {
4831 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004832 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 } else {
4834 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004835 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004838}
4839
4840/* Set the nonce for a multipart authenticated encryption or decryption
4841 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004842psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4843 const uint8_t *nonce,
4844 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004845{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004846 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4847
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004849 status = PSA_ERROR_BAD_STATE;
4850 goto exit;
4851 }
4852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004854 status = PSA_ERROR_BAD_STATE;
4855 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004856 }
4857
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4859 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004860 status = PSA_ERROR_INVALID_ARGUMENT;
4861 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004862 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4865 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004866
Paul Elliott39dc6b82021-05-11 19:16:09 +01004867exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004869 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 } else {
4871 psa_aead_abort(operation);
4872 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004873
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004875}
4876
4877/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004878psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4879 size_t ad_length,
4880 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004881{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004882 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004885 status = PSA_ERROR_BAD_STATE;
4886 goto exit;
4887 }
4888
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 if (operation->lengths_set || operation->ad_started ||
4890 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004891 status = PSA_ERROR_BAD_STATE;
4892 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004893 }
4894
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004896#if defined(PSA_WANT_ALG_GCM)
4897 case PSA_ALG_GCM:
4898 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 * bits. Without the guard this code will generate warnings on 32bit
4900 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004901#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 if (((uint64_t) ad_length) >> 61 != 0 ||
4903 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004904 status = PSA_ERROR_INVALID_ARGUMENT;
4905 goto exit;
4906 }
Paul Elliott325d3742021-09-27 17:56:28 +01004907#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004908 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004909#endif /* PSA_WANT_ALG_GCM */
4910#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004911 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004913 status = PSA_ERROR_INVALID_ARGUMENT;
4914 goto exit;
4915 }
4916 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004917#endif /* PSA_WANT_ALG_CCM */
4918#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004919 case PSA_ALG_CHACHA20_POLY1305:
4920 /* No length restrictions for ChaChaPoly. */
4921 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004922#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004923 default:
4924 break;
4925 }
Paul Elliott325d3742021-09-27 17:56:28 +01004926
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4928 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004929
Paul Elliott39dc6b82021-05-11 19:16:09 +01004930exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004932 operation->ad_remaining = ad_length;
4933 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004934 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 } else {
4936 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004937 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004938
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004940}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004941
4942/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004943psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4944 const uint8_t *input,
4945 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004946{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004947 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4948
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004950 status = PSA_ERROR_BAD_STATE;
4951 goto exit;
4952 }
4953
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004955 status = PSA_ERROR_BAD_STATE;
4956 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004957 }
4958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 if (operation->lengths_set) {
4960 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004961 status = PSA_ERROR_INVALID_ARGUMENT;
4962 goto exit;
4963 }
4964
4965 operation->ad_remaining -= input_length;
4966 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004967#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004969 status = PSA_ERROR_BAD_STATE;
4970 goto exit;
4971 }
4972#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004973
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 status = psa_driver_wrapper_aead_update_ad(operation, input,
4975 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004976
Paul Elliott39dc6b82021-05-11 19:16:09 +01004977exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004979 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 } else {
4981 psa_aead_abort(operation);
4982 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004983
Gilles Peskine449bd832023-01-11 14:50:10 +01004984 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004985}
4986
4987/* Encrypt or decrypt a message fragment in an active multipart AEAD
4988 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004989psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4990 const uint8_t *input,
4991 size_t input_length,
4992 uint8_t *output,
4993 size_t output_size,
4994 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004995{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004996 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004997
4998 *output_length = 0;
4999
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005001 status = PSA_ERROR_BAD_STATE;
5002 goto exit;
5003 }
5004
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005006 status = PSA_ERROR_BAD_STATE;
5007 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005008 }
5009
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005011 /* Additional data length was supplied, but not all the additional
5012 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005014 status = PSA_ERROR_INVALID_ARGUMENT;
5015 goto exit;
5016 }
5017
5018 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005020 status = PSA_ERROR_INVALID_ARGUMENT;
5021 goto exit;
5022 }
5023
5024 operation->body_remaining -= input_length;
5025 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005026#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005028 status = PSA_ERROR_BAD_STATE;
5029 goto exit;
5030 }
5031#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005032
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5034 output, output_size,
5035 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005036
Paul Elliott39dc6b82021-05-11 19:16:09 +01005037exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005039 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 } else {
5041 psa_aead_abort(operation);
5042 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005043
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005045}
5046
Gilles Peskine449bd832023-01-11 14:50:10 +01005047static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005048{
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 if (operation->id == 0 || !operation->nonce_set) {
5050 return PSA_ERROR_BAD_STATE;
5051 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005052
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5054 operation->body_remaining != 0)) {
5055 return PSA_ERROR_INVALID_ARGUMENT;
5056 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005057
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005059}
5060
Paul Elliott302ff6b2021-04-20 18:10:30 +01005061/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005062psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5063 uint8_t *ciphertext,
5064 size_t ciphertext_size,
5065 size_t *ciphertext_length,
5066 uint8_t *tag,
5067 size_t tag_size,
5068 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005069{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005070 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5071
Paul Elliott302ff6b2021-04-20 18:10:30 +01005072 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005073 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005074
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 status = psa_aead_final_checks(operation);
5076 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005077 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005079
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005081 status = PSA_ERROR_BAD_STATE;
5082 goto exit;
5083 }
5084
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5086 ciphertext_size,
5087 ciphertext_length,
5088 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005089
Paul Elliott39dc6b82021-05-11 19:16:09 +01005090exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005091
5092
Paul Elliottf47b0952021-05-21 18:02:33 +01005093 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005094 * the zero tag size, make sure the tag is set to something implausible.
5095 * Even if the operation succeeds, make sure we clear the rest of the
5096 * buffer to prevent potential leakage of anything previously placed in
5097 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005098 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005099
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005101
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005103}
5104
5105/* Finish authenticating and decrypting a message in a multipart AEAD
5106 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005107psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5108 uint8_t *plaintext,
5109 size_t plaintext_size,
5110 size_t *plaintext_length,
5111 const uint8_t *tag,
5112 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005113{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005114 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5115
Paul Elliott302ff6b2021-04-20 18:10:30 +01005116 *plaintext_length = 0;
5117
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 status = psa_aead_final_checks(operation);
5119 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005122
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005124 status = PSA_ERROR_BAD_STATE;
5125 goto exit;
5126 }
5127
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5129 plaintext_size,
5130 plaintext_length,
5131 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005132
5133exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005135
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005137}
5138
5139/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005140psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005141{
5142 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5143
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005145 /* The object has (apparently) been initialized but it is not (yet)
5146 * in use. It's ok to call abort on such an object, and there's
5147 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005149 }
5150
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005152
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005154
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005156}
5157
Gilles Peskinee59236f2018-01-27 23:32:46 +01005158/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005159/* Generators */
5160/****************************************************************/
5161
Przemek Stekiel69c46792022-06-10 12:59:51 +02005162#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005163 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005164 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305165 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305166 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005167#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005168#endif /* At least one builtin KDF */
5169
Przemek Stekiel69c46792022-06-10 12:59:51 +02005170#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005171 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5172 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5173static psa_status_t psa_key_derivation_start_hmac(
5174 psa_mac_operation_t *operation,
5175 psa_algorithm_t hash_alg,
5176 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005178{
5179 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5182 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5183 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005184
Steven Cooreman72f736a2021-05-07 14:14:37 +02005185 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005187
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 status = psa_driver_wrapper_mac_sign_setup(operation,
5189 &attributes,
5190 hmac_key, hmac_key_length,
5191 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005192
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 psa_reset_key_attributes(&attributes);
5194 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005195}
5196#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005197
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005198#define HKDF_STATE_INIT 0 /* no input yet */
5199#define HKDF_STATE_STARTED 1 /* got salt */
5200#define HKDF_STATE_KEYED 2 /* got key */
5201#define HKDF_STATE_OUTPUT 3 /* output started */
5202
Gilles Peskinecbe66502019-05-16 16:59:18 +02005203static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005205{
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5207 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5208 } else {
5209 return operation->alg;
5210 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005211}
5212
Gilles Peskine449bd832023-01-11 14:50:10 +01005213psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005214{
5215 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5217 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005218 /* The object has (apparently) been initialized but it is not
5219 * in use. It's ok to call abort on such an object, and there's
5220 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005222#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5224 mbedtls_free(operation->ctx.hkdf.info);
5225 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5226 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005227#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005228#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5229 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5231 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5232 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5233 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005234 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005236 }
5237
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005239 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005241 }
5242
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005244 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005246 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005247#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005249 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005251 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005252#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005253 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005254
5255 /* We leave the fields Ai and output_block to be erased safely by the
5256 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005258#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5259 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005260#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5262 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5263 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5264 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005265#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305266#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305267 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305268 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005269 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305270 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305271 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305272
5273 status = PSA_SUCCESS;
5274 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305275#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005276 {
5277 status = PSA_ERROR_BAD_STATE;
5278 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 mbedtls_platform_zeroize(operation, sizeof(*operation));
5280 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005281}
5282
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005283psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005285{
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005289 }
5290
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005291 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005293}
5294
Gilles Peskine449bd832023-01-11 14:50:10 +01005295psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5296 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005297{
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 if (operation->alg == 0) {
5299 return PSA_ERROR_BAD_STATE;
5300 }
5301 if (capacity > operation->capacity) {
5302 return PSA_ERROR_INVALID_ARGUMENT;
5303 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005304 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005306}
5307
Przemek Stekiel69c46792022-06-10 12:59:51 +02005308#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005309/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005310static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5311 psa_algorithm_t kdf_alg,
5312 uint8_t *output,
5313 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005314{
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5316 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005317 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005318 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005319#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005321#else
5322 const uint8_t last_block = 0xff;
5323#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005324
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 if (hkdf->state < HKDF_STATE_KEYED ||
5326 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005327#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005329#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 )) {
5331 return PSA_ERROR_BAD_STATE;
5332 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005333 hkdf->state = HKDF_STATE_OUTPUT;
5334
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005336 /* Copy what remains of the current block */
5337 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005339 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 }
5341 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005342 output += n;
5343 output_length -= n;
5344 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005346 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005348 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005349 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005350 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005351 * object was corrupted or if this function is called directly
5352 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 if (hkdf->block_number == last_block) {
5354 return PSA_ERROR_BAD_STATE;
5355 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005356
5357 /* We need a new block */
5358 ++hkdf->block_number;
5359 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005360
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5362 hash_alg,
5363 hkdf->prk,
5364 hash_length);
5365 if (status != PSA_SUCCESS) {
5366 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005367 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005368
5369 if (hkdf->block_number != 1) {
5370 status = psa_mac_update(&hkdf->hmac,
5371 hkdf->output_block,
5372 hash_length);
5373 if (status != PSA_SUCCESS) {
5374 return status;
5375 }
5376 }
5377 status = psa_mac_update(&hkdf->hmac,
5378 hkdf->info,
5379 hkdf->info_length);
5380 if (status != PSA_SUCCESS) {
5381 return status;
5382 }
5383 status = psa_mac_update(&hkdf->hmac,
5384 &hkdf->block_number, 1);
5385 if (status != PSA_SUCCESS) {
5386 return status;
5387 }
5388 status = psa_mac_sign_finish(&hkdf->hmac,
5389 hkdf->output_block,
5390 sizeof(hkdf->output_block),
5391 &hmac_output_length);
5392 if (status != PSA_SUCCESS) {
5393 return status;
5394 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005395 }
5396
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005398}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005399#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005400
John Durkop07cc04a2020-11-16 22:08:34 -08005401#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5402 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005403static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5404 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005406{
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5408 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005409 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5410 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005411 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005412
Janos Follath7742fee2019-06-17 12:58:10 +01005413 /* We can't be wanting more output after block 0xff, otherwise
5414 * the capacity check in psa_key_derivation_output_bytes() would have
5415 * prevented this call. It could happen only if the operation
5416 * object was corrupted or if this function is called directly
5417 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 if (tls12_prf->block_number == 0xff) {
5419 return PSA_ERROR_CORRUPTION_DETECTED;
5420 }
Janos Follath7742fee2019-06-17 12:58:10 +01005421
5422 /* We need a new block */
5423 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005424 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005425
5426 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5427 *
5428 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5429 *
5430 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5431 * HMAC_hash(secret, A(2) + seed) +
5432 * HMAC_hash(secret, A(3) + seed) + ...
5433 *
5434 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005435 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005436 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005437 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005438 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005439 * is currently extracted as `output_block` and where i is
5440 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005441 */
5442
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 status = psa_key_derivation_start_hmac(&hmac,
5444 hash_alg,
5445 tls12_prf->secret,
5446 tls12_prf->secret_length);
5447 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005448 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005450
5451 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005453 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5454 * the variable seed and in this instance means it in the context of the
5455 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 status = psa_mac_update(&hmac,
5457 tls12_prf->label,
5458 tls12_prf->label_length);
5459 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005460 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 }
5462 status = psa_mac_update(&hmac,
5463 tls12_prf->seed,
5464 tls12_prf->seed_length);
5465 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005466 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 }
5468 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005469 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5471 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005472 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005474 }
5475
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 status = psa_mac_sign_finish(&hmac,
5477 tls12_prf->Ai, hash_length,
5478 &hmac_output_length);
5479 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005480 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 }
5482 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005483 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005485
5486 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 status = psa_key_derivation_start_hmac(&hmac,
5488 hash_alg,
5489 tls12_prf->secret,
5490 tls12_prf->secret_length);
5491 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005492 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 }
5494 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5495 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005496 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 }
5498 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5499 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005500 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 }
5502 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5503 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005504 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 }
5506 status = psa_mac_sign_finish(&hmac,
5507 tls12_prf->output_block, hash_length,
5508 &hmac_output_length);
5509 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005510 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005512
Janos Follath7742fee2019-06-17 12:58:10 +01005513
5514cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 cleanup_status = psa_mac_abort(&hmac);
5516 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005517 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005521}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005522
Janos Follath844eb0e2019-06-19 12:10:49 +01005523static psa_status_t psa_key_derivation_tls12_prf_read(
5524 psa_tls12_prf_key_derivation_t *tls12_prf,
5525 psa_algorithm_t alg,
5526 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005528{
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5530 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005531 psa_status_t status;
5532 uint8_t offset, length;
5533
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005535 case PSA_TLS12_PRF_STATE_LABEL_SET:
5536 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5537 break;
5538 case PSA_TLS12_PRF_STATE_OUTPUT:
5539 break;
5540 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005542 }
5543
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005545 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 if (tls12_prf->left_in_block == 0) {
5547 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5548 alg);
5549 if (status != PSA_SUCCESS) {
5550 return status;
5551 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005552
5553 continue;
5554 }
5555
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005557 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005559 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005561
5562 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005564 output += length;
5565 output_length -= length;
5566 tls12_prf->left_in_block -= length;
5567 }
5568
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005570}
John Durkop07cc04a2020-11-16 22:08:34 -08005571#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5572 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005573
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005574#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5575static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5576 psa_tls12_ecjpake_to_pms_t *ecjpake,
5577 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005579{
5580 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005581 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 if (output_length != 32) {
5584 return PSA_ERROR_INVALID_ARGUMENT;
5585 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005586
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5588 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5589 &output_size);
5590 if (status != PSA_SUCCESS) {
5591 return status;
5592 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005593
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 if (output_size != output_length) {
5595 return PSA_ERROR_GENERIC_ERROR;
5596 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005597
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005599}
5600#endif
5601
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305602#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305603static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5604 psa_pbkdf2_key_derivation_t *pbkdf2,
5605 psa_algorithm_t prf_alg,
5606 uint8_t prf_output_length,
5607 psa_key_attributes_t *attributes)
5608{
5609 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305610 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305611 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305612 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305613 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305614 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305615 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305616
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305617 mac_operation.is_sign = 1;
5618 mac_operation.mac_size = prf_output_length;
5619 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305620
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305621 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5622 attributes,
5623 pbkdf2->password,
5624 pbkdf2->password_length,
5625 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305626 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305627 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305628 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305629 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5630 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305631 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305632 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305633 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305634 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305635 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305636 }
5637 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5638 &mac_output_length);
5639 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305640 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305641 }
5642
5643 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305644 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305645 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305646 }
5647
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305648 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305649
5650 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305651 /* We are passing prf_output_length as mac_size because the driver
5652 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305653 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305654 status = psa_driver_wrapper_mac_compute(attributes,
5655 pbkdf2->password,
5656 pbkdf2->password_length,
5657 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305658 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305659 &mac_output_length);
5660 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305661 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305662 }
5663
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305664 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305665 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305666
5667cleanup:
5668 /* Zeroise buffers to clear sensitive data from memory. */
5669 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5670 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305671}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305672
5673static psa_status_t psa_key_derivation_pbkdf2_read(
5674 psa_pbkdf2_key_derivation_t *pbkdf2,
5675 psa_algorithm_t kdf_alg,
5676 uint8_t *output,
5677 size_t output_length)
5678{
5679 psa_status_t status;
5680 psa_algorithm_t prf_alg;
5681 uint8_t prf_output_length;
5682 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5683 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5684 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5685
5686 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5687 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5688 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5689 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305690 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5691 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305692 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305693 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305694 } else {
5695 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305696 }
5697
5698 switch (pbkdf2->state) {
5699 case PSA_PBKDF2_STATE_PASSWORD_SET:
5700 /* Initially we need a new block so bytes_used is equal to block size*/
5701 pbkdf2->bytes_used = prf_output_length;
5702 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5703 break;
5704 case PSA_PBKDF2_STATE_OUTPUT:
5705 break;
5706 default:
5707 return PSA_ERROR_BAD_STATE;
5708 }
5709
5710 while (output_length != 0) {
5711 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5712 if (n > output_length) {
5713 n = (uint8_t) output_length;
5714 }
5715 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5716 output += n;
5717 output_length -= n;
5718 pbkdf2->bytes_used += n;
5719
5720 if (output_length == 0) {
5721 break;
5722 }
5723
5724 /* We need a new block */
5725 pbkdf2->bytes_used = 0;
5726 pbkdf2->block_number++;
5727
5728 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5729 prf_output_length,
5730 &attributes);
5731 if (status != PSA_SUCCESS) {
5732 return status;
5733 }
5734 }
5735
5736 return PSA_SUCCESS;
5737}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305738#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305739
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005740psa_status_t psa_key_derivation_output_bytes(
5741 psa_key_derivation_operation_t *operation,
5742 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005744{
5745 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005747
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005749 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005751 }
5752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005754 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005755 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005756 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005757 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005758 goto exit;
5759 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005761 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005762 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005763 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5764 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005765 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005766 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005768 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005769 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005770
Przemek Stekiel69c46792022-06-10 12:59:51 +02005771#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5773 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5774 output, output_length);
5775 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005776#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005777#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5778 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5780 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5781 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5782 kdf_alg, output,
5783 output_length);
5784 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005785#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5786 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005787#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005789 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5791 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005792#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305793#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305794 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305795 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5796 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305797 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305798#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005799
Gilles Peskineeab56e42018-07-12 17:12:33 +02005800 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005801 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005803 }
5804
5805exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005807 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005808 * This allows us to differentiate between exhausted operations and
5809 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5810 * operations. */
5811 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005813 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005815 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005817}
5818
David Brown7807bf72021-02-09 16:28:23 -07005819#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005820static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005821{
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 if (data_size >= 8) {
5823 mbedtls_des_key_set_parity(data);
5824 }
5825 if (data_size >= 16) {
5826 mbedtls_des_key_set_parity(data + 8);
5827 }
5828 if (data_size >= 24) {
5829 mbedtls_des_key_set_parity(data + 16);
5830 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005831}
David Brown7807bf72021-02-09 16:28:23 -07005832#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005833
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005834/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 * ECC keys on a Weierstrass elliptic curve require the generation
5836 * of a private key which is an integer
5837 * in the range [1, N - 1], where N is the boundary of the private key domain:
5838 * N is the prime p for Diffie-Hellman, or the order of the
5839 * curve’s base point for ECC.
5840 *
5841 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5842 * This function generates the private key using the following process:
5843 *
5844 * 1. Draw a byte string of length ceiling(m/8) bytes.
5845 * 2. If m is not a multiple of 8, set the most significant
5846 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5847 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5848 * 4. If k > N - 2, discard the result and return to step 1.
5849 * 5. Output k + 1 as the private key.
5850 *
5851 * This method allows compliance to NIST standards, specifically the methods titled
5852 * Key-Pair Generation by Testing Candidates in the following publications:
5853 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5854 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5855 * Diffie-Hellman keys.
5856 *
5857 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5858 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5859 *
5860 * Note: Function allocates memory for *data buffer, so given *data should be
5861 * always NULL.
5862 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005863#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5864#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005865static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005866 psa_key_slot_t *slot,
5867 size_t bits,
5868 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005869 uint8_t **data
5870 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005871{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005872 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005873 mbedtls_mpi k;
5874 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005876 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005877 size_t m;
5878 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005879
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 mbedtls_mpi_init(&k);
5881 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005882
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005883 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005885 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005887
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005889 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005890 goto cleanup;
5891 }
5892
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005893 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005895
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005897
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005898 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005899 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005900 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005901
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005902 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005903
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005904 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005906
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005907 /* Note: This function is always called with *data == NULL and it
5908 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 *data = mbedtls_calloc(1, m_bytes);
5910 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005911 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005912 goto cleanup;
5913 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005914
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005916 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005918 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005920
5921 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005923 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005924 * (8 * ceiling(m/8) - m) bits of the first byte in
5925 * the string to zero.
5926 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005928 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005929 }
5930
5931 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 * big-endian byte string.
5933 */
5934 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005935
5936 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 * Result of comparison is returned. When it indicates error
5938 * then this function is called again.
5939 */
5940 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005941 }
5942
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005943 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5945 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005946cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 if (ret != 0) {
5948 status = mbedtls_to_psa_error(ret);
5949 }
5950 if (status != PSA_SUCCESS) {
5951 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005952 *data = NULL;
5953 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 mbedtls_mpi_free(&k);
5955 mbedtls_mpi_free(&diff_N_2);
5956 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005957}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005958
5959/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5960 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5961 *
5962 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5963 * draw a 32-byte string and process it as specified in
5964 * Elliptic Curves for Security [RFC7748] §5.
5965 *
5966 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5967 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5968 *
5969 * Note: Function allocates memory for *data buffer, so given *data should be
5970 * always NULL.
5971 */
5972
5973static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5974 size_t bits,
5975 psa_key_derivation_operation_t *operation,
5976 uint8_t **data
5977 )
5978{
5979 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005980 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005981
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005983 case 255:
5984 output_length = 32;
5985 break;
5986 case 448:
5987 output_length = 56;
5988 break;
5989 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005991 break;
5992 }
5993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005995
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 if (*data == NULL) {
5997 return PSA_ERROR_INSUFFICIENT_MEMORY;
5998 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005999
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006001
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006003 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006005
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006007 case 255:
6008 (*data)[0] &= 248;
6009 (*data)[31] &= 127;
6010 (*data)[31] |= 64;
6011 break;
6012 case 448:
6013 (*data)[0] &= 252;
6014 (*data)[55] |= 128;
6015 break;
6016 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006018 break;
6019 }
6020
6021 return status;
6022}
Valerio Setti86587ab2023-06-27 13:09:30 +02006023#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6024static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6025 psa_key_slot_t *slot, size_t bits,
6026 psa_key_derivation_operation_t *operation, uint8_t **data)
6027{
6028 (void) slot;
6029 (void) bits;
6030 (void) operation;
6031 (void) data;
6032 return PSA_ERROR_NOT_SUPPORTED;
6033}
6034
6035static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6036 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6037{
6038 (void) bits;
6039 (void) operation;
6040 (void) data;
6041 return PSA_ERROR_NOT_SUPPORTED;
6042}
6043#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6044#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006045
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006046static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006047 psa_key_slot_t *slot,
6048 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006050{
6051 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306053 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006054 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006055 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006056
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6058 return PSA_ERROR_INVALID_ARGUMENT;
6059 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006060
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006061#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006062 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6064 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6065 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006066 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6068 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006069 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 }
6071 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006072 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6074 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006075 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006077 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006078 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006079#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006080 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 if (key_type_is_raw_bytes(slot->attr.type)) {
6082 if (bits % 8 != 0) {
6083 return PSA_ERROR_INVALID_ARGUMENT;
6084 }
6085 data = mbedtls_calloc(1, bytes);
6086 if (data == NULL) {
6087 return PSA_ERROR_INSUFFICIENT_MEMORY;
6088 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006089
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 status = psa_key_derivation_output_bytes(operation, data, bytes);
6091 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006092 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 }
David Brown12ca5032021-02-11 11:02:00 -07006094#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6096 psa_des_set_key_parity(data, bytes);
6097 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006098#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 } else {
6100 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006101 }
Ronald Crondd04d422020-11-28 15:14:42 +01006102
Ronald Cronbf33c932020-11-28 18:06:53 +01006103 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006104 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006106 };
6107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6109 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6110 &storage_size);
6111 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 }
Archanad8a83dc2021-06-14 10:04:16 +05306114 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 status = psa_allocate_buffer_to_slot(slot, storage_size);
6116 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306117 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 }
Archanad8a83dc2021-06-14 10:04:16 +05306119
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 status = psa_driver_wrapper_import_key(&attributes,
6121 data, bytes,
6122 slot->key.data,
6123 slot->key.bytes,
6124 &slot->key.bytes, &bits);
6125 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006126 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006128
6129exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 mbedtls_free(data);
6131 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006132}
6133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6135 psa_key_derivation_operation_t *operation,
6136 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006137{
6138 psa_status_t status;
6139 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006140 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006141
Ronald Cron81709fc2020-11-14 12:10:32 +01006142 *key = MBEDTLS_SVC_KEY_ID_INIT;
6143
Gilles Peskine0f84d622019-09-12 19:03:13 +02006144 /* Reject any attempt to create a zero-length key so that we don't
6145 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 if (psa_get_key_bits(attributes) == 0) {
6147 return PSA_ERROR_INVALID_ARGUMENT;
6148 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 if (operation->alg == PSA_ALG_NONE) {
6151 return PSA_ERROR_BAD_STATE;
6152 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006153
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 if (!operation->can_output_key) {
6155 return PSA_ERROR_NOT_PERMITTED;
6156 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006157
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6159 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006160#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006162 /* Deriving a key in a secure element is not implemented yet. */
6163 status = PSA_ERROR_NOT_SUPPORTED;
6164 }
6165#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 if (status == PSA_SUCCESS) {
6167 status = psa_generate_derived_key_internal(slot,
6168 attributes->core.bits,
6169 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006170 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 if (status == PSA_SUCCESS) {
6172 status = psa_finish_key_creation(slot, driver, key);
6173 }
6174 if (status != PSA_SUCCESS) {
6175 psa_fail_key_creation(slot, driver);
6176 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006177
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006179}
6180
Gilles Peskineeab56e42018-07-12 17:12:33 +02006181
6182
6183/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006184/* Key derivation */
6185/****************************************************************/
6186
Steven Cooreman932ffb72021-02-15 12:14:32 +01006187#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006188static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006189{
6190#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6192 return 1;
6193 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006194#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006195#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6197 return 1;
6198 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006199#endif
6200#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6202 return 1;
6203 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006204#endif
6205#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6207 return 1;
6208 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006209#endif
6210#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6212 return 1;
6213 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006214#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006215#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6217 return 1;
6218 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006219#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306220#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6221 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6222 return 1;
6223 }
6224#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306225#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6226 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6227 return 1;
6228 }
6229#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006231}
6232
Gilles Peskine449bd832023-01-11 14:50:10 +01006233static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006234{
6235 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 psa_status_t status = psa_hash_setup(&operation, alg);
6237 psa_hash_abort(&operation);
6238 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006239}
6240
Gilles Peskine969c5d62019-01-16 15:53:06 +01006241static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006242 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006244{
Janos Follath5fe19732019-06-20 15:09:30 +01006245 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6246 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006248
Gilles Peskine969c5d62019-01-16 15:53:06 +01006249 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 if (!is_kdf_alg_supported(kdf_alg)) {
6251 return PSA_ERROR_NOT_SUPPORTED;
6252 }
John Durkop07cc04a2020-11-16 22:08:34 -08006253
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006254 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306255 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6257 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306258 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6259 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6260 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306261 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306262 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 if (hash_size == 0) {
6264 return PSA_ERROR_NOT_SUPPORTED;
6265 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006266
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006267 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6268 * we might fail later, which is somewhat unfriendly and potentially
6269 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 psa_status_t status = psa_hash_try_support(hash_alg);
6271 if (status != PSA_SUCCESS) {
6272 return status;
6273 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006274 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6277 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6278 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6279 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006280 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006281#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006282 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6284 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006285 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006287#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6288 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 operation->capacity = 255 * hash_size;
6290 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006291}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006294{
6295#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 if (alg == PSA_ALG_ECDH) {
6297 return PSA_SUCCESS;
6298 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006299#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006300#if defined(PSA_WANT_ALG_FFDH)
6301 if (alg == PSA_ALG_FFDH) {
6302 return PSA_SUCCESS;
6303 }
6304#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006305 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006307}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006308
6309static int psa_key_derivation_allows_free_form_secret_input(
6310 psa_algorithm_t kdf_alg)
6311{
6312#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6313 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6314 return 0;
6315 }
6316#endif
6317 (void) kdf_alg;
6318 return 1;
6319}
John Durkop07cc04a2020-11-16 22:08:34 -08006320#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6323 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006324{
6325 psa_status_t status;
6326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 if (operation->alg != 0) {
6328 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006329 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006330
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6332 return PSA_ERROR_INVALID_ARGUMENT;
6333 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6334#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6335 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6336 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6337 status = psa_key_agreement_try_support(ka_alg);
6338 if (status != PSA_SUCCESS) {
6339 return status;
6340 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006341 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6342 return PSA_ERROR_INVALID_ARGUMENT;
6343 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6345#else
6346 return PSA_ERROR_NOT_SUPPORTED;
6347#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6348 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6349#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6350 status = psa_key_derivation_setup_kdf(operation, alg);
6351#else
6352 return PSA_ERROR_NOT_SUPPORTED;
6353#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6354 } else {
6355 return PSA_ERROR_INVALID_ARGUMENT;
6356 }
6357
6358 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006359 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 }
6361 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006362}
6363
Przemek Stekiel69c46792022-06-10 12:59:51 +02006364#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006365static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6366 psa_algorithm_t kdf_alg,
6367 psa_key_derivation_step_t step,
6368 const uint8_t *data,
6369 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006370{
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006372 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006374 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006375#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6377 return PSA_ERROR_INVALID_ARGUMENT;
6378 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006379#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 if (hkdf->state != HKDF_STATE_INIT) {
6381 return PSA_ERROR_BAD_STATE;
6382 } else {
6383 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6384 hash_alg,
6385 data, data_length);
6386 if (status != PSA_SUCCESS) {
6387 return status;
6388 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006389 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006391 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006392 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006393#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006395 /* We shouldn't be in different state as HKDF_EXPAND only allows
6396 * two inputs: SECRET (this case) and INFO which does not modify
6397 * the state. It could happen only if the hkdf
6398 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 if (hkdf->state != HKDF_STATE_INIT) {
6400 return PSA_ERROR_BAD_STATE;
6401 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006402
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006403 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6405 return PSA_ERROR_INVALID_ARGUMENT;
6406 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 memcpy(hkdf->prk, data, data_length);
6409 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006410#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006411 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006412 /* HKDF: If no salt was provided, use an empty salt.
6413 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006415#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6417 return PSA_ERROR_BAD_STATE;
6418 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006419#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6421 hash_alg,
6422 NULL, 0);
6423 if (status != PSA_SUCCESS) {
6424 return status;
6425 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006426 hkdf->state = HKDF_STATE_STARTED;
6427 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 if (hkdf->state != HKDF_STATE_STARTED) {
6429 return PSA_ERROR_BAD_STATE;
6430 }
6431 status = psa_mac_update(&hkdf->hmac,
6432 data, data_length);
6433 if (status != PSA_SUCCESS) {
6434 return status;
6435 }
6436 status = psa_mac_sign_finish(&hkdf->hmac,
6437 hkdf->prk,
6438 sizeof(hkdf->prk),
6439 &data_length);
6440 if (status != PSA_SUCCESS) {
6441 return status;
6442 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006443 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006444
Gilles Peskine2b522db2019-04-12 15:11:49 +02006445 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006446 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006447#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006449 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006451 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006453#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006454 {
6455 /* Block 0 is empty, and the next block will be
6456 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006458 }
6459
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006461 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006462#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6464 return PSA_ERROR_INVALID_ARGUMENT;
6465 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006466#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006467#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6469 hkdf->state == HKDF_STATE_INIT) {
6470 return PSA_ERROR_BAD_STATE;
6471 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006472#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 if (hkdf->state == HKDF_STATE_OUTPUT) {
6474 return PSA_ERROR_BAD_STATE;
6475 }
6476 if (hkdf->info_set) {
6477 return PSA_ERROR_BAD_STATE;
6478 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006479 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 if (data_length != 0) {
6481 hkdf->info = mbedtls_calloc(1, data_length);
6482 if (hkdf->info == NULL) {
6483 return PSA_ERROR_INSUFFICIENT_MEMORY;
6484 }
6485 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006486 }
6487 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006489 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006491 }
6492}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006493#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006494
John Durkop07cc04a2020-11-16 22:08:34 -08006495#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6496 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006497static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6498 const uint8_t *data,
6499 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006500{
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6502 return PSA_ERROR_BAD_STATE;
6503 }
Janos Follathf08e2652019-06-13 09:05:41 +01006504
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 if (data_length != 0) {
6506 prf->seed = mbedtls_calloc(1, data_length);
6507 if (prf->seed == NULL) {
6508 return PSA_ERROR_INSUFFICIENT_MEMORY;
6509 }
Janos Follathf08e2652019-06-13 09:05:41 +01006510
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006512 prf->seed_length = data_length;
6513 }
Janos Follathf08e2652019-06-13 09:05:41 +01006514
Gilles Peskine43f958b2020-12-13 14:55:14 +01006515 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006516
Gilles Peskine449bd832023-01-11 14:50:10 +01006517 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006518}
6519
Gilles Peskine449bd832023-01-11 14:50:10 +01006520static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6521 const uint8_t *data,
6522 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006523{
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6525 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6526 return PSA_ERROR_BAD_STATE;
6527 }
Janos Follath81550542019-06-13 14:26:34 +01006528
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 if (data_length != 0) {
6530 prf->secret = mbedtls_calloc(1, data_length);
6531 if (prf->secret == NULL) {
6532 return PSA_ERROR_INSUFFICIENT_MEMORY;
6533 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006534
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006536 prf->secret_length = data_length;
6537 }
Janos Follath81550542019-06-13 14:26:34 +01006538
Gilles Peskine43f958b2020-12-13 14:55:14 +01006539 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006540
Gilles Peskine449bd832023-01-11 14:50:10 +01006541 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006542}
6543
Gilles Peskine449bd832023-01-11 14:50:10 +01006544static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6545 const uint8_t *data,
6546 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006547{
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6549 return PSA_ERROR_BAD_STATE;
6550 }
Janos Follath63028dd2019-06-13 09:15:47 +01006551
Gilles Peskine449bd832023-01-11 14:50:10 +01006552 if (data_length != 0) {
6553 prf->label = mbedtls_calloc(1, data_length);
6554 if (prf->label == NULL) {
6555 return PSA_ERROR_INSUFFICIENT_MEMORY;
6556 }
Janos Follath63028dd2019-06-13 09:15:47 +01006557
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006559 prf->label_length = data_length;
6560 }
Janos Follath63028dd2019-06-13 09:15:47 +01006561
Gilles Peskine43f958b2020-12-13 14:55:14 +01006562 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006563
Gilles Peskine449bd832023-01-11 14:50:10 +01006564 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006565}
6566
Gilles Peskine449bd832023-01-11 14:50:10 +01006567static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6568 psa_key_derivation_step_t step,
6569 const uint8_t *data,
6570 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006571{
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006573 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006575 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006577 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006579 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006581 }
6582}
John Durkop07cc04a2020-11-16 22:08:34 -08006583#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6584 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6585
6586#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6587static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6588 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006589 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006590 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006591{
6592 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6594 4 + data_length + prf->other_secret_length :
6595 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006596
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6598 return PSA_ERROR_INVALID_ARGUMENT;
6599 }
John Durkop07cc04a2020-11-16 22:08:34 -08006600
Gilles Peskine449bd832023-01-11 14:50:10 +01006601 uint8_t *pms = mbedtls_calloc(1, pms_len);
6602 if (pms == NULL) {
6603 return PSA_ERROR_INSUFFICIENT_MEMORY;
6604 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006605 uint8_t *cur = pms;
6606
6607 /* pure-PSK:
6608 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006609 *
6610 * The premaster secret is formed as follows: if the PSK is N octets
6611 * long, concatenate a uint16 with the value N, N zero octets, a second
6612 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006613 *
6614 * mixed-PSK:
6615 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6616 * follows: concatenate a uint16 with the length of the other secret,
6617 * the other secret itself, uint16 with the length of PSK, and the
6618 * PSK itself.
6619 * For details please check:
6620 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6621 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6622 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006623 */
6624
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6626 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6627 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6628 if (prf->other_secret_length != 0) {
6629 memcpy(cur, prf->other_secret, prf->other_secret_length);
6630 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006631 cur += prf->other_secret_length;
6632 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006633 } else {
6634 *cur++ = MBEDTLS_BYTE_1(data_length);
6635 *cur++ = MBEDTLS_BYTE_0(data_length);
6636 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006637 cur += data_length;
6638 }
6639
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 *cur++ = MBEDTLS_BYTE_1(data_length);
6641 *cur++ = MBEDTLS_BYTE_0(data_length);
6642 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006643 cur += data_length;
6644
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006645 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006646
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006647 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006649}
Janos Follath6660f0e2019-06-17 08:44:03 +01006650
Przemek Stekiele47201b2022-04-19 13:53:28 +02006651static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6652 psa_tls12_prf_key_derivation_t *prf,
6653 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006654 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006655{
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6657 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006658 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006659
6660 if (data_length != 0) {
6661 prf->other_secret = mbedtls_calloc(1, data_length);
6662 if (prf->other_secret == NULL) {
6663 return PSA_ERROR_INSUFFICIENT_MEMORY;
6664 }
6665
6666 memcpy(prf->other_secret, data, data_length);
6667 prf->other_secret_length = data_length;
6668 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006669 prf->other_secret_length = 0;
6670 }
6671
6672 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6673
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006675}
6676
Janos Follath6660f0e2019-06-17 08:44:03 +01006677static psa_status_t psa_tls12_prf_psk_to_ms_input(
6678 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006679 psa_key_derivation_step_t step,
6680 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006681 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006682{
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006684 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 return psa_tls12_prf_psk_to_ms_set_key(prf,
6686 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006687 break;
6688 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6690 data,
6691 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006692 break;
6693 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006695 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006696
Przemek Stekiele47201b2022-04-19 13:53:28 +02006697 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006698}
John Durkop07cc04a2020-11-16 22:08:34 -08006699#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006700
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006701#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6702static psa_status_t psa_tls12_ecjpake_to_pms_input(
6703 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006704 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006705 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006706 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006707{
Gilles Peskine449bd832023-01-11 14:50:10 +01006708 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6709 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6710 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006711 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006712
6713 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 if (data[0] != 0x04) {
6715 return PSA_ERROR_INVALID_ARGUMENT;
6716 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006717
6718 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006720
Gilles Peskine449bd832023-01-11 14:50:10 +01006721 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006722}
6723#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306724
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306725#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306726static psa_status_t psa_pbkdf2_set_input_cost(
6727 psa_pbkdf2_key_derivation_t *pbkdf2,
6728 psa_key_derivation_step_t step,
6729 uint64_t data)
6730{
6731 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6732 return PSA_ERROR_INVALID_ARGUMENT;
6733 }
6734
6735 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6736 return PSA_ERROR_BAD_STATE;
6737 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306738
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306739 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306740 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306741 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306742
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306743 if (data == 0) {
6744 return PSA_ERROR_INVALID_ARGUMENT;
6745 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306746
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306747 pbkdf2->input_cost = data;
6748 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6749
6750 return PSA_SUCCESS;
6751}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306752
6753static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6754 const uint8_t *data,
6755 size_t data_length)
6756{
Gilles Peskineead17662023-08-20 22:02:51 +02006757 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6758 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6759 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6760 /* Appending to existing salt. No state change. */
6761 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306762 return PSA_ERROR_BAD_STATE;
6763 }
6764
Gilles Peskineca57d782023-07-20 19:08:06 +02006765 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006766 /* Appending an empty string, nothing to do. */
6767 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306768 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306769
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306770 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6771 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306772 return PSA_ERROR_INSUFFICIENT_MEMORY;
6773 }
6774
Gilles Peskineead17662023-08-20 22:02:51 +02006775 if (pbkdf2->salt_length != 0) {
6776 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6777 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306778 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306779 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306780 mbedtls_free(pbkdf2->salt);
6781 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306782 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306783 return PSA_SUCCESS;
6784}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306785
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306786#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306787static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306788 const uint8_t *input,
6789 size_t input_len,
6790 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306791 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306792{
6793 psa_status_t status = PSA_SUCCESS;
6794 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6795 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306796 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306797 } else {
6798 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306799 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306800 }
6801 return status;
6802}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306803#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306804
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306805#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306806static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6807 size_t input_len,
6808 uint8_t *output,
6809 size_t *output_len)
6810{
6811 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306812 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306813 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306814 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306815 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6816 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6817 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306818 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306819 * mac_size as the driver function sets mac_output_length = mac_size
6820 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306821 status = psa_driver_wrapper_mac_compute(&attributes,
6822 zeros, sizeof(zeros),
6823 PSA_ALG_CMAC, input, input_len,
6824 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306825 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6826 128U,
6827 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306828 output_len);
6829 } else {
6830 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306831 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306832 }
6833 return status;
6834}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306835#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306836
6837static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306838 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306839 const uint8_t *data,
6840 size_t data_length)
6841{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306842 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306843 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6844 return PSA_ERROR_BAD_STATE;
6845 }
6846
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306847#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306848 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6849 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6850 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6851 pbkdf2->password,
6852 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306853 } else
6854#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6855#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6856 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306857 status = psa_pbkdf2_cmac_set_password(data, data_length,
6858 pbkdf2->password,
6859 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306860 } else
6861#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6862 {
6863 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306864 }
6865
6866 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6867
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306868 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306869}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306870
6871static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306872 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306873 psa_key_derivation_step_t step,
6874 const uint8_t *data,
6875 size_t data_length)
6876{
6877 switch (step) {
6878 case PSA_KEY_DERIVATION_INPUT_SALT:
6879 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6880 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306881 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306882 default:
6883 return PSA_ERROR_INVALID_ARGUMENT;
6884 }
6885}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306886#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306887
Gilles Peskineb8965192019-09-24 16:21:10 +02006888/** Check whether the given key type is acceptable for the given
6889 * input step of a key derivation.
6890 *
6891 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6892 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6893 * Both secret and non-secret inputs can alternatively have the type
6894 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6895 * that the input was passed as a buffer rather than via a key object.
6896 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006897static int psa_key_derivation_check_input_type(
6898 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006899 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006900{
Gilles Peskine449bd832023-01-11 14:50:10 +01006901 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006902 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 if (key_type == PSA_KEY_TYPE_DERIVE) {
6904 return PSA_SUCCESS;
6905 }
6906 if (key_type == PSA_KEY_TYPE_NONE) {
6907 return PSA_SUCCESS;
6908 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006909 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006910 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 if (key_type == PSA_KEY_TYPE_DERIVE) {
6912 return PSA_SUCCESS;
6913 }
6914 if (key_type == PSA_KEY_TYPE_NONE) {
6915 return PSA_SUCCESS;
6916 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006917 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006918 case PSA_KEY_DERIVATION_INPUT_LABEL:
6919 case PSA_KEY_DERIVATION_INPUT_SALT:
6920 case PSA_KEY_DERIVATION_INPUT_INFO:
6921 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6923 return PSA_SUCCESS;
6924 }
6925 if (key_type == PSA_KEY_TYPE_NONE) {
6926 return PSA_SUCCESS;
6927 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006928 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306929 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6930 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6931 return PSA_SUCCESS;
6932 }
6933 if (key_type == PSA_KEY_TYPE_DERIVE) {
6934 return PSA_SUCCESS;
6935 }
6936 if (key_type == PSA_KEY_TYPE_NONE) {
6937 return PSA_SUCCESS;
6938 }
6939 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006940 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006942}
6943
Janos Follathb80a94e2019-06-12 15:54:46 +01006944static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006945 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006946 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006947 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006948 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006950{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006951 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006953
Gilles Peskine449bd832023-01-11 14:50:10 +01006954 status = psa_key_derivation_check_input_type(step, key_type);
6955 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006956 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006958
Przemek Stekiel69c46792022-06-10 12:59:51 +02006959#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006960 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6961 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6962 step, data, data_length);
6963 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006964#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006965#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6967 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6968 step, data, data_length);
6969 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006970#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6971#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006972 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6973 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6974 step, data, data_length);
6975 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006976#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006977#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006978 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006979 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6981 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006982#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306983#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306984 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306985 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6986 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306987 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306988#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006989 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006990 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006991 (void) data;
6992 (void) data_length;
6993 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006994 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006995 }
6996
Gilles Peskine224b0d62019-09-23 18:13:17 +02006997exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006998 if (status != PSA_SUCCESS) {
6999 psa_key_derivation_abort(operation);
7000 }
7001 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007002}
7003
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307004static psa_status_t psa_key_derivation_input_integer_internal(
7005 psa_key_derivation_operation_t *operation,
7006 psa_key_derivation_step_t step,
7007 uint64_t value)
7008{
7009 psa_status_t status;
7010 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7011
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307012#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307013 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307014 status = psa_pbkdf2_set_input_cost(
7015 &operation->ctx.pbkdf2, step, value);
7016 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307017#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307018 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307019 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307020 (void) value;
7021 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307022 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307023 }
7024
7025 if (status != PSA_SUCCESS) {
7026 psa_key_derivation_abort(operation);
7027 }
7028 return status;
7029}
7030
Janos Follath51f4a0f2019-06-14 11:35:55 +01007031psa_status_t psa_key_derivation_input_bytes(
7032 psa_key_derivation_operation_t *operation,
7033 psa_key_derivation_step_t step,
7034 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007036{
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 return psa_key_derivation_input_internal(operation, step,
7038 PSA_KEY_TYPE_NONE,
7039 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007040}
7041
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307042psa_status_t psa_key_derivation_input_integer(
7043 psa_key_derivation_operation_t *operation,
7044 psa_key_derivation_step_t step,
7045 uint64_t value)
7046{
7047 return psa_key_derivation_input_integer_internal(operation, step, value);
7048}
7049
Janos Follath51f4a0f2019-06-14 11:35:55 +01007050psa_status_t psa_key_derivation_input_key(
7051 psa_key_derivation_operation_t *operation,
7052 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007054{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007055 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007056 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007057 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007058
Ronald Cron5c522922020-11-14 16:35:34 +01007059 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7061 if (status != PSA_SUCCESS) {
7062 psa_key_derivation_abort(operation);
7063 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007064 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007065
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307066 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7067 * permission to output to a key object. */
7068 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7069 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007070 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007072
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 status = psa_key_derivation_input_internal(operation,
7074 step, slot->attr.type,
7075 slot->key.data,
7076 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007077
Gilles Peskine449bd832023-01-11 14:50:10 +01007078 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007079
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007081}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007082
7083
7084
7085/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007086/* Key agreement */
7087/****************************************************************/
7088
Gilles Peskine449bd832023-01-11 14:50:10 +01007089psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7090 const uint8_t *key_buffer,
7091 size_t key_buffer_size,
7092 psa_algorithm_t alg,
7093 const uint8_t *peer_key,
7094 size_t peer_key_length,
7095 uint8_t *shared_secret,
7096 size_t shared_secret_size,
7097 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007098{
Gilles Peskine449bd832023-01-11 14:50:10 +01007099 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007100#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007101 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7103 key_buffer_size, alg,
7104 peer_key, peer_key_length,
7105 shared_secret,
7106 shared_secret_size,
7107 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007108#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007109
7110#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7111 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007112 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007113 peer_key,
7114 peer_key_length,
7115 key_buffer,
7116 key_buffer_size,
7117 shared_secret,
7118 shared_secret_size,
7119 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007120#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7121
Gilles Peskine01d718c2018-09-18 12:01:02 +02007122 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007123 (void) attributes;
7124 (void) key_buffer;
7125 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007126 (void) peer_key;
7127 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007128 (void) shared_secret;
7129 (void) shared_secret_size;
7130 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007132 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007133}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007134
Aditya Deshpande17845b82022-10-13 17:21:01 +01007135/** Internal function for raw key agreement
7136 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007137 * to the driver's implementation if a driver is present.
7138 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007139 * (psa_key_agreement_raw_builtin).
7140 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007141static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7142 psa_key_slot_t *private_key,
7143 const uint8_t *peer_key,
7144 size_t peer_key_length,
7145 uint8_t *shared_secret,
7146 size_t shared_secret_size,
7147 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007148{
Gilles Peskine449bd832023-01-11 14:50:10 +01007149 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7150 return PSA_ERROR_NOT_SUPPORTED;
7151 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007152
7153 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007155 };
7156
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 return psa_driver_wrapper_key_agreement(&attributes,
7158 private_key->key.data,
7159 private_key->key.bytes, alg,
7160 peer_key, peer_key_length,
7161 shared_secret,
7162 shared_secret_size,
7163 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007164}
7165
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007166/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007167 * to potentially free embedded data structures and wipe confidential data.
7168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007169static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7170 psa_key_derivation_step_t step,
7171 psa_key_slot_t *private_key,
7172 const uint8_t *peer_key,
7173 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007174{
7175 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007176 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007177 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007179
7180 /* Step 1: run the secret agreement algorithm to generate the shared
7181 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007182 status = psa_key_agreement_raw_internal(ka_alg,
7183 private_key,
7184 peer_key, peer_key_length,
7185 shared_secret,
7186 sizeof(shared_secret),
7187 &shared_secret_length);
7188 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007189 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007190 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007191
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007192 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007193 * the shared secret. A shared secret is permitted wherever a key
7194 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 status = psa_key_derivation_input_internal(operation, step,
7196 PSA_KEY_TYPE_DERIVE,
7197 shared_secret,
7198 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007199exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007200 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7201 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007202}
7203
Gilles Peskine449bd832023-01-11 14:50:10 +01007204psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7205 psa_key_derivation_step_t step,
7206 mbedtls_svc_key_id_t private_key,
7207 const uint8_t *peer_key,
7208 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007209{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007210 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007211 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007212 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007213
Gilles Peskine449bd832023-01-11 14:50:10 +01007214 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7215 return PSA_ERROR_INVALID_ARGUMENT;
7216 }
Ronald Cron5c522922020-11-14 16:35:34 +01007217 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7219 if (status != PSA_SUCCESS) {
7220 return status;
7221 }
7222 status = psa_key_agreement_internal(operation, step,
7223 slot,
7224 peer_key, peer_key_length);
7225 if (status != PSA_SUCCESS) {
7226 psa_key_derivation_abort(operation);
7227 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007228 /* If a private key has been added as SECRET, we allow the derived
7229 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007231 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007233 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007234
Gilles Peskine449bd832023-01-11 14:50:10 +01007235 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007236
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007238}
7239
Gilles Peskine449bd832023-01-11 14:50:10 +01007240psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7241 mbedtls_svc_key_id_t private_key,
7242 const uint8_t *peer_key,
7243 size_t peer_key_length,
7244 uint8_t *output,
7245 size_t output_size,
7246 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007247{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007248 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007249 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007250 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007251 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007252
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007254 status = PSA_ERROR_INVALID_ARGUMENT;
7255 goto exit;
7256 }
Ronald Cron5c522922020-11-14 16:35:34 +01007257 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007258 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7259 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007260 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007262
Gilles Peskine3e561302022-04-14 00:17:15 +02007263 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7264 * for the output size. The PSA specification only guarantees that this
7265 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7266 * but it might be nice to allow smaller buffers if the output fits.
7267 * At the time of writing this comment, with only ECDH implemented,
7268 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7269 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7270 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007271 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7273 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007274 status = PSA_ERROR_BUFFER_TOO_SMALL;
7275 goto exit;
7276 }
7277
Gilles Peskine449bd832023-01-11 14:50:10 +01007278 status = psa_key_agreement_raw_internal(alg, slot,
7279 peer_key, peer_key_length,
7280 output, output_size,
7281 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007282
7283exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007284 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007285 /* If an error happens and is not handled properly, the output
7286 * may be used as a key to protect sensitive data. Arrange for such
7287 * a key to be random, which is likely to result in decryption or
7288 * verification errors. This is better than filling the buffer with
7289 * some constant data such as zeros, which would result in the data
7290 * being protected with a reproducible, easily knowable key.
7291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007292 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007293 *output_length = output_size;
7294 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007295
Gilles Peskine449bd832023-01-11 14:50:10 +01007296 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007297
Gilles Peskine449bd832023-01-11 14:50:10 +01007298 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007299}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007300
7301
Gilles Peskine30524eb2020-11-13 17:02:26 +01007302
Gilles Peskine86a440b2018-11-12 18:39:40 +01007303/****************************************************************/
7304/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007305/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007306
Gilles Peskine672a7712023-04-28 21:00:28 +02007307#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7308#include "entropy_poll.h"
7309#endif
7310
Gilles Peskine30524eb2020-11-13 17:02:26 +01007311/** Initialize the PSA random generator.
7312 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007313static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007314{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007315#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007316 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007317#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7318
Gilles Peskine30524eb2020-11-13 17:02:26 +01007319 /* Set default configuration if
7320 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007322 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007323 }
7324 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007325 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007327
Gilles Peskine449bd832023-01-11 14:50:10 +01007328 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007329#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7330 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7331 /* The PSA entropy injection feature depends on using NV seed as an entropy
7332 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007333 mbedtls_entropy_add_source(&rng->entropy,
7334 mbedtls_nv_seed_poll, NULL,
7335 MBEDTLS_ENTROPY_BLOCK_SIZE,
7336 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007337#endif
7338
Gilles Peskine449bd832023-01-11 14:50:10 +01007339 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007340#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007341}
7342
7343/** Deinitialize the PSA random generator.
7344 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007345static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007346{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007347#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007348 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007349#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti83e0de82023-11-24 12:13:05 +01007350 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7351 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007352#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007353}
7354
7355/** Seed the PSA random generator.
7356 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007357static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007358{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007359#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7360 /* Do nothing: the external RNG seeds itself. */
7361 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007363#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007364 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7366 drbg_seed, sizeof(drbg_seed) - 1);
7367 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007368#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007369}
7370
Gilles Peskine449bd832023-01-11 14:50:10 +01007371psa_status_t psa_generate_random(uint8_t *output,
7372 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007373{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007374 GUARD_MODULE_INITIALIZED;
7375
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007376#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7377
7378 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007379 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7380 output, output_size,
7381 &output_length);
7382 if (status != PSA_SUCCESS) {
7383 return status;
7384 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007385 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007386 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007387 if (output_length != output_size) {
7388 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7389 }
7390 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007391
7392#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7393
Gilles Peskine449bd832023-01-11 14:50:10 +01007394 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007395 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7397 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7398 output_size);
7399 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7400 output, request_size);
7401 if (ret != 0) {
7402 return mbedtls_to_psa_error(ret);
7403 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007404 output_size -= request_size;
7405 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007406 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007407 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007408#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007409}
7410
Gilles Peskine8814fc42020-12-14 15:33:44 +01007411/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007412 *
7413 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7414 * `psa_generate_random(...)`. The state parameter is ignored since the
7415 * PSA API doesn't support passing an explicit state.
7416 *
7417 * In the non-external case, psa_generate_random() calls an
7418 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7419 * and semantics as mbedtls_psa_get_random(). As an optimization,
7420 * instead of doing this back-and-forth between the PSA API and the
7421 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7422 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007423 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007424#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7425int mbedtls_psa_get_random(void *p_rng,
7426 unsigned char *output,
7427 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007428{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007429 /* This function takes a pointer to the RNG state because that's what
7430 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7431 * its own state internally and doesn't let the caller access that state.
7432 * So we just ignore the state parameter, and in practice we'll pass
7433 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007434 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 psa_status_t status = psa_generate_random(output, output_size);
7436 if (status == PSA_SUCCESS) {
7437 return 0;
7438 } else {
7439 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7440 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007441}
7442#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7443
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007444#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007445psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7446 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007447{
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 if (global_data.initialized) {
7449 return PSA_ERROR_NOT_PERMITTED;
7450 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007451
Gilles Peskine449bd832023-01-11 14:50:10 +01007452 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7453 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7454 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7455 return PSA_ERROR_INVALID_ARGUMENT;
7456 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007457
Gilles Peskine449bd832023-01-11 14:50:10 +01007458 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007459}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007460#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007461
Ronald Cron3772afe2021-02-08 16:10:05 +01007462/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007463 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007464 * \param type The key type
7465 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007466 *
7467 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007468 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007469 * \retval #PSA_ERROR_INVALID_ARGUMENT
7470 * The size in bits of the key is not valid.
7471 * \retval #PSA_ERROR_NOT_SUPPORTED
7472 * The type and/or the size in bits of the key or the combination of
7473 * the two is not supported.
7474 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007475static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007477{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007478 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007479
Gilles Peskine449bd832023-01-11 14:50:10 +01007480 if (key_type_is_raw_bytes(type)) {
7481 status = psa_validate_unstructured_key_bit_size(type, bits);
7482 if (status != PSA_SUCCESS) {
7483 return status;
7484 }
7485 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007486#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007487 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7488 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7489 return PSA_ERROR_NOT_SUPPORTED;
7490 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007491 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007492 return PSA_ERROR_NOT_SUPPORTED;
7493 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007494
Ronald Cron01b2aba2020-10-05 09:42:02 +02007495 /* Accept only byte-aligned keys, for the same reasons as
7496 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007497 if (bits % 8 != 0) {
7498 return PSA_ERROR_NOT_SUPPORTED;
7499 }
7500 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007501#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007502
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007503#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007504 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007505 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007506 return PSA_SUCCESS;
7507 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007508#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007509
Valerio Settia55f0422023-07-10 15:34:41 +02007510#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007511 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007512 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007513 return PSA_ERROR_NOT_SUPPORTED;
7514 }
7515 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007516#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007517 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007518 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007519 }
7520
Gilles Peskine449bd832023-01-11 14:50:10 +01007521 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007522}
7523
Ronald Cron55ed0592020-10-05 10:30:40 +02007524psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007525 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007527{
7528 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007529 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007530
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 if ((attributes->domain_parameters == NULL) &&
7532 (attributes->domain_parameters_size != 0)) {
7533 return PSA_ERROR_INVALID_ARGUMENT;
7534 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007535
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 if (key_type_is_raw_bytes(type)) {
7537 status = psa_generate_random(key_buffer, key_buffer_size);
7538 if (status != PSA_SUCCESS) {
7539 return status;
7540 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007541
David Brown12ca5032021-02-11 11:02:00 -07007542#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007543 if (type == PSA_KEY_TYPE_DES) {
7544 psa_des_set_key_parity(key_buffer, key_buffer_size);
7545 }
David Brown8107e312021-02-12 08:08:46 -07007546#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007547 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007548
Valerio Setti76df8c12023-07-11 14:11:28 +02007549#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007550 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7551 return mbedtls_psa_rsa_generate_key(attributes,
7552 key_buffer,
7553 key_buffer_size,
7554 key_buffer_length);
7555 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007556#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007557
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007558#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007559 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7560 return mbedtls_psa_ecp_generate_key(attributes,
7561 key_buffer,
7562 key_buffer_size,
7563 key_buffer_length);
7564 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007565#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007566
Valerio Settia55f0422023-07-10 15:34:41 +02007567#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007568 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7569 return mbedtls_psa_ffdh_generate_key(attributes,
7570 key_buffer,
7571 key_buffer_size,
7572 key_buffer_length);
7573 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007574#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007575 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007576 (void) key_buffer_length;
7577 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007578 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007579
Gilles Peskine449bd832023-01-11 14:50:10 +01007580 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007581}
Darryl Green0c6575a2018-11-07 16:05:30 +00007582
Gilles Peskine449bd832023-01-11 14:50:10 +01007583psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7584 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007585{
7586 psa_status_t status;
7587 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007588 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007589 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007590
Ronald Cron81709fc2020-11-14 12:10:32 +01007591 *key = MBEDTLS_SVC_KEY_ID_INIT;
7592
Gilles Peskine0f84d622019-09-12 19:03:13 +02007593 /* Reject any attempt to create a zero-length key so that we don't
7594 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 if (psa_get_key_bits(attributes) == 0) {
7596 return PSA_ERROR_INVALID_ARGUMENT;
7597 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007598
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007599 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007600 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7601 return PSA_ERROR_INVALID_ARGUMENT;
7602 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007603
Gilles Peskine449bd832023-01-11 14:50:10 +01007604 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7605 &slot, &driver);
7606 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007607 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007608 }
Gilles Peskine11792082019-08-06 18:36:36 +02007609
Ronald Cron977c2472020-10-13 08:32:21 +02007610 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307611 * storage ( thus not in the case of generating a key in a secure element
7612 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7613 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007614 if (slot->key.data == NULL) {
7615 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7616 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007617 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007618 attributes->core.type, attributes->core.bits);
7619 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007620 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007621 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007622
7623 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007624 attributes->core.type,
7625 attributes->core.bits);
7626 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007627 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007628 attributes, &key_buffer_size);
7629 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007630 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007631 }
Ronald Cron977c2472020-10-13 08:32:21 +02007632 }
Ronald Cron977c2472020-10-13 08:32:21 +02007633
Gilles Peskine449bd832023-01-11 14:50:10 +01007634 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7635 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007636 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 }
Ronald Cron977c2472020-10-13 08:32:21 +02007638 }
7639
Gilles Peskine449bd832023-01-11 14:50:10 +01007640 status = psa_driver_wrapper_generate_key(attributes,
7641 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007642
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 if (status != PSA_SUCCESS) {
7644 psa_remove_key_data_from_memory(slot);
7645 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007646
Gilles Peskine11792082019-08-06 18:36:36 +02007647exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007648 if (status == PSA_SUCCESS) {
7649 status = psa_finish_key_creation(slot, driver, key);
7650 }
7651 if (status != PSA_SUCCESS) {
7652 psa_fail_key_creation(slot, driver);
7653 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007654
Gilles Peskine449bd832023-01-11 14:50:10 +01007655 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007656}
7657
Gilles Peskinee59236f2018-01-27 23:32:46 +01007658/****************************************************************/
7659/* Module setup */
7660/****************************************************************/
7661
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007662#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007663psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007664 void (* entropy_init)(mbedtls_entropy_context *ctx),
7665 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007666{
Gilles Peskine449bd832023-01-11 14:50:10 +01007667 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7668 return PSA_ERROR_BAD_STATE;
7669 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007670 global_data.rng.entropy_init = entropy_init;
7671 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007672 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007673}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007674#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007675
Gilles Peskine449bd832023-01-11 14:50:10 +01007676void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007677{
Gilles Peskine449bd832023-01-11 14:50:10 +01007678 psa_wipe_all_key_slots();
Valerio Setti83e0de82023-11-24 12:13:05 +01007679 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7680 mbedtls_psa_random_free(&global_data.rng);
7681 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007682 /* Wipe all remaining data, including configuration.
7683 * In particular, this sets all state indicator to the value
7684 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007686
7687 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007689}
7690
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007691#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7692/** Recover a transaction that was interrupted by a power failure.
7693 *
7694 * This function is called during initialization, before psa_crypto_init()
7695 * returns. If this function returns a failure status, the initialization
7696 * fails.
7697 */
7698static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007699 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007700{
Gilles Peskine449bd832023-01-11 14:50:10 +01007701 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007702 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7703 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007704 /* TODO - fall through to the failure case until this
7705 * is implemented.
7706 * https://github.com/ARMmbed/mbed-crypto/issues/218
7707 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007708 default:
7709 /* We found an unsupported transaction in the storage.
7710 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007711 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007712 }
7713}
7714#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7715
Gilles Peskine449bd832023-01-11 14:50:10 +01007716psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007717{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007718 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007719
Gilles Peskinec6b69072018-11-20 21:42:52 +01007720 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007721 if (global_data.initialized != 0) {
7722 return PSA_SUCCESS;
7723 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007724
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007725 /* Init drivers */
7726 status = psa_driver_wrapper_init();
7727 if (status != PSA_SUCCESS) {
7728 goto exit;
7729 }
7730 global_data.drivers_initialized = 1;
7731
Valerio Setti402cfba2023-11-13 10:24:32 +01007732 status = psa_initialize_key_slots();
7733 if (status != PSA_SUCCESS) {
7734 goto exit;
7735 }
7736
Gilles Peskine30524eb2020-11-13 17:02:26 +01007737 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007738 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007739 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007740 status = mbedtls_psa_random_seed(&global_data.rng);
7741 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007742 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007743 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007744 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007745
Gilles Peskine4b734222019-07-24 15:56:31 +02007746#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007747 status = psa_crypto_load_transaction();
7748 if (status == PSA_SUCCESS) {
7749 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7750 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007751 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007752 }
7753 status = psa_crypto_stop_transaction();
7754 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007755 /* There's no transaction to complete. It's all good. */
7756 status = PSA_SUCCESS;
7757 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007758#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007759
Gilles Peskinec6b69072018-11-20 21:42:52 +01007760 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007761 global_data.initialized = 1;
7762
7763exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007764 if (status != PSA_SUCCESS) {
7765 mbedtls_psa_crypto_free();
7766 }
7767 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007768}
7769
Yanray Wangffc3c482023-07-11 12:01:04 +08007770#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007771psa_status_t psa_crypto_driver_pake_get_password_len(
7772 const psa_crypto_driver_pake_inputs_t *inputs,
7773 size_t *password_len)
7774{
7775 if (inputs->password_len == 0) {
7776 return PSA_ERROR_BAD_STATE;
7777 }
7778
7779 *password_len = inputs->password_len;
7780
7781 return PSA_SUCCESS;
7782}
7783
7784psa_status_t psa_crypto_driver_pake_get_password(
7785 const psa_crypto_driver_pake_inputs_t *inputs,
7786 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7787{
7788 if (inputs->password_len == 0) {
7789 return PSA_ERROR_BAD_STATE;
7790 }
7791
7792 if (buffer_size < inputs->password_len) {
7793 return PSA_ERROR_BUFFER_TOO_SMALL;
7794 }
7795
7796 memcpy(buffer, inputs->password, inputs->password_len);
7797 *buffer_length = inputs->password_len;
7798
7799 return PSA_SUCCESS;
7800}
7801
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007802psa_status_t psa_crypto_driver_pake_get_user_len(
7803 const psa_crypto_driver_pake_inputs_t *inputs,
7804 size_t *user_len)
7805{
7806 if (inputs->user_len == 0) {
7807 return PSA_ERROR_BAD_STATE;
7808 }
7809
7810 *user_len = inputs->user_len;
7811
7812 return PSA_SUCCESS;
7813}
7814
7815psa_status_t psa_crypto_driver_pake_get_user(
7816 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007817 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007818{
7819 if (inputs->user_len == 0) {
7820 return PSA_ERROR_BAD_STATE;
7821 }
7822
Przemek Stekielc0e62502023-03-14 11:49:36 +01007823 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007824 return PSA_ERROR_BUFFER_TOO_SMALL;
7825 }
7826
Przemek Stekielc0e62502023-03-14 11:49:36 +01007827 memcpy(user_id, inputs->user, inputs->user_len);
7828 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007829
7830 return PSA_SUCCESS;
7831}
7832
7833psa_status_t psa_crypto_driver_pake_get_peer_len(
7834 const psa_crypto_driver_pake_inputs_t *inputs,
7835 size_t *peer_len)
7836{
7837 if (inputs->peer_len == 0) {
7838 return PSA_ERROR_BAD_STATE;
7839 }
7840
7841 *peer_len = inputs->peer_len;
7842
7843 return PSA_SUCCESS;
7844}
7845
7846psa_status_t psa_crypto_driver_pake_get_peer(
7847 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007848 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007849{
7850 if (inputs->peer_len == 0) {
7851 return PSA_ERROR_BAD_STATE;
7852 }
7853
Przemek Stekielc0e62502023-03-14 11:49:36 +01007854 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007855 return PSA_ERROR_BUFFER_TOO_SMALL;
7856 }
7857
Przemek Stekielc0e62502023-03-14 11:49:36 +01007858 memcpy(peer_id, inputs->peer, inputs->peer_len);
7859 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007860
7861 return PSA_SUCCESS;
7862}
7863
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007864psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7865 const psa_crypto_driver_pake_inputs_t *inputs,
7866 psa_pake_cipher_suite_t *cipher_suite)
7867{
7868 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7869 return PSA_ERROR_BAD_STATE;
7870 }
7871
7872 *cipher_suite = inputs->cipher_suite;
7873
7874 return PSA_SUCCESS;
7875}
7876
Neil Armstronga7d08c32022-06-01 18:21:20 +02007877psa_status_t psa_pake_setup(
7878 psa_pake_operation_t *operation,
7879 const psa_pake_cipher_suite_t *cipher_suite)
7880{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007881 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007882
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007883 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007884 status = PSA_ERROR_BAD_STATE;
7885 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007886 }
7887
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007888 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007889 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007890 status = PSA_ERROR_INVALID_ARGUMENT;
7891 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007892 }
7893
Przemek Stekiel51eac532022-12-07 11:04:51 +01007894 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7895
Przemek Stekiele12ed362022-12-21 12:54:46 +01007896 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007897 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7898 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007899 operation->data.inputs.cipher_suite = *cipher_suite;
7900
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007901#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007902 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007903 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007904 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007905
David Horstmann16f01512023-06-14 17:21:07 +01007906 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007907 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007908 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007909#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007910 {
7911 status = PSA_ERROR_NOT_SUPPORTED;
7912 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007913 }
7914
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007915 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7916
Przemek Stekiel51eac532022-12-07 11:04:51 +01007917 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007918exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007919 psa_pake_abort(operation);
7920 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007921}
7922
7923psa_status_t psa_pake_set_password_key(
7924 psa_pake_operation_t *operation,
7925 mbedtls_svc_key_id_t password)
7926{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007927 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007928 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7929 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007930 psa_key_attributes_t attributes;
7931 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007932
Przemek Stekiel51eac532022-12-07 11:04:51 +01007933 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007934 status = PSA_ERROR_BAD_STATE;
7935 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007936 }
7937
Przemek Stekiel6c764412022-11-22 14:05:12 +01007938 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7939 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007940 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007941 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007942 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007943 }
7944
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007945 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007946 .core = slot->attr
7947 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007948
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007949 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007950
Przemek Stekiel51eac532022-12-07 11:04:51 +01007951 if (type != PSA_KEY_TYPE_PASSWORD &&
7952 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7953 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007954 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007955 }
7956
Przemek Stekiel51eac532022-12-07 11:04:51 +01007957 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7958 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007959 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007960 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007961 }
7962
7963 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7964 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007965 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007966exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007967 if (status != PSA_SUCCESS) {
7968 psa_pake_abort(operation);
7969 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007970 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007971 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007972}
7973
7974psa_status_t psa_pake_set_user(
7975 psa_pake_operation_t *operation,
7976 const uint8_t *user_id,
7977 size_t user_id_len)
7978{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007979 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007980
7981 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007982 status = PSA_ERROR_BAD_STATE;
7983 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007984 }
7985
Przemek Stekiel51eac532022-12-07 11:04:51 +01007986 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007987 status = PSA_ERROR_INVALID_ARGUMENT;
7988 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007989 }
7990
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007991 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007992 status = PSA_ERROR_BAD_STATE;
7993 goto exit;
7994 }
7995
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007996 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7997 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007998 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7999 goto exit;
8000 }
8001
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008002 memcpy(operation->data.inputs.user, user_id, user_id_len);
8003 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008004
8005 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008006exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008007 psa_pake_abort(operation);
8008 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008009}
8010
8011psa_status_t psa_pake_set_peer(
8012 psa_pake_operation_t *operation,
8013 const uint8_t *peer_id,
8014 size_t peer_id_len)
8015{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008016 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008017
8018 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008019 status = PSA_ERROR_BAD_STATE;
8020 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008021 }
8022
Przemek Stekiel51eac532022-12-07 11:04:51 +01008023 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008024 status = PSA_ERROR_INVALID_ARGUMENT;
8025 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008026 }
8027
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008028 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008029 status = PSA_ERROR_BAD_STATE;
8030 goto exit;
8031 }
8032
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008033 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8034 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008035 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8036 goto exit;
8037 }
8038
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008039 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8040 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008041
8042 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008043exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008044 psa_pake_abort(operation);
8045 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008046}
8047
8048psa_status_t psa_pake_set_role(
8049 psa_pake_operation_t *operation,
8050 psa_pake_role_t role)
8051{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008052 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008053
Przemek Stekiel51eac532022-12-07 11:04:51 +01008054 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008055 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008056 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008057 }
8058
Przemek Stekiel09104b82023-03-06 14:11:51 +01008059 switch (operation->alg) {
8060#if defined(PSA_WANT_ALG_JPAKE)
8061 case PSA_ALG_JPAKE:
8062 if (role == PSA_PAKE_ROLE_NONE) {
8063 return PSA_SUCCESS;
8064 }
8065 status = PSA_ERROR_INVALID_ARGUMENT;
8066 break;
8067#endif
8068 default:
8069 (void) role;
8070 status = PSA_ERROR_NOT_SUPPORTED;
8071 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008072 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008073exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008074 psa_pake_abort(operation);
8075 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008076}
8077
David Horstmanne7f21e62023-05-12 18:17:21 +01008078/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008079#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008080static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008081 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008082{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008083 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008084 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008085 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008086
David Horstmann024e5c52023-06-14 15:48:21 +01008087 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008088 is_x1 = (stage->outputs < 1);
8089 } else {
8090 is_x1 = (stage->inputs < 1);
8091 }
8092
David Horstmann74a3d8c2023-06-14 18:28:19 +01008093 key_share_step = is_x1 ?
8094 PSA_JPAKE_X1_STEP_KEY_SHARE :
8095 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008096 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008097 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8098 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8099 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8100 } else {
8101 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008102 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008103 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008104}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008105#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008106
Przemek Stekiel2797d372022-12-22 11:19:22 +01008107static psa_status_t psa_pake_complete_inputs(
8108 psa_pake_operation_t *operation)
8109{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008110 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008111 /* Create copy of the inputs on stack as inputs share memory
8112 with the driver context which will be setup by the driver. */
8113 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008114
Przemek Stekielfde11282023-03-13 16:06:09 +01008115 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008116 return PSA_ERROR_BAD_STATE;
8117 }
8118
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008119 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008120 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8121 return PSA_ERROR_BAD_STATE;
8122 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008123 }
8124
Przemek Stekiel18620a32023-01-17 16:34:52 +01008125 /* Clear driver context */
8126 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8127
8128 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008129
8130 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008131 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008132
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008133 /* User and peer are translated to role. */
8134 mbedtls_free(inputs.user);
8135 mbedtls_free(inputs.peer);
8136
Przemek Stekiel2797d372022-12-22 11:19:22 +01008137 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008138#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008139 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008140 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008141 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008142#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008143 {
8144 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008145 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008146 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008147 return status;
8148}
8149
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008150#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008151static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008152 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008153 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008154 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008155{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008156 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8157 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8158 step != PSA_PAKE_STEP_ZK_PROOF) {
8159 return PSA_ERROR_INVALID_ARGUMENT;
8160 }
8161
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008162 psa_jpake_computation_stage_t *computation_stage =
8163 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008164
David Horstmann5da95602023-06-08 15:37:12 +01008165 if (computation_stage->round != PSA_JPAKE_FIRST &&
8166 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008167 return PSA_ERROR_BAD_STATE;
8168 }
8169
David Horstmanne7f21e62023-05-12 18:17:21 +01008170 /* Check that the step we are given is the one we were expecting */
8171 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008172 return PSA_ERROR_BAD_STATE;
8173 }
8174
David Horstmanne7f21e62023-05-12 18:17:21 +01008175 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8176 computation_stage->inputs == 0 &&
8177 computation_stage->outputs == 0) {
8178 /* Start of the round, so function decides whether we are inputting
8179 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008180 computation_stage->io_mode = io_mode;
8181 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008182 /* Middle of the round so the mode we are in must match the function
8183 * called by the user */
8184 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008185 }
8186
8187 return PSA_SUCCESS;
8188}
8189
David Horstmanne7f21e62023-05-12 18:17:21 +01008190static psa_status_t psa_jpake_epilogue(
8191 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008192 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008193{
David Horstmanne7f21e62023-05-12 18:17:21 +01008194 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008195 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008196
David Horstmanne7f21e62023-05-12 18:17:21 +01008197 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8198 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008199 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008200 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008201 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008202 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008203 }
8204 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008205 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008206 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008207 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008208 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008209 }
8210 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008211 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8212 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008213 /* End of a round, move to the next round */
8214 stage->inputs = 0;
8215 stage->outputs = 0;
8216 stage->round++;
8217 }
8218 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008219 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008220 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008221 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008222 return PSA_SUCCESS;
8223}
David Horstmanne7f21e62023-05-12 18:17:21 +01008224
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008225#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008226
Neil Armstronga7d08c32022-06-01 18:21:20 +02008227psa_status_t psa_pake_output(
8228 psa_pake_operation_t *operation,
8229 psa_pake_step_t step,
8230 uint8_t *output,
8231 size_t output_size,
8232 size_t *output_length)
8233{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008234 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008235 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008236 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008237
8238 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008239 status = psa_pake_complete_inputs(operation);
8240 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008241 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008242 }
8243 }
8244
8245 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008246 status = PSA_ERROR_BAD_STATE;
8247 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008248 }
8249
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008250 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008251 status = PSA_ERROR_INVALID_ARGUMENT;
8252 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008253 }
8254
Przemek Stekiele12ed362022-12-21 12:54:46 +01008255 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008256#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008257 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008258 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008259 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008260 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008261 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008262 driver_step = convert_jpake_computation_stage_to_driver_step(
8263 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008264 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008265#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008266 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008267 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008268 status = PSA_ERROR_NOT_SUPPORTED;
8269 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008270 }
8271
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008272 status = psa_driver_wrapper_pake_output(operation, driver_step,
8273 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008274
8275 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008276 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008277 }
8278
8279 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008280#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008281 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008282 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008283 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008284 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008285 }
8286 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008287#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008288 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008289 status = PSA_ERROR_NOT_SUPPORTED;
8290 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008291 }
8292
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008293 return PSA_SUCCESS;
8294exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008295 psa_pake_abort(operation);
8296 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008297}
8298
8299psa_status_t psa_pake_input(
8300 psa_pake_operation_t *operation,
8301 psa_pake_step_t step,
8302 const uint8_t *input,
8303 size_t input_length)
8304{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008305 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008306 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008307 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8308 operation->primitive,
8309 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008310
8311 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008312 status = psa_pake_complete_inputs(operation);
8313 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008314 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008315 }
8316 }
8317
8318 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008319 status = PSA_ERROR_BAD_STATE;
8320 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008321 }
8322
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008323 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008324 status = PSA_ERROR_INVALID_ARGUMENT;
8325 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008326 }
8327
Przemek Stekiele12ed362022-12-21 12:54:46 +01008328 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008329#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008330 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008331 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008332 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008333 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008334 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008335 driver_step = convert_jpake_computation_stage_to_driver_step(
8336 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008337 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008338#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008339 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008340 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008341 status = PSA_ERROR_NOT_SUPPORTED;
8342 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008343 }
8344
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008345 status = psa_driver_wrapper_pake_input(operation, driver_step,
8346 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008347
8348 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008349 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008350 }
8351
8352 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008353#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008354 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008355 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008356 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008357 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008358 }
8359 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008360#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008361 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008362 status = PSA_ERROR_NOT_SUPPORTED;
8363 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008364 }
8365
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008366 return PSA_SUCCESS;
8367exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008368 psa_pake_abort(operation);
8369 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008370}
8371
8372psa_status_t psa_pake_get_implicit_key(
8373 psa_pake_operation_t *operation,
8374 psa_key_derivation_operation_t *output)
8375{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008376 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008377 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008378 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008379 size_t shared_key_len = 0;
8380
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008381 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008382 status = PSA_ERROR_BAD_STATE;
8383 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008384 }
8385
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008386#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008387 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008388 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008389 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008390 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008391 status = PSA_ERROR_BAD_STATE;
8392 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008393 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008394 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008395#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008396 {
8397 status = PSA_ERROR_NOT_SUPPORTED;
8398 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008399 }
8400
Przemek Stekiel0c781802022-11-29 14:53:13 +01008401 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8402 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008403 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008404 &shared_key_len);
8405
8406 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008407 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008408 }
8409
8410 status = psa_key_derivation_input_bytes(output,
8411 PSA_KEY_DERIVATION_INPUT_SECRET,
8412 shared_key,
8413 shared_key_len);
8414
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008415 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008416exit:
8417 abort_status = psa_pake_abort(operation);
8418 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008419}
8420
8421psa_status_t psa_pake_abort(
8422 psa_pake_operation_t *operation)
8423{
Przemek Stekield69dca92023-01-31 19:59:20 +01008424 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008425
Przemek Stekield69dca92023-01-31 19:59:20 +01008426 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008427 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008428 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008429
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008430 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8431 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008432 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008433 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008434 }
8435 if (operation->data.inputs.user != NULL) {
8436 mbedtls_free(operation->data.inputs.user);
8437 }
8438 if (operation->data.inputs.peer != NULL) {
8439 mbedtls_free(operation->data.inputs.peer);
8440 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008441 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008442 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008443
Przemek Stekield69dca92023-01-31 19:59:20 +01008444 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008445}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008446#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008447
Gilles Peskinee59236f2018-01-27 23:32:46 +01008448#endif /* MBEDTLS_PSA_CRYPTO_C */