blob: d39310981146e98e39669a3f19e9b557fff477aa [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 Peskine449bd832023-01-11 14:50:10 +0100420psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
421 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200422{
423 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200425 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200426 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200427 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200428 case PSA_KEY_TYPE_PASSWORD:
429 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200430 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100431#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200432 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (bits != 128 && bits != 192 && bits != 256) {
434 return PSA_ERROR_INVALID_ARGUMENT;
435 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200436 break;
437#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200438#if defined(PSA_WANT_KEY_TYPE_ARIA)
439 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (bits != 128 && bits != 192 && bits != 256) {
441 return PSA_ERROR_INVALID_ARGUMENT;
442 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200443 break;
444#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100445#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (bits != 128 && bits != 192 && bits != 256) {
448 return PSA_ERROR_INVALID_ARGUMENT;
449 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200450 break;
451#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100452#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200453 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (bits != 64 && bits != 128 && bits != 192) {
455 return PSA_ERROR_INVALID_ARGUMENT;
456 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200457 break;
458#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100459#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200460 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (bits != 256) {
462 return PSA_ERROR_INVALID_ARGUMENT;
463 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200464 break;
465#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200468 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (bits % 8 != 0) {
470 return PSA_ERROR_INVALID_ARGUMENT;
471 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200474}
475
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100476/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100477 *
Steven Cooremancd640932021-03-08 12:00:27 +0100478 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
479 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100480 *
481 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100482 * \param[in] key_type The key type of the key to be used with the
483 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100484 *
485 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100486 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100487 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100488 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100489 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100490MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100491 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100493{
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (PSA_ALG_IS_HMAC(algorithm)) {
495 if (key_type == PSA_KEY_TYPE_HMAC) {
496 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100497 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100498 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
501 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
502 * key. */
503 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
504 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
505 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
506 * the block length (larger than 1) for block ciphers. */
507 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
508 return PSA_SUCCESS;
509 }
510 }
511 }
512
513 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100514}
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
517 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200518{
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (slot->key.data != NULL) {
520 return PSA_ERROR_ALREADY_EXISTS;
521 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 slot->key.data = mbedtls_calloc(1, buffer_length);
524 if (slot->key.data == NULL) {
525 return PSA_ERROR_INSUFFICIENT_MEMORY;
526 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200527
Ronald Cronea0f8a62020-11-25 17:52:23 +0100528 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200530}
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
533 const uint8_t *data,
534 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200535{
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 psa_status_t status = psa_allocate_buffer_to_slot(slot,
537 data_length);
538 if (status != PSA_SUCCESS) {
539 return status;
540 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 memcpy(slot->key.data, data, data_length);
543 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200544}
545
Ronald Crona0fe59f2020-11-29 11:14:53 +0100546psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100547 const psa_key_attributes_t *attributes,
548 const uint8_t *data, size_t data_length,
549 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100552 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
553 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200555 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (data_length == 0) {
557 return PSA_ERROR_NOT_SUPPORTED;
558 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (key_type_is_raw_bytes(type)) {
561 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
564 *bits);
565 if (status != PSA_SUCCESS) {
566 return status;
567 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200568
Ronald Crondd04d422020-11-28 15:14:42 +0100569 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100571 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 return PSA_SUCCESS;
575 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200576#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200577 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100578 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200579 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100580 return PSA_ERROR_INVALID_ARGUMENT;
581 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200582 return mbedtls_psa_ffdh_import_key(attributes,
583 data, data_length,
584 key_buffer, key_buffer_size,
585 key_buffer_length,
586 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100587 }
Valerio Settia55f0422023-07-10 15:34:41 +0200588#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200589 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200590#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
592 if (PSA_KEY_TYPE_IS_ECC(type)) {
593 return mbedtls_psa_ecp_import_key(attributes,
594 data, data_length,
595 key_buffer, key_buffer_size,
596 key_buffer_length,
597 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100598 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200599#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800600 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200601#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
602 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
604 if (PSA_KEY_TYPE_IS_RSA(type)) {
605 return mbedtls_psa_rsa_import_key(attributes,
606 data, data_length,
607 key_buffer, key_buffer_size,
608 key_buffer_length,
609 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200610 }
Valerio Settife478902023-07-25 12:27:19 +0200611#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
612 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800613 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100614 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100617}
618
Gilles Peskinef603c712019-01-19 13:40:11 +0100619/** Calculate the intersection of two algorithm usage policies.
620 *
621 * Return 0 (which allows no operation) on incompatibility.
622 */
623static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100624 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100625 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100627{
Gilles Peskine549ea862019-05-22 11:45:59 +0200628 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (alg1 == alg2) {
630 return alg1;
631 }
Steven Cooreman03488022021-02-18 12:07:20 +0100632 /* If the policies are from the same hash-and-sign family, check
633 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
635 PSA_ALG_IS_SIGN_HASH(alg2) &&
636 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
637 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
638 return alg2;
639 }
640 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
641 return alg1;
642 }
Steven Cooreman03488022021-02-18 12:07:20 +0100643 }
644 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100645 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100646 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
648 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
649 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
650 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
651 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100652 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100653
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100654 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
656 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
657 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
658 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100659 }
660 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
662 (alg1_len <= alg2_len)) {
663 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100664 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
666 (alg2_len <= alg1_len)) {
667 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100668 }
669 }
670 /* If the policies are from the same MAC family, check whether one
671 * of them is a minimum-MAC-length policy. Calculate the most
672 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
674 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
675 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100676 /* Validate the combination of key type and algorithm. Since the base
677 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
679 return 0;
680 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100681
Steven Cooreman31a876d2021-03-03 20:47:40 +0100682 /* Get the (exact or at-least) output lengths for both sides of the
683 * requested intersection. None of the currently supported algorithms
684 * have an output length dependent on the actual key size, so setting it
685 * to a bogus value of 0 is currently OK.
686 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100687 * Note that for at-least-this-length wildcard algorithms, the output
688 * length is set to the shortest allowed length, which allows us to
689 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
691 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100692 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100693
Steven Cooremana1d83222021-02-25 10:20:29 +0100694 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
696 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
697 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100698 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100699
700 /* If only one is an at-least-this-length policy, the intersection would
701 * be the other (fixed-length) policy as long as said fixed length is
702 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
704 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100705 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
707 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100708 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100709
Steven Cooremancd640932021-03-08 12:00:27 +0100710 /* If none of them are wildcards, check whether they define the same tag
711 * length. This is still possible here when one is default-length and
712 * the other specific-length. Ensure to always return the
713 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (alg1_len == alg2_len) {
715 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
716 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100717 }
718 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100720}
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722static int psa_key_algorithm_permits(psa_key_type_t key_type,
723 psa_algorithm_t policy_alg,
724 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200725{
Gilles Peskine549ea862019-05-22 11:45:59 +0200726 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (requested_alg == policy_alg) {
728 return 1;
729 }
Steven Cooreman03488022021-02-18 12:07:20 +0100730 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
731 * and requested_alg is the same hash-and-sign family with any hash,
732 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
734 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
735 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
736 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100737 }
738 /* If policy_alg is a wildcard AEAD algorithm of the same base as
739 * the requested algorithm, check the requested tag length to be
740 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (PSA_ALG_IS_AEAD(policy_alg) &&
742 PSA_ALG_IS_AEAD(requested_alg) &&
743 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
744 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
745 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
746 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
747 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100748 }
Steven Cooremancd640932021-03-08 12:00:27 +0100749 /* If policy_alg is a MAC algorithm of the same base as the requested
750 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (PSA_ALG_IS_MAC(policy_alg) &&
752 PSA_ALG_IS_MAC(requested_alg) &&
753 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
754 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100755 /* Validate the combination of key type and algorithm. Since the policy
756 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
758 return 0;
759 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100760
Steven Cooreman31a876d2021-03-03 20:47:40 +0100761 /* Get both the requested output length for the algorithm which is to be
762 * verified, and the default output length for the base algorithm.
763 * Note that none of the currently supported algorithms have an output
764 * length dependent on actual key size, so setting it to a bogus value
765 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100766 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100768 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 key_type, 0,
770 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100771
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100772 /* If the policy is default-length, only allow an algorithm with
773 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
775 return requested_output_length == default_output_length;
776 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100777
778 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100779 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
781 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
782 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100783 }
784
Steven Cooremancd640932021-03-08 12:00:27 +0100785 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
786 * check for the requested MAC length to be equal to or longer than the
787 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
789 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
790 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100791 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200792 }
Steven Cooremance48e852020-10-05 16:02:45 +0200793 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200794 * a key derivation with that key agreement should also be allowed. This
795 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
797 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
798 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
799 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200800 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100801 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200803}
804
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100805/** Test whether a policy permits an algorithm.
806 *
807 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100808 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100809 * \note This function requires providing the key type for which the policy is
810 * being validated, since some algorithm policy definitions (e.g. MAC)
811 * have different properties depending on what kind of cipher it is
812 * combined with.
813 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100814 * \retval PSA_SUCCESS When \p alg is a specific algorithm
815 * allowed by the \p policy.
816 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
817 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
818 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100819 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
821 psa_key_type_t key_type,
822 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100823{
Steven Cooremand788fab2021-02-25 11:29:17 +0100824 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (alg == 0) {
826 return PSA_ERROR_INVALID_ARGUMENT;
827 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100828
Steven Cooreman7e39f052021-02-23 14:18:32 +0100829 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if (PSA_ALG_IS_WILDCARD(alg)) {
831 return PSA_ERROR_INVALID_ARGUMENT;
832 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
835 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
836 return PSA_SUCCESS;
837 } else {
838 return PSA_ERROR_NOT_PERMITTED;
839 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100840}
841
Gilles Peskinef603c712019-01-19 13:40:11 +0100842/** Restrict a key policy based on a constraint.
843 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100844 * \note This function requires providing the key type for which the policy is
845 * being restricted, since some algorithm policy definitions (e.g. MAC)
846 * have different properties depending on what kind of cipher it is
847 * combined with.
848 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100849 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100850 * \param[in,out] policy The policy to restrict.
851 * \param[in] constraint The policy constraint to apply.
852 *
853 * \retval #PSA_SUCCESS
854 * \c *policy contains the intersection of the original value of
855 * \c *policy and \c *constraint.
856 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100857 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100858 * \c *policy is unchanged.
859 */
860static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100861 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100862 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100864{
865 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 psa_key_policy_algorithm_intersection(key_type, policy->alg,
867 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200868 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
870 constraint->alg2);
871 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
872 return PSA_ERROR_INVALID_ARGUMENT;
873 }
874 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
875 return PSA_ERROR_INVALID_ARGUMENT;
876 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100877 policy->usage &= constraint->usage;
878 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200879 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100881}
882
Przemek Stekiel061f6942022-11-30 10:51:35 +0100883/** Get the description of a key given its identifier and policy constraints
884 * and lock it.
885 *
886 * The key must have allow all the usage flags set in \p usage. If \p alg is
887 * nonzero, the key must allow operations with this algorithm. If \p alg is
888 * zero, the algorithm is not checked.
889 *
890 * In case of a persistent key, the function loads the description of the key
891 * into a key slot if not already done.
892 *
893 * On success, the returned key slot is locked. It is the responsibility of
894 * the caller to unlock the key slot when it does not access it anymore.
895 */
896static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100897 mbedtls_svc_key_id_t key,
898 psa_key_slot_t **p_slot,
899 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100901{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200902 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100903 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 status = psa_get_and_lock_key_slot(key, p_slot);
906 if (status != PSA_SUCCESS) {
907 return status;
908 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200909 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100910
911 /* Enforce that usage policy for the key slot contains all the flags
912 * required by the usage parameter. There is one exception: public
913 * keys can always be exported, so we treat public key objects as
914 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100916 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100920 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200921 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100922 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100923
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800924 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 if (alg != 0) {
926 status = psa_key_policy_permits(&slot->attr.policy,
927 slot->attr.type,
928 alg);
929 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100930 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100932 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200935
936error:
937 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100941}
Darryl Green940d72c2018-07-13 13:18:51 +0100942
Ronald Cron5c522922020-11-14 16:35:34 +0100943/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200944 *
945 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200946 * available, as opposed to a key in a secure element and/or to be used
947 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200948 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200949 * This is a temporary function that may be used instead of
950 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
951 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200952 *
Ronald Cron5c522922020-11-14 16:35:34 +0100953 * On success, the returned key slot is locked. It is the responsibility of the
954 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200955 */
Ronald Cron5c522922020-11-14 16:35:34 +0100956static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
957 mbedtls_svc_key_id_t key,
958 psa_key_slot_t **p_slot,
959 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200961{
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
963 usage, alg);
964 if (status != PSA_SUCCESS) {
965 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200966 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
969 psa_unlock_key_slot(*p_slot);
970 *p_slot = NULL;
971 return PSA_ERROR_NOT_SUPPORTED;
972 }
973
974 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200975}
Gilles Peskine28f8f302019-07-24 13:30:31 +0200976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +0000978{
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +0100980 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 }
Ronald Cronea0f8a62020-11-25 17:52:23 +0100982
Ronald Cronea0f8a62020-11-25 17:52:23 +0100983 slot->key.data = NULL;
984 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +0000987}
988
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100989/** Completely wipe a slot in memory, including its policy.
990 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100992{
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +0100994
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 /*
996 * As the return error code may not be handled in case of multiple errors,
997 * do our best to report an unexpected lock counter. Assert with
998 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
999 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1000 * function is called as part of the execution of a test suite, the
1001 * execution of the test suite is stopped in error if the assertion fails.
1002 */
1003 if (slot->lock_count != 1) {
1004 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001005 status = PSA_ERROR_CORRUPTION_DETECTED;
1006 }
1007
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001008 /* Multipart operations may still be using the key. This is safe
1009 * because all multipart operation objects are independent from
1010 * the key slot: if they need to access the key after the setup
1011 * phase, they have a copy of the key. Note that this means that
1012 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001013 /* At this point, key material and other type-specific content has
1014 * been wiped. Clear remaining metadata. We can call memset and not
1015 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 memset(slot, 0, sizeof(*slot));
1017 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001018}
1019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001021{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001022 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001023 psa_status_t status; /* status of the last operation */
1024 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001025#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1026 psa_se_drv_table_entry_t *driver;
1027#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (mbedtls_svc_key_id_is_null(key)) {
1030 return PSA_SUCCESS;
1031 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001032
Ronald Cronf2911112020-10-29 17:51:10 +01001033 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001034 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001035 * key, this will load the key description from persistent memory if not
1036 * done yet. We cannot avoid this loading as without it we don't know if
1037 * the key is operated by an SE or not and this information is needed by
1038 * the current implementation.
1039 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 status = psa_get_and_lock_key_slot(key, &slot);
1041 if (status != PSA_SUCCESS) {
1042 return status;
1043 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001044
Ronald Cronf2911112020-10-29 17:51:10 +01001045 /*
1046 * If the key slot containing the key description is under access by the
1047 * library (apart from the present access), the key cannot be destroyed
1048 * yet. For the time being, just return in error. Eventually (to be
1049 * implemented), the key should be destroyed when all accesses have
1050 * stopped.
1051 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (slot->lock_count > 1) {
1053 psa_unlock_key_slot(slot);
1054 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001055 }
1056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001058 /* Refuse the destruction of a read-only key (which may or may not work
1059 * if we attempt it, depending on whether the key is merely read-only
1060 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001061 * Just do the best we can, which is to wipe the copy in memory
1062 * (done in this function's cleanup code). */
1063 overall_status = PSA_ERROR_NOT_PERMITTED;
1064 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001065 }
1066
Gilles Peskine354f7672019-07-12 23:46:38 +02001067#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1069 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001070 /* For a key in a secure element, we need to do three things:
1071 * remove the key file in internal storage, destroy the
1072 * key inside the secure element, and update the driver's
1073 * persistent data. Start a transaction that will encompass these
1074 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001076 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001078 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 status = psa_crypto_save_transaction();
1080 if (status != PSA_SUCCESS) {
1081 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001082 /* We should still try to destroy the key in the secure
1083 * element and the key metadata in storage. This is especially
1084 * important if the error is that the storage is full.
1085 * But how to do it exactly without risking an inconsistent
1086 * state after a reset?
1087 * https://github.com/ARMmbed/mbed-crypto/issues/215
1088 */
1089 overall_status = status;
1090 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001091 }
1092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 status = psa_destroy_se_key(driver,
1094 psa_key_slot_get_slot_number(slot));
1095 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001096 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001098 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001099#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1100
Darryl Greend49a4992018-06-18 17:27:26 +01001101#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1103 status = psa_destroy_persistent_key(slot->attr.id);
1104 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001105 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001107
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001108 /* TODO: other slots may have a copy of the same key. We should
1109 * invalidate them.
1110 * https://github.com/ARMmbed/mbed-crypto/issues/214
1111 */
Darryl Greend49a4992018-06-18 17:27:26 +01001112 }
1113#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001114
Gilles Peskinefc762652019-07-22 19:30:34 +02001115#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (driver != NULL) {
1117 status = psa_save_se_persistent_data(driver);
1118 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001119 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 }
1121 status = psa_crypto_stop_transaction();
1122 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001123 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001125 }
1126#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1127
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001128exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001130 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001132 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 }
1134 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001135}
1136
Valerio Settib2bcedb2023-07-10 11:24:00 +02001137#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001138 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001139static psa_status_t psa_get_rsa_public_exponent(
1140 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001142{
1143 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001145 uint8_t *buffer = NULL;
1146 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1150 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001151 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 }
1153 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001154 /* It's the default value, which is reported as an empty string,
1155 * so there's nothing to do. */
1156 goto exit;
1157 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 buflen = mbedtls_mpi_size(&mpi);
1160 buffer = mbedtls_calloc(1, buflen);
1161 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001162 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1163 goto exit;
1164 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1166 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001167 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001169 attributes->domain_parameters = buffer;
1170 attributes->domain_parameters_size = buflen;
1171
1172exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 mbedtls_mpi_free(&mpi);
1174 if (ret != 0) {
1175 mbedtls_free(buffer);
1176 }
1177 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001178}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001179#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001180 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001181
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001182/** Retrieve all the publicly-accessible attributes of a key.
1183 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001184psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1185 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001186{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001187 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001188 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001189 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1194 if (status != PSA_SUCCESS) {
1195 return status;
1196 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001197
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001198 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1200 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001201
1202#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1204 psa_set_key_slot_number(attributes,
1205 psa_key_slot_get_slot_number(slot));
1206 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001207#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001210#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1211 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001212 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001213 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001214 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Pengyu Lvf75893b2023-12-08 17:21:39 +08001215 /* TODO: This is a temporary situation where domain parameters are deprecated,
1216 * but we need it for namely generating an RSA key with a non-default exponent.
1217 * This would be improved after https://github.com/Mbed-TLS/mbedtls/issues/6494.
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001218 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001220 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001221
Ronald Cron90857082020-11-25 14:58:33 +01001222 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 slot->attr.type,
1224 slot->key.data,
1225 slot->key.bytes,
1226 &rsa);
1227 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001228 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 status = psa_get_rsa_public_exponent(rsa,
1232 attributes);
1233 mbedtls_rsa_free(rsa);
1234 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001235 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001236 break;
Pengyu Lve9efbc22023-12-08 16:59:08 +08001237#else
1238 case PSA_KEY_TYPE_RSA_KEY_PAIR:
1239 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1240 attributes->domain_parameters = NULL;
1241 attributes->domain_parameters_size = SIZE_MAX;
1242 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001243#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1244 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001245 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001246 default:
1247 /* Nothing else to do. */
1248 break;
1249 }
1250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (status != PSA_SUCCESS) {
1252 psa_reset_key_attributes(attributes);
1253 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001258}
1259
Gilles Peskinec8000c02019-08-02 20:15:51 +02001260#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1261psa_status_t psa_get_key_slot_number(
1262 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001264{
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001266 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 return PSA_SUCCESS;
1268 } else {
1269 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001270 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001271}
1272#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1275 size_t key_buffer_size,
1276 uint8_t *data,
1277 size_t data_size,
1278 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001279{
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if (key_buffer_size > data_size) {
1281 return PSA_ERROR_BUFFER_TOO_SMALL;
1282 }
1283 memcpy(data, key_buffer, key_buffer_size);
1284 memset(data + key_buffer_size, 0,
1285 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001286 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001288}
1289
Ronald Cron7285cda2020-11-26 14:46:37 +01001290psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001291 const psa_key_attributes_t *attributes,
1292 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001294{
Ronald Crond18b5f82020-11-25 16:46:05 +01001295 psa_key_type_t type = attributes->core.type;
1296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (key_type_is_raw_bytes(type) ||
1298 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001299 PSA_KEY_TYPE_IS_ECC(type) ||
1300 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 return psa_export_key_buffer_internal(
1302 key_buffer, key_buffer_size,
1303 data, data_size, data_length);
1304 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001305 /* This shouldn't happen in the reference implementation, but
1306 it is valid for a special-purpose implementation to omit
1307 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001309 }
1310}
1311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1313 uint8_t *data,
1314 size_t data_size,
1315 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001316{
1317 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1318 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1319 psa_key_slot_t *slot;
1320
Ronald Crone907e552021-01-18 13:22:38 +01001321 /* Reject a zero-length output buffer now, since this can never be a
1322 * valid key representation. This way we know that data must be a valid
1323 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (data_size == 0) {
1325 return PSA_ERROR_BUFFER_TOO_SMALL;
1326 }
Ronald Crone907e552021-01-18 13:22:38 +01001327
Ronald Cron9486f9d2020-11-26 13:36:23 +01001328 /* Set the key to empty now, so that even when there are errors, we always
1329 * set data_length to a value between 0 and data_size. On error, setting
1330 * the key to empty is a good choice because an empty key representation is
1331 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001332 *data_length = 0;
1333
Ronald Cron9486f9d2020-11-26 13:36:23 +01001334 /* Export requires the EXPORT flag. There is an exception for public keys,
1335 * which don't require any flag, but
1336 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1339 PSA_KEY_USAGE_EXPORT, 0);
1340 if (status != PSA_SUCCESS) {
1341 return status;
1342 }
mohammad160306e79202018-03-28 13:17:44 +03001343
Ronald Crond18b5f82020-11-25 16:46:05 +01001344 psa_key_attributes_t attributes = {
1345 .core = slot->attr
1346 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 status = psa_driver_wrapper_export_key(&attributes,
1348 slot->key.data, slot->key.bytes,
1349 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001354}
1355
Ronald Cron7285cda2020-11-26 14:46:37 +01001356psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001357 const psa_key_attributes_t *attributes,
1358 const uint8_t *key_buffer,
1359 size_t key_buffer_size,
1360 uint8_t *data,
1361 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001363{
Ronald Crond18b5f82020-11-25 16:46:05 +01001364 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001365
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001366 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001367 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1368 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001369 /* Exporting public -> public */
1370 return psa_export_key_buffer_internal(
1371 key_buffer, key_buffer_size,
1372 data, data_size, data_length);
1373 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001374#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001375 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1376 return mbedtls_psa_rsa_export_public_key(attributes,
1377 key_buffer,
1378 key_buffer_size,
1379 data,
1380 data_size,
1381 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001382#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001383 /* We don't know how to convert a private RSA key to public. */
1384 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001385#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001386 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001387 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001388#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001389 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1390 return mbedtls_psa_ecp_export_public_key(attributes,
1391 key_buffer,
1392 key_buffer_size,
1393 data,
1394 data_size,
1395 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001396#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001397 /* We don't know how to convert a private ECC key to public */
1398 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001399#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001400 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001401 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001402#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001403 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001404 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001405 key_buffer,
1406 key_buffer_size,
1407 data, data_size,
1408 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001409#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001410 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001411#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001412 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001414 (void) key_buffer;
1415 (void) key_buffer_size;
1416 (void) data;
1417 (void) data_size;
1418 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001420 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001421}
Ronald Crond18b5f82020-11-25 16:46:05 +01001422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1424 uint8_t *data,
1425 size_t data_size,
1426 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001427{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001428 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001429 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001430 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001431 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001432
Ronald Crone907e552021-01-18 13:22:38 +01001433 /* Reject a zero-length output buffer now, since this can never be a
1434 * valid key representation. This way we know that data must be a valid
1435 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 if (data_size == 0) {
1437 return PSA_ERROR_BUFFER_TOO_SMALL;
1438 }
Ronald Crone907e552021-01-18 13:22:38 +01001439
Darryl Greendd8fb772018-11-07 16:00:44 +00001440 /* Set the key to empty now, so that even when there are errors, we always
1441 * set data_length to a value between 0 and data_size. On error, setting
1442 * the key to empty is a good choice because an empty key representation is
1443 * unlikely to be accepted anywhere. */
1444 *data_length = 0;
1445
1446 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1448 if (status != PSA_SUCCESS) {
1449 return status;
1450 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1453 status = PSA_ERROR_INVALID_ARGUMENT;
1454 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001455 }
1456
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001457 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001458 .core = slot->attr
1459 };
Ronald Cron67227982020-11-26 15:16:05 +01001460 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001461 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001463
1464exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001468}
1469
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001470MBEDTLS_STATIC_ASSERT(
1471 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1472 "One or more key attribute flag is listed as both external-only and dual-use")
1473MBEDTLS_STATIC_ASSERT(
1474 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1475 "One or more key attribute flag is listed as both internal-only and dual-use")
1476MBEDTLS_STATIC_ASSERT(
1477 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1478 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001479
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001480/** Validate that a key policy is internally well-formed.
1481 *
1482 * This function only rejects invalid policies. It does not validate the
1483 * consistency of the policy with respect to other attributes of the key
1484 * such as the key type.
1485 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001486static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001487{
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1489 PSA_KEY_USAGE_COPY |
1490 PSA_KEY_USAGE_ENCRYPT |
1491 PSA_KEY_USAGE_DECRYPT |
1492 PSA_KEY_USAGE_SIGN_MESSAGE |
1493 PSA_KEY_USAGE_VERIFY_MESSAGE |
1494 PSA_KEY_USAGE_SIGN_HASH |
1495 PSA_KEY_USAGE_VERIFY_HASH |
1496 PSA_KEY_USAGE_VERIFY_DERIVATION |
1497 PSA_KEY_USAGE_DERIVE)) != 0) {
1498 return PSA_ERROR_INVALID_ARGUMENT;
1499 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001502}
1503
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001504/** Validate the internal consistency of key attributes.
1505 *
1506 * This function only rejects invalid attribute values. If does not
1507 * validate the consistency of the attributes with any key data that may
1508 * be involved in the creation of the key.
1509 *
1510 * Call this function early in the key creation process.
1511 *
1512 * \param[in] attributes Key attributes for the new key.
1513 * \param[out] p_drv On any return, the driver for the key, if any.
1514 * NULL for a transparent key.
1515 *
1516 */
1517static psa_status_t psa_validate_key_attributes(
1518 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001520{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001521 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1523 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 status = psa_validate_key_location(lifetime, p_drv);
1526 if (status != PSA_SUCCESS) {
1527 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001528 }
1529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 status = psa_validate_key_persistence(lifetime);
1531 if (status != PSA_SUCCESS) {
1532 return status;
1533 }
1534
1535 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1536 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1537 return PSA_ERROR_INVALID_ARGUMENT;
1538 }
1539 } else {
1540 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1541 return PSA_ERROR_INVALID_ARGUMENT;
1542 }
1543 }
1544
1545 status = psa_validate_key_policy(&attributes->core.policy);
1546 if (status != PSA_SUCCESS) {
1547 return status;
1548 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001549
1550 /* Refuse to create overly large keys.
1551 * Note that this doesn't trigger on import if the attributes don't
1552 * explicitly specify a size (so psa_get_key_bits returns 0), so
1553 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1555 return PSA_ERROR_NOT_SUPPORTED;
1556 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001557
Gilles Peskine91e8c332019-08-02 19:19:39 +02001558 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1560 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1561 return PSA_ERROR_INVALID_ARGUMENT;
1562 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001565}
1566
Gilles Peskine4747d192019-04-17 15:05:45 +02001567/** Prepare a key slot to receive key material.
1568 *
1569 * This function allocates a key slot and sets its metadata.
1570 *
1571 * If this function fails, call psa_fail_key_creation().
1572 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001573 * This function is intended to be used as follows:
1574 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001575 * it with the specified attributes, and in case of a volatile key assign it
1576 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001577 * -# Populate the slot with the key material.
1578 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1579 * In case of failure at any step, stop the sequence and call
1580 * psa_fail_key_creation().
1581 *
Ronald Cron5c522922020-11-14 16:35:34 +01001582 * On success, the key slot is locked. It is the responsibility of the caller
1583 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001584 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001585 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001586 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001587 * \param[out] p_slot On success, a pointer to the prepared slot.
1588 * \param[out] p_drv On any return, the driver for the key, if any.
1589 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001590 *
1591 * \retval #PSA_SUCCESS
1592 * The key slot is ready to receive key material.
1593 * \return If this function fails, the key slot is an invalid state.
1594 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001595 */
1596static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001597 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001598 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001599 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001601{
1602 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001603 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001604 psa_key_slot_t *slot;
1605
Gilles Peskinedf179142019-07-15 22:02:14 +02001606 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001607 *p_drv = NULL;
1608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 status = psa_validate_key_attributes(attributes, p_drv);
1610 if (status != PSA_SUCCESS) {
1611 return status;
1612 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1615 if (status != PSA_SUCCESS) {
1616 return status;
1617 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001618 slot = *p_slot;
1619
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001620 /* We're storing the declared bit-size of the key. It's up to each
1621 * creation mechanism to verify that this information is correct.
1622 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001623 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001624 * is optional (import, copy). In case of a volatile key, assign it the
1625 * volatile key identifier associated to the slot returned to contain its
1626 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001627
1628 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001630#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1631 slot->attr.id = volatile_key_id;
1632#else
1633 slot->attr.id.key_id = volatile_key_id;
1634#endif
1635 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001636
Gilles Peskine91e8c332019-08-02 19:19:39 +02001637 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001638 * external-only flags, query `attributes`. Thanks to the check
1639 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001640 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001641 * may have set. */
1642 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001643
Gilles Peskinecbaff462019-07-12 23:46:04 +02001644#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001645 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001646 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001647 * create the key file in internal storage, create the
1648 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001649 * persistent data. This is done by starting a transaction that will
1650 * encompass these three actions.
1651 * For registering a volatile key, we just need to find an appropriate
1652 * slot number inside the SE. Since the key is designated volatile, creating
1653 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001654 /* The first thing to do is to find a slot number for the new key.
1655 * We save the slot number in persistent storage as part of the
1656 * transaction data. It will be needed to recover if the power
1657 * fails during the key creation process, to clean up on the secure
1658 * element side after restarting. Obtaining a slot number from the
1659 * secure element driver updates its persistent state, but we do not yet
1660 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001661 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001663 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1665 &slot_number);
1666 if (status != PSA_SUCCESS) {
1667 return status;
1668 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1671 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001672 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001673 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001674 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 status = psa_crypto_save_transaction();
1676 if (status != PSA_SUCCESS) {
1677 (void) psa_crypto_stop_transaction();
1678 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001679 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001680 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001681
1682 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001684 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001687 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001689 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001690#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1691
Ryan Everett975d4112023-11-16 13:37:51 +00001692 slot->status = PSA_SLOT_OCCUPIED;
1693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001695}
Gilles Peskine4747d192019-04-17 15:05:45 +02001696
1697/** Finalize the creation of a key once its key material has been set.
1698 *
1699 * This entails writing the key to persistent storage.
1700 *
1701 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001702 * See the documentation of psa_start_key_creation() for the intended use
1703 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001704 *
Ronald Cron5c522922020-11-14 16:35:34 +01001705 * If the finalization succeeds, the function unlocks the key slot (it was
1706 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1707 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001708 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001709 * \param[in,out] slot Pointer to the slot with key material.
1710 * \param[in] driver The secure element driver for the key,
1711 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001712 * \param[out] key On success, identifier of the key. Note that the
1713 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001714 *
1715 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001716 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001717 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1718 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1719 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1720 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1721 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1722 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001723 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001724 * \return If this function fails, the key slot is an invalid state.
1725 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001726 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001727static psa_status_t psa_finish_key_creation(
1728 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001729 psa_se_drv_table_entry_t *driver,
1730 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001731{
1732 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001733 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001734 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001735
1736#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001738#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001740 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001741 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001743
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001744 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1745 sizeof(data.slot_number),
1746 "Slot number size does not match psa_se_key_data_storage_t");
1747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1749 status = psa_save_persistent_key(&slot->attr,
1750 (uint8_t *) &data,
1751 sizeof(data));
1752 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001753#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1754 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001755 /* Key material is saved in export representation in the slot, so
1756 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 status = psa_save_persistent_key(&slot->attr,
1758 slot->key.data,
1759 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001760 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001761 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001762#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1763
Gilles Peskinecbaff462019-07-12 23:46:04 +02001764#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001765 /* Finish the transaction for a key creation. This does not
1766 * happen when registering an existing key. Detect this case
1767 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001768 * creation of a persistent key in a secure element requires a transaction,
1769 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 if (driver != NULL &&
1771 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1772 status = psa_save_se_persistent_data(driver);
1773 if (status != PSA_SUCCESS) {
1774 psa_destroy_persistent_key(slot->attr.id);
1775 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001776 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001778 }
1779#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001782 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 status = psa_unlock_key_slot(slot);
1784 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001785 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001787 }
Ronald Cron50972942020-11-14 11:28:25 +01001788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001790}
1791
1792/** Abort the creation of a key.
1793 *
1794 * You may call this function after calling psa_start_key_creation(),
1795 * or after psa_finish_key_creation() fails. In other circumstances, this
1796 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001797 * See the documentation of psa_start_key_creation() for the intended use
1798 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001799 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001800 * \param[in,out] slot Pointer to the slot with key material.
1801 * \param[in] driver The secure element driver for the key,
1802 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001803 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001804static void psa_fail_key_creation(psa_key_slot_t *slot,
1805 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001806{
Gilles Peskine011e4282019-06-26 18:34:38 +02001807 (void) driver;
1808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001810 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001812
1813#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001814 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001815 * element, and the failure happened later (when saving metadata
1816 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001817 * element.
1818 * https://github.com/ARMmbed/mbed-crypto/issues/217
1819 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001820
Gilles Peskined7729582019-08-05 15:55:54 +02001821 /* Abort the ongoing transaction if any (there may not be one if
1822 * the creation process failed before starting one, or if the
1823 * key creation is a registration of a key in a secure element).
1824 * Earlier functions must already have done what it takes to undo any
1825 * partial creation. All that's left is to update the transaction data
1826 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001828#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001831}
1832
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001833/** Validate optional attributes during key creation.
1834 *
1835 * Some key attributes are optional during key creation. If they are
1836 * specified in the attributes structure, check that they are consistent
1837 * with the data in the slot.
1838 *
1839 * This function should be called near the end of key creation, after
1840 * the slot in memory is fully populated but before saving persistent data.
1841 */
1842static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001843 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001845{
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 if (attributes->core.type != 0) {
1847 if (attributes->core.type != slot->attr.type) {
1848 return PSA_ERROR_INVALID_ARGUMENT;
1849 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001850 }
1851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001853#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1854 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1856 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001857 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001858 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001859 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001860
Ronald Cron90857082020-11-25 14:58:33 +01001861 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 slot->attr.type,
1863 slot->key.data,
1864 slot->key.bytes,
1865 &rsa);
1866 if (status != PSA_SUCCESS) {
1867 return status;
1868 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 mbedtls_mpi_init(&actual);
1871 mbedtls_mpi_init(&required);
1872 ret = mbedtls_rsa_export(rsa,
1873 NULL, NULL, NULL, NULL, &actual);
1874 mbedtls_rsa_free(rsa);
1875 mbedtls_free(rsa);
1876 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001877 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 }
1879 ret = mbedtls_mpi_read_binary(&required,
1880 attributes->domain_parameters,
1881 attributes->domain_parameters_size);
1882 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001883 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 }
1885 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001886 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 }
1888rsa_exit:
1889 mbedtls_mpi_free(&actual);
1890 mbedtls_mpi_free(&required);
1891 if (ret != 0) {
1892 return mbedtls_to_psa_error(ret);
1893 }
1894 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02001895#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
1896 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001897 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001900 }
1901 }
1902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 if (attributes->core.bits != 0) {
1904 if (attributes->core.bits != slot->attr.bits) {
1905 return PSA_ERROR_INVALID_ARGUMENT;
1906 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001907 }
1908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001910}
1911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1913 const uint8_t *data,
1914 size_t data_length,
1915 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001916{
1917 psa_status_t status;
1918 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001919 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001920 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301921 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001922
Ronald Cron81709fc2020-11-14 12:10:32 +01001923 *key = MBEDTLS_SVC_KEY_ID_INIT;
1924
Gilles Peskine0f84d622019-09-12 19:03:13 +02001925 /* Reject zero-length symmetric keys (including raw data key objects).
1926 * This also rejects any key which might be encoded as an empty string,
1927 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 if (data_length == 0) {
1929 return PSA_ERROR_INVALID_ARGUMENT;
1930 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001931
Archana449608b2021-09-08 15:36:05 +05301932 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 if (data_length > SIZE_MAX / 8) {
1934 return PSA_ERROR_NOT_SUPPORTED;
1935 }
Archana6ed4bda2021-08-04 10:47:15 +05301936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1938 &slot, &driver);
1939 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001940 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001942
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001943 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301944 * storage ( thus not in the case of importing a key in a secure element
1945 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1946 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if (slot->key.data == NULL) {
1948 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301949 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 attributes, data, data_length, &storage_size);
1951 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301952 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 }
Archanad8a83dc2021-06-14 10:04:16 +05301954 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 status = psa_allocate_buffer_to_slot(slot, storage_size);
1956 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001957 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001959 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001960
1961 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 status = psa_driver_wrapper_import_key(attributes,
1963 data, data_length,
1964 slot->key.data,
1965 slot->key.bytes,
1966 &slot->key.bytes, &bits);
1967 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001968 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001970
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001972 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001974 status = PSA_ERROR_INVALID_ARGUMENT;
1975 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001976 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001977
Archana6ed4bda2021-08-04 10:47:15 +05301978 /* Enforce a size limit, and in particular ensure that the bit
1979 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301981 status = PSA_ERROR_NOT_SUPPORTED;
1982 goto exit;
1983 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 status = psa_validate_optional_attributes(slot, attributes);
1985 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001986 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001990exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 if (status != PSA_SUCCESS) {
1992 psa_fail_key_creation(slot, driver);
1993 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001996}
1997
Gilles Peskined7729582019-08-05 15:55:54 +02001998#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1999psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002001{
2002 psa_status_t status;
2003 psa_key_slot_t *slot = NULL;
2004 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002005 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002006
2007 /* Leaving attributes unspecified is not currently supported.
2008 * It could make sense to query the key type and size from the
2009 * secure element, but not all secure elements support this
2010 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2012 return PSA_ERROR_NOT_SUPPORTED;
2013 }
2014 if (psa_get_key_bits(attributes) == 0) {
2015 return PSA_ERROR_NOT_SUPPORTED;
2016 }
Gilles Peskined7729582019-08-05 15:55:54 +02002017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2019 &slot, &driver);
2020 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002021 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 }
Gilles Peskined7729582019-08-05 15:55:54 +02002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002025
2026exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (status != PSA_SUCCESS) {
2028 psa_fail_key_creation(slot, driver);
2029 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002030
Gilles Peskined7729582019-08-05 15:55:54 +02002031 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 psa_close_key(key);
2033 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002034}
2035#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2038 const psa_key_attributes_t *specified_attributes,
2039 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002040{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002041 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002042 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002043 psa_key_slot_t *source_slot = NULL;
2044 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002045 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002046 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302047 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002048
Ronald Cron81709fc2020-11-14 12:10:32 +01002049 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2050
Archana8a180362021-07-05 02:18:48 +05302051 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2053 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002054 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 status = psa_validate_optional_attributes(source_slot,
2058 specified_attributes);
2059 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002060 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002062
Archana9d17bf42021-09-10 06:22:44 +05302063 /* The target key type and number of bits have been validated by
2064 * psa_validate_optional_attributes() to be either equal to zero or
2065 * equal to the ones of the source key. So it is safe to inherit
2066 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302067 * */
Archana9d17bf42021-09-10 06:22:44 +05302068 actual_attributes.core.bits = source_slot->attr.bits;
2069 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302070
2071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 status = psa_restrict_key_policy(source_slot->attr.type,
2073 &actual_attributes.core.policy,
2074 &source_slot->attr.policy);
2075 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2080 &target_slot, &driver);
2081 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002082 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 }
2084 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2085 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302086 /*
Archana9d17bf42021-09-10 06:22:44 +05302087 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302088 * the source key would need to be exported as plaintext and re-imported
2089 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302090 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302091 * appropriate API invocations from the application, if needed.
2092 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002093 status = PSA_ERROR_NOT_SUPPORTED;
2094 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002095 }
Archana8a180362021-07-05 02:18:48 +05302096 /*
2097 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302098 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302099 * - For opaque keys this translates to an invocation of the drivers'
2100 * copy_key entry point through the dispatch layer.
2101 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2103 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2104 &storage_size);
2105 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302106 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 }
Archana374fe5b2021-09-08 15:50:28 +05302108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2110 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302111 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 }
Archana374fe5b2021-09-08 15:50:28 +05302113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 status = psa_driver_wrapper_copy_key(&actual_attributes,
2115 source_slot->key.data,
2116 source_slot->key.bytes,
2117 target_slot->key.data,
2118 target_slot->key.bytes,
2119 &target_slot->key.bytes);
2120 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 }
2123 } else {
2124 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302125 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 source_slot->key.bytes);
2127 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302128 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 }
Archana8a180362021-07-05 02:18:48 +05302130 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002132exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (status != PSA_SUCCESS) {
2134 psa_fail_key_creation(target_slot, driver);
2135 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002140}
2141
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002142
2143
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002144/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002145/* Message digests */
2146/****************************************************************/
2147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002149{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002150 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (operation->id == 0) {
2152 return PSA_SUCCESS;
2153 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002156 operation->id = 0;
2157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002159}
2160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2162 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002163{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002164 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002165
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002166 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002168 status = PSA_ERROR_BAD_STATE;
2169 goto exit;
2170 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002173 status = PSA_ERROR_INVALID_ARGUMENT;
2174 goto exit;
2175 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002176
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002177 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2178 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002182
2183exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (status != PSA_SUCCESS) {
2185 psa_hash_abort(operation);
2186 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002187
2188 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002189}
2190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2192 const uint8_t *input,
2193 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002195 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002198 status = PSA_ERROR_BAD_STATE;
2199 goto exit;
2200 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002201
Steven Cooremanc8288352021-03-04 14:02:19 +01002202 /* Don't require hash implementations to behave correctly on a
2203 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if (input_length == 0) {
2205 return PSA_SUCCESS;
2206 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002209
2210exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 if (status != PSA_SUCCESS) {
2212 psa_hash_abort(operation);
2213 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002216}
2217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2219 uint8_t *hash,
2220 size_t hash_size,
2221 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002222{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002223 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (operation->id == 0) {
2225 return PSA_ERROR_BAD_STATE;
2226 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002227
Steven Cooreman1e582352021-02-18 17:24:37 +01002228 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 operation, hash, hash_size, hash_length);
2230 psa_hash_abort(operation);
2231 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002232}
2233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2235 const uint8_t *hash,
2236 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002237{
Ronald Cron69a63422021-10-18 09:47:58 +02002238 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002240 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 operation,
2242 actual_hash, sizeof(actual_hash),
2243 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002246 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002250 status = PSA_ERROR_INVALID_SIGNATURE;
2251 goto exit;
2252 }
2253
Dave Rodgman78701152023-08-29 14:20:18 +01002254 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002255 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002257
2258exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2260 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002261 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002265}
2266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267psa_status_t psa_hash_compute(psa_algorithm_t alg,
2268 const uint8_t *input, size_t input_length,
2269 uint8_t *hash, size_t hash_size,
2270 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002271{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002272 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (!PSA_ALG_IS_HASH(alg)) {
2274 return PSA_ERROR_INVALID_ARGUMENT;
2275 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2278 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002279}
2280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281psa_status_t psa_hash_compare(psa_algorithm_t alg,
2282 const uint8_t *input, size_t input_length,
2283 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002284{
Ronald Cron69a63422021-10-18 09:47:58 +02002285 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002286 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 if (!PSA_ALG_IS_HASH(alg)) {
2289 return PSA_ERROR_INVALID_ARGUMENT;
2290 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002291
Steven Cooreman1e582352021-02-18 17:24:37 +01002292 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 alg, input, input_length,
2294 actual_hash, sizeof(actual_hash),
2295 &actual_hash_length);
2296 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002297 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 }
2299 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002300 status = PSA_ERROR_INVALID_SIGNATURE;
2301 goto exit;
2302 }
Dave Rodgman78701152023-08-29 14:20:18 +01002303 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002304 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002306
2307exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2309 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002310}
2311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2313 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002314{
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (source_operation->id == 0 ||
2316 target_operation->id != 0) {
2317 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002318 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2321 target_operation);
2322 if (status != PSA_SUCCESS) {
2323 psa_hash_abort(target_operation);
2324 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002327}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002328
2329
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002331/* MAC */
2332/****************************************************************/
2333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002335{
Steven Cooremane6804192021-03-19 18:28:56 +01002336 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (operation->id == 0) {
2338 return PSA_SUCCESS;
2339 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002342 operation->mac_size = 0;
2343 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002344 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002347}
2348
Ronald Cron2dff3b22021-06-17 16:33:22 +02002349static psa_status_t psa_mac_finalize_alg_and_key_validation(
2350 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002351 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002353{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002354 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 psa_key_type_t key_type = psa_get_key_type(attributes);
2356 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (!PSA_ALG_IS_MAC(alg)) {
2359 return PSA_ERROR_INVALID_ARGUMENT;
2360 }
Steven Cooremane6804192021-03-19 18:28:56 +01002361
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002362 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 status = psa_mac_key_can_do(alg, key_type);
2364 if (status != PSA_SUCCESS) {
2365 return status;
2366 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002367
Steven Cooremand1a68f12021-05-10 11:13:41 +02002368 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002372 /* A very short MAC is too short for security since it can be
2373 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2374 * so we make this our minimum, even though 32 bits is still
2375 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002377 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2380 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002381 /* It's impossible to "truncate" to a larger length than the full length
2382 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002384 }
2385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002387 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2388 * that is disabled in the compile-time configuration. The result can
2389 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2390 * configuration into account. In this case, force a return of
2391 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2392 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2393 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2394 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2395 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002397 }
2398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002400}
2401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2403 mbedtls_svc_key_id_t key,
2404 psa_algorithm_t alg,
2405 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002406{
2407 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2408 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002409 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002410 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002411
2412 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002414 status = PSA_ERROR_BAD_STATE;
2415 goto exit;
2416 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002417
2418 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 key,
2420 &slot,
2421 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2422 alg);
2423 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002424 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002426
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002427 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002428 .core = slot->attr
2429 };
2430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2432 &operation->mac_size);
2433 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002434 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002436
2437 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002438 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (is_sign) {
2440 status = psa_driver_wrapper_mac_sign_setup(operation,
2441 &attributes,
2442 slot->key.data,
2443 slot->key.bytes,
2444 alg);
2445 } else {
2446 status = psa_driver_wrapper_mac_verify_setup(operation,
2447 &attributes,
2448 slot->key.data,
2449 slot->key.bytes,
2450 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002452
Gilles Peskinefbfac682018-07-08 20:51:54 +02002453exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 if (status != PSA_SUCCESS) {
2455 psa_mac_abort(operation);
2456 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002461}
2462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2464 mbedtls_svc_key_id_t key,
2465 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002466{
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002468}
2469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2471 mbedtls_svc_key_id_t key,
2472 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002473{
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002475}
2476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2478 const uint8_t *input,
2479 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002480{
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 if (operation->id == 0) {
2482 return PSA_ERROR_BAD_STATE;
2483 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002484
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002485 /* Don't require hash implementations to behave correctly on a
2486 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 if (input_length == 0) {
2488 return PSA_SUCCESS;
2489 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2492 input, input_length);
2493 if (status != PSA_SUCCESS) {
2494 psa_mac_abort(operation);
2495 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002496
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002498}
2499
Gilles Peskine449bd832023-01-11 14:50:10 +01002500psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2501 uint8_t *mac,
2502 size_t mac_size,
2503 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002504{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002505 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2506 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002509 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002510 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002511 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002514 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002515 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002516 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002517
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002518 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2519 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002521 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002522 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002523 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002526 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002527 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002528 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 status = psa_driver_wrapper_mac_sign_finish(operation,
2531 mac, operation->mac_size,
2532 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002533
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002534exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002535 /* In case of success, set the potential excess room in the output buffer
2536 * to an invalid value, to avoid potentially leaking a longer MAC.
2537 * In case of error, set the output length and content to a safe default,
2538 * such that in case the caller misses an error check, the output would be
2539 * an unachievable MAC.
2540 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002542 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002543 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002544 }
mohammad16036df908f2018-04-02 08:34:15 -07002545
Paul Elliottdc42ca82023-02-24 18:11:59 +00002546 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002551}
2552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2554 const uint8_t *mac,
2555 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002556{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002557 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2558 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002561 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002562 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002563 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002566 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002567 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002568 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002571 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002572 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002573 }
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 status = psa_driver_wrapper_mac_verify_finish(operation,
2576 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002577
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002578exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582}
2583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2585 psa_algorithm_t alg,
2586 const uint8_t *input,
2587 size_t input_length,
2588 uint8_t *mac,
2589 size_t mac_size,
2590 size_t *mac_length,
2591 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002592{
2593 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002594 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2595 psa_key_slot_t *slot;
2596 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002597 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598
Ronald Cron51131b52021-06-17 17:17:20 +02002599 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 key,
2601 &slot,
2602 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2603 alg);
2604 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002605 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002607
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002608 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002609 .core = slot->attr
2610 };
2611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2613 &operation_mac_size);
2614 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002615 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002619 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002620 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002621 }
2622
2623 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 &attributes,
2625 slot->key.data, slot->key.bytes,
2626 alg,
2627 input, input_length,
2628 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002629
2630exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002631 /* In case of success, set the potential excess room in the output buffer
2632 * to an invalid value, to avoid potentially leaking a longer MAC.
2633 * In case of error, set the output length and content to a safe default,
2634 * such that in case the caller misses an error check, the output would be
2635 * an unachievable MAC.
2636 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002638 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002639 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002640 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002641
2642 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002643
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002647}
2648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002650 psa_algorithm_t alg,
2651 const uint8_t *input,
2652 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 uint8_t *mac,
2654 size_t mac_size,
2655 size_t *mac_length)
2656{
2657 return psa_mac_compute_internal(key, alg,
2658 input, input_length,
2659 mac, mac_size, mac_length, 1);
2660}
2661
2662psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2663 psa_algorithm_t alg,
2664 const uint8_t *input,
2665 size_t input_length,
2666 const uint8_t *mac,
2667 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002668{
2669 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002670 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2671 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 status = psa_mac_compute_internal(key, alg,
2674 input, input_length,
2675 actual_mac, sizeof(actual_mac),
2676 &actual_mac_length, 0);
2677 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002678 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002682 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002683 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002684 }
Dave Rodgman78701152023-08-29 14:20:18 +01002685 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002686 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002687 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002688 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002689
2690exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002694}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002695
Gilles Peskinee59236f2018-01-27 23:32:46 +01002696/****************************************************************/
2697/* Asymmetric cryptography */
2698/****************************************************************/
2699
Gilles Peskine449bd832023-01-11 14:50:10 +01002700static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2701 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002702{
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 if (input_is_message) {
2704 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2705 return PSA_ERROR_INVALID_ARGUMENT;
2706 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2709 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2710 return PSA_ERROR_INVALID_ARGUMENT;
2711 }
2712 }
2713 } else {
2714 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2715 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002716 }
2717 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002720}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2723 int input_is_message,
2724 psa_algorithm_t alg,
2725 const uint8_t *input,
2726 size_t input_length,
2727 uint8_t *signature,
2728 size_t signature_size,
2729 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002730{
2731 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2732 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2733 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002734 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002735
2736 *signature_length = 0;
2737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 status = psa_sign_verify_check_alg(input_is_message, alg);
2739 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002740 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002742
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002743 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002744 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002745 * buffer can in principle be empty since it doesn't actually have
2746 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 if (signature_size == 0) {
2748 return PSA_ERROR_BUFFER_TOO_SMALL;
2749 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002750
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002751 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 key, &slot,
2753 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2754 PSA_KEY_USAGE_SIGN_HASH,
2755 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002756
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002758 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002762 status = PSA_ERROR_INVALID_ARGUMENT;
2763 goto exit;
2764 }
2765
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002766 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002768 };
2769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002771 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002772 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002773 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 signature, signature_size, signature_length);
2775 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002776
2777 status = psa_driver_wrapper_sign_hash(
2778 &attributes, slot->key.data, slot->key.bytes,
2779 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002781 }
2782
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002783
2784exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002785 psa_wipe_tag_output_buffer(signature, status, signature_size,
2786 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002787
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002791}
2792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2794 int input_is_message,
2795 psa_algorithm_t alg,
2796 const uint8_t *input,
2797 size_t input_length,
2798 const uint8_t *signature,
2799 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002800{
2801 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2802 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2803 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 status = psa_sign_verify_check_alg(input_is_message, alg);
2806 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002807 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002809
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002810 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 key, &slot,
2812 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2813 PSA_KEY_USAGE_VERIFY_HASH,
2814 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002815
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 if (status != PSA_SUCCESS) {
2817 return status;
2818 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002819
2820 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002822 };
2823
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002825 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002826 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002827 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 signature, signature_length);
2829 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002830 status = psa_driver_wrapper_verify_hash(
2831 &attributes, slot->key.data, slot->key.bytes,
2832 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002834 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002835
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002837
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002839
2840}
2841
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002842psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002843 const psa_key_attributes_t *attributes,
2844 const uint8_t *key_buffer,
2845 size_t key_buffer_size,
2846 psa_algorithm_t alg,
2847 const uint8_t *input,
2848 size_t input_length,
2849 uint8_t *signature,
2850 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002852{
2853 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002854
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002856 size_t hash_length;
2857 uint8_t hash[PSA_HASH_MAX_SIZE];
2858
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002859 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 PSA_ALG_SIGN_GET_HASH(alg),
2861 input, input_length,
2862 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002863
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002865 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002867
2868 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 attributes, key_buffer, key_buffer_size,
2870 alg, hash, hash_length,
2871 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002872 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002875}
2876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2878 psa_algorithm_t alg,
2879 const uint8_t *input,
2880 size_t input_length,
2881 uint8_t *signature,
2882 size_t signature_size,
2883 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002884{
2885 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002886 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002888}
2889
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002890psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002891 const psa_key_attributes_t *attributes,
2892 const uint8_t *key_buffer,
2893 size_t key_buffer_size,
2894 psa_algorithm_t alg,
2895 const uint8_t *input,
2896 size_t input_length,
2897 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002899{
2900 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002901
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002903 size_t hash_length;
2904 uint8_t hash[PSA_HASH_MAX_SIZE];
2905
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002906 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 PSA_ALG_SIGN_GET_HASH(alg),
2908 input, input_length,
2909 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002910
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002912 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002914
2915 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 attributes, key_buffer, key_buffer_size,
2917 alg, hash, hash_length,
2918 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002919 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002922}
2923
Gilles Peskine449bd832023-01-11 14:50:10 +01002924psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2925 psa_algorithm_t alg,
2926 const uint8_t *input,
2927 size_t input_length,
2928 const uint8_t *signature,
2929 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002930{
2931 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002932 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002934}
2935
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002936psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002937 const psa_key_attributes_t *attributes,
2938 const uint8_t *key_buffer, size_t key_buffer_size,
2939 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002941{
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2943 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2944 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002945#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2947 return mbedtls_psa_rsa_sign_hash(
2948 attributes,
2949 key_buffer, key_buffer_size,
2950 alg, hash, hash_length,
2951 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002952#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2953 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 } else {
2955 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002956 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2958 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002959#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2961 return mbedtls_psa_ecdsa_sign_hash(
2962 attributes,
2963 key_buffer, key_buffer_size,
2964 alg, hash, hash_length,
2965 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002966#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2967 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 } else {
2969 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002970 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002971 }
Ronald Cron4501c982021-03-19 20:09:32 +01002972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 (void) key_buffer;
2974 (void) key_buffer_size;
2975 (void) hash;
2976 (void) hash_length;
2977 (void) signature;
2978 (void) signature_size;
2979 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002980
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002982}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002983
Gilles Peskine449bd832023-01-11 14:50:10 +01002984psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2985 psa_algorithm_t alg,
2986 const uint8_t *hash,
2987 size_t hash_length,
2988 uint8_t *signature,
2989 size_t signature_size,
2990 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002991{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002992 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002993 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002995}
2996
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002997psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002998 const psa_key_attributes_t *attributes,
2999 const uint8_t *key_buffer, size_t key_buffer_size,
3000 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003002{
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3004 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3005 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003006#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3008 return mbedtls_psa_rsa_verify_hash(
3009 attributes,
3010 key_buffer, key_buffer_size,
3011 alg, hash, hash_length,
3012 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003013#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3014 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 } else {
3016 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003017 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3019 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003020#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3022 return mbedtls_psa_ecdsa_verify_hash(
3023 attributes,
3024 key_buffer, key_buffer_size,
3025 alg, hash, hash_length,
3026 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003027#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3028 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 } else {
3030 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003031 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003032 }
Ronald Cron4501c982021-03-19 20:09:32 +01003033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 (void) key_buffer;
3035 (void) key_buffer_size;
3036 (void) hash;
3037 (void) hash_length;
3038 (void) signature;
3039 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003042}
3043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3045 psa_algorithm_t alg,
3046 const uint8_t *hash,
3047 size_t hash_length,
3048 const uint8_t *signature,
3049 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003050{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003051 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003052 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003054}
3055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3057 psa_algorithm_t alg,
3058 const uint8_t *input,
3059 size_t input_length,
3060 const uint8_t *salt,
3061 size_t salt_length,
3062 uint8_t *output,
3063 size_t output_size,
3064 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003065{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003066 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003067 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003068 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003069 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003070
Darryl Green5cc689a2018-07-24 15:34:10 +01003071 (void) input;
3072 (void) input_length;
3073 (void) salt;
3074 (void) output;
3075 (void) output_size;
3076
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003077 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003078
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3080 return PSA_ERROR_INVALID_ARGUMENT;
3081 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003082
Ronald Cron5c522922020-11-14 16:35:34 +01003083 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3085 if (status != PSA_SUCCESS) {
3086 return status;
3087 }
3088 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3089 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003090 status = PSA_ERROR_INVALID_ARGUMENT;
3091 goto exit;
3092 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003094 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003096 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003097
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003098 status = psa_driver_wrapper_asymmetric_encrypt(
3099 &attributes, slot->key.data, slot->key.bytes,
3100 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003102exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003104
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003106}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003107
Gilles Peskine449bd832023-01-11 14:50:10 +01003108psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3109 psa_algorithm_t alg,
3110 const uint8_t *input,
3111 size_t input_length,
3112 const uint8_t *salt,
3113 size_t salt_length,
3114 uint8_t *output,
3115 size_t output_size,
3116 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003117{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003118 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003119 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003120 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003121 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003122
Darryl Green5cc689a2018-07-24 15:34:10 +01003123 (void) input;
3124 (void) input_length;
3125 (void) salt;
3126 (void) output;
3127 (void) output_size;
3128
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003129 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3132 return PSA_ERROR_INVALID_ARGUMENT;
3133 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003134
Ronald Cron5c522922020-11-14 16:35:34 +01003135 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3137 if (status != PSA_SUCCESS) {
3138 return status;
3139 }
3140 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003141 status = PSA_ERROR_INVALID_ARGUMENT;
3142 goto exit;
3143 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003144
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003145 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003147 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003148
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003149 status = psa_driver_wrapper_asymmetric_decrypt(
3150 &attributes, slot->key.data, slot->key.bytes,
3151 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003153
3154exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003156
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003158}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003159
Paul Elliott9fe12f62022-11-30 19:16:02 +00003160/****************************************************************/
3161/* Asymmetric interruptible cryptography */
3162/****************************************************************/
3163
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003164static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3165
Paul Elliott9fe12f62022-11-30 19:16:02 +00003166void psa_interruptible_set_max_ops(uint32_t max_ops)
3167{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003168 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003169}
3170
3171uint32_t psa_interruptible_get_max_ops(void)
3172{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003173 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003174}
3175
Paul Elliott9fe12f62022-11-30 19:16:02 +00003176uint32_t psa_sign_hash_get_num_ops(
3177 const psa_sign_hash_interruptible_operation_t *operation)
3178{
Paul Elliott296ede92022-12-15 17:00:30 +00003179 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003180}
3181
3182uint32_t psa_verify_hash_get_num_ops(
3183 const psa_verify_hash_interruptible_operation_t *operation)
3184{
Paul Elliott296ede92022-12-15 17:00:30 +00003185 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003186}
3187
Paul Elliott59ad9452022-12-18 15:09:02 +00003188static psa_status_t psa_sign_hash_abort_internal(
3189 psa_sign_hash_interruptible_operation_t *operation)
3190{
3191 if (operation->id == 0) {
3192 /* The object has (apparently) been initialized but it is not (yet)
3193 * in use. It's ok to call abort on such an object, and there's
3194 * nothing to do. */
3195 return PSA_SUCCESS;
3196 }
3197
3198 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3199
3200 status = psa_driver_wrapper_sign_hash_abort(operation);
3201
3202 operation->id = 0;
3203
Paul Elliotte6145dc2023-02-07 12:51:21 +00003204 /* Do not clear either the error_occurred or num_ops elements here as they
3205 * only want to be cleared by the application calling abort, not by abort
3206 * being called at completion of an operation. */
3207
Paul Elliott59ad9452022-12-18 15:09:02 +00003208 return status;
3209}
3210
Paul Elliott9fe12f62022-11-30 19:16:02 +00003211psa_status_t psa_sign_hash_start(
3212 psa_sign_hash_interruptible_operation_t *operation,
3213 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3214 const uint8_t *hash, size_t hash_length)
3215{
3216 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3217 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3218 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003219 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003220
Paul Elliottc9774412023-02-06 15:14:07 +00003221 /* Check that start has not been previously called, or operation has not
3222 * previously errored. */
3223 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003224 return PSA_ERROR_BAD_STATE;
3225 }
3226
Paul Elliott9fe12f62022-11-30 19:16:02 +00003227 status = psa_sign_verify_check_alg(0, alg);
3228 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003229 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003230 return status;
3231 }
3232
3233 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3234 PSA_KEY_USAGE_SIGN_HASH,
3235 alg);
3236
3237 if (status != PSA_SUCCESS) {
3238 goto exit;
3239 }
3240
3241 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3242 status = PSA_ERROR_INVALID_ARGUMENT;
3243 goto exit;
3244 }
3245
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003246 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003247 .core = slot->attr
3248 };
3249
Paul Elliott296ede92022-12-15 17:00:30 +00003250 /* Ensure ops count gets reset, in case of operation re-use. */
3251 operation->num_ops = 0;
3252
Paul Elliott9fe12f62022-11-30 19:16:02 +00003253 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3254 slot->key.data,
3255 slot->key.bytes, alg,
3256 hash, hash_length);
3257exit:
3258
3259 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003260 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003261 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003262 }
3263
3264 unlock_status = psa_unlock_key_slot(slot);
3265
Paul Elliottc9774412023-02-06 15:14:07 +00003266 if (unlock_status != PSA_SUCCESS) {
3267 operation->error_occurred = 1;
3268 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003269
Paul Elliottc9774412023-02-06 15:14:07 +00003270 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003271}
3272
3273
3274psa_status_t psa_sign_hash_complete(
3275 psa_sign_hash_interruptible_operation_t *operation,
3276 uint8_t *signature, size_t signature_size,
3277 size_t *signature_length)
3278{
3279 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3280
3281 *signature_length = 0;
3282
Paul Elliottc9774412023-02-06 15:14:07 +00003283 /* Check that start has been called first, and that operation has not
3284 * previously errored. */
3285 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003286 status = PSA_ERROR_BAD_STATE;
3287 goto exit;
3288 }
3289
Paul Elliott096abc42023-02-03 18:33:23 +00003290 /* Immediately reject a zero-length signature buffer. This guarantees that
3291 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003292 if (signature_size == 0) {
3293 status = PSA_ERROR_BUFFER_TOO_SMALL;
3294 goto exit;
3295 }
3296
3297 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3298 signature_size,
3299 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003300
Paul Elliott296ede92022-12-15 17:00:30 +00003301 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003302 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003303
Paul Elliottebe225c2023-02-10 14:32:53 +00003304exit:
3305
Paul Elliott59200a22023-02-21 15:07:40 +00003306 psa_wipe_tag_output_buffer(signature, status, signature_size,
3307 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003308
Paul Elliott53bb3122023-02-10 14:22:22 +00003309 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003310 if (status != PSA_SUCCESS) {
3311 operation->error_occurred = 1;
3312 }
3313
Paul Elliott59ad9452022-12-18 15:09:02 +00003314 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003315 }
3316
3317 return status;
3318}
3319
3320psa_status_t psa_sign_hash_abort(
3321 psa_sign_hash_interruptible_operation_t *operation)
3322{
Paul Elliott59ad9452022-12-18 15:09:02 +00003323 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3324
3325 status = psa_sign_hash_abort_internal(operation);
3326
3327 /* We clear the number of ops done here, so that it is not cleared when
3328 * the operation fails or succeeds, only on manual abort. */
3329 operation->num_ops = 0;
3330
Paul Elliottc9774412023-02-06 15:14:07 +00003331 /* Likewise, failure state. */
3332 operation->error_occurred = 0;
3333
Paul Elliott59ad9452022-12-18 15:09:02 +00003334 return status;
3335}
3336
3337static psa_status_t psa_verify_hash_abort_internal(
3338 psa_verify_hash_interruptible_operation_t *operation)
3339{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003340 if (operation->id == 0) {
3341 /* The object has (apparently) been initialized but it is not (yet)
3342 * in use. It's ok to call abort on such an object, and there's
3343 * nothing to do. */
3344 return PSA_SUCCESS;
3345 }
3346
Paul Elliott59ad9452022-12-18 15:09:02 +00003347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3348
3349 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003350
3351 operation->id = 0;
3352
Paul Elliotte6145dc2023-02-07 12:51:21 +00003353 /* Do not clear either the error_occurred or num_ops elements here as they
3354 * only want to be cleared by the application calling abort, not by abort
3355 * being called at completion of an operation. */
3356
Paul Elliott59ad9452022-12-18 15:09:02 +00003357 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003358}
3359
3360psa_status_t psa_verify_hash_start(
3361 psa_verify_hash_interruptible_operation_t *operation,
3362 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3363 const uint8_t *hash, size_t hash_length,
3364 const uint8_t *signature, size_t signature_length)
3365{
3366 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3367 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3368 psa_key_slot_t *slot;
3369
Paul Elliottc9774412023-02-06 15:14:07 +00003370 /* Check that start has not been previously called, or operation has not
3371 * previously errored. */
3372 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003373 return PSA_ERROR_BAD_STATE;
3374 }
3375
3376 status = psa_sign_verify_check_alg(0, alg);
3377 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003378 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003379 return status;
3380 }
3381
3382 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3383 PSA_KEY_USAGE_VERIFY_HASH,
3384 alg);
3385
3386 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003387 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003388 return status;
3389 }
3390
3391 psa_key_attributes_t attributes = {
3392 .core = slot->attr
3393 };
3394
Paul Elliott296ede92022-12-15 17:00:30 +00003395 /* Ensure ops count gets reset, in case of operation re-use. */
3396 operation->num_ops = 0;
3397
Paul Elliott9fe12f62022-11-30 19:16:02 +00003398 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3399 slot->key.data,
3400 slot->key.bytes,
3401 alg, hash, hash_length,
3402 signature, signature_length);
3403
3404 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003405 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003406 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003407 }
3408
3409 unlock_status = psa_unlock_key_slot(slot);
3410
Paul Elliottc9774412023-02-06 15:14:07 +00003411 if (unlock_status != PSA_SUCCESS) {
3412 operation->error_occurred = 1;
3413 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003414
Paul Elliottc9774412023-02-06 15:14:07 +00003415 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003416}
3417
3418psa_status_t psa_verify_hash_complete(
3419 psa_verify_hash_interruptible_operation_t *operation)
3420{
3421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3422
Paul Elliottc9774412023-02-06 15:14:07 +00003423 /* Check that start has been called first, and that operation has not
3424 * previously errored. */
3425 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003426 status = PSA_ERROR_BAD_STATE;
3427 goto exit;
3428 }
3429
3430 status = psa_driver_wrapper_verify_hash_complete(operation);
3431
Paul Elliott296ede92022-12-15 17:00:30 +00003432 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003433 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003434 operation);
3435
Paul Elliottebe225c2023-02-10 14:32:53 +00003436exit:
3437
Paul Elliott9fe12f62022-11-30 19:16:02 +00003438 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003439 if (status != PSA_SUCCESS) {
3440 operation->error_occurred = 1;
3441 }
3442
Paul Elliott59ad9452022-12-18 15:09:02 +00003443 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003444 }
3445
3446 return status;
3447}
3448
3449psa_status_t psa_verify_hash_abort(
3450 psa_verify_hash_interruptible_operation_t *operation)
3451{
Paul Elliott59ad9452022-12-18 15:09:02 +00003452 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003453
Paul Elliott59ad9452022-12-18 15:09:02 +00003454 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003455
Paul Elliott59ad9452022-12-18 15:09:02 +00003456 /* We clear the number of ops done here, so that it is not cleared when
3457 * the operation fails or succeeds, only on manual abort. */
3458 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003459
Paul Elliottc9774412023-02-06 15:14:07 +00003460 /* Likewise, failure state. */
3461 operation->error_occurred = 0;
3462
Paul Elliott59ad9452022-12-18 15:09:02 +00003463 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003464}
3465
Paul Elliott588f8ed2022-12-02 18:10:26 +00003466/****************************************************************/
3467/* Asymmetric interruptible cryptography internal */
3468/* implementations */
3469/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003470
Paul Elliott588f8ed2022-12-02 18:10:26 +00003471void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3472{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003473
Paul Elliott588f8ed2022-12-02 18:10:26 +00003474#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3475 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3476 defined(MBEDTLS_ECP_RESTARTABLE)
3477
3478 /* Internal implementation uses zero to indicate infinite number max ops,
3479 * therefore avoid this value, and set to minimum possible. */
3480 if (max_ops == 0) {
3481 max_ops = 1;
3482 }
3483
Paul Elliott588f8ed2022-12-02 18:10:26 +00003484 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003485#else
3486 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003487#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3488 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3489 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3490}
3491
Paul Elliott588f8ed2022-12-02 18:10:26 +00003492uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003493 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003494{
3495#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3496 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3497 defined(MBEDTLS_ECP_RESTARTABLE)
3498
Paul Elliott93d9ca82023-02-15 18:14:21 +00003499 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003500#else
3501 (void) operation;
3502 return 0;
3503#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3504 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3505 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3506}
3507
3508uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003509 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003510{
3511 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3512 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3513 defined(MBEDTLS_ECP_RESTARTABLE)
3514
Paul Elliott93d9ca82023-02-15 18:14:21 +00003515 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003516#else
3517 (void) operation;
3518 return 0;
3519#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3520 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3521 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3522}
3523
3524psa_status_t mbedtls_psa_sign_hash_start(
3525 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3526 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3527 size_t key_buffer_size, psa_algorithm_t alg,
3528 const uint8_t *hash, size_t hash_length)
3529{
3530 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003531 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003532
Paul Elliott068fe072023-01-16 13:59:15 +00003533 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3534 return PSA_ERROR_NOT_SUPPORTED;
3535 }
3536
3537 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003538 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003539 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003540
3541#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003542 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3543 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003544
Paul Elliotta1c94092023-02-15 16:38:04 +00003545 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3546
Paul Elliott93d9ca82023-02-15 18:14:21 +00003547 /* Ensure num_ops is zero'ed in case of context re-use. */
3548 operation->num_ops = 0;
3549
Paul Elliott068fe072023-01-16 13:59:15 +00003550 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3551 attributes->core.bits,
3552 key_buffer,
3553 key_buffer_size,
3554 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003555
Paul Elliott068fe072023-01-16 13:59:15 +00003556 if (status != PSA_SUCCESS) {
3557 return status;
3558 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003559
Paul Elliott1bc59df2023-02-05 13:41:57 +00003560 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003561 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003562
Paul Elliott068fe072023-01-16 13:59:15 +00003563 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003564 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003565 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003566
Paul Elliott0290a762023-02-09 14:30:24 +00003567 /* We only need to store the same length of hash as the private key size
3568 * here, it would be truncated by the internal implementation anyway. */
3569 required_hash_length = (hash_length < operation->coordinate_bytes ?
3570 hash_length : operation->coordinate_bytes);
3571
Paul Elliottba70ad42023-02-15 18:23:53 +00003572 if (required_hash_length > sizeof(operation->hash)) {
3573 /* Shouldn't happen, but better safe than sorry. */
3574 return PSA_ERROR_CORRUPTION_DETECTED;
3575 }
3576
Paul Elliott0290a762023-02-09 14:30:24 +00003577 memcpy(operation->hash, hash, required_hash_length);
3578 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003579
3580 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003581
3582#else
Paul Elliott068fe072023-01-16 13:59:15 +00003583 (void) operation;
3584 (void) key_buffer;
3585 (void) key_buffer_size;
3586 (void) alg;
3587 (void) hash;
3588 (void) hash_length;
3589 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003590 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003591
Paul Elliott068fe072023-01-16 13:59:15 +00003592 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003593#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3594 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3595 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003596}
3597
3598psa_status_t mbedtls_psa_sign_hash_complete(
3599 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3600 uint8_t *signature, size_t signature_size,
3601 size_t *signature_length)
3602{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003603#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3604 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3605 defined(MBEDTLS_ECP_RESTARTABLE)
3606
Paul Elliotta1c94092023-02-15 16:38:04 +00003607 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003608 mbedtls_mpi r;
3609 mbedtls_mpi s;
3610
Paul Elliott46845252023-02-03 14:59:11 +00003611 mbedtls_mpi_init(&r);
3612 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003613
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003614 /* Ensure max_ops is set to the current value (or default). */
3615 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3616
Paul Elliotta1c94092023-02-15 16:38:04 +00003617 if (signature_size < 2 * operation->coordinate_bytes) {
3618 status = PSA_ERROR_BUFFER_TOO_SMALL;
3619 goto exit;
3620 }
3621
Paul Elliott588f8ed2022-12-02 18:10:26 +00003622 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003623
Paul Elliott588f8ed2022-12-02 18:10:26 +00003624#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3625 status = mbedtls_to_psa_error(
3626 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003627 &r,
3628 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003629 &operation->ctx->d,
3630 operation->hash,
3631 operation->hash_length,
3632 operation->md_alg,
3633 mbedtls_psa_get_random,
3634 MBEDTLS_PSA_RANDOM_STATE,
3635 &operation->restart_ctx));
3636#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003637 status = PSA_ERROR_NOT_SUPPORTED;
3638 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003639#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3640 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003641 status = mbedtls_to_psa_error(
3642 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003643 &r,
3644 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003645 &operation->ctx->d,
3646 operation->hash,
3647 operation->hash_length,
3648 mbedtls_psa_get_random,
3649 MBEDTLS_PSA_RANDOM_STATE,
3650 mbedtls_psa_get_random,
3651 MBEDTLS_PSA_RANDOM_STATE,
3652 &operation->restart_ctx));
3653 }
3654
Paul Elliottf8e5b562023-02-19 18:43:45 +00003655 /* Hide the fact that the restart context only holds a delta of number of
3656 * ops done during the last operation, not an absolute value. */
3657 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3658
Paul Elliott724bd252023-02-08 12:35:08 +00003659 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003660 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003661 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003662 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003663 operation->coordinate_bytes)
3664 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003665
3666 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003667 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003668 }
3669
3670 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003671 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003672 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003673 operation->coordinate_bytes,
3674 operation->coordinate_bytes)
3675 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003676
3677 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003678 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003679 }
3680
Paul Elliott1bc59df2023-02-05 13:41:57 +00003681 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003682
Paul Elliott724bd252023-02-08 12:35:08 +00003683 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003684 }
Paul Elliott724bd252023-02-08 12:35:08 +00003685
3686exit:
3687
3688 mbedtls_mpi_free(&r);
3689 mbedtls_mpi_free(&s);
3690 return status;
3691
Paul Elliott588f8ed2022-12-02 18:10:26 +00003692 #else
3693
3694 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003695 (void) signature;
3696 (void) signature_size;
3697 (void) signature_length;
3698
3699 return PSA_ERROR_NOT_SUPPORTED;
3700
3701#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3702 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3703 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3704}
3705
3706psa_status_t mbedtls_psa_sign_hash_abort(
3707 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3708{
3709
3710#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3711 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3712 defined(MBEDTLS_ECP_RESTARTABLE)
3713
3714 if (operation->ctx) {
3715 mbedtls_ecdsa_free(operation->ctx);
3716 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003717 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003718 }
3719
3720 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3721
Paul Elliott93d9ca82023-02-15 18:14:21 +00003722 operation->num_ops = 0;
3723
Paul Elliott588f8ed2022-12-02 18:10:26 +00003724 return PSA_SUCCESS;
3725
3726#else
3727
3728 (void) operation;
3729
3730 return PSA_ERROR_NOT_SUPPORTED;
3731
3732#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3733 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3734 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3735}
3736
3737psa_status_t mbedtls_psa_verify_hash_start(
3738 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3739 const psa_key_attributes_t *attributes,
3740 const uint8_t *key_buffer, size_t key_buffer_size,
3741 psa_algorithm_t alg,
3742 const uint8_t *hash, size_t hash_length,
3743 const uint8_t *signature, size_t signature_length)
3744{
3745 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003746 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003747 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003748
Paul Elliott068fe072023-01-16 13:59:15 +00003749 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3750 return PSA_ERROR_NOT_SUPPORTED;
3751 }
3752
3753 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003754 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003755 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003756
3757#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003758 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3759 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003760
Paul Elliotta1c94092023-02-15 16:38:04 +00003761 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3762 mbedtls_mpi_init(&operation->r);
3763 mbedtls_mpi_init(&operation->s);
3764
Paul Elliott93d9ca82023-02-15 18:14:21 +00003765 /* Ensure num_ops is zero'ed in case of context re-use. */
3766 operation->num_ops = 0;
3767
Paul Elliott068fe072023-01-16 13:59:15 +00003768 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3769 attributes->core.bits,
3770 key_buffer,
3771 key_buffer_size,
3772 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773
Paul Elliott068fe072023-01-16 13:59:15 +00003774 if (status != PSA_SUCCESS) {
3775 return status;
3776 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003777
Paul Elliottc569fc22023-02-10 13:02:54 +00003778 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003779
Paul Elliott1bc59df2023-02-05 13:41:57 +00003780 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003781 return PSA_ERROR_INVALID_SIGNATURE;
3782 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003783
Paul Elliott068fe072023-01-16 13:59:15 +00003784 status = mbedtls_to_psa_error(
3785 mbedtls_mpi_read_binary(&operation->r,
3786 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003787 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003788
Paul Elliott068fe072023-01-16 13:59:15 +00003789 if (status != PSA_SUCCESS) {
3790 return status;
3791 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003792
Paul Elliott068fe072023-01-16 13:59:15 +00003793 status = mbedtls_to_psa_error(
3794 mbedtls_mpi_read_binary(&operation->s,
3795 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003796 coordinate_bytes,
3797 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003798
Paul Elliott068fe072023-01-16 13:59:15 +00003799 if (status != PSA_SUCCESS) {
3800 return status;
3801 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802
Paul Elliott2c9843f2023-02-15 17:32:42 +00003803 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003804
Paul Elliott2c9843f2023-02-15 17:32:42 +00003805 if (status != PSA_SUCCESS) {
3806 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003807 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003808
Paul Elliott0290a762023-02-09 14:30:24 +00003809 /* We only need to store the same length of hash as the private key size
3810 * here, it would be truncated by the internal implementation anyway. */
3811 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3812 coordinate_bytes);
3813
Paul Elliottba70ad42023-02-15 18:23:53 +00003814 if (required_hash_length > sizeof(operation->hash)) {
3815 /* Shouldn't happen, but better safe than sorry. */
3816 return PSA_ERROR_CORRUPTION_DETECTED;
3817 }
3818
Paul Elliott0290a762023-02-09 14:30:24 +00003819 memcpy(operation->hash, hash, required_hash_length);
3820 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003821
3822 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003823#else
Paul Elliott068fe072023-01-16 13:59:15 +00003824 (void) operation;
3825 (void) key_buffer;
3826 (void) key_buffer_size;
3827 (void) alg;
3828 (void) hash;
3829 (void) hash_length;
3830 (void) signature;
3831 (void) signature_length;
3832 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003833 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003834 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003835
Paul Elliott068fe072023-01-16 13:59:15 +00003836 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003837#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3838 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3839 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003840}
3841
3842psa_status_t mbedtls_psa_verify_hash_complete(
3843 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3844{
3845
3846#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3847 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3848 defined(MBEDTLS_ECP_RESTARTABLE)
3849
Paul Elliottf8e5b562023-02-19 18:43:45 +00003850 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3851
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003852 /* Ensure max_ops is set to the current value (or default). */
3853 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3854
Paul Elliottf8e5b562023-02-19 18:43:45 +00003855 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003856 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3857 operation->hash,
3858 operation->hash_length,
3859 &operation->ctx->Q,
3860 &operation->r,
3861 &operation->s,
3862 &operation->restart_ctx));
3863
Paul Elliottf8e5b562023-02-19 18:43:45 +00003864 /* Hide the fact that the restart context only holds a delta of number of
3865 * ops done during the last operation, not an absolute value. */
3866 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3867
3868 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003869#else
3870 (void) operation;
3871
3872 return PSA_ERROR_NOT_SUPPORTED;
3873
3874#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3875 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3876 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3877}
3878
3879psa_status_t mbedtls_psa_verify_hash_abort(
3880 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3881{
3882
3883#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3884 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3885 defined(MBEDTLS_ECP_RESTARTABLE)
3886
3887 if (operation->ctx) {
3888 mbedtls_ecdsa_free(operation->ctx);
3889 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003890 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003891 }
3892
3893 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3894
Paul Elliott93d9ca82023-02-15 18:14:21 +00003895 operation->num_ops = 0;
3896
Paul Elliott588f8ed2022-12-02 18:10:26 +00003897 mbedtls_mpi_free(&operation->r);
3898 mbedtls_mpi_free(&operation->s);
3899
3900 return PSA_SUCCESS;
3901
3902#else
3903 (void) operation;
3904
3905 return PSA_ERROR_NOT_SUPPORTED;
3906
3907#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3908 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3909 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3910}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003911
mohammad1603503973b2018-03-12 15:59:30 +02003912/****************************************************************/
3913/* Symmetric cryptography */
3914/****************************************************************/
3915
Gilles Peskine449bd832023-01-11 14:50:10 +01003916static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3917 mbedtls_svc_key_id_t key,
3918 psa_algorithm_t alg,
3919 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003920{
3921 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3922 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003923 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3925 PSA_KEY_USAGE_ENCRYPT :
3926 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003927 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02003928
3929 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003931 status = PSA_ERROR_BAD_STATE;
3932 goto exit;
3933 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003934
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003936 status = PSA_ERROR_INVALID_ARGUMENT;
3937 goto exit;
3938 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003939
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3941 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003942 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003943 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003944
Ronald Cron49fafa92021-03-10 08:34:23 +01003945 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003946 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003947 * so we only set it (in the driver wrapper) after resources have been
3948 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003949 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003951 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003953 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 }
3955 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003956
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003957 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003959 };
3960
Ronald Cronab99ac22020-10-01 16:38:28 +02003961 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 if (cipher_operation == MBEDTLS_ENCRYPT) {
3963 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3964 &attributes,
3965 slot->key.data,
3966 slot->key.bytes,
3967 alg);
3968 } else {
3969 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3970 &attributes,
3971 slot->key.data,
3972 slot->key.bytes,
3973 alg);
3974 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003975
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003976exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 if (status != PSA_SUCCESS) {
3978 psa_cipher_abort(operation);
3979 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003980
Gilles Peskine449bd832023-01-11 14:50:10 +01003981 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003982
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003984}
3985
Gilles Peskine449bd832023-01-11 14:50:10 +01003986psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3987 mbedtls_svc_key_id_t key,
3988 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003989{
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003991}
3992
Gilles Peskine449bd832023-01-11 14:50:10 +01003993psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3994 mbedtls_svc_key_id_t key,
3995 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003996{
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003998}
3999
Gilles Peskine449bd832023-01-11 14:50:10 +01004000psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4001 uint8_t *iv,
4002 size_t iv_size,
4003 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004004{
Ronald Cron6d051732020-10-01 14:10:20 +02004005 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004006 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004007 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004008
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004010 status = PSA_ERROR_BAD_STATE;
4011 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004012 }
4013
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004015 status = PSA_ERROR_BAD_STATE;
4016 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004017 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004018
Ronald Cron2fb90522021-07-05 12:31:44 +02004019 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004020 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004021 status = PSA_ERROR_BUFFER_TOO_SMALL;
4022 goto exit;
4023 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004024
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004026 status = PSA_ERROR_GENERIC_ERROR;
4027 goto exit;
4028 }
4029
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 status = psa_generate_random(local_iv, default_iv_length);
4031 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004032 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004033 }
Ronald Cron5618a392021-03-26 09:52:26 +01004034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 status = psa_driver_wrapper_cipher_set_iv(operation,
4036 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004037
4038exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 if (status == PSA_SUCCESS) {
4040 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004041 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004042 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004044 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004046 }
Ronald Cron6d051732020-10-01 14:10:20 +02004047
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004049}
4050
Gilles Peskine449bd832023-01-11 14:50:10 +01004051psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4052 const uint8_t *iv,
4053 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004054{
Ronald Cron6d051732020-10-01 14:10:20 +02004055 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004058 status = PSA_ERROR_BAD_STATE;
4059 goto exit;
4060 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004061
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004063 status = PSA_ERROR_BAD_STATE;
4064 goto exit;
4065 }
Ronald Crona0d68172021-03-26 10:15:08 +01004066
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004068 status = PSA_ERROR_INVALID_ARGUMENT;
4069 goto exit;
4070 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004071
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 status = psa_driver_wrapper_cipher_set_iv(operation,
4073 iv,
4074 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004075
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004076exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004078 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 } else {
4080 psa_cipher_abort(operation);
4081 }
4082 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004083}
4084
Gilles Peskine449bd832023-01-11 14:50:10 +01004085psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4086 const uint8_t *input,
4087 size_t input_length,
4088 uint8_t *output,
4089 size_t output_size,
4090 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004091{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004095 status = PSA_ERROR_BAD_STATE;
4096 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004097 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004098
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004100 status = PSA_ERROR_BAD_STATE;
4101 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004102 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 status = psa_driver_wrapper_cipher_update(operation,
4105 input,
4106 input_length,
4107 output,
4108 output_size,
4109 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004110
4111exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 if (status != PSA_SUCCESS) {
4113 psa_cipher_abort(operation);
4114 }
Ronald Cron6d051732020-10-01 14:10:20 +02004115
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004117}
4118
Gilles Peskine449bd832023-01-11 14:50:10 +01004119psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4120 uint8_t *output,
4121 size_t output_size,
4122 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004123{
David Saadab4ecc272019-02-14 13:48:10 +02004124 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004125
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004127 status = PSA_ERROR_BAD_STATE;
4128 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004129 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004130
Gilles Peskine449bd832023-01-11 14:50:10 +01004131 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004132 status = PSA_ERROR_BAD_STATE;
4133 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004134 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004135
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 status = psa_driver_wrapper_cipher_finish(operation,
4137 output,
4138 output_size,
4139 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004140
4141exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 if (status == PSA_SUCCESS) {
4143 return psa_cipher_abort(operation);
4144 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004145 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004147
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004149 }
mohammad1603503973b2018-03-12 15:59:30 +02004150}
4151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004153{
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004155 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004156 * in use. It's ok to call abort on such an object, and there's
4157 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004159 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004162
Ronald Cron49fafa92021-03-10 08:34:23 +01004163 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004164 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004165 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004168}
4169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4171 psa_algorithm_t alg,
4172 const uint8_t *input,
4173 size_t input_length,
4174 uint8_t *output,
4175 size_t output_size,
4176 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004177{
4178 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004179 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004180 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004181 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4182 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004183 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004184
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004186 status = PSA_ERROR_INVALID_ARGUMENT;
4187 goto exit;
4188 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004189
Gilles Peskine449bd832023-01-11 14:50:10 +01004190 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4191 PSA_KEY_USAGE_ENCRYPT,
4192 alg);
4193 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004194 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004196
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004197 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004199 };
4200
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4202 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004203 status = PSA_ERROR_GENERIC_ERROR;
4204 goto exit;
4205 }
4206
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 if (default_iv_length > 0) {
4208 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004209 status = PSA_ERROR_BUFFER_TOO_SMALL;
4210 goto exit;
4211 }
4212
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 status = psa_generate_random(local_iv, default_iv_length);
4214 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004215 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004216 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004217 }
4218
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004219 status = psa_driver_wrapper_cipher_encrypt(
4220 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004221 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004222 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004224
4225exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 unlock_status = psa_unlock_key_slot(slot);
4227 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004228 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004229 }
Ronald Cron23919522021-07-08 19:00:07 +02004230
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 if (status == PSA_SUCCESS) {
4232 if (default_iv_length > 0) {
4233 memcpy(output, local_iv, default_iv_length);
4234 }
4235 *output_length += default_iv_length;
4236 } else {
4237 *output_length = 0;
4238 }
4239
4240 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004241}
4242
Gilles Peskine449bd832023-01-11 14:50:10 +01004243psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4244 psa_algorithm_t alg,
4245 const uint8_t *input,
4246 size_t input_length,
4247 uint8_t *output,
4248 size_t output_size,
4249 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004250{
4251 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004252 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004253 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004254 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004255
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004257 status = PSA_ERROR_INVALID_ARGUMENT;
4258 goto exit;
4259 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004260
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4262 PSA_KEY_USAGE_DECRYPT,
4263 alg);
4264 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004265 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004267
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004268 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004270 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004271
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4273 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004274 status = PSA_ERROR_INVALID_ARGUMENT;
4275 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004277 status = PSA_ERROR_INVALID_ARGUMENT;
4278 goto exit;
4279 }
4280
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004281 status = psa_driver_wrapper_cipher_decrypt(
4282 &attributes, slot->key.data, slot->key.bytes,
4283 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004285
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004286exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 unlock_status = psa_unlock_key_slot(slot);
4288 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004289 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004291
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004293 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 }
Ronald Cron23919522021-07-08 19:00:07 +02004295
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004297}
4298
4299
mohammad16035955c982018-04-26 00:53:03 +03004300/****************************************************************/
4301/* AEAD */
4302/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004303
Paul Elliottbaff51c2021-09-28 17:44:45 +01004304/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004305static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004306{
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004308}
4309
4310/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004311static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4312 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004313{
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004315
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004317#if defined(PSA_WANT_ALG_GCM)
4318 case PSA_ALG_GCM:
4319 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 * arbitrarily large nonces. Please note that we do not generally
4321 * recommend the usage of nonces of greater length than
4322 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4323 * size, which can then lead to collisions if you encrypt a very
4324 * large number of messages.*/
4325 if (nonce_length != 0) {
4326 return PSA_SUCCESS;
4327 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004328 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004329#endif /* PSA_WANT_ALG_GCM */
4330#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004331 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 if (nonce_length >= 7 && nonce_length <= 13) {
4333 return PSA_SUCCESS;
4334 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004335 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004336#endif /* PSA_WANT_ALG_CCM */
4337#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004338 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 if (nonce_length == 12) {
4340 return PSA_SUCCESS;
4341 } else if (nonce_length == 8) {
4342 return PSA_ERROR_NOT_SUPPORTED;
4343 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004344 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004345#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004346 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004347 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004348 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004349 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004352}
4353
Gilles Peskine449bd832023-01-11 14:50:10 +01004354static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004355{
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4357 return PSA_ERROR_INVALID_ARGUMENT;
4358 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004361}
4362
Gilles Peskine449bd832023-01-11 14:50:10 +01004363psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4364 psa_algorithm_t alg,
4365 const uint8_t *nonce,
4366 size_t nonce_length,
4367 const uint8_t *additional_data,
4368 size_t additional_data_length,
4369 const uint8_t *plaintext,
4370 size_t plaintext_length,
4371 uint8_t *ciphertext,
4372 size_t ciphertext_size,
4373 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004374{
Ronald Cron215633c2021-03-16 17:15:37 +01004375 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4376 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004377
mohammad1603f08a5502018-06-03 15:05:47 +03004378 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004379
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 status = psa_aead_check_algorithm(alg);
4381 if (status != PSA_SUCCESS) {
4382 return status;
4383 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004384
Ronald Cron9a986162021-03-26 12:40:07 +01004385 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4387 if (status != PSA_SUCCESS) {
4388 return status;
4389 }
mohammad16035955c982018-04-26 00:53:03 +03004390
Ronald Cron215633c2021-03-16 17:15:37 +01004391 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004393 };
mohammad16035955c982018-04-26 00:53:03 +03004394
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 status = psa_aead_check_nonce_length(alg, nonce_length);
4396 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004397 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004399
Ronald Cronde822812021-03-17 16:08:20 +01004400 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004401 &attributes, slot->key.data, slot->key.bytes,
4402 alg,
4403 nonce, nonce_length,
4404 additional_data, additional_data_length,
4405 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004407
Gilles Peskine449bd832023-01-11 14:50:10 +01004408 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4409 memset(ciphertext, 0, ciphertext_size);
4410 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004411
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004412exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004416}
4417
Gilles Peskine449bd832023-01-11 14:50:10 +01004418psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4419 psa_algorithm_t alg,
4420 const uint8_t *nonce,
4421 size_t nonce_length,
4422 const uint8_t *additional_data,
4423 size_t additional_data_length,
4424 const uint8_t *ciphertext,
4425 size_t ciphertext_length,
4426 uint8_t *plaintext,
4427 size_t plaintext_size,
4428 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004429{
Ronald Cron215633c2021-03-16 17:15:37 +01004430 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4431 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004432
Gilles Peskineee652a32018-06-01 19:23:52 +02004433 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004434
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 status = psa_aead_check_algorithm(alg);
4436 if (status != PSA_SUCCESS) {
4437 return status;
4438 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004439
Ronald Cron9a986162021-03-26 12:40:07 +01004440 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4442 if (status != PSA_SUCCESS) {
4443 return status;
4444 }
mohammad16035955c982018-04-26 00:53:03 +03004445
Ronald Cron215633c2021-03-16 17:15:37 +01004446 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004448 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 status = psa_aead_check_nonce_length(alg, nonce_length);
4451 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004452 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004454
Ronald Cronde822812021-03-17 16:08:20 +01004455 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004456 &attributes, slot->key.data, slot->key.bytes,
4457 alg,
4458 nonce, nonce_length,
4459 additional_data, additional_data_length,
4460 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004462
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 if (status != PSA_SUCCESS && plaintext_size != 0) {
4464 memset(plaintext, 0, plaintext_size);
4465 }
mohammad160360a64d02018-06-03 17:20:42 +03004466
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004467exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004469
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 return status;
mohammad16035955c982018-04-26 00:53:03 +03004471}
4472
Gilles Peskine449bd832023-01-11 14:50:10 +01004473static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4474{
4475 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004476
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004478#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004480 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4482 return PSA_ERROR_INVALID_ARGUMENT;
4483 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004484 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004485#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004486
Przemek Stekiel86679c72022-10-06 17:06:56 +02004487#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004489 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4491 return PSA_ERROR_INVALID_ARGUMENT;
4492 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004493 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004494#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004495
Przemek Stekiel86679c72022-10-06 17:06:56 +02004496#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004498 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 if (tag_len != 16) {
4500 return PSA_ERROR_INVALID_ARGUMENT;
4501 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004502 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004503#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004504
4505 default:
4506 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004508 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004510}
4511
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004512/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004513static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4514 int is_encrypt,
4515 mbedtls_svc_key_id_t key,
4516 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004517{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004518 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4519 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4520 psa_key_slot_t *slot = NULL;
4521 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004522 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 status = psa_aead_check_algorithm(alg);
4525 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004526 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004527 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004530 status = PSA_ERROR_BAD_STATE;
4531 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004532 }
4533
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 if (operation->nonce_set || operation->lengths_set ||
4535 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004536 status = PSA_ERROR_BAD_STATE;
4537 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004538 }
4539
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004541 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004543 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004545
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4547 alg);
4548 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004549 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004551
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004552 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004553 .core = slot->attr
4554 };
4555
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004557 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004559
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 if (is_encrypt) {
4561 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4562 &attributes,
4563 slot->key.data,
4564 slot->key.bytes,
4565 alg);
4566 } else {
4567 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4568 &attributes,
4569 slot->key.data,
4570 slot->key.bytes,
4571 alg);
4572 }
4573 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004574 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004576
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004578
4579exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004583 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004585 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 } else {
4587 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004588 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004589
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004591}
4592
Paul Elliott302ff6b2021-04-20 18:10:30 +01004593/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004594psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4595 mbedtls_svc_key_id_t key,
4596 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004597{
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004599}
4600
4601/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004602psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4603 mbedtls_svc_key_id_t key,
4604 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004605{
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004607}
4608
4609/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004610psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4611 uint8_t *nonce,
4612 size_t nonce_size,
4613 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004614{
4615 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004616 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004617 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004618
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004619 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004620
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004622 status = PSA_ERROR_BAD_STATE;
4623 goto exit;
4624 }
4625
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004627 status = PSA_ERROR_BAD_STATE;
4628 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004629 }
4630
Paul Elliotte193ea82021-10-01 13:00:16 +01004631 /* For CCM, this size may not be correct according to the PSA
4632 * specification. The PSA Crypto 1.0.1 specification states:
4633 *
4634 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4635 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4636 *
4637 * However this restriction that L has to be the smallest integer is not
4638 * applied in practice, and it is not implementable here since the
4639 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4641 operation->alg);
4642 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004643 status = PSA_ERROR_BUFFER_TOO_SMALL;
4644 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004645 }
4646
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 status = psa_generate_random(local_nonce, required_nonce_size);
4648 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004649 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004653
Paul Elliott39dc6b82021-05-11 19:16:09 +01004654exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 if (status == PSA_SUCCESS) {
4656 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004657 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 } else {
4659 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004660 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004661
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004663}
4664
4665/* Set the nonce for a multipart authenticated encryption or decryption
4666 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004667psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4668 const uint8_t *nonce,
4669 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004670{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004671 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4672
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004674 status = PSA_ERROR_BAD_STATE;
4675 goto exit;
4676 }
4677
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004679 status = PSA_ERROR_BAD_STATE;
4680 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004681 }
4682
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4684 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004685 status = PSA_ERROR_INVALID_ARGUMENT;
4686 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004687 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004688
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4690 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004691
Paul Elliott39dc6b82021-05-11 19:16:09 +01004692exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004694 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 } else {
4696 psa_aead_abort(operation);
4697 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004698
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004700}
4701
4702/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004703psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4704 size_t ad_length,
4705 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004706{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004707 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004710 status = PSA_ERROR_BAD_STATE;
4711 goto exit;
4712 }
4713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 if (operation->lengths_set || operation->ad_started ||
4715 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004716 status = PSA_ERROR_BAD_STATE;
4717 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004718 }
4719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004721#if defined(PSA_WANT_ALG_GCM)
4722 case PSA_ALG_GCM:
4723 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 * bits. Without the guard this code will generate warnings on 32bit
4725 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004726#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 if (((uint64_t) ad_length) >> 61 != 0 ||
4728 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004729 status = PSA_ERROR_INVALID_ARGUMENT;
4730 goto exit;
4731 }
Paul Elliott325d3742021-09-27 17:56:28 +01004732#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004733 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004734#endif /* PSA_WANT_ALG_GCM */
4735#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004736 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004738 status = PSA_ERROR_INVALID_ARGUMENT;
4739 goto exit;
4740 }
4741 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004742#endif /* PSA_WANT_ALG_CCM */
4743#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004744 case PSA_ALG_CHACHA20_POLY1305:
4745 /* No length restrictions for ChaChaPoly. */
4746 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004747#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004748 default:
4749 break;
4750 }
Paul Elliott325d3742021-09-27 17:56:28 +01004751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4753 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004754
Paul Elliott39dc6b82021-05-11 19:16:09 +01004755exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004757 operation->ad_remaining = ad_length;
4758 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004759 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 } else {
4761 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004762 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004765}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004766
4767/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004768psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4769 const uint8_t *input,
4770 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004771{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004772 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4773
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004775 status = PSA_ERROR_BAD_STATE;
4776 goto exit;
4777 }
4778
Gilles Peskine449bd832023-01-11 14:50:10 +01004779 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004780 status = PSA_ERROR_BAD_STATE;
4781 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004782 }
4783
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 if (operation->lengths_set) {
4785 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004786 status = PSA_ERROR_INVALID_ARGUMENT;
4787 goto exit;
4788 }
4789
4790 operation->ad_remaining -= input_length;
4791 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004792#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004794 status = PSA_ERROR_BAD_STATE;
4795 goto exit;
4796 }
4797#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004798
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 status = psa_driver_wrapper_aead_update_ad(operation, input,
4800 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004801
Paul Elliott39dc6b82021-05-11 19:16:09 +01004802exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004804 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 } else {
4806 psa_aead_abort(operation);
4807 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004808
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004810}
4811
4812/* Encrypt or decrypt a message fragment in an active multipart AEAD
4813 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004814psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4815 const uint8_t *input,
4816 size_t input_length,
4817 uint8_t *output,
4818 size_t output_size,
4819 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004820{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004821 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004822
4823 *output_length = 0;
4824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004826 status = PSA_ERROR_BAD_STATE;
4827 goto exit;
4828 }
4829
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004831 status = PSA_ERROR_BAD_STATE;
4832 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004833 }
4834
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004836 /* Additional data length was supplied, but not all the additional
4837 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004839 status = PSA_ERROR_INVALID_ARGUMENT;
4840 goto exit;
4841 }
4842
4843 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004845 status = PSA_ERROR_INVALID_ARGUMENT;
4846 goto exit;
4847 }
4848
4849 operation->body_remaining -= input_length;
4850 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004851#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004853 status = PSA_ERROR_BAD_STATE;
4854 goto exit;
4855 }
4856#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004857
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4859 output, output_size,
4860 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004861
Paul Elliott39dc6b82021-05-11 19:16:09 +01004862exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004864 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 } else {
4866 psa_aead_abort(operation);
4867 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004870}
4871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004873{
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 if (operation->id == 0 || !operation->nonce_set) {
4875 return PSA_ERROR_BAD_STATE;
4876 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4879 operation->body_remaining != 0)) {
4880 return PSA_ERROR_INVALID_ARGUMENT;
4881 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004884}
4885
Paul Elliott302ff6b2021-04-20 18:10:30 +01004886/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004887psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4888 uint8_t *ciphertext,
4889 size_t ciphertext_size,
4890 size_t *ciphertext_length,
4891 uint8_t *tag,
4892 size_t tag_size,
4893 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004894{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004895 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4896
Paul Elliott302ff6b2021-04-20 18:10:30 +01004897 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004898 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004899
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 status = psa_aead_final_checks(operation);
4901 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004902 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004904
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004906 status = PSA_ERROR_BAD_STATE;
4907 goto exit;
4908 }
4909
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4911 ciphertext_size,
4912 ciphertext_length,
4913 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004914
Paul Elliott39dc6b82021-05-11 19:16:09 +01004915exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004916
4917
Paul Elliottf47b0952021-05-21 18:02:33 +01004918 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004919 * the zero tag size, make sure the tag is set to something implausible.
4920 * Even if the operation succeeds, make sure we clear the rest of the
4921 * buffer to prevent potential leakage of anything previously placed in
4922 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004923 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004924
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004926
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004928}
4929
4930/* Finish authenticating and decrypting a message in a multipart AEAD
4931 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004932psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4933 uint8_t *plaintext,
4934 size_t plaintext_size,
4935 size_t *plaintext_length,
4936 const uint8_t *tag,
4937 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004938{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004939 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4940
Paul Elliott302ff6b2021-04-20 18:10:30 +01004941 *plaintext_length = 0;
4942
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 status = psa_aead_final_checks(operation);
4944 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004945 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004947
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004949 status = PSA_ERROR_BAD_STATE;
4950 goto exit;
4951 }
4952
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4954 plaintext_size,
4955 plaintext_length,
4956 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004957
4958exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004960
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004962}
4963
4964/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004965psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004966{
4967 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4968
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004970 /* The object has (apparently) been initialized but it is not (yet)
4971 * in use. It's ok to call abort on such an object, and there's
4972 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004974 }
4975
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004977
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004979
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004981}
4982
Gilles Peskinee59236f2018-01-27 23:32:46 +01004983/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004984/* Generators */
4985/****************************************************************/
4986
Przemek Stekiel69c46792022-06-10 12:59:51 +02004987#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004988 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004989 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05304990 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304991 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08004992#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004993#endif /* At least one builtin KDF */
4994
Przemek Stekiel69c46792022-06-10 12:59:51 +02004995#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004996 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4997 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4998static psa_status_t psa_key_derivation_start_hmac(
4999 psa_mac_operation_t *operation,
5000 psa_algorithm_t hash_alg,
5001 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005003{
5004 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5005 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5007 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5008 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005009
Steven Cooreman72f736a2021-05-07 14:14:37 +02005010 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005012
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 status = psa_driver_wrapper_mac_sign_setup(operation,
5014 &attributes,
5015 hmac_key, hmac_key_length,
5016 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005017
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 psa_reset_key_attributes(&attributes);
5019 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005020}
5021#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005022
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005023#define HKDF_STATE_INIT 0 /* no input yet */
5024#define HKDF_STATE_STARTED 1 /* got salt */
5025#define HKDF_STATE_KEYED 2 /* got key */
5026#define HKDF_STATE_OUTPUT 3 /* output started */
5027
Gilles Peskinecbe66502019-05-16 16:59:18 +02005028static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005030{
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5032 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5033 } else {
5034 return operation->alg;
5035 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005036}
5037
Gilles Peskine449bd832023-01-11 14:50:10 +01005038psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005039{
5040 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5042 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005043 /* The object has (apparently) been initialized but it is not
5044 * in use. It's ok to call abort on such an object, and there's
5045 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005047#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5049 mbedtls_free(operation->ctx.hkdf.info);
5050 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5051 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005052#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005053#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5054 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5056 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5057 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5058 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005059 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005061 }
5062
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005064 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005066 }
5067
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005069 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005071 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005072#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005074 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005076 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005077#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005078 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005079
5080 /* We leave the fields Ai and output_block to be erased safely by the
5081 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005083#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5084 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005085#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5087 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5088 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5089 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005090#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305091#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305092 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305093 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005094 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305095 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305096 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305097
5098 status = PSA_SUCCESS;
5099 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305100#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005101 {
5102 status = PSA_ERROR_BAD_STATE;
5103 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 mbedtls_platform_zeroize(operation, sizeof(*operation));
5105 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005106}
5107
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005110{
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005112 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005114 }
5115
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005118}
5119
Gilles Peskine449bd832023-01-11 14:50:10 +01005120psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5121 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005122{
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 if (operation->alg == 0) {
5124 return PSA_ERROR_BAD_STATE;
5125 }
5126 if (capacity > operation->capacity) {
5127 return PSA_ERROR_INVALID_ARGUMENT;
5128 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005129 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005131}
5132
Przemek Stekiel69c46792022-06-10 12:59:51 +02005133#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005134/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005135static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5136 psa_algorithm_t kdf_alg,
5137 uint8_t *output,
5138 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005139{
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5141 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005142 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005143 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005144#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005146#else
5147 const uint8_t last_block = 0xff;
5148#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 if (hkdf->state < HKDF_STATE_KEYED ||
5151 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005152#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005154#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 )) {
5156 return PSA_ERROR_BAD_STATE;
5157 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005158 hkdf->state = HKDF_STATE_OUTPUT;
5159
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005161 /* Copy what remains of the current block */
5162 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005164 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 }
5166 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005167 output += n;
5168 output_length -= n;
5169 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005170 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005171 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005173 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005174 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005175 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005176 * object was corrupted or if this function is called directly
5177 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 if (hkdf->block_number == last_block) {
5179 return PSA_ERROR_BAD_STATE;
5180 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005181
5182 /* We need a new block */
5183 ++hkdf->block_number;
5184 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005185
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5187 hash_alg,
5188 hkdf->prk,
5189 hash_length);
5190 if (status != PSA_SUCCESS) {
5191 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005192 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005193
5194 if (hkdf->block_number != 1) {
5195 status = psa_mac_update(&hkdf->hmac,
5196 hkdf->output_block,
5197 hash_length);
5198 if (status != PSA_SUCCESS) {
5199 return status;
5200 }
5201 }
5202 status = psa_mac_update(&hkdf->hmac,
5203 hkdf->info,
5204 hkdf->info_length);
5205 if (status != PSA_SUCCESS) {
5206 return status;
5207 }
5208 status = psa_mac_update(&hkdf->hmac,
5209 &hkdf->block_number, 1);
5210 if (status != PSA_SUCCESS) {
5211 return status;
5212 }
5213 status = psa_mac_sign_finish(&hkdf->hmac,
5214 hkdf->output_block,
5215 sizeof(hkdf->output_block),
5216 &hmac_output_length);
5217 if (status != PSA_SUCCESS) {
5218 return status;
5219 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005220 }
5221
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005223}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005224#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005225
John Durkop07cc04a2020-11-16 22:08:34 -08005226#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5227 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005228static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5229 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005231{
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5233 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005234 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5235 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005236 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005237
Janos Follath7742fee2019-06-17 12:58:10 +01005238 /* We can't be wanting more output after block 0xff, otherwise
5239 * the capacity check in psa_key_derivation_output_bytes() would have
5240 * prevented this call. It could happen only if the operation
5241 * object was corrupted or if this function is called directly
5242 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 if (tls12_prf->block_number == 0xff) {
5244 return PSA_ERROR_CORRUPTION_DETECTED;
5245 }
Janos Follath7742fee2019-06-17 12:58:10 +01005246
5247 /* We need a new block */
5248 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005249 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005250
5251 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5252 *
5253 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5254 *
5255 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5256 * HMAC_hash(secret, A(2) + seed) +
5257 * HMAC_hash(secret, A(3) + seed) + ...
5258 *
5259 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005260 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005261 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005262 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005263 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005264 * is currently extracted as `output_block` and where i is
5265 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005266 */
5267
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 status = psa_key_derivation_start_hmac(&hmac,
5269 hash_alg,
5270 tls12_prf->secret,
5271 tls12_prf->secret_length);
5272 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005273 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005275
5276 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005278 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5279 * the variable seed and in this instance means it in the context of the
5280 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 status = psa_mac_update(&hmac,
5282 tls12_prf->label,
5283 tls12_prf->label_length);
5284 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005285 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 }
5287 status = psa_mac_update(&hmac,
5288 tls12_prf->seed,
5289 tls12_prf->seed_length);
5290 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005291 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 }
5293 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005294 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5296 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005297 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005299 }
5300
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 status = psa_mac_sign_finish(&hmac,
5302 tls12_prf->Ai, hash_length,
5303 &hmac_output_length);
5304 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005305 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 }
5307 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005308 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005310
5311 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 status = psa_key_derivation_start_hmac(&hmac,
5313 hash_alg,
5314 tls12_prf->secret,
5315 tls12_prf->secret_length);
5316 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005317 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 }
5319 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5320 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005321 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 }
5323 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5324 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005325 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 }
5327 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5328 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005329 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 }
5331 status = psa_mac_sign_finish(&hmac,
5332 tls12_prf->output_block, hash_length,
5333 &hmac_output_length);
5334 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005335 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005337
Janos Follath7742fee2019-06-17 12:58:10 +01005338
5339cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 cleanup_status = psa_mac_abort(&hmac);
5341 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005342 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005344
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005346}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005347
Janos Follath844eb0e2019-06-19 12:10:49 +01005348static psa_status_t psa_key_derivation_tls12_prf_read(
5349 psa_tls12_prf_key_derivation_t *tls12_prf,
5350 psa_algorithm_t alg,
5351 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005353{
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5355 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005356 psa_status_t status;
5357 uint8_t offset, length;
5358
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005360 case PSA_TLS12_PRF_STATE_LABEL_SET:
5361 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5362 break;
5363 case PSA_TLS12_PRF_STATE_OUTPUT:
5364 break;
5365 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005367 }
5368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005370 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 if (tls12_prf->left_in_block == 0) {
5372 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5373 alg);
5374 if (status != PSA_SUCCESS) {
5375 return status;
5376 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005377
5378 continue;
5379 }
5380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005382 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005384 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005386
5387 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005389 output += length;
5390 output_length -= length;
5391 tls12_prf->left_in_block -= length;
5392 }
5393
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005395}
John Durkop07cc04a2020-11-16 22:08:34 -08005396#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5397 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005398
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005399#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5400static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5401 psa_tls12_ecjpake_to_pms_t *ecjpake,
5402 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005404{
5405 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005406 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005407
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 if (output_length != 32) {
5409 return PSA_ERROR_INVALID_ARGUMENT;
5410 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005411
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5413 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5414 &output_size);
5415 if (status != PSA_SUCCESS) {
5416 return status;
5417 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005418
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 if (output_size != output_length) {
5420 return PSA_ERROR_GENERIC_ERROR;
5421 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005422
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005424}
5425#endif
5426
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305427#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305428static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5429 psa_pbkdf2_key_derivation_t *pbkdf2,
5430 psa_algorithm_t prf_alg,
5431 uint8_t prf_output_length,
5432 psa_key_attributes_t *attributes)
5433{
5434 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305435 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305436 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305437 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305438 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305439 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305440 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305441
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305442 mac_operation.is_sign = 1;
5443 mac_operation.mac_size = prf_output_length;
5444 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305445
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305446 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5447 attributes,
5448 pbkdf2->password,
5449 pbkdf2->password_length,
5450 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305451 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305452 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305453 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305454 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5455 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305456 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305457 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305458 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305459 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305460 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305461 }
5462 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5463 &mac_output_length);
5464 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305465 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305466 }
5467
5468 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305469 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305470 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305471 }
5472
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305473 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305474
5475 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305476 /* We are passing prf_output_length as mac_size because the driver
5477 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305478 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305479 status = psa_driver_wrapper_mac_compute(attributes,
5480 pbkdf2->password,
5481 pbkdf2->password_length,
5482 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305483 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305484 &mac_output_length);
5485 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305486 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305487 }
5488
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305489 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305490 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305491
5492cleanup:
5493 /* Zeroise buffers to clear sensitive data from memory. */
5494 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5495 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305496}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305497
5498static psa_status_t psa_key_derivation_pbkdf2_read(
5499 psa_pbkdf2_key_derivation_t *pbkdf2,
5500 psa_algorithm_t kdf_alg,
5501 uint8_t *output,
5502 size_t output_length)
5503{
5504 psa_status_t status;
5505 psa_algorithm_t prf_alg;
5506 uint8_t prf_output_length;
5507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5508 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5509 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5510
5511 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5512 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5513 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5514 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305515 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5516 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305517 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305518 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305519 } else {
5520 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305521 }
5522
5523 switch (pbkdf2->state) {
5524 case PSA_PBKDF2_STATE_PASSWORD_SET:
5525 /* Initially we need a new block so bytes_used is equal to block size*/
5526 pbkdf2->bytes_used = prf_output_length;
5527 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5528 break;
5529 case PSA_PBKDF2_STATE_OUTPUT:
5530 break;
5531 default:
5532 return PSA_ERROR_BAD_STATE;
5533 }
5534
5535 while (output_length != 0) {
5536 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5537 if (n > output_length) {
5538 n = (uint8_t) output_length;
5539 }
5540 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5541 output += n;
5542 output_length -= n;
5543 pbkdf2->bytes_used += n;
5544
5545 if (output_length == 0) {
5546 break;
5547 }
5548
5549 /* We need a new block */
5550 pbkdf2->bytes_used = 0;
5551 pbkdf2->block_number++;
5552
5553 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5554 prf_output_length,
5555 &attributes);
5556 if (status != PSA_SUCCESS) {
5557 return status;
5558 }
5559 }
5560
5561 return PSA_SUCCESS;
5562}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305563#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305564
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005565psa_status_t psa_key_derivation_output_bytes(
5566 psa_key_derivation_operation_t *operation,
5567 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005569{
5570 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005572
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005574 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005576 }
5577
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005579 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005580 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005581 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005582 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005583 goto exit;
5584 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005586 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005587 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005588 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5589 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005590 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005591 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005593 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005594 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005595
Przemek Stekiel69c46792022-06-10 12:59:51 +02005596#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5598 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5599 output, output_length);
5600 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005601#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005602#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5603 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5605 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5606 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5607 kdf_alg, output,
5608 output_length);
5609 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005610#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5611 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005612#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005614 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5616 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005617#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305618#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305619 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305620 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5621 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305622 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305623#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005624
Gilles Peskineeab56e42018-07-12 17:12:33 +02005625 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005626 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005628 }
5629
5630exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005632 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005633 * This allows us to differentiate between exhausted operations and
5634 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5635 * operations. */
5636 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005637 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005638 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005640 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005642}
5643
David Brown7807bf72021-02-09 16:28:23 -07005644#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005645static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005646{
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 if (data_size >= 8) {
5648 mbedtls_des_key_set_parity(data);
5649 }
5650 if (data_size >= 16) {
5651 mbedtls_des_key_set_parity(data + 8);
5652 }
5653 if (data_size >= 24) {
5654 mbedtls_des_key_set_parity(data + 16);
5655 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005656}
David Brown7807bf72021-02-09 16:28:23 -07005657#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005658
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005659/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 * ECC keys on a Weierstrass elliptic curve require the generation
5661 * of a private key which is an integer
5662 * in the range [1, N - 1], where N is the boundary of the private key domain:
5663 * N is the prime p for Diffie-Hellman, or the order of the
5664 * curve’s base point for ECC.
5665 *
5666 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5667 * This function generates the private key using the following process:
5668 *
5669 * 1. Draw a byte string of length ceiling(m/8) bytes.
5670 * 2. If m is not a multiple of 8, set the most significant
5671 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5672 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5673 * 4. If k > N - 2, discard the result and return to step 1.
5674 * 5. Output k + 1 as the private key.
5675 *
5676 * This method allows compliance to NIST standards, specifically the methods titled
5677 * Key-Pair Generation by Testing Candidates in the following publications:
5678 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5679 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5680 * Diffie-Hellman keys.
5681 *
5682 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5683 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5684 *
5685 * Note: Function allocates memory for *data buffer, so given *data should be
5686 * always NULL.
5687 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005688#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5689#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005690static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005691 psa_key_slot_t *slot,
5692 size_t bits,
5693 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005694 uint8_t **data
5695 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005696{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005697 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005698 mbedtls_mpi k;
5699 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005700 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005701 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005702 size_t m;
5703 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005704
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 mbedtls_mpi_init(&k);
5706 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005707
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005708 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005710 mbedtls_ecp_group_id grp_id =
Valerio Settiddba51e2023-12-21 10:16:33 +01005711 mbedtls_ecc_group_from_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005714 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005715 goto cleanup;
5716 }
5717
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005718 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005720
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005722
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005723 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005724 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005725 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005726
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005727 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005728
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005729 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005731
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005732 /* Note: This function is always called with *data == NULL and it
5733 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 *data = mbedtls_calloc(1, m_bytes);
5735 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005736 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005737 goto cleanup;
5738 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005739
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005741 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005743 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005745
5746 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005748 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005749 * (8 * ceiling(m/8) - m) bits of the first byte in
5750 * the string to zero.
5751 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005753 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005754 }
5755
5756 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 * big-endian byte string.
5758 */
5759 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005760
5761 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 * Result of comparison is returned. When it indicates error
5763 * then this function is called again.
5764 */
5765 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005766 }
5767
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005768 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5770 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005771cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 if (ret != 0) {
5773 status = mbedtls_to_psa_error(ret);
5774 }
5775 if (status != PSA_SUCCESS) {
5776 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005777 *data = NULL;
5778 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 mbedtls_mpi_free(&k);
5780 mbedtls_mpi_free(&diff_N_2);
5781 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005782}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005783
5784/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5785 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5786 *
5787 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5788 * draw a 32-byte string and process it as specified in
5789 * Elliptic Curves for Security [RFC7748] §5.
5790 *
5791 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5792 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5793 *
5794 * Note: Function allocates memory for *data buffer, so given *data should be
5795 * always NULL.
5796 */
5797
5798static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5799 size_t bits,
5800 psa_key_derivation_operation_t *operation,
5801 uint8_t **data
5802 )
5803{
5804 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005805 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005808 case 255:
5809 output_length = 32;
5810 break;
5811 case 448:
5812 output_length = 56;
5813 break;
5814 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005816 break;
5817 }
5818
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 if (*data == NULL) {
5822 return PSA_ERROR_INSUFFICIENT_MEMORY;
5823 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005826
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005828 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005830
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005832 case 255:
5833 (*data)[0] &= 248;
5834 (*data)[31] &= 127;
5835 (*data)[31] |= 64;
5836 break;
5837 case 448:
5838 (*data)[0] &= 252;
5839 (*data)[55] |= 128;
5840 break;
5841 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005843 break;
5844 }
5845
5846 return status;
5847}
Valerio Setti86587ab2023-06-27 13:09:30 +02005848#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5849static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5850 psa_key_slot_t *slot, size_t bits,
5851 psa_key_derivation_operation_t *operation, uint8_t **data)
5852{
5853 (void) slot;
5854 (void) bits;
5855 (void) operation;
5856 (void) data;
5857 return PSA_ERROR_NOT_SUPPORTED;
5858}
5859
5860static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5861 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5862{
5863 (void) bits;
5864 (void) operation;
5865 (void) data;
5866 return PSA_ERROR_NOT_SUPPORTED;
5867}
5868#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5869#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005870
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005871static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005872 psa_key_slot_t *slot,
5873 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005875{
5876 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305878 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005879 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005880 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5883 return PSA_ERROR_INVALID_ARGUMENT;
5884 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005885
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005886#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005887 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5889 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5890 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005891 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5893 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005894 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 }
5896 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005897 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5899 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005900 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005902 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005903 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005904#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005905 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 if (key_type_is_raw_bytes(slot->attr.type)) {
5907 if (bits % 8 != 0) {
5908 return PSA_ERROR_INVALID_ARGUMENT;
5909 }
5910 data = mbedtls_calloc(1, bytes);
5911 if (data == NULL) {
5912 return PSA_ERROR_INSUFFICIENT_MEMORY;
5913 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005914
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 status = psa_key_derivation_output_bytes(operation, data, bytes);
5916 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005917 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 }
David Brown12ca5032021-02-11 11:02:00 -07005919#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5921 psa_des_set_key_parity(data, bytes);
5922 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005923#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 } else {
5925 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005926 }
Ronald Crondd04d422020-11-28 15:14:42 +01005927
Ronald Cronbf33c932020-11-28 18:06:53 +01005928 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005929 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005931 };
5932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5934 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5935 &storage_size);
5936 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305937 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 }
Archanad8a83dc2021-06-14 10:04:16 +05305939 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 status = psa_allocate_buffer_to_slot(slot, storage_size);
5941 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305942 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 }
Archanad8a83dc2021-06-14 10:04:16 +05305944
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 status = psa_driver_wrapper_import_key(&attributes,
5946 data, bytes,
5947 slot->key.data,
5948 slot->key.bytes,
5949 &slot->key.bytes, &bits);
5950 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005951 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005953
5954exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 mbedtls_free(data);
5956 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005957}
5958
Gilles Peskine449bd832023-01-11 14:50:10 +01005959psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5960 psa_key_derivation_operation_t *operation,
5961 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005962{
5963 psa_status_t status;
5964 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005965 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005966
Ronald Cron81709fc2020-11-14 12:10:32 +01005967 *key = MBEDTLS_SVC_KEY_ID_INIT;
5968
Gilles Peskine0f84d622019-09-12 19:03:13 +02005969 /* Reject any attempt to create a zero-length key so that we don't
5970 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 if (psa_get_key_bits(attributes) == 0) {
5972 return PSA_ERROR_INVALID_ARGUMENT;
5973 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005974
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 if (operation->alg == PSA_ALG_NONE) {
5976 return PSA_ERROR_BAD_STATE;
5977 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 if (!operation->can_output_key) {
5980 return PSA_ERROR_NOT_PERMITTED;
5981 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005982
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5984 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005985#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005987 /* Deriving a key in a secure element is not implemented yet. */
5988 status = PSA_ERROR_NOT_SUPPORTED;
5989 }
5990#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 if (status == PSA_SUCCESS) {
5992 status = psa_generate_derived_key_internal(slot,
5993 attributes->core.bits,
5994 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005995 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 if (status == PSA_SUCCESS) {
5997 status = psa_finish_key_creation(slot, driver, key);
5998 }
5999 if (status != PSA_SUCCESS) {
6000 psa_fail_key_creation(slot, driver);
6001 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006002
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006004}
6005
Gilles Peskineeab56e42018-07-12 17:12:33 +02006006
6007
6008/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006009/* Key derivation */
6010/****************************************************************/
6011
Steven Cooreman932ffb72021-02-15 12:14:32 +01006012#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006013static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006014{
6015#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6017 return 1;
6018 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006019#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006020#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6022 return 1;
6023 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006024#endif
6025#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6027 return 1;
6028 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006029#endif
6030#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6032 return 1;
6033 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006034#endif
6035#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6037 return 1;
6038 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006039#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006040#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6042 return 1;
6043 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006044#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306045#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6046 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6047 return 1;
6048 }
6049#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306050#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6051 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6052 return 1;
6053 }
6054#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006056}
6057
Gilles Peskine449bd832023-01-11 14:50:10 +01006058static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006059{
6060 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 psa_status_t status = psa_hash_setup(&operation, alg);
6062 psa_hash_abort(&operation);
6063 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006064}
6065
Gilles Peskine969c5d62019-01-16 15:53:06 +01006066static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006067 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006069{
Janos Follath5fe19732019-06-20 15:09:30 +01006070 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6071 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006073
Gilles Peskine969c5d62019-01-16 15:53:06 +01006074 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 if (!is_kdf_alg_supported(kdf_alg)) {
6076 return PSA_ERROR_NOT_SUPPORTED;
6077 }
John Durkop07cc04a2020-11-16 22:08:34 -08006078
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006079 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306080 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6082 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306083 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6084 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6085 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306086 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306087 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 if (hash_size == 0) {
6089 return PSA_ERROR_NOT_SUPPORTED;
6090 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006091
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006092 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6093 * we might fail later, which is somewhat unfriendly and potentially
6094 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 psa_status_t status = psa_hash_try_support(hash_alg);
6096 if (status != PSA_SUCCESS) {
6097 return status;
6098 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006099 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6102 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6103 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6104 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006105 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006106#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006107 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6109 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006110 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006112#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6113 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 operation->capacity = 255 * hash_size;
6115 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006116}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006117
Gilles Peskine449bd832023-01-11 14:50:10 +01006118static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006119{
6120#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 if (alg == PSA_ALG_ECDH) {
6122 return PSA_SUCCESS;
6123 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006124#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006125#if defined(PSA_WANT_ALG_FFDH)
6126 if (alg == PSA_ALG_FFDH) {
6127 return PSA_SUCCESS;
6128 }
6129#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006130 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006132}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006133
6134static int psa_key_derivation_allows_free_form_secret_input(
6135 psa_algorithm_t kdf_alg)
6136{
6137#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6138 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6139 return 0;
6140 }
6141#endif
6142 (void) kdf_alg;
6143 return 1;
6144}
John Durkop07cc04a2020-11-16 22:08:34 -08006145#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6148 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006149{
6150 psa_status_t status;
6151
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 if (operation->alg != 0) {
6153 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006154 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006155
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6157 return PSA_ERROR_INVALID_ARGUMENT;
6158 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6159#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6160 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6161 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6162 status = psa_key_agreement_try_support(ka_alg);
6163 if (status != PSA_SUCCESS) {
6164 return status;
6165 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006166 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6167 return PSA_ERROR_INVALID_ARGUMENT;
6168 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6170#else
6171 return PSA_ERROR_NOT_SUPPORTED;
6172#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6173 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6174#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6175 status = psa_key_derivation_setup_kdf(operation, alg);
6176#else
6177 return PSA_ERROR_NOT_SUPPORTED;
6178#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6179 } else {
6180 return PSA_ERROR_INVALID_ARGUMENT;
6181 }
6182
6183 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006184 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 }
6186 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006187}
6188
Przemek Stekiel69c46792022-06-10 12:59:51 +02006189#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006190static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6191 psa_algorithm_t kdf_alg,
6192 psa_key_derivation_step_t step,
6193 const uint8_t *data,
6194 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006195{
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006197 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006199 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006200#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 PSA_ERROR_INVALID_ARGUMENT;
6203 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006204#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 if (hkdf->state != HKDF_STATE_INIT) {
6206 return PSA_ERROR_BAD_STATE;
6207 } else {
6208 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6209 hash_alg,
6210 data, data_length);
6211 if (status != PSA_SUCCESS) {
6212 return status;
6213 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006214 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006216 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006217 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006218#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006220 /* We shouldn't be in different state as HKDF_EXPAND only allows
6221 * two inputs: SECRET (this case) and INFO which does not modify
6222 * the state. It could happen only if the hkdf
6223 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 if (hkdf->state != HKDF_STATE_INIT) {
6225 return PSA_ERROR_BAD_STATE;
6226 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006227
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006228 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6230 return PSA_ERROR_INVALID_ARGUMENT;
6231 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006232
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 memcpy(hkdf->prk, data, data_length);
6234 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006235#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006236 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006237 /* HKDF: If no salt was provided, use an empty salt.
6238 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006240#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6242 return PSA_ERROR_BAD_STATE;
6243 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006244#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6246 hash_alg,
6247 NULL, 0);
6248 if (status != PSA_SUCCESS) {
6249 return status;
6250 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006251 hkdf->state = HKDF_STATE_STARTED;
6252 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 if (hkdf->state != HKDF_STATE_STARTED) {
6254 return PSA_ERROR_BAD_STATE;
6255 }
6256 status = psa_mac_update(&hkdf->hmac,
6257 data, data_length);
6258 if (status != PSA_SUCCESS) {
6259 return status;
6260 }
6261 status = psa_mac_sign_finish(&hkdf->hmac,
6262 hkdf->prk,
6263 sizeof(hkdf->prk),
6264 &data_length);
6265 if (status != PSA_SUCCESS) {
6266 return status;
6267 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006268 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006269
Gilles Peskine2b522db2019-04-12 15:11:49 +02006270 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006271 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006272#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006274 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006276 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006278#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006279 {
6280 /* Block 0 is empty, and the next block will be
6281 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006283 }
6284
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006286 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006287#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6289 return PSA_ERROR_INVALID_ARGUMENT;
6290 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006291#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006292#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6294 hkdf->state == HKDF_STATE_INIT) {
6295 return PSA_ERROR_BAD_STATE;
6296 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006297#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 if (hkdf->state == HKDF_STATE_OUTPUT) {
6299 return PSA_ERROR_BAD_STATE;
6300 }
6301 if (hkdf->info_set) {
6302 return PSA_ERROR_BAD_STATE;
6303 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006304 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 if (data_length != 0) {
6306 hkdf->info = mbedtls_calloc(1, data_length);
6307 if (hkdf->info == NULL) {
6308 return PSA_ERROR_INSUFFICIENT_MEMORY;
6309 }
6310 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006311 }
6312 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006314 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006316 }
6317}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006318#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006319
John Durkop07cc04a2020-11-16 22:08:34 -08006320#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6321 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006322static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6323 const uint8_t *data,
6324 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006325{
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6327 return PSA_ERROR_BAD_STATE;
6328 }
Janos Follathf08e2652019-06-13 09:05:41 +01006329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 if (data_length != 0) {
6331 prf->seed = mbedtls_calloc(1, data_length);
6332 if (prf->seed == NULL) {
6333 return PSA_ERROR_INSUFFICIENT_MEMORY;
6334 }
Janos Follathf08e2652019-06-13 09:05:41 +01006335
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006337 prf->seed_length = data_length;
6338 }
Janos Follathf08e2652019-06-13 09:05:41 +01006339
Gilles Peskine43f958b2020-12-13 14:55:14 +01006340 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006341
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006343}
6344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6346 const uint8_t *data,
6347 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006348{
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6350 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6351 return PSA_ERROR_BAD_STATE;
6352 }
Janos Follath81550542019-06-13 14:26:34 +01006353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 if (data_length != 0) {
6355 prf->secret = mbedtls_calloc(1, data_length);
6356 if (prf->secret == NULL) {
6357 return PSA_ERROR_INSUFFICIENT_MEMORY;
6358 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006361 prf->secret_length = data_length;
6362 }
Janos Follath81550542019-06-13 14:26:34 +01006363
Gilles Peskine43f958b2020-12-13 14:55:14 +01006364 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006365
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006367}
6368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6370 const uint8_t *data,
6371 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006372{
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6374 return PSA_ERROR_BAD_STATE;
6375 }
Janos Follath63028dd2019-06-13 09:15:47 +01006376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 if (data_length != 0) {
6378 prf->label = mbedtls_calloc(1, data_length);
6379 if (prf->label == NULL) {
6380 return PSA_ERROR_INSUFFICIENT_MEMORY;
6381 }
Janos Follath63028dd2019-06-13 09:15:47 +01006382
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006384 prf->label_length = data_length;
6385 }
Janos Follath63028dd2019-06-13 09:15:47 +01006386
Gilles Peskine43f958b2020-12-13 14:55:14 +01006387 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006390}
6391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6393 psa_key_derivation_step_t step,
6394 const uint8_t *data,
6395 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006396{
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006398 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006400 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006402 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006404 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006406 }
6407}
John Durkop07cc04a2020-11-16 22:08:34 -08006408#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6409 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6410
6411#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6412static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6413 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006414 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006416{
6417 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6419 4 + data_length + prf->other_secret_length :
6420 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006421
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6423 return PSA_ERROR_INVALID_ARGUMENT;
6424 }
John Durkop07cc04a2020-11-16 22:08:34 -08006425
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 uint8_t *pms = mbedtls_calloc(1, pms_len);
6427 if (pms == NULL) {
6428 return PSA_ERROR_INSUFFICIENT_MEMORY;
6429 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006430 uint8_t *cur = pms;
6431
6432 /* pure-PSK:
6433 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006434 *
6435 * The premaster secret is formed as follows: if the PSK is N octets
6436 * long, concatenate a uint16 with the value N, N zero octets, a second
6437 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006438 *
6439 * mixed-PSK:
6440 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6441 * follows: concatenate a uint16 with the length of the other secret,
6442 * the other secret itself, uint16 with the length of PSK, and the
6443 * PSK itself.
6444 * For details please check:
6445 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6446 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6447 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006448 */
6449
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6451 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6452 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6453 if (prf->other_secret_length != 0) {
6454 memcpy(cur, prf->other_secret, prf->other_secret_length);
6455 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006456 cur += prf->other_secret_length;
6457 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 } else {
6459 *cur++ = MBEDTLS_BYTE_1(data_length);
6460 *cur++ = MBEDTLS_BYTE_0(data_length);
6461 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006462 cur += data_length;
6463 }
6464
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 *cur++ = MBEDTLS_BYTE_1(data_length);
6466 *cur++ = MBEDTLS_BYTE_0(data_length);
6467 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006468 cur += data_length;
6469
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006470 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006471
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006472 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006474}
Janos Follath6660f0e2019-06-17 08:44:03 +01006475
Przemek Stekiele47201b2022-04-19 13:53:28 +02006476static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6477 psa_tls12_prf_key_derivation_t *prf,
6478 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006480{
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6482 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006483 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006484
6485 if (data_length != 0) {
6486 prf->other_secret = mbedtls_calloc(1, data_length);
6487 if (prf->other_secret == NULL) {
6488 return PSA_ERROR_INSUFFICIENT_MEMORY;
6489 }
6490
6491 memcpy(prf->other_secret, data, data_length);
6492 prf->other_secret_length = data_length;
6493 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006494 prf->other_secret_length = 0;
6495 }
6496
6497 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6498
Gilles Peskine449bd832023-01-11 14:50:10 +01006499 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006500}
6501
Janos Follath6660f0e2019-06-17 08:44:03 +01006502static psa_status_t psa_tls12_prf_psk_to_ms_input(
6503 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006504 psa_key_derivation_step_t step,
6505 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006507{
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006509 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 return psa_tls12_prf_psk_to_ms_set_key(prf,
6511 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006512 break;
6513 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6515 data,
6516 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006517 break;
6518 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006520 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006521
Przemek Stekiele47201b2022-04-19 13:53:28 +02006522 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006523}
John Durkop07cc04a2020-11-16 22:08:34 -08006524#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006525
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006526#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6527static psa_status_t psa_tls12_ecjpake_to_pms_input(
6528 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006529 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006530 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006532{
Gilles Peskine449bd832023-01-11 14:50:10 +01006533 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6534 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6535 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006536 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006537
6538 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 if (data[0] != 0x04) {
6540 return PSA_ERROR_INVALID_ARGUMENT;
6541 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006542
6543 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006544 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006545
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006547}
6548#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306549
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306550#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306551static psa_status_t psa_pbkdf2_set_input_cost(
6552 psa_pbkdf2_key_derivation_t *pbkdf2,
6553 psa_key_derivation_step_t step,
6554 uint64_t data)
6555{
6556 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6557 return PSA_ERROR_INVALID_ARGUMENT;
6558 }
6559
6560 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6561 return PSA_ERROR_BAD_STATE;
6562 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306563
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306564 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306565 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306566 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306567
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306568 if (data == 0) {
6569 return PSA_ERROR_INVALID_ARGUMENT;
6570 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306571
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306572 pbkdf2->input_cost = data;
6573 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6574
6575 return PSA_SUCCESS;
6576}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306577
6578static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6579 const uint8_t *data,
6580 size_t data_length)
6581{
Gilles Peskineead17662023-08-20 22:02:51 +02006582 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6583 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6584 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6585 /* Appending to existing salt. No state change. */
6586 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306587 return PSA_ERROR_BAD_STATE;
6588 }
6589
Gilles Peskineca57d782023-07-20 19:08:06 +02006590 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006591 /* Appending an empty string, nothing to do. */
6592 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306593 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306594
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306595 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6596 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306597 return PSA_ERROR_INSUFFICIENT_MEMORY;
6598 }
6599
Gilles Peskineead17662023-08-20 22:02:51 +02006600 if (pbkdf2->salt_length != 0) {
6601 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6602 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306603 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306604 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306605 mbedtls_free(pbkdf2->salt);
6606 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306607 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306608 return PSA_SUCCESS;
6609}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306610
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306611#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306612static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306613 const uint8_t *input,
6614 size_t input_len,
6615 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306616 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306617{
6618 psa_status_t status = PSA_SUCCESS;
6619 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6620 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306621 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306622 } else {
6623 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306624 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306625 }
6626 return status;
6627}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306628#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306629
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306630#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306631static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6632 size_t input_len,
6633 uint8_t *output,
6634 size_t *output_len)
6635{
6636 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306637 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306638 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306639 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306640 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6641 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6642 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306643 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306644 * mac_size as the driver function sets mac_output_length = mac_size
6645 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306646 status = psa_driver_wrapper_mac_compute(&attributes,
6647 zeros, sizeof(zeros),
6648 PSA_ALG_CMAC, input, input_len,
6649 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306650 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6651 128U,
6652 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306653 output_len);
6654 } else {
6655 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306656 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306657 }
6658 return status;
6659}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306660#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306661
6662static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306663 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306664 const uint8_t *data,
6665 size_t data_length)
6666{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306667 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306668 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6669 return PSA_ERROR_BAD_STATE;
6670 }
6671
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306672#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306673 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6674 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6675 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6676 pbkdf2->password,
6677 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306678 } else
6679#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6680#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6681 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306682 status = psa_pbkdf2_cmac_set_password(data, data_length,
6683 pbkdf2->password,
6684 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306685 } else
6686#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6687 {
6688 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306689 }
6690
6691 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6692
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306693 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306694}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306695
6696static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306697 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306698 psa_key_derivation_step_t step,
6699 const uint8_t *data,
6700 size_t data_length)
6701{
6702 switch (step) {
6703 case PSA_KEY_DERIVATION_INPUT_SALT:
6704 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6705 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306706 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306707 default:
6708 return PSA_ERROR_INVALID_ARGUMENT;
6709 }
6710}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306711#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306712
Gilles Peskineb8965192019-09-24 16:21:10 +02006713/** Check whether the given key type is acceptable for the given
6714 * input step of a key derivation.
6715 *
6716 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6717 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6718 * Both secret and non-secret inputs can alternatively have the type
6719 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6720 * that the input was passed as a buffer rather than via a key object.
6721 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006722static int psa_key_derivation_check_input_type(
6723 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006724 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006725{
Gilles Peskine449bd832023-01-11 14:50:10 +01006726 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006727 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 if (key_type == PSA_KEY_TYPE_DERIVE) {
6729 return PSA_SUCCESS;
6730 }
6731 if (key_type == PSA_KEY_TYPE_NONE) {
6732 return PSA_SUCCESS;
6733 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006734 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006735 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006736 if (key_type == PSA_KEY_TYPE_DERIVE) {
6737 return PSA_SUCCESS;
6738 }
6739 if (key_type == PSA_KEY_TYPE_NONE) {
6740 return PSA_SUCCESS;
6741 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006742 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006743 case PSA_KEY_DERIVATION_INPUT_LABEL:
6744 case PSA_KEY_DERIVATION_INPUT_SALT:
6745 case PSA_KEY_DERIVATION_INPUT_INFO:
6746 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006747 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6748 return PSA_SUCCESS;
6749 }
6750 if (key_type == PSA_KEY_TYPE_NONE) {
6751 return PSA_SUCCESS;
6752 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006753 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306754 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6755 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6756 return PSA_SUCCESS;
6757 }
6758 if (key_type == PSA_KEY_TYPE_DERIVE) {
6759 return PSA_SUCCESS;
6760 }
6761 if (key_type == PSA_KEY_TYPE_NONE) {
6762 return PSA_SUCCESS;
6763 }
6764 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006765 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006767}
6768
Janos Follathb80a94e2019-06-12 15:54:46 +01006769static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006770 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006771 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006772 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006773 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006775{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006776 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006777 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006778
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 status = psa_key_derivation_check_input_type(step, key_type);
6780 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006781 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006782 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006783
Przemek Stekiel69c46792022-06-10 12:59:51 +02006784#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6786 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6787 step, data, data_length);
6788 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006789#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006790#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6792 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6793 step, data, data_length);
6794 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006795#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6796#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6798 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6799 step, data, data_length);
6800 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006801#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006802#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006803 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006804 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6806 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006807#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306808#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306809 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306810 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6811 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306812 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306813#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006814 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006815 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006816 (void) data;
6817 (void) data_length;
6818 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006820 }
6821
Gilles Peskine224b0d62019-09-23 18:13:17 +02006822exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 if (status != PSA_SUCCESS) {
6824 psa_key_derivation_abort(operation);
6825 }
6826 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006827}
6828
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306829static psa_status_t psa_key_derivation_input_integer_internal(
6830 psa_key_derivation_operation_t *operation,
6831 psa_key_derivation_step_t step,
6832 uint64_t value)
6833{
6834 psa_status_t status;
6835 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6836
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306837#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306838 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306839 status = psa_pbkdf2_set_input_cost(
6840 &operation->ctx.pbkdf2, step, value);
6841 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306842#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306843 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306844 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306845 (void) value;
6846 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306847 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306848 }
6849
6850 if (status != PSA_SUCCESS) {
6851 psa_key_derivation_abort(operation);
6852 }
6853 return status;
6854}
6855
Janos Follath51f4a0f2019-06-14 11:35:55 +01006856psa_status_t psa_key_derivation_input_bytes(
6857 psa_key_derivation_operation_t *operation,
6858 psa_key_derivation_step_t step,
6859 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006861{
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 return psa_key_derivation_input_internal(operation, step,
6863 PSA_KEY_TYPE_NONE,
6864 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006865}
6866
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306867psa_status_t psa_key_derivation_input_integer(
6868 psa_key_derivation_operation_t *operation,
6869 psa_key_derivation_step_t step,
6870 uint64_t value)
6871{
6872 return psa_key_derivation_input_integer_internal(operation, step, value);
6873}
6874
Janos Follath51f4a0f2019-06-14 11:35:55 +01006875psa_status_t psa_key_derivation_input_key(
6876 psa_key_derivation_operation_t *operation,
6877 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006878 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006879{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006880 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006881 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006882 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006883
Ronald Cron5c522922020-11-14 16:35:34 +01006884 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6886 if (status != PSA_SUCCESS) {
6887 psa_key_derivation_abort(operation);
6888 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006889 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006890
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306891 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6892 * permission to output to a key object. */
6893 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6894 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006895 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006897
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 status = psa_key_derivation_input_internal(operation,
6899 step, slot->attr.type,
6900 slot->key.data,
6901 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006902
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006904
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006906}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006907
6908
6909
6910/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006911/* Key agreement */
6912/****************************************************************/
6913
Gilles Peskine449bd832023-01-11 14:50:10 +01006914psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6915 const uint8_t *key_buffer,
6916 size_t key_buffer_size,
6917 psa_algorithm_t alg,
6918 const uint8_t *peer_key,
6919 size_t peer_key_length,
6920 uint8_t *shared_secret,
6921 size_t shared_secret_size,
6922 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006923{
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006925#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006926 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6928 key_buffer_size, alg,
6929 peer_key, peer_key_length,
6930 shared_secret,
6931 shared_secret_size,
6932 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006933#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006934
6935#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6936 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006937 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006938 peer_key,
6939 peer_key_length,
6940 key_buffer,
6941 key_buffer_size,
6942 shared_secret,
6943 shared_secret_size,
6944 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006945#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6946
Gilles Peskine01d718c2018-09-18 12:01:02 +02006947 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006948 (void) attributes;
6949 (void) key_buffer;
6950 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006951 (void) peer_key;
6952 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006953 (void) shared_secret;
6954 (void) shared_secret_size;
6955 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006957 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006958}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006959
Aditya Deshpande17845b82022-10-13 17:21:01 +01006960/** Internal function for raw key agreement
6961 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006962 * to the driver's implementation if a driver is present.
6963 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006964 * (psa_key_agreement_raw_builtin).
6965 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006966static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6967 psa_key_slot_t *private_key,
6968 const uint8_t *peer_key,
6969 size_t peer_key_length,
6970 uint8_t *shared_secret,
6971 size_t shared_secret_size,
6972 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006973{
Gilles Peskine449bd832023-01-11 14:50:10 +01006974 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6975 return PSA_ERROR_NOT_SUPPORTED;
6976 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006977
6978 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006980 };
6981
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 return psa_driver_wrapper_key_agreement(&attributes,
6983 private_key->key.data,
6984 private_key->key.bytes, alg,
6985 peer_key, peer_key_length,
6986 shared_secret,
6987 shared_secret_size,
6988 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006989}
6990
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006991/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006992 * to potentially free embedded data structures and wipe confidential data.
6993 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006994static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6995 psa_key_derivation_step_t step,
6996 psa_key_slot_t *private_key,
6997 const uint8_t *peer_key,
6998 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006999{
7000 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007001 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007002 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007004
7005 /* Step 1: run the secret agreement algorithm to generate the shared
7006 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 status = psa_key_agreement_raw_internal(ka_alg,
7008 private_key,
7009 peer_key, peer_key_length,
7010 shared_secret,
7011 sizeof(shared_secret),
7012 &shared_secret_length);
7013 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007014 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007015 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007016
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007017 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007018 * the shared secret. A shared secret is permitted wherever a key
7019 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007020 status = psa_key_derivation_input_internal(operation, step,
7021 PSA_KEY_TYPE_DERIVE,
7022 shared_secret,
7023 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007024exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7026 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007027}
7028
Gilles Peskine449bd832023-01-11 14:50:10 +01007029psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7030 psa_key_derivation_step_t step,
7031 mbedtls_svc_key_id_t private_key,
7032 const uint8_t *peer_key,
7033 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007034{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007035 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007036 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007037 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007038
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7040 return PSA_ERROR_INVALID_ARGUMENT;
7041 }
Ronald Cron5c522922020-11-14 16:35:34 +01007042 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007043 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7044 if (status != PSA_SUCCESS) {
7045 return status;
7046 }
7047 status = psa_key_agreement_internal(operation, step,
7048 slot,
7049 peer_key, peer_key_length);
7050 if (status != PSA_SUCCESS) {
7051 psa_key_derivation_abort(operation);
7052 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007053 /* If a private key has been added as SECRET, we allow the derived
7054 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007055 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007056 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007058 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007059
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007061
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007063}
7064
Gilles Peskine449bd832023-01-11 14:50:10 +01007065psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7066 mbedtls_svc_key_id_t private_key,
7067 const uint8_t *peer_key,
7068 size_t peer_key_length,
7069 uint8_t *output,
7070 size_t output_size,
7071 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007072{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007073 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007074 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007075 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007076 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007077
Gilles Peskine449bd832023-01-11 14:50:10 +01007078 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007079 status = PSA_ERROR_INVALID_ARGUMENT;
7080 goto exit;
7081 }
Ronald Cron5c522922020-11-14 16:35:34 +01007082 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7084 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007085 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007087
Gilles Peskine3e561302022-04-14 00:17:15 +02007088 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7089 * for the output size. The PSA specification only guarantees that this
7090 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7091 * but it might be nice to allow smaller buffers if the output fits.
7092 * At the time of writing this comment, with only ECDH implemented,
7093 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7094 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7095 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007096 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7098 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007099 status = PSA_ERROR_BUFFER_TOO_SMALL;
7100 goto exit;
7101 }
7102
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 status = psa_key_agreement_raw_internal(alg, slot,
7104 peer_key, peer_key_length,
7105 output, output_size,
7106 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007107
7108exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007110 /* If an error happens and is not handled properly, the output
7111 * may be used as a key to protect sensitive data. Arrange for such
7112 * a key to be random, which is likely to result in decryption or
7113 * verification errors. This is better than filling the buffer with
7114 * some constant data such as zeros, which would result in the data
7115 * being protected with a reproducible, easily knowable key.
7116 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007118 *output_length = output_size;
7119 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007120
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007122
Gilles Peskine449bd832023-01-11 14:50:10 +01007123 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007124}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007125
7126
Gilles Peskine30524eb2020-11-13 17:02:26 +01007127
Gilles Peskine86a440b2018-11-12 18:39:40 +01007128/****************************************************************/
7129/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007130/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007131
Gilles Peskine672a7712023-04-28 21:00:28 +02007132#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7133#include "entropy_poll.h"
7134#endif
7135
Gilles Peskine30524eb2020-11-13 17:02:26 +01007136/** Initialize the PSA random generator.
7137 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007138static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007139{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007140#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007142#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7143
Gilles Peskine30524eb2020-11-13 17:02:26 +01007144 /* Set default configuration if
7145 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007147 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007148 }
7149 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007150 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007151 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007152
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007154#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7155 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7156 /* The PSA entropy injection feature depends on using NV seed as an entropy
7157 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 mbedtls_entropy_add_source(&rng->entropy,
7159 mbedtls_nv_seed_poll, NULL,
7160 MBEDTLS_ENTROPY_BLOCK_SIZE,
7161 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007162#endif
7163
Gilles Peskine449bd832023-01-11 14:50:10 +01007164 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007165#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007166}
7167
7168/** Deinitialize the PSA random generator.
7169 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007170static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007171{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007172#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007173 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007174#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti83e0de82023-11-24 12:13:05 +01007175 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7176 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007177#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007178}
7179
7180/** Seed the PSA random generator.
7181 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007182static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007183{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007184#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7185 /* Do nothing: the external RNG seeds itself. */
7186 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007187 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007188#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007189 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007190 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7191 drbg_seed, sizeof(drbg_seed) - 1);
7192 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007193#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007194}
7195
Gilles Peskine449bd832023-01-11 14:50:10 +01007196psa_status_t psa_generate_random(uint8_t *output,
7197 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007198{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007199 GUARD_MODULE_INITIALIZED;
7200
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007201#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7202
7203 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007204 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7205 output, output_size,
7206 &output_length);
7207 if (status != PSA_SUCCESS) {
7208 return status;
7209 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007210 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007211 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007212 if (output_length != output_size) {
7213 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7214 }
7215 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007216
7217#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7218
Gilles Peskine449bd832023-01-11 14:50:10 +01007219 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007220 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7222 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7223 output_size);
7224 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7225 output, request_size);
7226 if (ret != 0) {
7227 return mbedtls_to_psa_error(ret);
7228 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007229 output_size -= request_size;
7230 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007231 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007233#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007234}
7235
Gilles Peskine8814fc42020-12-14 15:33:44 +01007236/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007237 *
7238 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7239 * `psa_generate_random(...)`. The state parameter is ignored since the
7240 * PSA API doesn't support passing an explicit state.
7241 *
7242 * In the non-external case, psa_generate_random() calls an
7243 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7244 * and semantics as mbedtls_psa_get_random(). As an optimization,
7245 * instead of doing this back-and-forth between the PSA API and the
7246 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7247 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007248 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007249#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7250int mbedtls_psa_get_random(void *p_rng,
7251 unsigned char *output,
7252 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007253{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007254 /* This function takes a pointer to the RNG state because that's what
7255 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7256 * its own state internally and doesn't let the caller access that state.
7257 * So we just ignore the state parameter, and in practice we'll pass
7258 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007259 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 psa_status_t status = psa_generate_random(output, output_size);
7261 if (status == PSA_SUCCESS) {
7262 return 0;
7263 } else {
7264 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7265 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007266}
7267#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7268
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007269#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007270psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7271 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007272{
Gilles Peskine449bd832023-01-11 14:50:10 +01007273 if (global_data.initialized) {
7274 return PSA_ERROR_NOT_PERMITTED;
7275 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007276
Gilles Peskine449bd832023-01-11 14:50:10 +01007277 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7278 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7279 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7280 return PSA_ERROR_INVALID_ARGUMENT;
7281 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007282
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007284}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007285#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007286
Ronald Cron3772afe2021-02-08 16:10:05 +01007287/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007288 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007289 * \param type The key type
7290 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007291 *
7292 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007293 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007294 * \retval #PSA_ERROR_INVALID_ARGUMENT
7295 * The size in bits of the key is not valid.
7296 * \retval #PSA_ERROR_NOT_SUPPORTED
7297 * The type and/or the size in bits of the key or the combination of
7298 * the two is not supported.
7299 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007300static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007301 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007302{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007303 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007304
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 if (key_type_is_raw_bytes(type)) {
7306 status = psa_validate_unstructured_key_bit_size(type, bits);
7307 if (status != PSA_SUCCESS) {
7308 return status;
7309 }
7310 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007311#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007312 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7313 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7314 return PSA_ERROR_NOT_SUPPORTED;
7315 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007316 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007317 return PSA_ERROR_NOT_SUPPORTED;
7318 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007319
Ronald Cron01b2aba2020-10-05 09:42:02 +02007320 /* Accept only byte-aligned keys, for the same reasons as
7321 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 if (bits % 8 != 0) {
7323 return PSA_ERROR_NOT_SUPPORTED;
7324 }
7325 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007326#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007327
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007328#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007329 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007330 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007331 return PSA_SUCCESS;
7332 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007333#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007334
Valerio Settia55f0422023-07-10 15:34:41 +02007335#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007336 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007337 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007338 return PSA_ERROR_NOT_SUPPORTED;
7339 }
7340 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007341#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007342 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007343 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007344 }
7345
Gilles Peskine449bd832023-01-11 14:50:10 +01007346 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007347}
7348
Ronald Cron55ed0592020-10-05 10:30:40 +02007349psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007350 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007351 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007352{
7353 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007354 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007355
Gilles Peskine449bd832023-01-11 14:50:10 +01007356 if ((attributes->domain_parameters == NULL) &&
7357 (attributes->domain_parameters_size != 0)) {
7358 return PSA_ERROR_INVALID_ARGUMENT;
7359 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007360
Gilles Peskine449bd832023-01-11 14:50:10 +01007361 if (key_type_is_raw_bytes(type)) {
7362 status = psa_generate_random(key_buffer, key_buffer_size);
7363 if (status != PSA_SUCCESS) {
7364 return status;
7365 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007366
David Brown12ca5032021-02-11 11:02:00 -07007367#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007368 if (type == PSA_KEY_TYPE_DES) {
7369 psa_des_set_key_parity(key_buffer, key_buffer_size);
7370 }
David Brown8107e312021-02-12 08:08:46 -07007371#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007372 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007373
Valerio Setti76df8c12023-07-11 14:11:28 +02007374#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7376 return mbedtls_psa_rsa_generate_key(attributes,
7377 key_buffer,
7378 key_buffer_size,
7379 key_buffer_length);
7380 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007381#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007382
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007383#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007384 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7385 return mbedtls_psa_ecp_generate_key(attributes,
7386 key_buffer,
7387 key_buffer_size,
7388 key_buffer_length);
7389 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007390#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007391
Valerio Settia55f0422023-07-10 15:34:41 +02007392#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007393 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7394 return mbedtls_psa_ffdh_generate_key(attributes,
7395 key_buffer,
7396 key_buffer_size,
7397 key_buffer_length);
7398 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007399#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007400 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007401 (void) key_buffer_length;
7402 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007403 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007404
Gilles Peskine449bd832023-01-11 14:50:10 +01007405 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007406}
Darryl Green0c6575a2018-11-07 16:05:30 +00007407
Gilles Peskine449bd832023-01-11 14:50:10 +01007408psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7409 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007410{
7411 psa_status_t status;
7412 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007413 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007414 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007415
Ronald Cron81709fc2020-11-14 12:10:32 +01007416 *key = MBEDTLS_SVC_KEY_ID_INIT;
7417
Gilles Peskine0f84d622019-09-12 19:03:13 +02007418 /* Reject any attempt to create a zero-length key so that we don't
7419 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 if (psa_get_key_bits(attributes) == 0) {
7421 return PSA_ERROR_INVALID_ARGUMENT;
7422 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007423
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007424 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007425 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7426 return PSA_ERROR_INVALID_ARGUMENT;
7427 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007428
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7430 &slot, &driver);
7431 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007432 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 }
Gilles Peskine11792082019-08-06 18:36:36 +02007434
Ronald Cron977c2472020-10-13 08:32:21 +02007435 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307436 * storage ( thus not in the case of generating a key in a secure element
7437 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7438 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 if (slot->key.data == NULL) {
7440 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7441 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007442 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 attributes->core.type, attributes->core.bits);
7444 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007445 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007446 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007447
7448 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 attributes->core.type,
7450 attributes->core.bits);
7451 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007452 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 attributes, &key_buffer_size);
7454 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007455 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007456 }
Ronald Cron977c2472020-10-13 08:32:21 +02007457 }
Ronald Cron977c2472020-10-13 08:32:21 +02007458
Gilles Peskine449bd832023-01-11 14:50:10 +01007459 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7460 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007461 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007462 }
Ronald Cron977c2472020-10-13 08:32:21 +02007463 }
7464
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 status = psa_driver_wrapper_generate_key(attributes,
7466 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007467
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 if (status != PSA_SUCCESS) {
7469 psa_remove_key_data_from_memory(slot);
7470 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007471
Gilles Peskine11792082019-08-06 18:36:36 +02007472exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007473 if (status == PSA_SUCCESS) {
7474 status = psa_finish_key_creation(slot, driver, key);
7475 }
7476 if (status != PSA_SUCCESS) {
7477 psa_fail_key_creation(slot, driver);
7478 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007479
Gilles Peskine449bd832023-01-11 14:50:10 +01007480 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007481}
7482
Gilles Peskinee59236f2018-01-27 23:32:46 +01007483/****************************************************************/
7484/* Module setup */
7485/****************************************************************/
7486
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007487#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007488psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 void (* entropy_init)(mbedtls_entropy_context *ctx),
7490 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007491{
Gilles Peskine449bd832023-01-11 14:50:10 +01007492 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7493 return PSA_ERROR_BAD_STATE;
7494 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007495 global_data.rng.entropy_init = entropy_init;
7496 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007497 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007498}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007499#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007500
Gilles Peskine449bd832023-01-11 14:50:10 +01007501void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007502{
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 psa_wipe_all_key_slots();
Valerio Setti83e0de82023-11-24 12:13:05 +01007504 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7505 mbedtls_psa_random_free(&global_data.rng);
7506 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007507 /* Wipe all remaining data, including configuration.
7508 * In particular, this sets all state indicator to the value
7509 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007511
7512 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007513 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007514}
7515
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007516#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7517/** Recover a transaction that was interrupted by a power failure.
7518 *
7519 * This function is called during initialization, before psa_crypto_init()
7520 * returns. If this function returns a failure status, the initialization
7521 * fails.
7522 */
7523static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007524 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007525{
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007527 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7528 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007529 /* TODO - fall through to the failure case until this
7530 * is implemented.
7531 * https://github.com/ARMmbed/mbed-crypto/issues/218
7532 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007533 default:
7534 /* We found an unsupported transaction in the storage.
7535 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007537 }
7538}
7539#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7540
Gilles Peskine449bd832023-01-11 14:50:10 +01007541psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007542{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007543 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007544
Gilles Peskinec6b69072018-11-20 21:42:52 +01007545 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 if (global_data.initialized != 0) {
7547 return PSA_SUCCESS;
7548 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007549
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007550 /* Init drivers */
7551 status = psa_driver_wrapper_init();
7552 if (status != PSA_SUCCESS) {
7553 goto exit;
7554 }
7555 global_data.drivers_initialized = 1;
7556
Valerio Setti402cfba2023-11-13 10:24:32 +01007557 status = psa_initialize_key_slots();
7558 if (status != PSA_SUCCESS) {
7559 goto exit;
7560 }
7561
Gilles Peskine30524eb2020-11-13 17:02:26 +01007562 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007563 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007564 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 status = mbedtls_psa_random_seed(&global_data.rng);
7566 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007567 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007568 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007569 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007570
Gilles Peskine4b734222019-07-24 15:56:31 +02007571#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007572 status = psa_crypto_load_transaction();
7573 if (status == PSA_SUCCESS) {
7574 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7575 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007576 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 }
7578 status = psa_crypto_stop_transaction();
7579 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007580 /* There's no transaction to complete. It's all good. */
7581 status = PSA_SUCCESS;
7582 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007583#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007584
Gilles Peskinec6b69072018-11-20 21:42:52 +01007585 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007586 global_data.initialized = 1;
7587
7588exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 if (status != PSA_SUCCESS) {
7590 mbedtls_psa_crypto_free();
7591 }
7592 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007593}
7594
Yanray Wangffc3c482023-07-11 12:01:04 +08007595#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007596psa_status_t psa_crypto_driver_pake_get_password_len(
7597 const psa_crypto_driver_pake_inputs_t *inputs,
7598 size_t *password_len)
7599{
7600 if (inputs->password_len == 0) {
7601 return PSA_ERROR_BAD_STATE;
7602 }
7603
7604 *password_len = inputs->password_len;
7605
7606 return PSA_SUCCESS;
7607}
7608
7609psa_status_t psa_crypto_driver_pake_get_password(
7610 const psa_crypto_driver_pake_inputs_t *inputs,
7611 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7612{
7613 if (inputs->password_len == 0) {
7614 return PSA_ERROR_BAD_STATE;
7615 }
7616
7617 if (buffer_size < inputs->password_len) {
7618 return PSA_ERROR_BUFFER_TOO_SMALL;
7619 }
7620
7621 memcpy(buffer, inputs->password, inputs->password_len);
7622 *buffer_length = inputs->password_len;
7623
7624 return PSA_SUCCESS;
7625}
7626
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007627psa_status_t psa_crypto_driver_pake_get_user_len(
7628 const psa_crypto_driver_pake_inputs_t *inputs,
7629 size_t *user_len)
7630{
7631 if (inputs->user_len == 0) {
7632 return PSA_ERROR_BAD_STATE;
7633 }
7634
7635 *user_len = inputs->user_len;
7636
7637 return PSA_SUCCESS;
7638}
7639
7640psa_status_t psa_crypto_driver_pake_get_user(
7641 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007642 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007643{
7644 if (inputs->user_len == 0) {
7645 return PSA_ERROR_BAD_STATE;
7646 }
7647
Przemek Stekielc0e62502023-03-14 11:49:36 +01007648 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007649 return PSA_ERROR_BUFFER_TOO_SMALL;
7650 }
7651
Przemek Stekielc0e62502023-03-14 11:49:36 +01007652 memcpy(user_id, inputs->user, inputs->user_len);
7653 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007654
7655 return PSA_SUCCESS;
7656}
7657
7658psa_status_t psa_crypto_driver_pake_get_peer_len(
7659 const psa_crypto_driver_pake_inputs_t *inputs,
7660 size_t *peer_len)
7661{
7662 if (inputs->peer_len == 0) {
7663 return PSA_ERROR_BAD_STATE;
7664 }
7665
7666 *peer_len = inputs->peer_len;
7667
7668 return PSA_SUCCESS;
7669}
7670
7671psa_status_t psa_crypto_driver_pake_get_peer(
7672 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007673 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007674{
7675 if (inputs->peer_len == 0) {
7676 return PSA_ERROR_BAD_STATE;
7677 }
7678
Przemek Stekielc0e62502023-03-14 11:49:36 +01007679 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007680 return PSA_ERROR_BUFFER_TOO_SMALL;
7681 }
7682
Przemek Stekielc0e62502023-03-14 11:49:36 +01007683 memcpy(peer_id, inputs->peer, inputs->peer_len);
7684 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007685
7686 return PSA_SUCCESS;
7687}
7688
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007689psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7690 const psa_crypto_driver_pake_inputs_t *inputs,
7691 psa_pake_cipher_suite_t *cipher_suite)
7692{
7693 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7694 return PSA_ERROR_BAD_STATE;
7695 }
7696
7697 *cipher_suite = inputs->cipher_suite;
7698
7699 return PSA_SUCCESS;
7700}
7701
Neil Armstronga7d08c32022-06-01 18:21:20 +02007702psa_status_t psa_pake_setup(
7703 psa_pake_operation_t *operation,
7704 const psa_pake_cipher_suite_t *cipher_suite)
7705{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007706 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007707
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007708 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007709 status = PSA_ERROR_BAD_STATE;
7710 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007711 }
7712
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007713 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007714 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007715 status = PSA_ERROR_INVALID_ARGUMENT;
7716 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007717 }
7718
Przemek Stekiel51eac532022-12-07 11:04:51 +01007719 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7720
Przemek Stekiele12ed362022-12-21 12:54:46 +01007721 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007722 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7723 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007724 operation->data.inputs.cipher_suite = *cipher_suite;
7725
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007726#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007727 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007728 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007729 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007730
David Horstmann16f01512023-06-14 17:21:07 +01007731 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007732 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007733 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007734#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007735 {
7736 status = PSA_ERROR_NOT_SUPPORTED;
7737 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007738 }
7739
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007740 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7741
Przemek Stekiel51eac532022-12-07 11:04:51 +01007742 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007743exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007744 psa_pake_abort(operation);
7745 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007746}
7747
7748psa_status_t psa_pake_set_password_key(
7749 psa_pake_operation_t *operation,
7750 mbedtls_svc_key_id_t password)
7751{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007752 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007753 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7754 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007755 psa_key_attributes_t attributes;
7756 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007757
Przemek Stekiel51eac532022-12-07 11:04:51 +01007758 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007759 status = PSA_ERROR_BAD_STATE;
7760 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007761 }
7762
Przemek Stekiel6c764412022-11-22 14:05:12 +01007763 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7764 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007765 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007766 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007767 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007768 }
7769
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007770 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007771 .core = slot->attr
7772 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007773
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007774 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007775
Przemek Stekiel51eac532022-12-07 11:04:51 +01007776 if (type != PSA_KEY_TYPE_PASSWORD &&
7777 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7778 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007779 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007780 }
7781
Przemek Stekiel51eac532022-12-07 11:04:51 +01007782 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7783 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007784 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007785 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007786 }
7787
7788 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7789 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007790 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007791exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007792 if (status != PSA_SUCCESS) {
7793 psa_pake_abort(operation);
7794 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007795 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007796 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007797}
7798
7799psa_status_t psa_pake_set_user(
7800 psa_pake_operation_t *operation,
7801 const uint8_t *user_id,
7802 size_t user_id_len)
7803{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007804 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007805
7806 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007807 status = PSA_ERROR_BAD_STATE;
7808 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007809 }
7810
Przemek Stekiel51eac532022-12-07 11:04:51 +01007811 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007812 status = PSA_ERROR_INVALID_ARGUMENT;
7813 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007814 }
7815
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007816 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007817 status = PSA_ERROR_BAD_STATE;
7818 goto exit;
7819 }
7820
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007821 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7822 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007823 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7824 goto exit;
7825 }
7826
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007827 memcpy(operation->data.inputs.user, user_id, user_id_len);
7828 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007829
7830 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007831exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007832 psa_pake_abort(operation);
7833 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007834}
7835
7836psa_status_t psa_pake_set_peer(
7837 psa_pake_operation_t *operation,
7838 const uint8_t *peer_id,
7839 size_t peer_id_len)
7840{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007841 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007842
7843 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007844 status = PSA_ERROR_BAD_STATE;
7845 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007846 }
7847
Przemek Stekiel51eac532022-12-07 11:04:51 +01007848 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007849 status = PSA_ERROR_INVALID_ARGUMENT;
7850 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007851 }
7852
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007853 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007854 status = PSA_ERROR_BAD_STATE;
7855 goto exit;
7856 }
7857
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007858 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7859 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007860 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7861 goto exit;
7862 }
7863
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007864 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7865 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007866
7867 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007868exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007869 psa_pake_abort(operation);
7870 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007871}
7872
7873psa_status_t psa_pake_set_role(
7874 psa_pake_operation_t *operation,
7875 psa_pake_role_t role)
7876{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007877 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007878
Przemek Stekiel51eac532022-12-07 11:04:51 +01007879 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007880 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007881 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007882 }
7883
Przemek Stekiel09104b82023-03-06 14:11:51 +01007884 switch (operation->alg) {
7885#if defined(PSA_WANT_ALG_JPAKE)
7886 case PSA_ALG_JPAKE:
7887 if (role == PSA_PAKE_ROLE_NONE) {
7888 return PSA_SUCCESS;
7889 }
7890 status = PSA_ERROR_INVALID_ARGUMENT;
7891 break;
7892#endif
7893 default:
7894 (void) role;
7895 status = PSA_ERROR_NOT_SUPPORTED;
7896 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007897 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007898exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007899 psa_pake_abort(operation);
7900 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007901}
7902
David Horstmanne7f21e62023-05-12 18:17:21 +01007903/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007904#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007905static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007906 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007907{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007908 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007909 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007910 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007911
David Horstmann024e5c52023-06-14 15:48:21 +01007912 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007913 is_x1 = (stage->outputs < 1);
7914 } else {
7915 is_x1 = (stage->inputs < 1);
7916 }
7917
David Horstmann74a3d8c2023-06-14 18:28:19 +01007918 key_share_step = is_x1 ?
7919 PSA_JPAKE_X1_STEP_KEY_SHARE :
7920 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007921 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007922 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7923 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7924 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7925 } else {
7926 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007927 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01007928 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01007929}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007930#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007931
Przemek Stekiel2797d372022-12-22 11:19:22 +01007932static psa_status_t psa_pake_complete_inputs(
7933 psa_pake_operation_t *operation)
7934{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007935 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007936 /* Create copy of the inputs on stack as inputs share memory
7937 with the driver context which will be setup by the driver. */
7938 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007939
Przemek Stekielfde11282023-03-13 16:06:09 +01007940 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007941 return PSA_ERROR_BAD_STATE;
7942 }
7943
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007944 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007945 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7946 return PSA_ERROR_BAD_STATE;
7947 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007948 }
7949
Przemek Stekiel18620a32023-01-17 16:34:52 +01007950 /* Clear driver context */
7951 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7952
7953 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007954
7955 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01007956 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007957
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007958 /* User and peer are translated to role. */
7959 mbedtls_free(inputs.user);
7960 mbedtls_free(inputs.peer);
7961
Przemek Stekiel2797d372022-12-22 11:19:22 +01007962 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007963#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007964 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007965 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007966 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007967#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007968 {
7969 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007970 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007971 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007972 return status;
7973}
7974
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007975#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01007976static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01007977 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01007978 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01007979 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007980{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007981 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7982 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7983 step != PSA_PAKE_STEP_ZK_PROOF) {
7984 return PSA_ERROR_INVALID_ARGUMENT;
7985 }
7986
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007987 psa_jpake_computation_stage_t *computation_stage =
7988 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007989
David Horstmann5da95602023-06-08 15:37:12 +01007990 if (computation_stage->round != PSA_JPAKE_FIRST &&
7991 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007992 return PSA_ERROR_BAD_STATE;
7993 }
7994
David Horstmanne7f21e62023-05-12 18:17:21 +01007995 /* Check that the step we are given is the one we were expecting */
7996 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007997 return PSA_ERROR_BAD_STATE;
7998 }
7999
David Horstmanne7f21e62023-05-12 18:17:21 +01008000 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8001 computation_stage->inputs == 0 &&
8002 computation_stage->outputs == 0) {
8003 /* Start of the round, so function decides whether we are inputting
8004 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008005 computation_stage->io_mode = io_mode;
8006 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008007 /* Middle of the round so the mode we are in must match the function
8008 * called by the user */
8009 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008010 }
8011
8012 return PSA_SUCCESS;
8013}
8014
David Horstmanne7f21e62023-05-12 18:17:21 +01008015static psa_status_t psa_jpake_epilogue(
8016 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008017 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008018{
David Horstmanne7f21e62023-05-12 18:17:21 +01008019 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008020 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008021
David Horstmanne7f21e62023-05-12 18:17:21 +01008022 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8023 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008024 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008025 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008026 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008027 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008028 }
8029 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008030 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008031 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008032 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008033 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008034 }
8035 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008036 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8037 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008038 /* End of a round, move to the next round */
8039 stage->inputs = 0;
8040 stage->outputs = 0;
8041 stage->round++;
8042 }
8043 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008044 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008045 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008046 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008047 return PSA_SUCCESS;
8048}
David Horstmanne7f21e62023-05-12 18:17:21 +01008049
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008050#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008051
Neil Armstronga7d08c32022-06-01 18:21:20 +02008052psa_status_t psa_pake_output(
8053 psa_pake_operation_t *operation,
8054 psa_pake_step_t step,
8055 uint8_t *output,
8056 size_t output_size,
8057 size_t *output_length)
8058{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008059 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008060 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008061 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008062
8063 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008064 status = psa_pake_complete_inputs(operation);
8065 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008066 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008067 }
8068 }
8069
8070 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008071 status = PSA_ERROR_BAD_STATE;
8072 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008073 }
8074
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008075 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008076 status = PSA_ERROR_INVALID_ARGUMENT;
8077 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008078 }
8079
Przemek Stekiele12ed362022-12-21 12:54:46 +01008080 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008081#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008082 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008083 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008084 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008085 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008086 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008087 driver_step = convert_jpake_computation_stage_to_driver_step(
8088 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008089 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008090#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008091 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008092 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008093 status = PSA_ERROR_NOT_SUPPORTED;
8094 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008095 }
8096
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008097 status = psa_driver_wrapper_pake_output(operation, driver_step,
8098 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008099
8100 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008101 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008102 }
8103
8104 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008105#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008106 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008107 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008108 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008109 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008110 }
8111 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008112#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008113 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008114 status = PSA_ERROR_NOT_SUPPORTED;
8115 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008116 }
8117
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008118 return PSA_SUCCESS;
8119exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008120 psa_pake_abort(operation);
8121 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008122}
8123
8124psa_status_t psa_pake_input(
8125 psa_pake_operation_t *operation,
8126 psa_pake_step_t step,
8127 const uint8_t *input,
8128 size_t input_length)
8129{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008130 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008131 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008132 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8133 operation->primitive,
8134 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008135
8136 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008137 status = psa_pake_complete_inputs(operation);
8138 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008139 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008140 }
8141 }
8142
8143 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008144 status = PSA_ERROR_BAD_STATE;
8145 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008146 }
8147
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008148 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008149 status = PSA_ERROR_INVALID_ARGUMENT;
8150 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008151 }
8152
Przemek Stekiele12ed362022-12-21 12:54:46 +01008153 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008154#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008155 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008156 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008157 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008158 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008159 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008160 driver_step = convert_jpake_computation_stage_to_driver_step(
8161 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008162 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008163#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008164 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008165 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008166 status = PSA_ERROR_NOT_SUPPORTED;
8167 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008168 }
8169
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008170 status = psa_driver_wrapper_pake_input(operation, driver_step,
8171 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008172
8173 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008174 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008175 }
8176
8177 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008178#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008179 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008180 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008181 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008182 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008183 }
8184 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008185#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008186 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008187 status = PSA_ERROR_NOT_SUPPORTED;
8188 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008189 }
8190
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008191 return PSA_SUCCESS;
8192exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008193 psa_pake_abort(operation);
8194 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008195}
8196
8197psa_status_t psa_pake_get_implicit_key(
8198 psa_pake_operation_t *operation,
8199 psa_key_derivation_operation_t *output)
8200{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008201 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008202 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008203 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008204 size_t shared_key_len = 0;
8205
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008206 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008207 status = PSA_ERROR_BAD_STATE;
8208 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008209 }
8210
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008211#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008212 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008213 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008214 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008215 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008216 status = PSA_ERROR_BAD_STATE;
8217 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008218 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008219 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008220#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008221 {
8222 status = PSA_ERROR_NOT_SUPPORTED;
8223 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008224 }
8225
Przemek Stekiel0c781802022-11-29 14:53:13 +01008226 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8227 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008228 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008229 &shared_key_len);
8230
8231 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008232 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008233 }
8234
8235 status = psa_key_derivation_input_bytes(output,
8236 PSA_KEY_DERIVATION_INPUT_SECRET,
8237 shared_key,
8238 shared_key_len);
8239
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008240 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008241exit:
8242 abort_status = psa_pake_abort(operation);
8243 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008244}
8245
8246psa_status_t psa_pake_abort(
8247 psa_pake_operation_t *operation)
8248{
Przemek Stekield69dca92023-01-31 19:59:20 +01008249 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008250
Przemek Stekield69dca92023-01-31 19:59:20 +01008251 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008252 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008253 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008254
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008255 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8256 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008257 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008258 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008259 }
8260 if (operation->data.inputs.user != NULL) {
8261 mbedtls_free(operation->data.inputs.user);
8262 }
8263 if (operation->data.inputs.peer != NULL) {
8264 mbedtls_free(operation->data.inputs.peer);
8265 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008266 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008267 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008268
Przemek Stekield69dca92023-01-31 19:59:20 +01008269 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008270}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008271#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008272
Gilles Peskinee59236f2018-01-27 23:32:46 +01008273#endif /* MBEDTLS_PSA_CRYPTO_C */