blob: ca01e7649155c4bc8f38f828416560717b6a9b6f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinee59236f2018-01-27 23:32:46 +01007 */
8
Gilles Peskinedb09ef62020-06-03 01:43:33 +02009#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010010#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010011
12#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030013
John Durkop07cc04a2020-11-16 22:08:34 -080014#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
15#include "check_crypto_config.h"
16#endif
17
Gilles Peskinee59236f2018-01-27 23:32:46 +010018#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010019#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010020
Ronald Crond6d28882020-12-14 14:56:02 +010021#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010022#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010023#include "psa_crypto_invasive.h"
Xiaokang Qiane9c39c42023-08-09 08:50:19 +000024#include "psa_crypto_driver_wrappers.h"
Xiaokang Qianfe9666b2023-09-11 10:36:20 +000025#include "psa_crypto_driver_wrappers_no_static.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010027#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010028#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010029#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010031#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020033#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020034#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskineb2b64d32020-12-14 16:43:58 +010040#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010041
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include <stdlib.h>
43#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010046#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020047#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000048#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020049#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010050#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020051#include "mbedtls/chacha20.h"
52#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/cipher.h"
54#include "mbedtls/ccm.h"
55#include "mbedtls/cmac.h"
Dave Rodgman78701152023-08-29 14:20:18 +010056#include "mbedtls/constant_time.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020058#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010059#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010060#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/error.h"
62#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/md5.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010064#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000065#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010066#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000067#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/sha1.h"
71#include "mbedtls/sha256.h"
72#include "mbedtls/sha512.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010073#include "mbedtls/psa_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020075#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
76 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
77 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020078#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020079#endif
80
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010081/****************************************************************/
82/* Global data, support functions and library management */
83/****************************************************************/
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020086{
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020088}
89
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010090/* Values for psa_global_data_t::rng_state */
91#define RNG_NOT_INITIALIZED 0
92#define RNG_INITIALIZED 1
93#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010094
Gilles Peskine449bd832023-01-11 14:50:10 +010095typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +010096 uint8_t initialized;
97 uint8_t rng_state;
98 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +010099 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100100} psa_global_data_t;
101
102static psa_global_data_t global_data;
103
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100104#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
105mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
106 &global_data.rng.drbg;
107#endif
108
itayzafrir0adf0fc2018-09-06 16:24:41 +0300109#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (global_data.initialized == 0) \
111 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300112
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100113int psa_can_do_hash(psa_algorithm_t hash_alg)
114{
115 (void) hash_alg;
116 return global_data.drivers_initialized;
117}
Valerio Settic6f004f2023-12-12 11:27:36 +0100118
Valerio Setti1fff4f22023-12-28 14:19:34 +0100119int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
Valerio Settic6f004f2023-12-12 11:27:36 +0100120{
Valerio Setti1fff4f22023-12-28 14:19:34 +0100121 (void) key_type;
Valerio Settic6f004f2023-12-12 11:27:36 +0100122 (void) cipher_alg;
123 return global_data.drivers_initialized;
124}
125
126
Valerio Settia55f0422023-07-10 15:34:41 +0200127#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200128 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200129 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200130static int psa_is_dh_key_size_valid(size_t bits)
131{
Valerio Setti504a1022024-01-17 15:19:30 +0100132 switch (bits) {
133#if defined(PSA_WANT_DH_RFC7919_2048)
134 case 2048:
135 return 1;
136#endif /* PSA_WANT_DH_RFC7919_2048 */
137#if defined(PSA_WANT_DH_RFC7919_3072)
138 case 3072:
139 return 1;
140#endif /* PSA_WANT_DH_RFC7919_3072 */
141#if defined(PSA_WANT_DH_RFC7919_4096)
142 case 4096:
143 return 1;
144#endif /* PSA_WANT_DH_RFC7919_4096 */
145#if defined(PSA_WANT_DH_RFC7919_6144)
146 case 6144:
147 return 1;
148#endif /* PSA_WANT_DH_RFC7919_6144 */
149#if defined(PSA_WANT_DH_RFC7919_8192)
150 case 8192:
151 return 1;
152#endif /* PSA_WANT_DH_RFC7919_8192 */
153 default:
154 return 0;
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200155 }
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200156}
Valerio Settia55f0422023-07-10 15:34:41 +0200157#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200158 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200159 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100162{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100163 /* Mbed TLS error codes can combine a high-level error code and a
164 * low-level error code. The low-level error usually reflects the
165 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 int low_level_ret = -(-ret & 0x007f);
167 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100168 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100170
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100171#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100172 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
173 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100175 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
176 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100177#endif
178
179#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200180 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
181 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
182 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
183 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
184 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200186 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200188 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100190#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200191
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100192#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000193 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100194 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100196#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100197
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100198#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100203#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100204
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100205#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200206 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100208#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200209
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100210#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200211 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200213 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100215#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200216
Dave Rodgman509b5672023-08-16 19:26:23 +0100217#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100218 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100222 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100224 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100226 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100228 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100232#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
235 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100236 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
237 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100240 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
241 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100243 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100245#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100246
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100247#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100248 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100250#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100251
Gilles Peskinee59236f2018-01-27 23:32:46 +0100252 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
253 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
254 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100256
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100257#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100258 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200260 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100262 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100264#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100265
Gilles Peskine82e57d12020-11-13 21:31:17 +0100266#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100268 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
269 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100270 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100272 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
273 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100275 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100277#endif
278
Dave Rodgmandea266f2023-08-31 11:52:43 +0100279#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100282 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100286#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100287 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100289#endif
290#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100291
Dave Rodgman509b5672023-08-16 19:26:23 +0100292#if defined(MBEDTLS_BIGNUM_C)
293#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100294 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100296#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100297 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100299 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100301 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100303 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100305 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100307 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100309 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100311#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100312
Dave Rodgman509b5672023-08-16 19:26:23 +0100313#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100314 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100316 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
317 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100319#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
320 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100321 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100323#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100324 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
325 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100327 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100329 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
330 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100332 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100334 case MBEDTLS_ERR_PK_INVALID_ALG:
335 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
336 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100338 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200340 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100342#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100343
Gilles Peskineff2d2002019-05-06 15:26:23 +0200344 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200346 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200348
Dave Rodgman509b5672023-08-16 19:26:23 +0100349#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100350 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100352 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100354 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100356 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100358 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
359 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100361 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100363 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100365 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100367#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200368
Dave Rodgman1dab4452023-09-01 09:59:51 +0100369#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300370 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300371 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300373 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300375 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300377 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
378 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300380 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100382 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100384
Dave Rodgman509b5672023-08-16 19:26:23 +0100385#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000386 case MBEDTLS_ERR_ECP_IN_PROGRESS:
387 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100388#endif
389#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000390
Janos Follatha13b9052019-11-22 12:48:59 +0000391 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300393
Gilles Peskinee59236f2018-01-27 23:32:46 +0100394 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100396 }
397}
398
Paul Elliottdc42ca82023-02-24 18:11:59 +0000399/**
400 * \brief For output buffers which contain "tags"
401 * (outputs that may be checked for validity like
402 * hashes, MACs and signatures), fill the unused
403 * part of the output buffer (the whole buffer on
404 * error, the trailing part on success) with
405 * something that isn't a valid tag (barring an
406 * attack on the tag and deliberately-crafted
407 * input), in case the caller doesn't check the
408 * return status properly.
409 *
410 * \param output_buffer Pointer to buffer to wipe. May not be NULL
411 * unless \p output_buffer_size is zero.
412 * \param status Status of function called to generate
413 * output_buffer originally
414 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
415 * could be NULL.
416 * \param output_buffer_length Length of data written to output_buffer, must be
417 * less than \p output_buffer_size
418 */
419static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000420 size_t output_buffer_size, size_t output_buffer_length)
421{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000422 size_t offset = 0;
423
424 if (output_buffer_size == 0) {
425 /* If output_buffer_size is 0 then we have nothing to do. We must not
426 call memset because output_buffer may be NULL in this case */
427 return;
428 }
429
430 if (status == PSA_SUCCESS) {
431 offset = output_buffer_length;
432 }
433
434 memset(output_buffer + offset, '!', output_buffer_size - offset);
435}
436
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
439 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200440{
441 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200443 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200444 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200445 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200446 case PSA_KEY_TYPE_PASSWORD:
447 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200448 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100449#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200450 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (bits != 128 && bits != 192 && bits != 256) {
452 return PSA_ERROR_INVALID_ARGUMENT;
453 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200454 break;
455#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200456#if defined(PSA_WANT_KEY_TYPE_ARIA)
457 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if (bits != 128 && bits != 192 && bits != 256) {
459 return PSA_ERROR_INVALID_ARGUMENT;
460 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200461 break;
462#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100463#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200464 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (bits != 128 && bits != 192 && bits != 256) {
466 return PSA_ERROR_INVALID_ARGUMENT;
467 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200468 break;
469#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100470#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200471 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (bits != 64 && bits != 128 && bits != 192) {
473 return PSA_ERROR_INVALID_ARGUMENT;
474 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200475 break;
476#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100477#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200478 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (bits != 256) {
480 return PSA_ERROR_INVALID_ARGUMENT;
481 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200482 break;
483#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200484 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200486 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (bits % 8 != 0) {
488 return PSA_ERROR_INVALID_ARGUMENT;
489 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492}
493
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100494/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100495 *
Steven Cooremancd640932021-03-08 12:00:27 +0100496 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
497 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100498 *
499 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100500 * \param[in] key_type The key type of the key to be used with the
501 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100502 *
503 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100504 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100505 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100506 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100507 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100508MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100509 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100511{
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (PSA_ALG_IS_HMAC(algorithm)) {
513 if (key_type == PSA_KEY_TYPE_HMAC) {
514 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100515 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100516 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
519 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
520 * key. */
521 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
522 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
523 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
524 * the block length (larger than 1) for block ciphers. */
525 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
526 return PSA_SUCCESS;
527 }
528 }
529 }
530
531 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100532}
533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
535 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200536{
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (slot->key.data != NULL) {
538 return PSA_ERROR_ALREADY_EXISTS;
539 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 slot->key.data = mbedtls_calloc(1, buffer_length);
542 if (slot->key.data == NULL) {
543 return PSA_ERROR_INSUFFICIENT_MEMORY;
544 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200545
Ronald Cronea0f8a62020-11-25 17:52:23 +0100546 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200548}
549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
551 const uint8_t *data,
552 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200553{
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 psa_status_t status = psa_allocate_buffer_to_slot(slot,
555 data_length);
556 if (status != PSA_SUCCESS) {
557 return status;
558 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 memcpy(slot->key.data, data, data_length);
561 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200562}
563
Ronald Crona0fe59f2020-11-29 11:14:53 +0100564psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100565 const psa_key_attributes_t *attributes,
566 const uint8_t *data, size_t data_length,
567 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100569{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100570 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
571 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100572
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200573 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (data_length == 0) {
575 return PSA_ERROR_NOT_SUPPORTED;
576 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (key_type_is_raw_bytes(type)) {
579 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
582 *bits);
583 if (status != PSA_SUCCESS) {
584 return status;
585 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200586
Ronald Crondd04d422020-11-28 15:14:42 +0100587 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100589 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return PSA_SUCCESS;
593 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200594#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200595 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100596 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200597 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Valerio Setti504a1022024-01-17 15:19:30 +0100598 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100599 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200600 return mbedtls_psa_ffdh_import_key(attributes,
601 data, data_length,
602 key_buffer, key_buffer_size,
603 key_buffer_length,
604 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100605 }
Valerio Settia55f0422023-07-10 15:34:41 +0200606#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200607 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200608#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
610 if (PSA_KEY_TYPE_IS_ECC(type)) {
611 return mbedtls_psa_ecp_import_key(attributes,
612 data, data_length,
613 key_buffer, key_buffer_size,
614 key_buffer_length,
615 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100616 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200617#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800618 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200619#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
620 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
622 if (PSA_KEY_TYPE_IS_RSA(type)) {
623 return mbedtls_psa_rsa_import_key(attributes,
624 data, data_length,
625 key_buffer, key_buffer_size,
626 key_buffer_length,
627 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200628 }
Valerio Settife478902023-07-25 12:27:19 +0200629#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
630 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800631 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100632 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100635}
636
Gilles Peskinef603c712019-01-19 13:40:11 +0100637/** Calculate the intersection of two algorithm usage policies.
638 *
639 * Return 0 (which allows no operation) on incompatibility.
640 */
641static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100642 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100643 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100645{
Gilles Peskine549ea862019-05-22 11:45:59 +0200646 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (alg1 == alg2) {
648 return alg1;
649 }
Steven Cooreman03488022021-02-18 12:07:20 +0100650 /* If the policies are from the same hash-and-sign family, check
651 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
653 PSA_ALG_IS_SIGN_HASH(alg2) &&
654 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
655 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
656 return alg2;
657 }
658 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
659 return alg1;
660 }
Steven Cooreman03488022021-02-18 12:07:20 +0100661 }
662 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100663 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100664 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
666 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
667 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
668 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
669 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100670 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100671
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100672 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
674 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
675 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
676 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100677 }
678 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
680 (alg1_len <= alg2_len)) {
681 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100682 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
684 (alg2_len <= alg1_len)) {
685 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100686 }
687 }
688 /* If the policies are from the same MAC family, check whether one
689 * of them is a minimum-MAC-length policy. Calculate the most
690 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
692 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
693 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100694 /* Validate the combination of key type and algorithm. Since the base
695 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
697 return 0;
698 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100699
Steven Cooreman31a876d2021-03-03 20:47:40 +0100700 /* Get the (exact or at-least) output lengths for both sides of the
701 * requested intersection. None of the currently supported algorithms
702 * have an output length dependent on the actual key size, so setting it
703 * to a bogus value of 0 is currently OK.
704 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100705 * Note that for at-least-this-length wildcard algorithms, the output
706 * length is set to the shortest allowed length, which allows us to
707 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
709 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100710 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100711
Steven Cooremana1d83222021-02-25 10:20:29 +0100712 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
714 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
715 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100716 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100717
718 /* If only one is an at-least-this-length policy, the intersection would
719 * be the other (fixed-length) policy as long as said fixed length is
720 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
722 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100723 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
725 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100726 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100727
Steven Cooremancd640932021-03-08 12:00:27 +0100728 /* If none of them are wildcards, check whether they define the same tag
729 * length. This is still possible here when one is default-length and
730 * the other specific-length. Ensure to always return the
731 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (alg1_len == alg2_len) {
733 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
734 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100735 }
736 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100738}
739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740static int psa_key_algorithm_permits(psa_key_type_t key_type,
741 psa_algorithm_t policy_alg,
742 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200743{
Gilles Peskine549ea862019-05-22 11:45:59 +0200744 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (requested_alg == policy_alg) {
746 return 1;
747 }
Steven Cooreman03488022021-02-18 12:07:20 +0100748 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
749 * and requested_alg is the same hash-and-sign family with any hash,
750 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
752 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
753 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
754 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100755 }
756 /* If policy_alg is a wildcard AEAD algorithm of the same base as
757 * the requested algorithm, check the requested tag length to be
758 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 if (PSA_ALG_IS_AEAD(policy_alg) &&
760 PSA_ALG_IS_AEAD(requested_alg) &&
761 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
762 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
763 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
764 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
765 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100766 }
Steven Cooremancd640932021-03-08 12:00:27 +0100767 /* If policy_alg is a MAC algorithm of the same base as the requested
768 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (PSA_ALG_IS_MAC(policy_alg) &&
770 PSA_ALG_IS_MAC(requested_alg) &&
771 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
772 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100773 /* Validate the combination of key type and algorithm. Since the policy
774 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
776 return 0;
777 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100778
Steven Cooreman31a876d2021-03-03 20:47:40 +0100779 /* Get both the requested output length for the algorithm which is to be
780 * verified, and the default output length for the base algorithm.
781 * Note that none of the currently supported algorithms have an output
782 * length dependent on actual key size, so setting it to a bogus value
783 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100784 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100786 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 key_type, 0,
788 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100789
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100790 /* If the policy is default-length, only allow an algorithm with
791 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
793 return requested_output_length == default_output_length;
794 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100795
796 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100797 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
799 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
800 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100801 }
802
Steven Cooremancd640932021-03-08 12:00:27 +0100803 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
804 * check for the requested MAC length to be equal to or longer than the
805 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
807 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
808 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100809 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200810 }
Steven Cooremance48e852020-10-05 16:02:45 +0200811 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200812 * a key derivation with that key agreement should also be allowed. This
813 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
815 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
816 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
817 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200818 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100819 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200821}
822
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100823/** Test whether a policy permits an algorithm.
824 *
825 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100826 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100827 * \note This function requires providing the key type for which the policy is
828 * being validated, since some algorithm policy definitions (e.g. MAC)
829 * have different properties depending on what kind of cipher it is
830 * combined with.
831 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100832 * \retval PSA_SUCCESS When \p alg is a specific algorithm
833 * allowed by the \p policy.
834 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
835 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
836 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100837 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100838static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
839 psa_key_type_t key_type,
840 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841{
Steven Cooremand788fab2021-02-25 11:29:17 +0100842 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if (alg == 0) {
844 return PSA_ERROR_INVALID_ARGUMENT;
845 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100846
Steven Cooreman7e39f052021-02-23 14:18:32 +0100847 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (PSA_ALG_IS_WILDCARD(alg)) {
849 return PSA_ERROR_INVALID_ARGUMENT;
850 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
853 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
854 return PSA_SUCCESS;
855 } else {
856 return PSA_ERROR_NOT_PERMITTED;
857 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100858}
859
Gilles Peskinef603c712019-01-19 13:40:11 +0100860/** Restrict a key policy based on a constraint.
861 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100862 * \note This function requires providing the key type for which the policy is
863 * being restricted, since some algorithm policy definitions (e.g. MAC)
864 * have different properties depending on what kind of cipher it is
865 * combined with.
866 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100867 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100868 * \param[in,out] policy The policy to restrict.
869 * \param[in] constraint The policy constraint to apply.
870 *
871 * \retval #PSA_SUCCESS
872 * \c *policy contains the intersection of the original value of
873 * \c *policy and \c *constraint.
874 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100875 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100876 * \c *policy is unchanged.
877 */
878static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100879 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100880 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100882{
883 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 psa_key_policy_algorithm_intersection(key_type, policy->alg,
885 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200886 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
888 constraint->alg2);
889 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
890 return PSA_ERROR_INVALID_ARGUMENT;
891 }
892 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
893 return PSA_ERROR_INVALID_ARGUMENT;
894 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100895 policy->usage &= constraint->usage;
896 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200897 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100899}
900
Przemek Stekiel061f6942022-11-30 10:51:35 +0100901/** Get the description of a key given its identifier and policy constraints
902 * and lock it.
903 *
904 * The key must have allow all the usage flags set in \p usage. If \p alg is
905 * nonzero, the key must allow operations with this algorithm. If \p alg is
906 * zero, the algorithm is not checked.
907 *
908 * In case of a persistent key, the function loads the description of the key
909 * into a key slot if not already done.
910 *
Ryan Everett098c6652024-01-03 13:03:36 +0000911 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +0000912 * It is the responsibility of the caller to then unregister
913 * once they have finished reading the contents of the slot.
914 * The caller unregisters by calling psa_unregister_read() or
915 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
916 * if and only if the caller already holds the global key slot mutex
917 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
918 * the unregister with mutex lock and unlock operations.
Przemek Stekiel061f6942022-11-30 10:51:35 +0100919 */
920static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100921 mbedtls_svc_key_id_t key,
922 psa_key_slot_t **p_slot,
923 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100925{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100927 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 status = psa_get_and_lock_key_slot(key, p_slot);
930 if (status != PSA_SUCCESS) {
931 return status;
932 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200933 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100934
935 /* Enforce that usage policy for the key slot contains all the flags
936 * required by the usage parameter. There is one exception: public
937 * keys can always be exported, so we treat public key objects as
938 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100940 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100944 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200945 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100946 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100947
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800948 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (alg != 0) {
950 status = psa_key_policy_permits(&slot->attr.policy,
951 slot->attr.type,
952 alg);
953 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100954 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100956 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200959
960error:
961 *p_slot = NULL;
Ryan Everettfb792ca2024-01-31 13:40:05 +0000962 psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100965}
Darryl Green940d72c2018-07-13 13:18:51 +0100966
Ronald Cron5c522922020-11-14 16:35:34 +0100967/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200968 *
969 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200970 * available, as opposed to a key in a secure element and/or to be used
971 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200972 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200973 * This is a temporary function that may be used instead of
974 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
975 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200976 *
Ryan Everett098c6652024-01-03 13:03:36 +0000977 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +0000978 * It is the responsibility of the caller to then unregister
979 * once they have finished reading the contents of the slot.
980 * The caller unregisters by calling psa_unregister_read() or
981 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
982 * if and only if the caller already holds the global key slot mutex
983 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
984 * psa_unregister_read() with mutex lock and unlock operations.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200985 */
Ronald Cron5c522922020-11-14 16:35:34 +0100986static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
987 mbedtls_svc_key_id_t key,
988 psa_key_slot_t **p_slot,
989 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200991{
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
993 usage, alg);
994 if (status != PSA_SUCCESS) {
995 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200996 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everettfb792ca2024-01-31 13:40:05 +0000999 psa_unregister_read_under_mutex(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 *p_slot = NULL;
1001 return PSA_ERROR_NOT_SUPPORTED;
1002 }
1003
1004 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001005}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001008{
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001010 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001012
Ronald Cronea0f8a62020-11-25 17:52:23 +01001013 slot->key.data = NULL;
1014 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001017}
1018
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001019/** Completely wipe a slot in memory, including its policy.
1020 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001021psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001022{
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 /*
1026 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +00001027 * do our best to report an unexpected amount of registered readers or
1028 * an unexpected state.
1029 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1030 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1032 * function is called as part of the execution of a test suite, the
1033 * execution of the test suite is stopped in error if the assertion fails.
1034 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001035 switch (slot->state) {
1036 case PSA_SLOT_FULL:
1037 /* In this state psa_wipe_key_slot() must only be called if the
1038 * caller is the last reader. */
1039 case PSA_SLOT_PENDING_DELETION:
1040 /* In this state psa_wipe_key_slot() must only be called if the
1041 * caller is the last reader. */
1042 if (slot->registered_readers != 1) {
1043 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1);
1044 status = PSA_ERROR_CORRUPTION_DETECTED;
1045 }
1046 break;
1047 case PSA_SLOT_FILLING:
1048 /* In this state registered_readers must be 0. */
1049 if (slot->registered_readers != 0) {
1050 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0);
1051 status = PSA_ERROR_CORRUPTION_DETECTED;
1052 }
1053 break;
1054 case PSA_SLOT_EMPTY:
1055 /* The slot is already empty, it cannot be wiped. */
1056 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1057 status = PSA_ERROR_CORRUPTION_DETECTED;
1058 break;
1059 default:
1060 /* The slot's state is invalid. */
1061 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001062 }
1063
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001064 /* Multipart operations may still be using the key. This is safe
1065 * because all multipart operation objects are independent from
1066 * the key slot: if they need to access the key after the setup
1067 * phase, they have a copy of the key. Note that this means that
1068 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001069 /* At this point, key material and other type-specific content has
1070 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001071 * zeroize because the metadata is not particularly sensitive.
1072 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 memset(slot, 0, sizeof(*slot));
1074 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001075}
1076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001078{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001079 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001080 psa_status_t status; /* status of the last operation */
1081 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001082#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1083 psa_se_drv_table_entry_t *driver;
1084#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (mbedtls_svc_key_id_is_null(key)) {
1087 return PSA_SUCCESS;
1088 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001089
Ronald Cronf2911112020-10-29 17:51:10 +01001090 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001091 * Get the description of the key in a key slot, and register to read it.
1092 * In the case of a persistent key, this will load the key description
1093 * from persistent memory if not done yet.
1094 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001095 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001096 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 status = psa_get_and_lock_key_slot(key, &slot);
1098 if (status != PSA_SUCCESS) {
1099 return status;
1100 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001101
Ryan Everettc053d962024-01-25 17:56:32 +00001102#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001103 /* We cannot unlock between setting the state to PENDING_DELETION
1104 * and destroying the key in storage, as otherwise another thread
1105 * could load the key into a new slot and the key will not be
1106 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001107 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1108 &mbedtls_threading_key_slot_mutex));
1109#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001110 /* Set the key slot containing the key description's state to
1111 * PENDING_DELETION. This stops new operations from registering
1112 * to read the slot. Current readers can safely continue to access
1113 * the key within the slot; the last registered reader will
1114 * automatically wipe the slot when they call psa_unregister_read().
1115 * If the key is persistent, we can now delete the copy of the key
1116 * from memory. If the key is opaque, we require the driver to
1117 * deal with the deletion. */
Ryan Everettc053d962024-01-25 17:56:32 +00001118 status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1119 PSA_SLOT_PENDING_DELETION);
1120
1121 if (status != PSA_SUCCESS) {
1122 goto exit;
1123 }
Ronald Cronf2911112020-10-29 17:51:10 +01001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001126 /* Refuse the destruction of a read-only key (which may or may not work
1127 * if we attempt it, depending on whether the key is merely read-only
1128 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001129 * Just do the best we can, which is to wipe the copy in memory
1130 * (done in this function's cleanup code). */
1131 overall_status = PSA_ERROR_NOT_PERMITTED;
1132 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001133 }
1134
Gilles Peskine354f7672019-07-12 23:46:38 +02001135#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1137 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001138 /* For a key in a secure element, we need to do three things:
1139 * remove the key file in internal storage, destroy the
1140 * key inside the secure element, and update the driver's
1141 * persistent data. Start a transaction that will encompass these
1142 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001144 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001146 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 status = psa_crypto_save_transaction();
1148 if (status != PSA_SUCCESS) {
1149 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001150 /* We should still try to destroy the key in the secure
1151 * element and the key metadata in storage. This is especially
1152 * important if the error is that the storage is full.
1153 * But how to do it exactly without risking an inconsistent
1154 * state after a reset?
1155 * https://github.com/ARMmbed/mbed-crypto/issues/215
1156 */
1157 overall_status = status;
1158 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001159 }
1160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 status = psa_destroy_se_key(driver,
1162 psa_key_slot_get_slot_number(slot));
1163 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001164 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001166 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001167#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1168
Darryl Greend49a4992018-06-18 17:27:26 +01001169#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001171 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001172 * The slot will still hold a copy of the key until the last reader
1173 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 status = psa_destroy_persistent_key(slot->attr.id);
1175 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001176 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 }
Darryl Greend49a4992018-06-18 17:27:26 +01001178 }
1179#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001180
Gilles Peskinefc762652019-07-22 19:30:34 +02001181#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if (driver != NULL) {
1183 status = psa_save_se_persistent_data(driver);
1184 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001185 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 }
1187 status = psa_crypto_stop_transaction();
1188 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001189 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001191 }
1192#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1193
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001194exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001195 /* Unregister from reading the slot. If we are the last active reader
1196 * then this will wipe the slot. */
1197 status = psa_unregister_read(slot);
1198 /* Prioritize CORRUPTION_DETECTED from unregistering over
1199 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001201 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 }
Ryan Everettc053d962024-01-25 17:56:32 +00001203
1204#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001205 /* Don't overwrite existing errors if the unlock fails. */
1206 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001207 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1208 &mbedtls_threading_key_slot_mutex));
1209#endif
1210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001212}
1213
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001214/** Retrieve all the publicly-accessible attributes of a key.
1215 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1217 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001218{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001219 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001220 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1225 if (status != PSA_SUCCESS) {
1226 return status;
1227 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001228
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001229 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1231 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001232
1233#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1235 psa_set_key_slot_number(attributes,
1236 psa_key_slot_get_slot_number(slot));
1237 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001238#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001239
Gilles Peskine97c0b2f2024-02-16 00:49:46 +01001240 return psa_unregister_read_under_mutex(slot);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001241}
1242
Gilles Peskinec8000c02019-08-02 20:15:51 +02001243#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1244psa_status_t psa_get_key_slot_number(
1245 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001247{
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001249 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 return PSA_SUCCESS;
1251 } else {
1252 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001253 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001254}
1255#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1258 size_t key_buffer_size,
1259 uint8_t *data,
1260 size_t data_size,
1261 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001262{
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 if (key_buffer_size > data_size) {
1264 return PSA_ERROR_BUFFER_TOO_SMALL;
1265 }
1266 memcpy(data, key_buffer, key_buffer_size);
1267 memset(data + key_buffer_size, 0,
1268 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001269 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001271}
1272
Ronald Cron7285cda2020-11-26 14:46:37 +01001273psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001274 const psa_key_attributes_t *attributes,
1275 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001277{
Ronald Crond18b5f82020-11-25 16:46:05 +01001278 psa_key_type_t type = attributes->core.type;
1279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if (key_type_is_raw_bytes(type) ||
1281 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001282 PSA_KEY_TYPE_IS_ECC(type) ||
1283 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 return psa_export_key_buffer_internal(
1285 key_buffer, key_buffer_size,
1286 data, data_size, data_length);
1287 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001288 /* This shouldn't happen in the reference implementation, but
1289 it is valid for a special-purpose implementation to omit
1290 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001292 }
1293}
1294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1296 uint8_t *data,
1297 size_t data_size,
1298 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001299{
1300 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1301 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1302 psa_key_slot_t *slot;
1303
Ronald Crone907e552021-01-18 13:22:38 +01001304 /* Reject a zero-length output buffer now, since this can never be a
1305 * valid key representation. This way we know that data must be a valid
1306 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (data_size == 0) {
1308 return PSA_ERROR_BUFFER_TOO_SMALL;
1309 }
Ronald Crone907e552021-01-18 13:22:38 +01001310
Ronald Cron9486f9d2020-11-26 13:36:23 +01001311 /* Set the key to empty now, so that even when there are errors, we always
1312 * set data_length to a value between 0 and data_size. On error, setting
1313 * the key to empty is a good choice because an empty key representation is
1314 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001315 *data_length = 0;
1316
Ronald Cron9486f9d2020-11-26 13:36:23 +01001317 /* Export requires the EXPORT flag. There is an exception for public keys,
1318 * which don't require any flag, but
1319 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1320 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1322 PSA_KEY_USAGE_EXPORT, 0);
1323 if (status != PSA_SUCCESS) {
1324 return status;
1325 }
mohammad160306e79202018-03-28 13:17:44 +03001326
Ronald Crond18b5f82020-11-25 16:46:05 +01001327 psa_key_attributes_t attributes = {
1328 .core = slot->attr
1329 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 status = psa_driver_wrapper_export_key(&attributes,
1331 slot->key.data, slot->key.bytes,
1332 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001333
Ryan Everetta103ec92024-01-31 13:59:57 +00001334 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001337}
1338
Ronald Cron7285cda2020-11-26 14:46:37 +01001339psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001340 const psa_key_attributes_t *attributes,
1341 const uint8_t *key_buffer,
1342 size_t key_buffer_size,
1343 uint8_t *data,
1344 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001346{
Ronald Crond18b5f82020-11-25 16:46:05 +01001347 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001348
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001349 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001350 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1351 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001352 /* Exporting public -> public */
1353 return psa_export_key_buffer_internal(
1354 key_buffer, key_buffer_size,
1355 data, data_size, data_length);
1356 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001357#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001358 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1359 return mbedtls_psa_rsa_export_public_key(attributes,
1360 key_buffer,
1361 key_buffer_size,
1362 data,
1363 data_size,
1364 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001365#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001366 /* We don't know how to convert a private RSA key to public. */
1367 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001368#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001369 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001370 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001371#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001372 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1373 return mbedtls_psa_ecp_export_public_key(attributes,
1374 key_buffer,
1375 key_buffer_size,
1376 data,
1377 data_size,
1378 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001379#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001380 /* We don't know how to convert a private ECC key to public */
1381 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001382#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001383 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001384 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001385#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001386 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001387 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001388 key_buffer,
1389 key_buffer_size,
1390 data, data_size,
1391 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001392#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001393 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001394#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001395 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001397 (void) key_buffer;
1398 (void) key_buffer_size;
1399 (void) data;
1400 (void) data_size;
1401 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001403 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001404}
Ronald Crond18b5f82020-11-25 16:46:05 +01001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1407 uint8_t *data,
1408 size_t data_size,
1409 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001410{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001411 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001412 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001413 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001414 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001415
Ronald Crone907e552021-01-18 13:22:38 +01001416 /* Reject a zero-length output buffer now, since this can never be a
1417 * valid key representation. This way we know that data must be a valid
1418 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if (data_size == 0) {
1420 return PSA_ERROR_BUFFER_TOO_SMALL;
1421 }
Ronald Crone907e552021-01-18 13:22:38 +01001422
Darryl Greendd8fb772018-11-07 16:00:44 +00001423 /* Set the key to empty now, so that even when there are errors, we always
1424 * set data_length to a value between 0 and data_size. On error, setting
1425 * the key to empty is a good choice because an empty key representation is
1426 * unlikely to be accepted anywhere. */
1427 *data_length = 0;
1428
1429 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1431 if (status != PSA_SUCCESS) {
1432 return status;
1433 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1436 status = PSA_ERROR_INVALID_ARGUMENT;
1437 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001438 }
1439
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001440 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001441 .core = slot->attr
1442 };
Ronald Cron67227982020-11-26 15:16:05 +01001443 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001444 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001446
1447exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001448 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001451}
1452
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001453MBEDTLS_STATIC_ASSERT(
1454 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1455 "One or more key attribute flag is listed as both external-only and dual-use")
1456MBEDTLS_STATIC_ASSERT(
1457 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1458 "One or more key attribute flag is listed as both internal-only and dual-use")
1459MBEDTLS_STATIC_ASSERT(
1460 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1461 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001462
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001463/** Validate that a key policy is internally well-formed.
1464 *
1465 * This function only rejects invalid policies. It does not validate the
1466 * consistency of the policy with respect to other attributes of the key
1467 * such as the key type.
1468 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001470{
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1472 PSA_KEY_USAGE_COPY |
1473 PSA_KEY_USAGE_ENCRYPT |
1474 PSA_KEY_USAGE_DECRYPT |
1475 PSA_KEY_USAGE_SIGN_MESSAGE |
1476 PSA_KEY_USAGE_VERIFY_MESSAGE |
1477 PSA_KEY_USAGE_SIGN_HASH |
1478 PSA_KEY_USAGE_VERIFY_HASH |
1479 PSA_KEY_USAGE_VERIFY_DERIVATION |
1480 PSA_KEY_USAGE_DERIVE)) != 0) {
1481 return PSA_ERROR_INVALID_ARGUMENT;
1482 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001485}
1486
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001487/** Validate the internal consistency of key attributes.
1488 *
1489 * This function only rejects invalid attribute values. If does not
1490 * validate the consistency of the attributes with any key data that may
1491 * be involved in the creation of the key.
1492 *
1493 * Call this function early in the key creation process.
1494 *
1495 * \param[in] attributes Key attributes for the new key.
1496 * \param[out] p_drv On any return, the driver for the key, if any.
1497 * NULL for a transparent key.
1498 *
1499 */
1500static psa_status_t psa_validate_key_attributes(
1501 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001503{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001504 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1506 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 status = psa_validate_key_location(lifetime, p_drv);
1509 if (status != PSA_SUCCESS) {
1510 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001511 }
1512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 status = psa_validate_key_persistence(lifetime);
1514 if (status != PSA_SUCCESS) {
1515 return status;
1516 }
1517
1518 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1519 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1520 return PSA_ERROR_INVALID_ARGUMENT;
1521 }
1522 } else {
1523 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1524 return PSA_ERROR_INVALID_ARGUMENT;
1525 }
1526 }
1527
1528 status = psa_validate_key_policy(&attributes->core.policy);
1529 if (status != PSA_SUCCESS) {
1530 return status;
1531 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001532
1533 /* Refuse to create overly large keys.
1534 * Note that this doesn't trigger on import if the attributes don't
1535 * explicitly specify a size (so psa_get_key_bits returns 0), so
1536 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1538 return PSA_ERROR_NOT_SUPPORTED;
1539 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001540
Gilles Peskine91e8c332019-08-02 19:19:39 +02001541 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1543 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1544 return PSA_ERROR_INVALID_ARGUMENT;
1545 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001548}
1549
Gilles Peskine4747d192019-04-17 15:05:45 +02001550/** Prepare a key slot to receive key material.
1551 *
1552 * This function allocates a key slot and sets its metadata.
1553 *
1554 * If this function fails, call psa_fail_key_creation().
1555 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001556 * This function is intended to be used as follows:
1557 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001558 * it with the specified attributes, and in case of a volatile key assign it
1559 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001560 * -# Populate the slot with the key material.
1561 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1562 * In case of failure at any step, stop the sequence and call
1563 * psa_fail_key_creation().
1564 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001565 * On success, the key slot's state is PSA_SLOT_FILLING.
1566 * It is the responsibility of the caller to change the slot's state to
1567 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001568 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001569 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001570 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001571 * \param[out] p_slot On success, a pointer to the prepared slot.
1572 * \param[out] p_drv On any return, the driver for the key, if any.
1573 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001574 *
1575 * \retval #PSA_SUCCESS
1576 * The key slot is ready to receive key material.
1577 * \return If this function fails, the key slot is an invalid state.
1578 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001579 */
1580static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001581 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001582 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001583 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001585{
1586 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001587 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001588 psa_key_slot_t *slot;
1589
Gilles Peskinedf179142019-07-15 22:02:14 +02001590 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001591 *p_drv = NULL;
1592
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 status = psa_validate_key_attributes(attributes, p_drv);
1594 if (status != PSA_SUCCESS) {
1595 return status;
1596 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001597
Ryan Everett3d8118d2024-01-30 16:58:47 +00001598#if defined(MBEDTLS_THREADING_C)
1599 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1600 &mbedtls_threading_key_slot_mutex));
1601#endif
Ryan Everettb69118e2024-01-02 15:54:32 +00001602 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001603#if defined(MBEDTLS_THREADING_C)
1604 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1605 &mbedtls_threading_key_slot_mutex));
1606#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (status != PSA_SUCCESS) {
1608 return status;
1609 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001610 slot = *p_slot;
1611
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001612 /* We're storing the declared bit-size of the key. It's up to each
1613 * creation mechanism to verify that this information is correct.
1614 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001615 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001616 * is optional (import, copy). In case of a volatile key, assign it the
1617 * volatile key identifier associated to the slot returned to contain its
1618 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001619
1620 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001622#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1623 slot->attr.id = volatile_key_id;
1624#else
1625 slot->attr.id.key_id = volatile_key_id;
1626#endif
1627 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001628
Gilles Peskine91e8c332019-08-02 19:19:39 +02001629 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001630 * external-only flags, query `attributes`. Thanks to the check
1631 * in psa_validate_key_attributes(), this leaves the dual-use
Ryan Everettb69118e2024-01-02 15:54:32 +00001632 * flags and any internal flag that psa_reserve_free_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001633 * may have set. */
1634 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001635
Gilles Peskinecbaff462019-07-12 23:46:04 +02001636#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001637 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001638 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001639 * create the key file in internal storage, create the
1640 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001641 * persistent data. This is done by starting a transaction that will
1642 * encompass these three actions.
1643 * For registering a volatile key, we just need to find an appropriate
1644 * slot number inside the SE. Since the key is designated volatile, creating
1645 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001646 /* The first thing to do is to find a slot number for the new key.
1647 * We save the slot number in persistent storage as part of the
1648 * transaction data. It will be needed to recover if the power
1649 * fails during the key creation process, to clean up on the secure
1650 * element side after restarting. Obtaining a slot number from the
1651 * secure element driver updates its persistent state, but we do not yet
1652 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001653 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001655 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1657 &slot_number);
1658 if (status != PSA_SUCCESS) {
1659 return status;
1660 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1663 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001664 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001665 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001666 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 status = psa_crypto_save_transaction();
1668 if (status != PSA_SUCCESS) {
1669 (void) psa_crypto_stop_transaction();
1670 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001671 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001672 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001673
1674 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001676 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001679 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001681 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001682#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001685}
Gilles Peskine4747d192019-04-17 15:05:45 +02001686
1687/** Finalize the creation of a key once its key material has been set.
1688 *
1689 * This entails writing the key to persistent storage.
1690 *
1691 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001692 * See the documentation of psa_start_key_creation() for the intended use
1693 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001694 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001695 * If the finalization succeeds, the function sets the key slot's state to
1696 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1697 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001698 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001699 * \param[in,out] slot Pointer to the slot with key material.
1700 * \param[in] driver The secure element driver for the key,
1701 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001702 * \param[out] key On success, identifier of the key. Note that the
1703 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001704 *
1705 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001706 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001707 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1708 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1709 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1710 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1711 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1712 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001713 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001714 * \return If this function fails, the key slot is an invalid state.
1715 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001716 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001717static psa_status_t psa_finish_key_creation(
1718 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001719 psa_se_drv_table_entry_t *driver,
1720 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001721{
1722 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001723 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001724 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001725
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001726#if defined(MBEDTLS_THREADING_C)
1727 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1728 &mbedtls_threading_key_slot_mutex));
1729#endif
1730
Gilles Peskine4747d192019-04-17 15:05:45 +02001731#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001733#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001735 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001736 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001738
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001739 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1740 sizeof(data.slot_number),
1741 "Slot number size does not match psa_se_key_data_storage_t");
1742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1744 status = psa_save_persistent_key(&slot->attr,
1745 (uint8_t *) &data,
1746 sizeof(data));
1747 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001748#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1749 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001750 /* Key material is saved in export representation in the slot, so
1751 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 status = psa_save_persistent_key(&slot->attr,
1753 slot->key.data,
1754 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001755 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001756 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001757#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1758
Gilles Peskinecbaff462019-07-12 23:46:04 +02001759#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001760 /* Finish the transaction for a key creation. This does not
1761 * happen when registering an existing key. Detect this case
1762 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001763 * creation of a persistent key in a secure element requires a transaction,
1764 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 if (driver != NULL &&
1766 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1767 status = psa_save_se_persistent_data(driver);
1768 if (status != PSA_SUCCESS) {
1769 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001770
1771#if defined(MBEDTLS_THREADING_C)
1772 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1773 &mbedtls_threading_key_slot_mutex));
1774#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 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;
Ryan Everettb69118e2024-01-02 15:54:32 +00001783 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1784 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001786 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001788 }
Ronald Cron50972942020-11-14 11:28:25 +01001789
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001790#if defined(MBEDTLS_THREADING_C)
1791 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1792 &mbedtls_threading_key_slot_mutex));
1793#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001795}
1796
1797/** Abort the creation of a key.
1798 *
1799 * You may call this function after calling psa_start_key_creation(),
1800 * or after psa_finish_key_creation() fails. In other circumstances, this
1801 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001802 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001803 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001804 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001805 * \param[in,out] slot Pointer to the slot with key material.
1806 * \param[in] driver The secure element driver for the key,
1807 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001808 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001809static void psa_fail_key_creation(psa_key_slot_t *slot,
1810 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001811{
Gilles Peskine011e4282019-06-26 18:34:38 +02001812 (void) driver;
1813
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001815 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001817
Ryan Everettb7101442024-01-23 20:09:49 +00001818#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00001819 /* If the lock operation fails we still wipe the slot.
1820 * Operations will no longer work after a failed lock,
1821 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00001822 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
1823#endif
1824
Gilles Peskine011e4282019-06-26 18:34:38 +02001825#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001826 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001827 * element, and the failure happened later (when saving metadata
1828 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001829 * element.
1830 * https://github.com/ARMmbed/mbed-crypto/issues/217
1831 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001832
Gilles Peskined7729582019-08-05 15:55:54 +02001833 /* Abort the ongoing transaction if any (there may not be one if
1834 * the creation process failed before starting one, or if the
1835 * key creation is a registration of a key in a secure element).
1836 * Earlier functions must already have done what it takes to undo any
1837 * partial creation. All that's left is to update the transaction data
1838 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001840#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1841
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00001843
1844#if defined(MBEDTLS_THREADING_C)
1845 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
1846#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02001847}
1848
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001849/** Validate optional attributes during key creation.
1850 *
1851 * Some key attributes are optional during key creation. If they are
1852 * specified in the attributes structure, check that they are consistent
1853 * with the data in the slot.
1854 *
1855 * This function should be called near the end of key creation, after
1856 * the slot in memory is fully populated but before saving persistent data.
1857 */
1858static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001859 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001861{
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 if (attributes->core.type != 0) {
1863 if (attributes->core.type != slot->attr.type) {
1864 return PSA_ERROR_INVALID_ARGUMENT;
1865 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001866 }
1867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 if (attributes->core.bits != 0) {
1869 if (attributes->core.bits != slot->attr.bits) {
1870 return PSA_ERROR_INVALID_ARGUMENT;
1871 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001872 }
1873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001875}
1876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1878 const uint8_t *data,
1879 size_t data_length,
1880 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001881{
1882 psa_status_t status;
1883 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001884 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001885 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301886 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001887
Ronald Cron81709fc2020-11-14 12:10:32 +01001888 *key = MBEDTLS_SVC_KEY_ID_INIT;
1889
Gilles Peskine0f84d622019-09-12 19:03:13 +02001890 /* Reject zero-length symmetric keys (including raw data key objects).
1891 * This also rejects any key which might be encoded as an empty string,
1892 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 if (data_length == 0) {
1894 return PSA_ERROR_INVALID_ARGUMENT;
1895 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001896
Archana449608b2021-09-08 15:36:05 +05301897 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 if (data_length > SIZE_MAX / 8) {
1899 return PSA_ERROR_NOT_SUPPORTED;
1900 }
Archana6ed4bda2021-08-04 10:47:15 +05301901
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1903 &slot, &driver);
1904 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001905 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001907
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001908 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301909 * storage ( thus not in the case of importing a key in a secure element
1910 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1911 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 if (slot->key.data == NULL) {
1913 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301914 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 attributes, data, data_length, &storage_size);
1916 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301917 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 }
Archanad8a83dc2021-06-14 10:04:16 +05301919 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 status = psa_allocate_buffer_to_slot(slot, storage_size);
1921 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001922 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001924 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001925
1926 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 status = psa_driver_wrapper_import_key(attributes,
1928 data, data_length,
1929 slot->key.data,
1930 slot->key.bytes,
1931 &slot->key.bytes, &bits);
1932 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001933 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001937 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001939 status = PSA_ERROR_INVALID_ARGUMENT;
1940 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001941 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001942
Archana6ed4bda2021-08-04 10:47:15 +05301943 /* Enforce a size limit, and in particular ensure that the bit
1944 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301946 status = PSA_ERROR_NOT_SUPPORTED;
1947 goto exit;
1948 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 status = psa_validate_optional_attributes(slot, attributes);
1950 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001951 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001955exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if (status != PSA_SUCCESS) {
1957 psa_fail_key_creation(slot, driver);
1958 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001961}
1962
Gilles Peskined7729582019-08-05 15:55:54 +02001963#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1964psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02001966{
1967 psa_status_t status;
1968 psa_key_slot_t *slot = NULL;
1969 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001970 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001971
1972 /* Leaving attributes unspecified is not currently supported.
1973 * It could make sense to query the key type and size from the
1974 * secure element, but not all secure elements support this
1975 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
1977 return PSA_ERROR_NOT_SUPPORTED;
1978 }
1979 if (psa_get_key_bits(attributes) == 0) {
1980 return PSA_ERROR_NOT_SUPPORTED;
1981 }
Gilles Peskined7729582019-08-05 15:55:54 +02001982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
1984 &slot, &driver);
1985 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02001986 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 }
Gilles Peskined7729582019-08-05 15:55:54 +02001988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02001990
1991exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 if (status != PSA_SUCCESS) {
1993 psa_fail_key_creation(slot, driver);
1994 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001995
Gilles Peskined7729582019-08-05 15:55:54 +02001996 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 psa_close_key(key);
1998 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02001999}
2000#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2001
Gilles Peskine449bd832023-01-11 14:50:10 +01002002psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2003 const psa_key_attributes_t *specified_attributes,
2004 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002005{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002006 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002007 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002008 psa_key_slot_t *source_slot = NULL;
2009 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002010 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002011 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302012 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002013
Ronald Cron81709fc2020-11-14 12:10:32 +01002014 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2015
Archana8a180362021-07-05 02:18:48 +05302016 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2018 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002019 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 status = psa_validate_optional_attributes(source_slot,
2023 specified_attributes);
2024 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002025 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002027
Archana9d17bf42021-09-10 06:22:44 +05302028 /* The target key type and number of bits have been validated by
2029 * psa_validate_optional_attributes() to be either equal to zero or
2030 * equal to the ones of the source key. So it is safe to inherit
2031 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302032 * */
Archana9d17bf42021-09-10 06:22:44 +05302033 actual_attributes.core.bits = source_slot->attr.bits;
2034 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302035
2036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 status = psa_restrict_key_policy(source_slot->attr.type,
2038 &actual_attributes.core.policy,
2039 &source_slot->attr.policy);
2040 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002041 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2045 &target_slot, &driver);
2046 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002047 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 }
2049 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2050 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302051 /*
Archana9d17bf42021-09-10 06:22:44 +05302052 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302053 * the source key would need to be exported as plaintext and re-imported
2054 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302055 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302056 * appropriate API invocations from the application, if needed.
2057 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002058 status = PSA_ERROR_NOT_SUPPORTED;
2059 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002060 }
Archana8a180362021-07-05 02:18:48 +05302061 /*
2062 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302063 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302064 * - For opaque keys this translates to an invocation of the drivers'
2065 * copy_key entry point through the dispatch layer.
2066 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2068 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2069 &storage_size);
2070 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302071 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 }
Archana374fe5b2021-09-08 15:50:28 +05302073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2075 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 }
Archana374fe5b2021-09-08 15:50:28 +05302078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 status = psa_driver_wrapper_copy_key(&actual_attributes,
2080 source_slot->key.data,
2081 source_slot->key.bytes,
2082 target_slot->key.data,
2083 target_slot->key.bytes,
2084 &target_slot->key.bytes);
2085 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302086 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 }
2088 } else {
2089 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302090 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 source_slot->key.bytes);
2092 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302093 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 }
Archana8a180362021-07-05 02:18:48 +05302095 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002097exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 if (status != PSA_SUCCESS) {
2099 psa_fail_key_creation(target_slot, driver);
2100 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002101
Ryan Everetta103ec92024-01-31 13:59:57 +00002102 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002105}
2106
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002107
2108
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002109/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002110/* Message digests */
2111/****************************************************************/
2112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002114{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002115 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 if (operation->id == 0) {
2117 return PSA_SUCCESS;
2118 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002121 operation->id = 0;
2122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002124}
2125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2127 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002128{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002129 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002130
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002131 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002133 status = PSA_ERROR_BAD_STATE;
2134 goto exit;
2135 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002138 status = PSA_ERROR_INVALID_ARGUMENT;
2139 goto exit;
2140 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002141
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002142 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2143 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002147
2148exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if (status != PSA_SUCCESS) {
2150 psa_hash_abort(operation);
2151 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002152
2153 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002154}
2155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2157 const uint8_t *input,
2158 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002159{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002160 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002163 status = PSA_ERROR_BAD_STATE;
2164 goto exit;
2165 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002166
Steven Cooremanc8288352021-03-04 14:02:19 +01002167 /* Don't require hash implementations to behave correctly on a
2168 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (input_length == 0) {
2170 return PSA_SUCCESS;
2171 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002174
2175exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 if (status != PSA_SUCCESS) {
2177 psa_hash_abort(operation);
2178 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002181}
2182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2184 uint8_t *hash,
2185 size_t hash_size,
2186 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002187{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002188 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if (operation->id == 0) {
2190 return PSA_ERROR_BAD_STATE;
2191 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002192
Steven Cooreman1e582352021-02-18 17:24:37 +01002193 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 operation, hash, hash_size, hash_length);
2195 psa_hash_abort(operation);
2196 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002197}
2198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2200 const uint8_t *hash,
2201 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002202{
Ronald Cron69a63422021-10-18 09:47:58 +02002203 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002204 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002205 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 operation,
2207 actual_hash, sizeof(actual_hash),
2208 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002211 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002215 status = PSA_ERROR_INVALID_SIGNATURE;
2216 goto exit;
2217 }
2218
Dave Rodgman78701152023-08-29 14:20:18 +01002219 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002220 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002222
2223exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2225 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002226 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002230}
2231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232psa_status_t psa_hash_compute(psa_algorithm_t alg,
2233 const uint8_t *input, size_t input_length,
2234 uint8_t *hash, size_t hash_size,
2235 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002236{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002237 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 if (!PSA_ALG_IS_HASH(alg)) {
2239 return PSA_ERROR_INVALID_ARGUMENT;
2240 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002241
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2243 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002244}
2245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246psa_status_t psa_hash_compare(psa_algorithm_t alg,
2247 const uint8_t *input, size_t input_length,
2248 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002249{
Ronald Cron69a63422021-10-18 09:47:58 +02002250 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002251 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 if (!PSA_ALG_IS_HASH(alg)) {
2254 return PSA_ERROR_INVALID_ARGUMENT;
2255 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002256
Steven Cooreman1e582352021-02-18 17:24:37 +01002257 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 alg, input, input_length,
2259 actual_hash, sizeof(actual_hash),
2260 &actual_hash_length);
2261 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002262 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 }
2264 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002265 status = PSA_ERROR_INVALID_SIGNATURE;
2266 goto exit;
2267 }
Dave Rodgman78701152023-08-29 14:20:18 +01002268 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002269 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002271
2272exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2274 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002275}
2276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2278 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002279{
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 if (source_operation->id == 0 ||
2281 target_operation->id != 0) {
2282 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002283 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2286 target_operation);
2287 if (status != PSA_SUCCESS) {
2288 psa_hash_abort(target_operation);
2289 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002292}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002293
2294
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002295/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002296/* MAC */
2297/****************************************************************/
2298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002300{
Steven Cooremane6804192021-03-19 18:28:56 +01002301 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (operation->id == 0) {
2303 return PSA_SUCCESS;
2304 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002307 operation->mac_size = 0;
2308 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002309 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002312}
2313
Ronald Cron2dff3b22021-06-17 16:33:22 +02002314static psa_status_t psa_mac_finalize_alg_and_key_validation(
2315 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002316 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002318{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002319 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 psa_key_type_t key_type = psa_get_key_type(attributes);
2321 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 if (!PSA_ALG_IS_MAC(alg)) {
2324 return PSA_ERROR_INVALID_ARGUMENT;
2325 }
Steven Cooremane6804192021-03-19 18:28:56 +01002326
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002327 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 status = psa_mac_key_can_do(alg, key_type);
2329 if (status != PSA_SUCCESS) {
2330 return status;
2331 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002332
Steven Cooremand1a68f12021-05-10 11:13:41 +02002333 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002337 /* A very short MAC is too short for security since it can be
2338 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2339 * so we make this our minimum, even though 32 bits is still
2340 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002342 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2345 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002346 /* It's impossible to "truncate" to a larger length than the full length
2347 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002349 }
2350
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002352 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2353 * that is disabled in the compile-time configuration. The result can
2354 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2355 * configuration into account. In this case, force a return of
2356 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2357 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2358 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2359 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2360 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002362 }
2363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002365}
2366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2368 mbedtls_svc_key_id_t key,
2369 psa_algorithm_t alg,
2370 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002371{
2372 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2373 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002374 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002375 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002376
2377 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002379 status = PSA_ERROR_BAD_STATE;
2380 goto exit;
2381 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002382
2383 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 key,
2385 &slot,
2386 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2387 alg);
2388 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002389 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002391
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002392 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002393 .core = slot->attr
2394 };
2395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2397 &operation->mac_size);
2398 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002399 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002401
2402 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002403 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 if (is_sign) {
2405 status = psa_driver_wrapper_mac_sign_setup(operation,
2406 &attributes,
2407 slot->key.data,
2408 slot->key.bytes,
2409 alg);
2410 } else {
2411 status = psa_driver_wrapper_mac_verify_setup(operation,
2412 &attributes,
2413 slot->key.data,
2414 slot->key.bytes,
2415 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002416 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002417
Gilles Peskinefbfac682018-07-08 20:51:54 +02002418exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 if (status != PSA_SUCCESS) {
2420 psa_mac_abort(operation);
2421 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002422
Ryan Everettfb9857f2024-02-14 12:16:41 +00002423 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002426}
2427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2429 mbedtls_svc_key_id_t key,
2430 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002431{
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002433}
2434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2436 mbedtls_svc_key_id_t key,
2437 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002438{
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002440}
2441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2443 const uint8_t *input,
2444 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002445{
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 if (operation->id == 0) {
2447 return PSA_ERROR_BAD_STATE;
2448 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002449
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002450 /* Don't require hash implementations to behave correctly on a
2451 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 if (input_length == 0) {
2453 return PSA_SUCCESS;
2454 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002455
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2457 input, input_length);
2458 if (status != PSA_SUCCESS) {
2459 psa_mac_abort(operation);
2460 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002463}
2464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2466 uint8_t *mac,
2467 size_t mac_size,
2468 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002469{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002470 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2471 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002474 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002475 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002476 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002479 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002480 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002481 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002482
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002483 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2484 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002486 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002487 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002488 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002491 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002492 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002493 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 status = psa_driver_wrapper_mac_sign_finish(operation,
2496 mac, operation->mac_size,
2497 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002498
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002499exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002500 /* In case of success, set the potential excess room in the output buffer
2501 * to an invalid value, to avoid potentially leaking a longer MAC.
2502 * In case of error, set the output length and content to a safe default,
2503 * such that in case the caller misses an error check, the output would be
2504 * an unachievable MAC.
2505 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002507 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002508 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002509 }
mohammad16036df908f2018-04-02 08:34:15 -07002510
Paul Elliottdc42ca82023-02-24 18:11:59 +00002511 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002516}
2517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2519 const uint8_t *mac,
2520 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002521{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002522 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2523 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002526 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002527 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002528 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002531 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002532 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002533 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002536 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002537 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002538 }
2539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 status = psa_driver_wrapper_mac_verify_finish(operation,
2541 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002542
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002543exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002547}
2548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2550 psa_algorithm_t alg,
2551 const uint8_t *input,
2552 size_t input_length,
2553 uint8_t *mac,
2554 size_t mac_size,
2555 size_t *mac_length,
2556 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002557{
2558 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002559 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2560 psa_key_slot_t *slot;
2561 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002562 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002563
Ronald Cron51131b52021-06-17 17:17:20 +02002564 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 key,
2566 &slot,
2567 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2568 alg);
2569 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002570 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002572
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002573 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002574 .core = slot->attr
2575 };
2576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2578 &operation_mac_size);
2579 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002580 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002584 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002585 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002586 }
2587
2588 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 &attributes,
2590 slot->key.data, slot->key.bytes,
2591 alg,
2592 input, input_length,
2593 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002594
2595exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002596 /* In case of success, set the potential excess room in the output buffer
2597 * to an invalid value, to avoid potentially leaking a longer MAC.
2598 * In case of error, set the output length and content to a safe default,
2599 * such that in case the caller misses an error check, the output would be
2600 * an unachievable MAC.
2601 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002603 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002604 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002605 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002606
2607 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002608
Ryan Everetta103ec92024-01-31 13:59:57 +00002609 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002612}
2613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002615 psa_algorithm_t alg,
2616 const uint8_t *input,
2617 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 uint8_t *mac,
2619 size_t mac_size,
2620 size_t *mac_length)
2621{
2622 return psa_mac_compute_internal(key, alg,
2623 input, input_length,
2624 mac, mac_size, mac_length, 1);
2625}
2626
2627psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2628 psa_algorithm_t alg,
2629 const uint8_t *input,
2630 size_t input_length,
2631 const uint8_t *mac,
2632 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002633{
2634 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002635 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2636 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 status = psa_mac_compute_internal(key, alg,
2639 input, input_length,
2640 actual_mac, sizeof(actual_mac),
2641 &actual_mac_length, 0);
2642 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002643 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002647 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002648 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002649 }
Dave Rodgman78701152023-08-29 14:20:18 +01002650 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002651 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002652 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002653 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002654
2655exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002659}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002660
Gilles Peskinee59236f2018-01-27 23:32:46 +01002661/****************************************************************/
2662/* Asymmetric cryptography */
2663/****************************************************************/
2664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2666 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002667{
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if (input_is_message) {
2669 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2670 return PSA_ERROR_INVALID_ARGUMENT;
2671 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2674 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2675 return PSA_ERROR_INVALID_ARGUMENT;
2676 }
2677 }
2678 } else {
2679 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2680 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002681 }
2682 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002685}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2688 int input_is_message,
2689 psa_algorithm_t alg,
2690 const uint8_t *input,
2691 size_t input_length,
2692 uint8_t *signature,
2693 size_t signature_size,
2694 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002695{
2696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2697 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2698 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002699 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002700
2701 *signature_length = 0;
2702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 status = psa_sign_verify_check_alg(input_is_message, alg);
2704 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002705 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002707
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002708 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002709 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002710 * buffer can in principle be empty since it doesn't actually have
2711 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 if (signature_size == 0) {
2713 return PSA_ERROR_BUFFER_TOO_SMALL;
2714 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002715
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002716 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 key, &slot,
2718 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2719 PSA_KEY_USAGE_SIGN_HASH,
2720 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002723 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002727 status = PSA_ERROR_INVALID_ARGUMENT;
2728 goto exit;
2729 }
2730
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002731 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002733 };
2734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002736 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002737 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002738 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 signature, signature_size, signature_length);
2740 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002741
2742 status = psa_driver_wrapper_sign_hash(
2743 &attributes, slot->key.data, slot->key.bytes,
2744 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002746 }
2747
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002748
2749exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002750 psa_wipe_tag_output_buffer(signature, status, signature_size,
2751 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002752
Ryan Everetta103ec92024-01-31 13:59:57 +00002753 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002756}
2757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2759 int input_is_message,
2760 psa_algorithm_t alg,
2761 const uint8_t *input,
2762 size_t input_length,
2763 const uint8_t *signature,
2764 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002765{
2766 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2767 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2768 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 status = psa_sign_verify_check_alg(input_is_message, alg);
2771 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002772 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002774
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002775 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 key, &slot,
2777 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2778 PSA_KEY_USAGE_VERIFY_HASH,
2779 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002780
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 if (status != PSA_SUCCESS) {
2782 return status;
2783 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002784
2785 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002787 };
2788
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002790 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002791 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002792 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 signature, signature_length);
2794 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002795 status = psa_driver_wrapper_verify_hash(
2796 &attributes, slot->key.data, slot->key.bytes,
2797 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002799 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002800
Ryan Everetta103ec92024-01-31 13:59:57 +00002801 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002804
2805}
2806
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002807psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002808 const psa_key_attributes_t *attributes,
2809 const uint8_t *key_buffer,
2810 size_t key_buffer_size,
2811 psa_algorithm_t alg,
2812 const uint8_t *input,
2813 size_t input_length,
2814 uint8_t *signature,
2815 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002817{
2818 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002819
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002821 size_t hash_length;
2822 uint8_t hash[PSA_HASH_MAX_SIZE];
2823
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002824 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 PSA_ALG_SIGN_GET_HASH(alg),
2826 input, input_length,
2827 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002828
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002830 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002832
2833 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 attributes, key_buffer, key_buffer_size,
2835 alg, hash, hash_length,
2836 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002837 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002840}
2841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2843 psa_algorithm_t alg,
2844 const uint8_t *input,
2845 size_t input_length,
2846 uint8_t *signature,
2847 size_t signature_size,
2848 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002849{
2850 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002851 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002853}
2854
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002855psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002856 const psa_key_attributes_t *attributes,
2857 const uint8_t *key_buffer,
2858 size_t key_buffer_size,
2859 psa_algorithm_t alg,
2860 const uint8_t *input,
2861 size_t input_length,
2862 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002864{
2865 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002866
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002868 size_t hash_length;
2869 uint8_t hash[PSA_HASH_MAX_SIZE];
2870
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002871 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 PSA_ALG_SIGN_GET_HASH(alg),
2873 input, input_length,
2874 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002875
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002877 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002879
2880 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 attributes, key_buffer, key_buffer_size,
2882 alg, hash, hash_length,
2883 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002884 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002887}
2888
Gilles Peskine449bd832023-01-11 14:50:10 +01002889psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2890 psa_algorithm_t alg,
2891 const uint8_t *input,
2892 size_t input_length,
2893 const uint8_t *signature,
2894 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002895{
2896 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002897 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002899}
2900
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002901psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002902 const psa_key_attributes_t *attributes,
2903 const uint8_t *key_buffer, size_t key_buffer_size,
2904 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002906{
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2908 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2909 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002910#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2912 return mbedtls_psa_rsa_sign_hash(
2913 attributes,
2914 key_buffer, key_buffer_size,
2915 alg, hash, hash_length,
2916 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002917#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2918 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 } else {
2920 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002921 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2923 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002924#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2926 return mbedtls_psa_ecdsa_sign_hash(
2927 attributes,
2928 key_buffer, key_buffer_size,
2929 alg, hash, hash_length,
2930 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002931#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2932 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 } else {
2934 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002935 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002936 }
Ronald Cron4501c982021-03-19 20:09:32 +01002937
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 (void) key_buffer;
2939 (void) key_buffer_size;
2940 (void) hash;
2941 (void) hash_length;
2942 (void) signature;
2943 (void) signature_size;
2944 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002947}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002948
Gilles Peskine449bd832023-01-11 14:50:10 +01002949psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2950 psa_algorithm_t alg,
2951 const uint8_t *hash,
2952 size_t hash_length,
2953 uint8_t *signature,
2954 size_t signature_size,
2955 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002956{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002957 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002958 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002960}
2961
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002962psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002963 const psa_key_attributes_t *attributes,
2964 const uint8_t *key_buffer, size_t key_buffer_size,
2965 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002967{
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
2969 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2970 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002971#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2973 return mbedtls_psa_rsa_verify_hash(
2974 attributes,
2975 key_buffer, key_buffer_size,
2976 alg, hash, hash_length,
2977 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002978#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2979 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 } else {
2981 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002982 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2984 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002985#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2987 return mbedtls_psa_ecdsa_verify_hash(
2988 attributes,
2989 key_buffer, key_buffer_size,
2990 alg, hash, hash_length,
2991 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002992#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2993 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 } else {
2995 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002996 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002997 }
Ronald Cron4501c982021-03-19 20:09:32 +01002998
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 (void) key_buffer;
3000 (void) key_buffer_size;
3001 (void) hash;
3002 (void) hash_length;
3003 (void) signature;
3004 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003005
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003007}
3008
Gilles Peskine449bd832023-01-11 14:50:10 +01003009psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3010 psa_algorithm_t alg,
3011 const uint8_t *hash,
3012 size_t hash_length,
3013 const uint8_t *signature,
3014 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003015{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003016 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003017 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003019}
3020
Gilles Peskine449bd832023-01-11 14:50:10 +01003021psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3022 psa_algorithm_t alg,
3023 const uint8_t *input,
3024 size_t input_length,
3025 const uint8_t *salt,
3026 size_t salt_length,
3027 uint8_t *output,
3028 size_t output_size,
3029 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003030{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003031 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003032 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003033 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003034 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003035
Darryl Green5cc689a2018-07-24 15:34:10 +01003036 (void) input;
3037 (void) input_length;
3038 (void) salt;
3039 (void) output;
3040 (void) output_size;
3041
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003042 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3045 return PSA_ERROR_INVALID_ARGUMENT;
3046 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003047
Valerio Setti5bb454a2024-01-15 10:43:16 +01003048 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3050 if (status != PSA_SUCCESS) {
3051 return status;
3052 }
3053 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3054 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003055 status = PSA_ERROR_INVALID_ARGUMENT;
3056 goto exit;
3057 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003058
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003059 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003061 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003062
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003063 status = psa_driver_wrapper_asymmetric_encrypt(
3064 &attributes, slot->key.data, slot->key.bytes,
3065 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003067exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003068 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003069
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003071}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3074 psa_algorithm_t alg,
3075 const uint8_t *input,
3076 size_t input_length,
3077 const uint8_t *salt,
3078 size_t salt_length,
3079 uint8_t *output,
3080 size_t output_size,
3081 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003082{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003083 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003084 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003085 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003086 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003087
Darryl Green5cc689a2018-07-24 15:34:10 +01003088 (void) input;
3089 (void) input_length;
3090 (void) salt;
3091 (void) output;
3092 (void) output_size;
3093
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003094 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003095
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3097 return PSA_ERROR_INVALID_ARGUMENT;
3098 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003099
Valerio Setti5bb454a2024-01-15 10:43:16 +01003100 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3102 if (status != PSA_SUCCESS) {
3103 return status;
3104 }
3105 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003106 status = PSA_ERROR_INVALID_ARGUMENT;
3107 goto exit;
3108 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003109
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003110 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003112 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003113
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003114 status = psa_driver_wrapper_asymmetric_decrypt(
3115 &attributes, slot->key.data, slot->key.bytes,
3116 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003118
3119exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003120 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003123}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003124
Paul Elliott9fe12f62022-11-30 19:16:02 +00003125/****************************************************************/
3126/* Asymmetric interruptible cryptography */
3127/****************************************************************/
3128
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003129static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3130
Paul Elliott9fe12f62022-11-30 19:16:02 +00003131void psa_interruptible_set_max_ops(uint32_t max_ops)
3132{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003133 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003134}
3135
3136uint32_t psa_interruptible_get_max_ops(void)
3137{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003138 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003139}
3140
Paul Elliott9fe12f62022-11-30 19:16:02 +00003141uint32_t psa_sign_hash_get_num_ops(
3142 const psa_sign_hash_interruptible_operation_t *operation)
3143{
Paul Elliott296ede92022-12-15 17:00:30 +00003144 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003145}
3146
3147uint32_t psa_verify_hash_get_num_ops(
3148 const psa_verify_hash_interruptible_operation_t *operation)
3149{
Paul Elliott296ede92022-12-15 17:00:30 +00003150 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003151}
3152
Paul Elliott59ad9452022-12-18 15:09:02 +00003153static psa_status_t psa_sign_hash_abort_internal(
3154 psa_sign_hash_interruptible_operation_t *operation)
3155{
3156 if (operation->id == 0) {
3157 /* The object has (apparently) been initialized but it is not (yet)
3158 * in use. It's ok to call abort on such an object, and there's
3159 * nothing to do. */
3160 return PSA_SUCCESS;
3161 }
3162
3163 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3164
3165 status = psa_driver_wrapper_sign_hash_abort(operation);
3166
3167 operation->id = 0;
3168
Paul Elliotte6145dc2023-02-07 12:51:21 +00003169 /* Do not clear either the error_occurred or num_ops elements here as they
3170 * only want to be cleared by the application calling abort, not by abort
3171 * being called at completion of an operation. */
3172
Paul Elliott59ad9452022-12-18 15:09:02 +00003173 return status;
3174}
3175
Paul Elliott9fe12f62022-11-30 19:16:02 +00003176psa_status_t psa_sign_hash_start(
3177 psa_sign_hash_interruptible_operation_t *operation,
3178 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3179 const uint8_t *hash, size_t hash_length)
3180{
3181 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3182 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3183 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003184 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003185
Paul Elliottc9774412023-02-06 15:14:07 +00003186 /* Check that start has not been previously called, or operation has not
3187 * previously errored. */
3188 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003189 return PSA_ERROR_BAD_STATE;
3190 }
3191
Paul Elliott9fe12f62022-11-30 19:16:02 +00003192 status = psa_sign_verify_check_alg(0, alg);
3193 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003194 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003195 return status;
3196 }
3197
3198 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3199 PSA_KEY_USAGE_SIGN_HASH,
3200 alg);
3201
3202 if (status != PSA_SUCCESS) {
3203 goto exit;
3204 }
3205
3206 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3207 status = PSA_ERROR_INVALID_ARGUMENT;
3208 goto exit;
3209 }
3210
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003211 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003212 .core = slot->attr
3213 };
3214
Paul Elliott296ede92022-12-15 17:00:30 +00003215 /* Ensure ops count gets reset, in case of operation re-use. */
3216 operation->num_ops = 0;
3217
Paul Elliott9fe12f62022-11-30 19:16:02 +00003218 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3219 slot->key.data,
3220 slot->key.bytes, alg,
3221 hash, hash_length);
3222exit:
3223
3224 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003225 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003226 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003227 }
3228
Ryan Everettdcc03d52024-02-14 15:44:13 +00003229 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003230
Paul Elliottc9774412023-02-06 15:14:07 +00003231 if (unlock_status != PSA_SUCCESS) {
3232 operation->error_occurred = 1;
3233 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003234
Paul Elliottc9774412023-02-06 15:14:07 +00003235 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003236}
3237
3238
3239psa_status_t psa_sign_hash_complete(
3240 psa_sign_hash_interruptible_operation_t *operation,
3241 uint8_t *signature, size_t signature_size,
3242 size_t *signature_length)
3243{
3244 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3245
3246 *signature_length = 0;
3247
Paul Elliottc9774412023-02-06 15:14:07 +00003248 /* Check that start has been called first, and that operation has not
3249 * previously errored. */
3250 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003251 status = PSA_ERROR_BAD_STATE;
3252 goto exit;
3253 }
3254
Paul Elliott096abc42023-02-03 18:33:23 +00003255 /* Immediately reject a zero-length signature buffer. This guarantees that
3256 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003257 if (signature_size == 0) {
3258 status = PSA_ERROR_BUFFER_TOO_SMALL;
3259 goto exit;
3260 }
3261
3262 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3263 signature_size,
3264 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003265
Paul Elliott296ede92022-12-15 17:00:30 +00003266 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003267 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003268
Paul Elliottebe225c2023-02-10 14:32:53 +00003269exit:
3270
Paul Elliott59200a22023-02-21 15:07:40 +00003271 psa_wipe_tag_output_buffer(signature, status, signature_size,
3272 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003273
Paul Elliott53bb3122023-02-10 14:22:22 +00003274 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003275 if (status != PSA_SUCCESS) {
3276 operation->error_occurred = 1;
3277 }
3278
Paul Elliott59ad9452022-12-18 15:09:02 +00003279 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003280 }
3281
3282 return status;
3283}
3284
3285psa_status_t psa_sign_hash_abort(
3286 psa_sign_hash_interruptible_operation_t *operation)
3287{
Paul Elliott59ad9452022-12-18 15:09:02 +00003288 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3289
3290 status = psa_sign_hash_abort_internal(operation);
3291
3292 /* We clear the number of ops done here, so that it is not cleared when
3293 * the operation fails or succeeds, only on manual abort. */
3294 operation->num_ops = 0;
3295
Paul Elliottc9774412023-02-06 15:14:07 +00003296 /* Likewise, failure state. */
3297 operation->error_occurred = 0;
3298
Paul Elliott59ad9452022-12-18 15:09:02 +00003299 return status;
3300}
3301
3302static psa_status_t psa_verify_hash_abort_internal(
3303 psa_verify_hash_interruptible_operation_t *operation)
3304{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003305 if (operation->id == 0) {
3306 /* The object has (apparently) been initialized but it is not (yet)
3307 * in use. It's ok to call abort on such an object, and there's
3308 * nothing to do. */
3309 return PSA_SUCCESS;
3310 }
3311
Paul Elliott59ad9452022-12-18 15:09:02 +00003312 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3313
3314 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003315
3316 operation->id = 0;
3317
Paul Elliotte6145dc2023-02-07 12:51:21 +00003318 /* Do not clear either the error_occurred or num_ops elements here as they
3319 * only want to be cleared by the application calling abort, not by abort
3320 * being called at completion of an operation. */
3321
Paul Elliott59ad9452022-12-18 15:09:02 +00003322 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003323}
3324
3325psa_status_t psa_verify_hash_start(
3326 psa_verify_hash_interruptible_operation_t *operation,
3327 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3328 const uint8_t *hash, size_t hash_length,
3329 const uint8_t *signature, size_t signature_length)
3330{
3331 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3332 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3333 psa_key_slot_t *slot;
3334
Paul Elliottc9774412023-02-06 15:14:07 +00003335 /* Check that start has not been previously called, or operation has not
3336 * previously errored. */
3337 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003338 return PSA_ERROR_BAD_STATE;
3339 }
3340
3341 status = psa_sign_verify_check_alg(0, alg);
3342 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003343 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003344 return status;
3345 }
3346
3347 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3348 PSA_KEY_USAGE_VERIFY_HASH,
3349 alg);
3350
3351 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003352 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003353 return status;
3354 }
3355
3356 psa_key_attributes_t attributes = {
3357 .core = slot->attr
3358 };
3359
Paul Elliott296ede92022-12-15 17:00:30 +00003360 /* Ensure ops count gets reset, in case of operation re-use. */
3361 operation->num_ops = 0;
3362
Paul Elliott9fe12f62022-11-30 19:16:02 +00003363 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3364 slot->key.data,
3365 slot->key.bytes,
3366 alg, hash, hash_length,
3367 signature, signature_length);
3368
3369 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003370 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003371 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003372 }
3373
Ryan Everett291267f2024-02-14 15:59:15 +00003374 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003375
Paul Elliottc9774412023-02-06 15:14:07 +00003376 if (unlock_status != PSA_SUCCESS) {
3377 operation->error_occurred = 1;
3378 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003379
Paul Elliottc9774412023-02-06 15:14:07 +00003380 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003381}
3382
3383psa_status_t psa_verify_hash_complete(
3384 psa_verify_hash_interruptible_operation_t *operation)
3385{
3386 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3387
Paul Elliottc9774412023-02-06 15:14:07 +00003388 /* Check that start has been called first, and that operation has not
3389 * previously errored. */
3390 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003391 status = PSA_ERROR_BAD_STATE;
3392 goto exit;
3393 }
3394
3395 status = psa_driver_wrapper_verify_hash_complete(operation);
3396
Paul Elliott296ede92022-12-15 17:00:30 +00003397 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003398 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003399 operation);
3400
Paul Elliottebe225c2023-02-10 14:32:53 +00003401exit:
3402
Paul Elliott9fe12f62022-11-30 19:16:02 +00003403 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003404 if (status != PSA_SUCCESS) {
3405 operation->error_occurred = 1;
3406 }
3407
Paul Elliott59ad9452022-12-18 15:09:02 +00003408 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003409 }
3410
3411 return status;
3412}
3413
3414psa_status_t psa_verify_hash_abort(
3415 psa_verify_hash_interruptible_operation_t *operation)
3416{
Paul Elliott59ad9452022-12-18 15:09:02 +00003417 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003418
Paul Elliott59ad9452022-12-18 15:09:02 +00003419 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003420
Paul Elliott59ad9452022-12-18 15:09:02 +00003421 /* We clear the number of ops done here, so that it is not cleared when
3422 * the operation fails or succeeds, only on manual abort. */
3423 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003424
Paul Elliottc9774412023-02-06 15:14:07 +00003425 /* Likewise, failure state. */
3426 operation->error_occurred = 0;
3427
Paul Elliott59ad9452022-12-18 15:09:02 +00003428 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003429}
3430
Paul Elliott588f8ed2022-12-02 18:10:26 +00003431/****************************************************************/
3432/* Asymmetric interruptible cryptography internal */
3433/* implementations */
3434/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003435
Paul Elliott588f8ed2022-12-02 18:10:26 +00003436void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3437{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003438
Paul Elliott588f8ed2022-12-02 18:10:26 +00003439#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3440 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3441 defined(MBEDTLS_ECP_RESTARTABLE)
3442
3443 /* Internal implementation uses zero to indicate infinite number max ops,
3444 * therefore avoid this value, and set to minimum possible. */
3445 if (max_ops == 0) {
3446 max_ops = 1;
3447 }
3448
Paul Elliott588f8ed2022-12-02 18:10:26 +00003449 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003450#else
3451 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003452#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3453 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3454 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3455}
3456
Paul Elliott588f8ed2022-12-02 18:10:26 +00003457uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003458 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003459{
3460#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3461 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3462 defined(MBEDTLS_ECP_RESTARTABLE)
3463
Paul Elliott93d9ca82023-02-15 18:14:21 +00003464 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003465#else
3466 (void) operation;
3467 return 0;
3468#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3469 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3470 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3471}
3472
3473uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003474 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003475{
3476 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3477 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3478 defined(MBEDTLS_ECP_RESTARTABLE)
3479
Paul Elliott93d9ca82023-02-15 18:14:21 +00003480 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003481#else
3482 (void) operation;
3483 return 0;
3484#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3485 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3486 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3487}
3488
3489psa_status_t mbedtls_psa_sign_hash_start(
3490 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3491 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3492 size_t key_buffer_size, psa_algorithm_t alg,
3493 const uint8_t *hash, size_t hash_length)
3494{
3495 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003496 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003497
Paul Elliott068fe072023-01-16 13:59:15 +00003498 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3499 return PSA_ERROR_NOT_SUPPORTED;
3500 }
3501
3502 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003503 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003504 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003505
3506#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003507 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3508 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003509
Paul Elliotta1c94092023-02-15 16:38:04 +00003510 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3511
Paul Elliott93d9ca82023-02-15 18:14:21 +00003512 /* Ensure num_ops is zero'ed in case of context re-use. */
3513 operation->num_ops = 0;
3514
Paul Elliott068fe072023-01-16 13:59:15 +00003515 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3516 attributes->core.bits,
3517 key_buffer,
3518 key_buffer_size,
3519 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003520
Paul Elliott068fe072023-01-16 13:59:15 +00003521 if (status != PSA_SUCCESS) {
3522 return status;
3523 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003524
Paul Elliott1bc59df2023-02-05 13:41:57 +00003525 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003526 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003527
Paul Elliott068fe072023-01-16 13:59:15 +00003528 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003529 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003530 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003531
Paul Elliott0290a762023-02-09 14:30:24 +00003532 /* We only need to store the same length of hash as the private key size
3533 * here, it would be truncated by the internal implementation anyway. */
3534 required_hash_length = (hash_length < operation->coordinate_bytes ?
3535 hash_length : operation->coordinate_bytes);
3536
Paul Elliottba70ad42023-02-15 18:23:53 +00003537 if (required_hash_length > sizeof(operation->hash)) {
3538 /* Shouldn't happen, but better safe than sorry. */
3539 return PSA_ERROR_CORRUPTION_DETECTED;
3540 }
3541
Paul Elliott0290a762023-02-09 14:30:24 +00003542 memcpy(operation->hash, hash, required_hash_length);
3543 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003544
3545 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003546
3547#else
Paul Elliott068fe072023-01-16 13:59:15 +00003548 (void) operation;
3549 (void) key_buffer;
3550 (void) key_buffer_size;
3551 (void) alg;
3552 (void) hash;
3553 (void) hash_length;
3554 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003555 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003556
Paul Elliott068fe072023-01-16 13:59:15 +00003557 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003558#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3559 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3560 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003561}
3562
3563psa_status_t mbedtls_psa_sign_hash_complete(
3564 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3565 uint8_t *signature, size_t signature_size,
3566 size_t *signature_length)
3567{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003568#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3569 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3570 defined(MBEDTLS_ECP_RESTARTABLE)
3571
Paul Elliotta1c94092023-02-15 16:38:04 +00003572 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003573 mbedtls_mpi r;
3574 mbedtls_mpi s;
3575
Paul Elliott46845252023-02-03 14:59:11 +00003576 mbedtls_mpi_init(&r);
3577 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003578
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003579 /* Ensure max_ops is set to the current value (or default). */
3580 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3581
Paul Elliotta1c94092023-02-15 16:38:04 +00003582 if (signature_size < 2 * operation->coordinate_bytes) {
3583 status = PSA_ERROR_BUFFER_TOO_SMALL;
3584 goto exit;
3585 }
3586
Paul Elliott588f8ed2022-12-02 18:10:26 +00003587 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003588
Paul Elliott588f8ed2022-12-02 18:10:26 +00003589#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3590 status = mbedtls_to_psa_error(
3591 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003592 &r,
3593 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003594 &operation->ctx->d,
3595 operation->hash,
3596 operation->hash_length,
3597 operation->md_alg,
3598 mbedtls_psa_get_random,
3599 MBEDTLS_PSA_RANDOM_STATE,
3600 &operation->restart_ctx));
3601#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003602 status = PSA_ERROR_NOT_SUPPORTED;
3603 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003604#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3605 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003606 status = mbedtls_to_psa_error(
3607 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003608 &r,
3609 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003610 &operation->ctx->d,
3611 operation->hash,
3612 operation->hash_length,
3613 mbedtls_psa_get_random,
3614 MBEDTLS_PSA_RANDOM_STATE,
3615 mbedtls_psa_get_random,
3616 MBEDTLS_PSA_RANDOM_STATE,
3617 &operation->restart_ctx));
3618 }
3619
Paul Elliottf8e5b562023-02-19 18:43:45 +00003620 /* Hide the fact that the restart context only holds a delta of number of
3621 * ops done during the last operation, not an absolute value. */
3622 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3623
Paul Elliott724bd252023-02-08 12:35:08 +00003624 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003625 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003626 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003627 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003628 operation->coordinate_bytes)
3629 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003630
3631 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003632 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003633 }
3634
3635 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003636 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003637 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003638 operation->coordinate_bytes,
3639 operation->coordinate_bytes)
3640 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003641
3642 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003643 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003644 }
3645
Paul Elliott1bc59df2023-02-05 13:41:57 +00003646 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003647
Paul Elliott724bd252023-02-08 12:35:08 +00003648 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003649 }
Paul Elliott724bd252023-02-08 12:35:08 +00003650
3651exit:
3652
3653 mbedtls_mpi_free(&r);
3654 mbedtls_mpi_free(&s);
3655 return status;
3656
Paul Elliott588f8ed2022-12-02 18:10:26 +00003657 #else
3658
3659 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003660 (void) signature;
3661 (void) signature_size;
3662 (void) signature_length;
3663
3664 return PSA_ERROR_NOT_SUPPORTED;
3665
3666#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3667 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3668 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3669}
3670
3671psa_status_t mbedtls_psa_sign_hash_abort(
3672 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3673{
3674
3675#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3676 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3677 defined(MBEDTLS_ECP_RESTARTABLE)
3678
3679 if (operation->ctx) {
3680 mbedtls_ecdsa_free(operation->ctx);
3681 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003682 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003683 }
3684
3685 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3686
Paul Elliott93d9ca82023-02-15 18:14:21 +00003687 operation->num_ops = 0;
3688
Paul Elliott588f8ed2022-12-02 18:10:26 +00003689 return PSA_SUCCESS;
3690
3691#else
3692
3693 (void) operation;
3694
3695 return PSA_ERROR_NOT_SUPPORTED;
3696
3697#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3698 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3699 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3700}
3701
3702psa_status_t mbedtls_psa_verify_hash_start(
3703 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3704 const psa_key_attributes_t *attributes,
3705 const uint8_t *key_buffer, size_t key_buffer_size,
3706 psa_algorithm_t alg,
3707 const uint8_t *hash, size_t hash_length,
3708 const uint8_t *signature, size_t signature_length)
3709{
3710 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003711 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003712 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003713
Paul Elliott068fe072023-01-16 13:59:15 +00003714 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3715 return PSA_ERROR_NOT_SUPPORTED;
3716 }
3717
3718 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003719 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003720 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003721
3722#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003723 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3724 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725
Paul Elliotta1c94092023-02-15 16:38:04 +00003726 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3727 mbedtls_mpi_init(&operation->r);
3728 mbedtls_mpi_init(&operation->s);
3729
Paul Elliott93d9ca82023-02-15 18:14:21 +00003730 /* Ensure num_ops is zero'ed in case of context re-use. */
3731 operation->num_ops = 0;
3732
Paul Elliott068fe072023-01-16 13:59:15 +00003733 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3734 attributes->core.bits,
3735 key_buffer,
3736 key_buffer_size,
3737 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003738
Paul Elliott068fe072023-01-16 13:59:15 +00003739 if (status != PSA_SUCCESS) {
3740 return status;
3741 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003742
Paul Elliottc569fc22023-02-10 13:02:54 +00003743 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003744
Paul Elliott1bc59df2023-02-05 13:41:57 +00003745 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003746 return PSA_ERROR_INVALID_SIGNATURE;
3747 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003748
Paul Elliott068fe072023-01-16 13:59:15 +00003749 status = mbedtls_to_psa_error(
3750 mbedtls_mpi_read_binary(&operation->r,
3751 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003752 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003753
Paul Elliott068fe072023-01-16 13:59:15 +00003754 if (status != PSA_SUCCESS) {
3755 return status;
3756 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003757
Paul Elliott068fe072023-01-16 13:59:15 +00003758 status = mbedtls_to_psa_error(
3759 mbedtls_mpi_read_binary(&operation->s,
3760 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003761 coordinate_bytes,
3762 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003763
Paul Elliott068fe072023-01-16 13:59:15 +00003764 if (status != PSA_SUCCESS) {
3765 return status;
3766 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767
Paul Elliott2c9843f2023-02-15 17:32:42 +00003768 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003769
Paul Elliott2c9843f2023-02-15 17:32:42 +00003770 if (status != PSA_SUCCESS) {
3771 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003772 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773
Paul Elliott0290a762023-02-09 14:30:24 +00003774 /* We only need to store the same length of hash as the private key size
3775 * here, it would be truncated by the internal implementation anyway. */
3776 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3777 coordinate_bytes);
3778
Paul Elliottba70ad42023-02-15 18:23:53 +00003779 if (required_hash_length > sizeof(operation->hash)) {
3780 /* Shouldn't happen, but better safe than sorry. */
3781 return PSA_ERROR_CORRUPTION_DETECTED;
3782 }
3783
Paul Elliott0290a762023-02-09 14:30:24 +00003784 memcpy(operation->hash, hash, required_hash_length);
3785 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003786
3787 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003788#else
Paul Elliott068fe072023-01-16 13:59:15 +00003789 (void) operation;
3790 (void) key_buffer;
3791 (void) key_buffer_size;
3792 (void) alg;
3793 (void) hash;
3794 (void) hash_length;
3795 (void) signature;
3796 (void) signature_length;
3797 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003798 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003799 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003800
Paul Elliott068fe072023-01-16 13:59:15 +00003801 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3803 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3804 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805}
3806
3807psa_status_t mbedtls_psa_verify_hash_complete(
3808 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3809{
3810
3811#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3812 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3813 defined(MBEDTLS_ECP_RESTARTABLE)
3814
Paul Elliottf8e5b562023-02-19 18:43:45 +00003815 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3816
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003817 /* Ensure max_ops is set to the current value (or default). */
3818 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3819
Paul Elliottf8e5b562023-02-19 18:43:45 +00003820 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003821 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3822 operation->hash,
3823 operation->hash_length,
3824 &operation->ctx->Q,
3825 &operation->r,
3826 &operation->s,
3827 &operation->restart_ctx));
3828
Paul Elliottf8e5b562023-02-19 18:43:45 +00003829 /* Hide the fact that the restart context only holds a delta of number of
3830 * ops done during the last operation, not an absolute value. */
3831 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3832
3833 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834#else
3835 (void) operation;
3836
3837 return PSA_ERROR_NOT_SUPPORTED;
3838
3839#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3840 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3841 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3842}
3843
3844psa_status_t mbedtls_psa_verify_hash_abort(
3845 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3846{
3847
3848#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3849 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3850 defined(MBEDTLS_ECP_RESTARTABLE)
3851
3852 if (operation->ctx) {
3853 mbedtls_ecdsa_free(operation->ctx);
3854 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003855 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003856 }
3857
3858 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3859
Paul Elliott93d9ca82023-02-15 18:14:21 +00003860 operation->num_ops = 0;
3861
Paul Elliott588f8ed2022-12-02 18:10:26 +00003862 mbedtls_mpi_free(&operation->r);
3863 mbedtls_mpi_free(&operation->s);
3864
3865 return PSA_SUCCESS;
3866
3867#else
3868 (void) operation;
3869
3870 return PSA_ERROR_NOT_SUPPORTED;
3871
3872#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3873 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3874 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3875}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003876
mohammad1603503973b2018-03-12 15:59:30 +02003877/****************************************************************/
3878/* Symmetric cryptography */
3879/****************************************************************/
3880
Gilles Peskine449bd832023-01-11 14:50:10 +01003881static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3882 mbedtls_svc_key_id_t key,
3883 psa_algorithm_t alg,
3884 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003885{
3886 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3887 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003888 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003889 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3890 PSA_KEY_USAGE_ENCRYPT :
3891 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003892 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02003893
3894 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003895 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003896 status = PSA_ERROR_BAD_STATE;
3897 goto exit;
3898 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003899
Gilles Peskine449bd832023-01-11 14:50:10 +01003900 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003901 status = PSA_ERROR_INVALID_ARGUMENT;
3902 goto exit;
3903 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003904
Gilles Peskine449bd832023-01-11 14:50:10 +01003905 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3906 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003907 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003908 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003909
Ronald Cron49fafa92021-03-10 08:34:23 +01003910 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003911 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003912 * so we only set it (in the driver wrapper) after resources have been
3913 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003914 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003916 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003917 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003918 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003919 }
3920 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003921
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003922 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003923 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003924 };
3925
Ronald Cronab99ac22020-10-01 16:38:28 +02003926 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003927 if (cipher_operation == MBEDTLS_ENCRYPT) {
3928 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3929 &attributes,
3930 slot->key.data,
3931 slot->key.bytes,
3932 alg);
3933 } else {
3934 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3935 &attributes,
3936 slot->key.data,
3937 slot->key.bytes,
3938 alg);
3939 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003940
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003941exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003942 if (status != PSA_SUCCESS) {
3943 psa_cipher_abort(operation);
3944 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003945
Ryan Everettc0053cc2024-02-14 16:27:13 +00003946 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003947
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003949}
3950
Gilles Peskine449bd832023-01-11 14:50:10 +01003951psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3952 mbedtls_svc_key_id_t key,
3953 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003954{
Gilles Peskine449bd832023-01-11 14:50:10 +01003955 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003956}
3957
Gilles Peskine449bd832023-01-11 14:50:10 +01003958psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3959 mbedtls_svc_key_id_t key,
3960 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003961{
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003963}
3964
Gilles Peskine449bd832023-01-11 14:50:10 +01003965psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3966 uint8_t *iv,
3967 size_t iv_size,
3968 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003969{
Ronald Cron6d051732020-10-01 14:10:20 +02003970 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003971 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07003972 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003973
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003975 status = PSA_ERROR_BAD_STATE;
3976 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003977 }
3978
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003980 status = PSA_ERROR_BAD_STATE;
3981 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003982 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003983
Ronald Cron2fb90522021-07-05 12:31:44 +02003984 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003986 status = PSA_ERROR_BUFFER_TOO_SMALL;
3987 goto exit;
3988 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003989
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02003991 status = PSA_ERROR_GENERIC_ERROR;
3992 goto exit;
3993 }
3994
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 status = psa_generate_random(local_iv, default_iv_length);
3996 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003997 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 }
Ronald Cron5618a392021-03-26 09:52:26 +01003999
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 status = psa_driver_wrapper_cipher_set_iv(operation,
4001 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004002
4003exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004004 if (status == PSA_SUCCESS) {
4005 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004006 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004007 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004009 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004011 }
Ronald Cron6d051732020-10-01 14:10:20 +02004012
Gilles Peskine449bd832023-01-11 14:50:10 +01004013 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004014}
4015
Gilles Peskine449bd832023-01-11 14:50:10 +01004016psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4017 const uint8_t *iv,
4018 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004019{
Ronald Cron6d051732020-10-01 14:10:20 +02004020 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004021
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004023 status = PSA_ERROR_BAD_STATE;
4024 goto exit;
4025 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004028 status = PSA_ERROR_BAD_STATE;
4029 goto exit;
4030 }
Ronald Crona0d68172021-03-26 10:15:08 +01004031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004033 status = PSA_ERROR_INVALID_ARGUMENT;
4034 goto exit;
4035 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004036
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 status = psa_driver_wrapper_cipher_set_iv(operation,
4038 iv,
4039 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004040
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004041exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004043 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 } else {
4045 psa_cipher_abort(operation);
4046 }
4047 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004048}
4049
Gilles Peskine449bd832023-01-11 14:50:10 +01004050psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4051 const uint8_t *input,
4052 size_t input_length,
4053 uint8_t *output,
4054 size_t output_size,
4055 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004056{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004057 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004058
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004060 status = PSA_ERROR_BAD_STATE;
4061 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004062 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004063
Gilles Peskine449bd832023-01-11 14:50:10 +01004064 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004065 status = PSA_ERROR_BAD_STATE;
4066 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004067 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004068
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 status = psa_driver_wrapper_cipher_update(operation,
4070 input,
4071 input_length,
4072 output,
4073 output_size,
4074 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004075
4076exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 if (status != PSA_SUCCESS) {
4078 psa_cipher_abort(operation);
4079 }
Ronald Cron6d051732020-10-01 14:10:20 +02004080
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004082}
4083
Gilles Peskine449bd832023-01-11 14:50:10 +01004084psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4085 uint8_t *output,
4086 size_t output_size,
4087 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004088{
David Saadab4ecc272019-02-14 13:48:10 +02004089 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004090
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004092 status = PSA_ERROR_BAD_STATE;
4093 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004094 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004095
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004097 status = PSA_ERROR_BAD_STATE;
4098 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004099 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 status = psa_driver_wrapper_cipher_finish(operation,
4102 output,
4103 output_size,
4104 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004105
4106exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 if (status == PSA_SUCCESS) {
4108 return psa_cipher_abort(operation);
4109 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004110 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004112
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004114 }
mohammad1603503973b2018-03-12 15:59:30 +02004115}
4116
Gilles Peskine449bd832023-01-11 14:50:10 +01004117psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004118{
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004120 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004121 * in use. It's ok to call abort on such an object, and there's
4122 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004124 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004125
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004127
Ronald Cron49fafa92021-03-10 08:34:23 +01004128 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004129 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004130 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004131
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004133}
4134
Gilles Peskine449bd832023-01-11 14:50:10 +01004135psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4136 psa_algorithm_t alg,
4137 const uint8_t *input,
4138 size_t input_length,
4139 uint8_t *output,
4140 size_t output_size,
4141 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004142{
4143 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004144 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004145 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004146 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4147 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004148 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004149
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004151 status = PSA_ERROR_INVALID_ARGUMENT;
4152 goto exit;
4153 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4156 PSA_KEY_USAGE_ENCRYPT,
4157 alg);
4158 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004159 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004161
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004162 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004164 };
4165
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4167 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004168 status = PSA_ERROR_GENERIC_ERROR;
4169 goto exit;
4170 }
4171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 if (default_iv_length > 0) {
4173 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004174 status = PSA_ERROR_BUFFER_TOO_SMALL;
4175 goto exit;
4176 }
4177
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 status = psa_generate_random(local_iv, default_iv_length);
4179 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004180 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004182 }
4183
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004184 status = psa_driver_wrapper_cipher_encrypt(
4185 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004186 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004187 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004189
4190exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004191 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004193 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004194 }
Ronald Cron23919522021-07-08 19:00:07 +02004195
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 if (status == PSA_SUCCESS) {
4197 if (default_iv_length > 0) {
4198 memcpy(output, local_iv, default_iv_length);
4199 }
4200 *output_length += default_iv_length;
4201 } else {
4202 *output_length = 0;
4203 }
4204
4205 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004206}
4207
Gilles Peskine449bd832023-01-11 14:50:10 +01004208psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4209 psa_algorithm_t alg,
4210 const uint8_t *input,
4211 size_t input_length,
4212 uint8_t *output,
4213 size_t output_size,
4214 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004215{
4216 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004217 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004218 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004219 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004222 status = PSA_ERROR_INVALID_ARGUMENT;
4223 goto exit;
4224 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004225
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4227 PSA_KEY_USAGE_DECRYPT,
4228 alg);
4229 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004230 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004232
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004233 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004235 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004236
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4238 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004239 status = PSA_ERROR_INVALID_ARGUMENT;
4240 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004242 status = PSA_ERROR_INVALID_ARGUMENT;
4243 goto exit;
4244 }
4245
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004246 status = psa_driver_wrapper_cipher_decrypt(
4247 &attributes, slot->key.data, slot->key.bytes,
4248 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004250
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004251exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004252 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004254 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004256
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004258 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 }
Ronald Cron23919522021-07-08 19:00:07 +02004260
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004262}
4263
4264
mohammad16035955c982018-04-26 00:53:03 +03004265/****************************************************************/
4266/* AEAD */
4267/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004268
Paul Elliottbaff51c2021-09-28 17:44:45 +01004269/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004270static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004271{
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004273}
4274
4275/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004276static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4277 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004278{
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004280
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004282#if defined(PSA_WANT_ALG_GCM)
4283 case PSA_ALG_GCM:
4284 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 * arbitrarily large nonces. Please note that we do not generally
4286 * recommend the usage of nonces of greater length than
4287 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4288 * size, which can then lead to collisions if you encrypt a very
4289 * large number of messages.*/
4290 if (nonce_length != 0) {
4291 return PSA_SUCCESS;
4292 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004293 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004294#endif /* PSA_WANT_ALG_GCM */
4295#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004296 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 if (nonce_length >= 7 && nonce_length <= 13) {
4298 return PSA_SUCCESS;
4299 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004300 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004301#endif /* PSA_WANT_ALG_CCM */
4302#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004303 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 if (nonce_length == 12) {
4305 return PSA_SUCCESS;
4306 } else if (nonce_length == 8) {
4307 return PSA_ERROR_NOT_SUPPORTED;
4308 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004309 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004310#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004311 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004312 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004314 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004315
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004317}
4318
Gilles Peskine449bd832023-01-11 14:50:10 +01004319static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004320{
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4322 return PSA_ERROR_INVALID_ARGUMENT;
4323 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004324
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004326}
4327
Gilles Peskine449bd832023-01-11 14:50:10 +01004328psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4329 psa_algorithm_t alg,
4330 const uint8_t *nonce,
4331 size_t nonce_length,
4332 const uint8_t *additional_data,
4333 size_t additional_data_length,
4334 const uint8_t *plaintext,
4335 size_t plaintext_length,
4336 uint8_t *ciphertext,
4337 size_t ciphertext_size,
4338 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004339{
Ronald Cron215633c2021-03-16 17:15:37 +01004340 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4341 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004342
mohammad1603f08a5502018-06-03 15:05:47 +03004343 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004344
Gilles Peskine449bd832023-01-11 14:50:10 +01004345 status = psa_aead_check_algorithm(alg);
4346 if (status != PSA_SUCCESS) {
4347 return status;
4348 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004349
Ronald Cron9a986162021-03-26 12:40:07 +01004350 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4352 if (status != PSA_SUCCESS) {
4353 return status;
4354 }
mohammad16035955c982018-04-26 00:53:03 +03004355
Ronald Cron215633c2021-03-16 17:15:37 +01004356 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004358 };
mohammad16035955c982018-04-26 00:53:03 +03004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 status = psa_aead_check_nonce_length(alg, nonce_length);
4361 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004362 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004364
Ronald Cronde822812021-03-17 16:08:20 +01004365 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004366 &attributes, slot->key.data, slot->key.bytes,
4367 alg,
4368 nonce, nonce_length,
4369 additional_data, additional_data_length,
4370 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004372
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4374 memset(ciphertext, 0, ciphertext_size);
4375 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004376
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004377exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004378 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004379
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004381}
4382
Gilles Peskine449bd832023-01-11 14:50:10 +01004383psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4384 psa_algorithm_t alg,
4385 const uint8_t *nonce,
4386 size_t nonce_length,
4387 const uint8_t *additional_data,
4388 size_t additional_data_length,
4389 const uint8_t *ciphertext,
4390 size_t ciphertext_length,
4391 uint8_t *plaintext,
4392 size_t plaintext_size,
4393 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004394{
Ronald Cron215633c2021-03-16 17:15:37 +01004395 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4396 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004397
Gilles Peskineee652a32018-06-01 19:23:52 +02004398 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004399
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 status = psa_aead_check_algorithm(alg);
4401 if (status != PSA_SUCCESS) {
4402 return status;
4403 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004404
Ronald Cron9a986162021-03-26 12:40:07 +01004405 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4407 if (status != PSA_SUCCESS) {
4408 return status;
4409 }
mohammad16035955c982018-04-26 00:53:03 +03004410
Ronald Cron215633c2021-03-16 17:15:37 +01004411 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004413 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 status = psa_aead_check_nonce_length(alg, nonce_length);
4416 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004417 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004419
Ronald Cronde822812021-03-17 16:08:20 +01004420 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004421 &attributes, slot->key.data, slot->key.bytes,
4422 alg,
4423 nonce, nonce_length,
4424 additional_data, additional_data_length,
4425 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004426 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004427
Gilles Peskine449bd832023-01-11 14:50:10 +01004428 if (status != PSA_SUCCESS && plaintext_size != 0) {
4429 memset(plaintext, 0, plaintext_size);
4430 }
mohammad160360a64d02018-06-03 17:20:42 +03004431
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004432exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004433 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004434
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 return status;
mohammad16035955c982018-04-26 00:53:03 +03004436}
4437
Gilles Peskine449bd832023-01-11 14:50:10 +01004438static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4439{
4440 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004441
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004443#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004445 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4447 return PSA_ERROR_INVALID_ARGUMENT;
4448 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004449 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004450#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004451
Przemek Stekiel86679c72022-10-06 17:06:56 +02004452#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004454 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4456 return PSA_ERROR_INVALID_ARGUMENT;
4457 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004458 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004459#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004460
Przemek Stekiel86679c72022-10-06 17:06:56 +02004461#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004463 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 if (tag_len != 16) {
4465 return PSA_ERROR_INVALID_ARGUMENT;
4466 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004467 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004468#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004469
4470 default:
4471 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004473 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004475}
4476
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004477/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004478static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4479 int is_encrypt,
4480 mbedtls_svc_key_id_t key,
4481 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004482{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004483 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4484 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4485 psa_key_slot_t *slot = NULL;
4486 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004487 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004488
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 status = psa_aead_check_algorithm(alg);
4490 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004491 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004493
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004495 status = PSA_ERROR_BAD_STATE;
4496 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004497 }
4498
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 if (operation->nonce_set || operation->lengths_set ||
4500 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004501 status = PSA_ERROR_BAD_STATE;
4502 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004503 }
4504
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004506 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004508 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4512 alg);
4513 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004514 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004516
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004517 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004518 .core = slot->attr
4519 };
4520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004522 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004524
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 if (is_encrypt) {
4526 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4527 &attributes,
4528 slot->key.data,
4529 slot->key.bytes,
4530 alg);
4531 } else {
4532 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4533 &attributes,
4534 slot->key.data,
4535 slot->key.bytes,
4536 alg);
4537 }
4538 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004539 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004541
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004543
4544exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00004545 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004546
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004548 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004550 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 } else {
4552 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004553 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004556}
4557
Paul Elliott302ff6b2021-04-20 18:10:30 +01004558/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004559psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4560 mbedtls_svc_key_id_t key,
4561 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004562{
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004564}
4565
4566/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004567psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4568 mbedtls_svc_key_id_t key,
4569 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004570{
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004572}
4573
4574/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004575psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4576 uint8_t *nonce,
4577 size_t nonce_size,
4578 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004579{
4580 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004581 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004582 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004583
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004584 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004587 status = PSA_ERROR_BAD_STATE;
4588 goto exit;
4589 }
4590
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004592 status = PSA_ERROR_BAD_STATE;
4593 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004594 }
4595
Paul Elliotte193ea82021-10-01 13:00:16 +01004596 /* For CCM, this size may not be correct according to the PSA
4597 * specification. The PSA Crypto 1.0.1 specification states:
4598 *
4599 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4600 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4601 *
4602 * However this restriction that L has to be the smallest integer is not
4603 * applied in practice, and it is not implementable here since the
4604 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4606 operation->alg);
4607 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004608 status = PSA_ERROR_BUFFER_TOO_SMALL;
4609 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004610 }
4611
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 status = psa_generate_random(local_nonce, required_nonce_size);
4613 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004614 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004616
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004618
Paul Elliott39dc6b82021-05-11 19:16:09 +01004619exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 if (status == PSA_SUCCESS) {
4621 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004622 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 } else {
4624 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004625 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004626
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004628}
4629
4630/* Set the nonce for a multipart authenticated encryption or decryption
4631 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004632psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4633 const uint8_t *nonce,
4634 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004635{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004636 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4637
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004639 status = PSA_ERROR_BAD_STATE;
4640 goto exit;
4641 }
4642
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004644 status = PSA_ERROR_BAD_STATE;
4645 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004646 }
4647
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4649 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004650 status = PSA_ERROR_INVALID_ARGUMENT;
4651 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004652 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004653
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4655 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004656
Paul Elliott39dc6b82021-05-11 19:16:09 +01004657exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004659 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 } else {
4661 psa_aead_abort(operation);
4662 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004663
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004665}
4666
4667/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004668psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4669 size_t ad_length,
4670 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004671{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004672 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4673
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004675 status = PSA_ERROR_BAD_STATE;
4676 goto exit;
4677 }
4678
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 if (operation->lengths_set || operation->ad_started ||
4680 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004681 status = PSA_ERROR_BAD_STATE;
4682 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004683 }
4684
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004686#if defined(PSA_WANT_ALG_GCM)
4687 case PSA_ALG_GCM:
4688 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 * bits. Without the guard this code will generate warnings on 32bit
4690 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004691#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 if (((uint64_t) ad_length) >> 61 != 0 ||
4693 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004694 status = PSA_ERROR_INVALID_ARGUMENT;
4695 goto exit;
4696 }
Paul Elliott325d3742021-09-27 17:56:28 +01004697#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004698 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004699#endif /* PSA_WANT_ALG_GCM */
4700#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004701 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004703 status = PSA_ERROR_INVALID_ARGUMENT;
4704 goto exit;
4705 }
4706 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004707#endif /* PSA_WANT_ALG_CCM */
4708#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004709 case PSA_ALG_CHACHA20_POLY1305:
4710 /* No length restrictions for ChaChaPoly. */
4711 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004712#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004713 default:
4714 break;
4715 }
Paul Elliott325d3742021-09-27 17:56:28 +01004716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4718 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004719
Paul Elliott39dc6b82021-05-11 19:16:09 +01004720exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004722 operation->ad_remaining = ad_length;
4723 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004724 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 } else {
4726 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004727 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004728
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004730}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004731
4732/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004733psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4734 const uint8_t *input,
4735 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004736{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004737 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4738
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004740 status = PSA_ERROR_BAD_STATE;
4741 goto exit;
4742 }
4743
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004745 status = PSA_ERROR_BAD_STATE;
4746 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004747 }
4748
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 if (operation->lengths_set) {
4750 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004751 status = PSA_ERROR_INVALID_ARGUMENT;
4752 goto exit;
4753 }
4754
4755 operation->ad_remaining -= input_length;
4756 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004757#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004759 status = PSA_ERROR_BAD_STATE;
4760 goto exit;
4761 }
4762#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 status = psa_driver_wrapper_aead_update_ad(operation, input,
4765 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004766
Paul Elliott39dc6b82021-05-11 19:16:09 +01004767exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004769 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 } else {
4771 psa_aead_abort(operation);
4772 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004773
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004775}
4776
4777/* Encrypt or decrypt a message fragment in an active multipart AEAD
4778 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004779psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4780 const uint8_t *input,
4781 size_t input_length,
4782 uint8_t *output,
4783 size_t output_size,
4784 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004785{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004786 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004787
4788 *output_length = 0;
4789
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004791 status = PSA_ERROR_BAD_STATE;
4792 goto exit;
4793 }
4794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004796 status = PSA_ERROR_BAD_STATE;
4797 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004798 }
4799
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004801 /* Additional data length was supplied, but not all the additional
4802 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004804 status = PSA_ERROR_INVALID_ARGUMENT;
4805 goto exit;
4806 }
4807
4808 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004810 status = PSA_ERROR_INVALID_ARGUMENT;
4811 goto exit;
4812 }
4813
4814 operation->body_remaining -= input_length;
4815 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004816#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004818 status = PSA_ERROR_BAD_STATE;
4819 goto exit;
4820 }
4821#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004822
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4824 output, output_size,
4825 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004826
Paul Elliott39dc6b82021-05-11 19:16:09 +01004827exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004829 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 } else {
4831 psa_aead_abort(operation);
4832 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004835}
4836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004838{
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 if (operation->id == 0 || !operation->nonce_set) {
4840 return PSA_ERROR_BAD_STATE;
4841 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004842
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4844 operation->body_remaining != 0)) {
4845 return PSA_ERROR_INVALID_ARGUMENT;
4846 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004847
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004849}
4850
Paul Elliott302ff6b2021-04-20 18:10:30 +01004851/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004852psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4853 uint8_t *ciphertext,
4854 size_t ciphertext_size,
4855 size_t *ciphertext_length,
4856 uint8_t *tag,
4857 size_t tag_size,
4858 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004859{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004860 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4861
Paul Elliott302ff6b2021-04-20 18:10:30 +01004862 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004863 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004864
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 status = psa_aead_final_checks(operation);
4866 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004867 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004869
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004871 status = PSA_ERROR_BAD_STATE;
4872 goto exit;
4873 }
4874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4876 ciphertext_size,
4877 ciphertext_length,
4878 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004879
Paul Elliott39dc6b82021-05-11 19:16:09 +01004880exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004881
4882
Paul Elliottf47b0952021-05-21 18:02:33 +01004883 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004884 * the zero tag size, make sure the tag is set to something implausible.
4885 * Even if the operation succeeds, make sure we clear the rest of the
4886 * buffer to prevent potential leakage of anything previously placed in
4887 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004888 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004889
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004891
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004893}
4894
4895/* Finish authenticating and decrypting a message in a multipart AEAD
4896 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004897psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4898 uint8_t *plaintext,
4899 size_t plaintext_size,
4900 size_t *plaintext_length,
4901 const uint8_t *tag,
4902 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004903{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004904 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4905
Paul Elliott302ff6b2021-04-20 18:10:30 +01004906 *plaintext_length = 0;
4907
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 status = psa_aead_final_checks(operation);
4909 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004910 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004912
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004914 status = PSA_ERROR_BAD_STATE;
4915 goto exit;
4916 }
4917
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4919 plaintext_size,
4920 plaintext_length,
4921 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004922
4923exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004927}
4928
4929/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004930psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004931{
4932 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004935 /* The object has (apparently) been initialized but it is not (yet)
4936 * in use. It's ok to call abort on such an object, and there's
4937 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004939 }
4940
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004942
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004944
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004946}
4947
Gilles Peskinee59236f2018-01-27 23:32:46 +01004948/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004949/* Generators */
4950/****************************************************************/
4951
Przemek Stekiel69c46792022-06-10 12:59:51 +02004952#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004953 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004954 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05304955 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304956 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08004957#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004958#endif /* At least one builtin KDF */
4959
Przemek Stekiel69c46792022-06-10 12:59:51 +02004960#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004961 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4962 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4963static psa_status_t psa_key_derivation_start_hmac(
4964 psa_mac_operation_t *operation,
4965 psa_algorithm_t hash_alg,
4966 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02004968{
4969 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4970 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004971 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4972 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4973 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02004974
Steven Cooreman72f736a2021-05-07 14:14:37 +02004975 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02004977
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 status = psa_driver_wrapper_mac_sign_setup(operation,
4979 &attributes,
4980 hmac_key, hmac_key_length,
4981 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02004982
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 psa_reset_key_attributes(&attributes);
4984 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02004985}
4986#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004987
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004988#define HKDF_STATE_INIT 0 /* no input yet */
4989#define HKDF_STATE_STARTED 1 /* got salt */
4990#define HKDF_STATE_KEYED 2 /* got key */
4991#define HKDF_STATE_OUTPUT 3 /* output started */
4992
Gilles Peskinecbe66502019-05-16 16:59:18 +02004993static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004995{
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4997 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4998 } else {
4999 return operation->alg;
5000 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005001}
5002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005004{
5005 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5007 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005008 /* The object has (apparently) been initialized but it is not
5009 * in use. It's ok to call abort on such an object, and there's
5010 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005012#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5014 mbedtls_free(operation->ctx.hkdf.info);
5015 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5016 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005017#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005018#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5019 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5021 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5022 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5023 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005024 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005026 }
5027
Gilles Peskine449bd832023-01-11 14:50:10 +01005028 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005029 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005031 }
5032
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005034 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005036 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005037#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005039 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005041 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005042#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005043 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005044
5045 /* We leave the fields Ai and output_block to be erased safely by the
5046 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005048#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5049 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005050#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5052 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5053 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5054 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005055#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305056#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305057 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305058 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005059 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305060 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305061 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305062
5063 status = PSA_SUCCESS;
5064 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305065#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005066 {
5067 status = PSA_ERROR_BAD_STATE;
5068 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 mbedtls_platform_zeroize(operation, sizeof(*operation));
5070 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005071}
5072
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005073psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005075{
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005077 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005079 }
5080
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005081 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005083}
5084
Gilles Peskine449bd832023-01-11 14:50:10 +01005085psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5086 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005087{
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 if (operation->alg == 0) {
5089 return PSA_ERROR_BAD_STATE;
5090 }
5091 if (capacity > operation->capacity) {
5092 return PSA_ERROR_INVALID_ARGUMENT;
5093 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005094 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005096}
5097
Przemek Stekiel69c46792022-06-10 12:59:51 +02005098#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005099/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005100static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5101 psa_algorithm_t kdf_alg,
5102 uint8_t *output,
5103 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005104{
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5106 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005107 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005108 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005109#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005111#else
5112 const uint8_t last_block = 0xff;
5113#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 if (hkdf->state < HKDF_STATE_KEYED ||
5116 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005117#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005119#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 )) {
5121 return PSA_ERROR_BAD_STATE;
5122 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005123 hkdf->state = HKDF_STATE_OUTPUT;
5124
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005126 /* Copy what remains of the current block */
5127 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005129 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 }
5131 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005132 output += n;
5133 output_length -= n;
5134 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005136 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005138 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005139 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005140 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005141 * object was corrupted or if this function is called directly
5142 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 if (hkdf->block_number == last_block) {
5144 return PSA_ERROR_BAD_STATE;
5145 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005146
5147 /* We need a new block */
5148 ++hkdf->block_number;
5149 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005150
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5152 hash_alg,
5153 hkdf->prk,
5154 hash_length);
5155 if (status != PSA_SUCCESS) {
5156 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005157 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005158
5159 if (hkdf->block_number != 1) {
5160 status = psa_mac_update(&hkdf->hmac,
5161 hkdf->output_block,
5162 hash_length);
5163 if (status != PSA_SUCCESS) {
5164 return status;
5165 }
5166 }
5167 status = psa_mac_update(&hkdf->hmac,
5168 hkdf->info,
5169 hkdf->info_length);
5170 if (status != PSA_SUCCESS) {
5171 return status;
5172 }
5173 status = psa_mac_update(&hkdf->hmac,
5174 &hkdf->block_number, 1);
5175 if (status != PSA_SUCCESS) {
5176 return status;
5177 }
5178 status = psa_mac_sign_finish(&hkdf->hmac,
5179 hkdf->output_block,
5180 sizeof(hkdf->output_block),
5181 &hmac_output_length);
5182 if (status != PSA_SUCCESS) {
5183 return status;
5184 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005185 }
5186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005188}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005189#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005190
John Durkop07cc04a2020-11-16 22:08:34 -08005191#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5192 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005193static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5194 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005196{
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5198 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005199 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5200 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005201 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005202
Janos Follath7742fee2019-06-17 12:58:10 +01005203 /* We can't be wanting more output after block 0xff, otherwise
5204 * the capacity check in psa_key_derivation_output_bytes() would have
5205 * prevented this call. It could happen only if the operation
5206 * object was corrupted or if this function is called directly
5207 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 if (tls12_prf->block_number == 0xff) {
5209 return PSA_ERROR_CORRUPTION_DETECTED;
5210 }
Janos Follath7742fee2019-06-17 12:58:10 +01005211
5212 /* We need a new block */
5213 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005214 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005215
5216 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5217 *
5218 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5219 *
5220 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5221 * HMAC_hash(secret, A(2) + seed) +
5222 * HMAC_hash(secret, A(3) + seed) + ...
5223 *
5224 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005225 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005226 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005227 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005228 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005229 * is currently extracted as `output_block` and where i is
5230 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005231 */
5232
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 status = psa_key_derivation_start_hmac(&hmac,
5234 hash_alg,
5235 tls12_prf->secret,
5236 tls12_prf->secret_length);
5237 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005238 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005240
5241 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005243 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5244 * the variable seed and in this instance means it in the context of the
5245 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 status = psa_mac_update(&hmac,
5247 tls12_prf->label,
5248 tls12_prf->label_length);
5249 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005250 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 }
5252 status = psa_mac_update(&hmac,
5253 tls12_prf->seed,
5254 tls12_prf->seed_length);
5255 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005256 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 }
5258 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005259 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5261 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005262 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005264 }
5265
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 status = psa_mac_sign_finish(&hmac,
5267 tls12_prf->Ai, hash_length,
5268 &hmac_output_length);
5269 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005270 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 }
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 HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 status = psa_key_derivation_start_hmac(&hmac,
5278 hash_alg,
5279 tls12_prf->secret,
5280 tls12_prf->secret_length);
5281 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005282 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 }
5284 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5285 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005286 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 }
5288 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5289 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005290 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 }
5292 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5293 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005294 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 }
5296 status = psa_mac_sign_finish(&hmac,
5297 tls12_prf->output_block, hash_length,
5298 &hmac_output_length);
5299 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005300 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005302
Janos Follath7742fee2019-06-17 12:58:10 +01005303
5304cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 cleanup_status = psa_mac_abort(&hmac);
5306 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005307 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005309
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005311}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005312
Janos Follath844eb0e2019-06-19 12:10:49 +01005313static psa_status_t psa_key_derivation_tls12_prf_read(
5314 psa_tls12_prf_key_derivation_t *tls12_prf,
5315 psa_algorithm_t alg,
5316 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005318{
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5320 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005321 psa_status_t status;
5322 uint8_t offset, length;
5323
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005325 case PSA_TLS12_PRF_STATE_LABEL_SET:
5326 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5327 break;
5328 case PSA_TLS12_PRF_STATE_OUTPUT:
5329 break;
5330 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005332 }
5333
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005335 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 if (tls12_prf->left_in_block == 0) {
5337 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5338 alg);
5339 if (status != PSA_SUCCESS) {
5340 return status;
5341 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005342
5343 continue;
5344 }
5345
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005347 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005349 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005351
5352 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005354 output += length;
5355 output_length -= length;
5356 tls12_prf->left_in_block -= length;
5357 }
5358
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005360}
John Durkop07cc04a2020-11-16 22:08:34 -08005361#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5362 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005363
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005364#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5365static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5366 psa_tls12_ecjpake_to_pms_t *ecjpake,
5367 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005369{
5370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005371 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005372
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 if (output_length != 32) {
5374 return PSA_ERROR_INVALID_ARGUMENT;
5375 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005376
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5378 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5379 &output_size);
5380 if (status != PSA_SUCCESS) {
5381 return status;
5382 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005383
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 if (output_size != output_length) {
5385 return PSA_ERROR_GENERIC_ERROR;
5386 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005387
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005389}
5390#endif
5391
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305392#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305393static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5394 psa_pbkdf2_key_derivation_t *pbkdf2,
5395 psa_algorithm_t prf_alg,
5396 uint8_t prf_output_length,
5397 psa_key_attributes_t *attributes)
5398{
5399 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305400 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305401 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305402 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305403 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305404 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305405 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305406
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305407 mac_operation.is_sign = 1;
5408 mac_operation.mac_size = prf_output_length;
5409 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305410
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305411 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5412 attributes,
5413 pbkdf2->password,
5414 pbkdf2->password_length,
5415 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305416 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305417 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305418 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305419 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5420 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305421 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305422 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305423 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305424 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305425 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305426 }
5427 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5428 &mac_output_length);
5429 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305430 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305431 }
5432
5433 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305434 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305435 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305436 }
5437
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305438 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305439
5440 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305441 /* We are passing prf_output_length as mac_size because the driver
5442 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305443 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305444 status = psa_driver_wrapper_mac_compute(attributes,
5445 pbkdf2->password,
5446 pbkdf2->password_length,
5447 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305448 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305449 &mac_output_length);
5450 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305451 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305452 }
5453
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305454 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305455 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305456
5457cleanup:
5458 /* Zeroise buffers to clear sensitive data from memory. */
5459 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5460 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305461}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305462
5463static psa_status_t psa_key_derivation_pbkdf2_read(
5464 psa_pbkdf2_key_derivation_t *pbkdf2,
5465 psa_algorithm_t kdf_alg,
5466 uint8_t *output,
5467 size_t output_length)
5468{
5469 psa_status_t status;
5470 psa_algorithm_t prf_alg;
5471 uint8_t prf_output_length;
5472 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5473 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5474 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5475
5476 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5477 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5478 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5479 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305480 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5481 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305482 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305483 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305484 } else {
5485 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305486 }
5487
5488 switch (pbkdf2->state) {
5489 case PSA_PBKDF2_STATE_PASSWORD_SET:
5490 /* Initially we need a new block so bytes_used is equal to block size*/
5491 pbkdf2->bytes_used = prf_output_length;
5492 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5493 break;
5494 case PSA_PBKDF2_STATE_OUTPUT:
5495 break;
5496 default:
5497 return PSA_ERROR_BAD_STATE;
5498 }
5499
5500 while (output_length != 0) {
5501 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5502 if (n > output_length) {
5503 n = (uint8_t) output_length;
5504 }
5505 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5506 output += n;
5507 output_length -= n;
5508 pbkdf2->bytes_used += n;
5509
5510 if (output_length == 0) {
5511 break;
5512 }
5513
5514 /* We need a new block */
5515 pbkdf2->bytes_used = 0;
5516 pbkdf2->block_number++;
5517
5518 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5519 prf_output_length,
5520 &attributes);
5521 if (status != PSA_SUCCESS) {
5522 return status;
5523 }
5524 }
5525
5526 return PSA_SUCCESS;
5527}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305528#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305529
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005530psa_status_t psa_key_derivation_output_bytes(
5531 psa_key_derivation_operation_t *operation,
5532 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005534{
5535 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005539 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005541 }
5542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005544 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005545 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005546 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005547 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005548 goto exit;
5549 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005551 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005552 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005553 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5554 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005555 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005556 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005558 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005559 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005560
Przemek Stekiel69c46792022-06-10 12:59:51 +02005561#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5563 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5564 output, output_length);
5565 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005566#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005567#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5568 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5570 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5571 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5572 kdf_alg, output,
5573 output_length);
5574 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005575#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5576 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005577#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005579 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5581 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005582#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305583#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305584 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305585 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5586 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305587 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305588#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005589
Gilles Peskineeab56e42018-07-12 17:12:33 +02005590 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005591 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005593 }
5594
5595exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005597 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005598 * This allows us to differentiate between exhausted operations and
5599 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5600 * operations. */
5601 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005603 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005605 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005607}
5608
David Brown7807bf72021-02-09 16:28:23 -07005609#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005610static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005611{
Gilles Peskine449bd832023-01-11 14:50:10 +01005612 if (data_size >= 8) {
5613 mbedtls_des_key_set_parity(data);
5614 }
5615 if (data_size >= 16) {
5616 mbedtls_des_key_set_parity(data + 8);
5617 }
5618 if (data_size >= 24) {
5619 mbedtls_des_key_set_parity(data + 16);
5620 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005621}
David Brown7807bf72021-02-09 16:28:23 -07005622#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005623
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005624/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 * ECC keys on a Weierstrass elliptic curve require the generation
5626 * of a private key which is an integer
5627 * in the range [1, N - 1], where N is the boundary of the private key domain:
5628 * N is the prime p for Diffie-Hellman, or the order of the
5629 * curve’s base point for ECC.
5630 *
5631 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5632 * This function generates the private key using the following process:
5633 *
5634 * 1. Draw a byte string of length ceiling(m/8) bytes.
5635 * 2. If m is not a multiple of 8, set the most significant
5636 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5637 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5638 * 4. If k > N - 2, discard the result and return to step 1.
5639 * 5. Output k + 1 as the private key.
5640 *
5641 * This method allows compliance to NIST standards, specifically the methods titled
5642 * Key-Pair Generation by Testing Candidates in the following publications:
5643 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5644 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5645 * Diffie-Hellman keys.
5646 *
5647 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5648 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5649 *
5650 * Note: Function allocates memory for *data buffer, so given *data should be
5651 * always NULL.
5652 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005653#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5654#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005655static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005656 psa_key_slot_t *slot,
5657 size_t bits,
5658 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005659 uint8_t **data
5660 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005661{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005662 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005663 mbedtls_mpi k;
5664 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005665 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005667 size_t m;
5668 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005669
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 mbedtls_mpi_init(&k);
5671 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005672
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005673 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005675 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01005676 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005677
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005679 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005680 goto cleanup;
5681 }
5682
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005683 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005687
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005688 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005689 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005690 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005691
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005692 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005693
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005694 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005696
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005697 /* Note: This function is always called with *data == NULL and it
5698 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 *data = mbedtls_calloc(1, m_bytes);
5700 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005701 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005702 goto cleanup;
5703 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005704
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005706 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005708 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005710
5711 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005712 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005713 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005714 * (8 * ceiling(m/8) - m) bits of the first byte in
5715 * the string to zero.
5716 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005718 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005719 }
5720
5721 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 * big-endian byte string.
5723 */
5724 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005725
5726 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 * Result of comparison is returned. When it indicates error
5728 * then this function is called again.
5729 */
5730 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005731 }
5732
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005733 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5735 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005736cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 if (ret != 0) {
5738 status = mbedtls_to_psa_error(ret);
5739 }
5740 if (status != PSA_SUCCESS) {
5741 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005742 *data = NULL;
5743 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 mbedtls_mpi_free(&k);
5745 mbedtls_mpi_free(&diff_N_2);
5746 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005747}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005748
5749/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5750 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5751 *
5752 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5753 * draw a 32-byte string and process it as specified in
5754 * Elliptic Curves for Security [RFC7748] §5.
5755 *
5756 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5757 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5758 *
5759 * Note: Function allocates memory for *data buffer, so given *data should be
5760 * always NULL.
5761 */
5762
5763static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5764 size_t bits,
5765 psa_key_derivation_operation_t *operation,
5766 uint8_t **data
5767 )
5768{
5769 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005770 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005771
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005773 case 255:
5774 output_length = 32;
5775 break;
5776 case 448:
5777 output_length = 56;
5778 break;
5779 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005781 break;
5782 }
5783
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 if (*data == NULL) {
5787 return PSA_ERROR_INSUFFICIENT_MEMORY;
5788 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005791
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005793 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005795
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005797 case 255:
5798 (*data)[0] &= 248;
5799 (*data)[31] &= 127;
5800 (*data)[31] |= 64;
5801 break;
5802 case 448:
5803 (*data)[0] &= 252;
5804 (*data)[55] |= 128;
5805 break;
5806 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005808 break;
5809 }
5810
5811 return status;
5812}
Valerio Setti86587ab2023-06-27 13:09:30 +02005813#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5814static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5815 psa_key_slot_t *slot, size_t bits,
5816 psa_key_derivation_operation_t *operation, uint8_t **data)
5817{
5818 (void) slot;
5819 (void) bits;
5820 (void) operation;
5821 (void) data;
5822 return PSA_ERROR_NOT_SUPPORTED;
5823}
5824
5825static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5826 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5827{
5828 (void) bits;
5829 (void) operation;
5830 (void) data;
5831 return PSA_ERROR_NOT_SUPPORTED;
5832}
5833#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5834#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005835
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005836static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005837 psa_key_slot_t *slot,
5838 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005840{
5841 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305843 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005844 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005845 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005846
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5848 return PSA_ERROR_INVALID_ARGUMENT;
5849 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005850
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005851#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005852 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5854 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5855 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005856 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5858 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005859 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 }
5861 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005862 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5864 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005865 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005867 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005868 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005869#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005870 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 if (key_type_is_raw_bytes(slot->attr.type)) {
5872 if (bits % 8 != 0) {
5873 return PSA_ERROR_INVALID_ARGUMENT;
5874 }
5875 data = mbedtls_calloc(1, bytes);
5876 if (data == NULL) {
5877 return PSA_ERROR_INSUFFICIENT_MEMORY;
5878 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005879
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 status = psa_key_derivation_output_bytes(operation, data, bytes);
5881 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005882 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 }
David Brown12ca5032021-02-11 11:02:00 -07005884#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5886 psa_des_set_key_parity(data, bytes);
5887 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005888#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 } else {
5890 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005891 }
Ronald Crondd04d422020-11-28 15:14:42 +01005892
Ronald Cronbf33c932020-11-28 18:06:53 +01005893 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005894 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005896 };
5897
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5899 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5900 &storage_size);
5901 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305902 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 }
Archanad8a83dc2021-06-14 10:04:16 +05305904 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 status = psa_allocate_buffer_to_slot(slot, storage_size);
5906 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305907 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 }
Archanad8a83dc2021-06-14 10:04:16 +05305909
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 status = psa_driver_wrapper_import_key(&attributes,
5911 data, bytes,
5912 slot->key.data,
5913 slot->key.bytes,
5914 &slot->key.bytes, &bits);
5915 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005916 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005918
5919exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 mbedtls_free(data);
5921 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005922}
5923
Gilles Peskine092ce512024-02-20 12:31:24 +01005924static const psa_key_production_parameters_t default_production_parameters =
5925 PSA_KEY_PRODUCTION_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005926
Gilles Peskine092ce512024-02-20 12:31:24 +01005927int psa_key_production_parameters_are_default(
5928 const psa_key_production_parameters_t *params,
5929 size_t params_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005930{
Gilles Peskine092ce512024-02-20 12:31:24 +01005931 if (params->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005932 return 0;
5933 }
Gilles Peskine092ce512024-02-20 12:31:24 +01005934 if (params_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005935 return 0;
5936 }
5937 return 1;
5938}
5939
5940psa_status_t psa_key_derivation_output_key_ext(
5941 const psa_key_attributes_t *attributes,
5942 psa_key_derivation_operation_t *operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005943 const psa_key_production_parameters_t *params,
5944 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005945 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005946{
5947 psa_status_t status;
5948 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005949 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005950
Ronald Cron81709fc2020-11-14 12:10:32 +01005951 *key = MBEDTLS_SVC_KEY_ID_INIT;
5952
Gilles Peskine0f84d622019-09-12 19:03:13 +02005953 /* Reject any attempt to create a zero-length key so that we don't
5954 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 if (psa_get_key_bits(attributes) == 0) {
5956 return PSA_ERROR_INVALID_ARGUMENT;
5957 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005958
Gilles Peskine092ce512024-02-20 12:31:24 +01005959 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005960 return PSA_ERROR_INVALID_ARGUMENT;
5961 }
5962
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 if (operation->alg == PSA_ALG_NONE) {
5964 return PSA_ERROR_BAD_STATE;
5965 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005966
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 if (!operation->can_output_key) {
5968 return PSA_ERROR_NOT_PERMITTED;
5969 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5972 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005973#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005975 /* Deriving a key in a secure element is not implemented yet. */
5976 status = PSA_ERROR_NOT_SUPPORTED;
5977 }
5978#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 if (status == PSA_SUCCESS) {
5980 status = psa_generate_derived_key_internal(slot,
5981 attributes->core.bits,
5982 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005983 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 if (status == PSA_SUCCESS) {
5985 status = psa_finish_key_creation(slot, driver, key);
5986 }
5987 if (status != PSA_SUCCESS) {
5988 psa_fail_key_creation(slot, driver);
5989 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005992}
5993
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005994psa_status_t psa_key_derivation_output_key(
5995 const psa_key_attributes_t *attributes,
5996 psa_key_derivation_operation_t *operation,
5997 mbedtls_svc_key_id_t *key)
5998{
Gilles Peskinec81393b2024-02-14 20:51:28 +01005999 return psa_key_derivation_output_key_ext(attributes, operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01006000 &default_production_parameters, 0,
Gilles Peskinec81393b2024-02-14 20:51:28 +01006001 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006002}
Gilles Peskineeab56e42018-07-12 17:12:33 +02006003
6004
6005/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006006/* Key derivation */
6007/****************************************************************/
6008
Steven Cooreman932ffb72021-02-15 12:14:32 +01006009#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006010static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006011{
6012#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6014 return 1;
6015 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006016#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006017#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6019 return 1;
6020 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006021#endif
6022#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6024 return 1;
6025 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006026#endif
6027#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6029 return 1;
6030 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006031#endif
6032#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6034 return 1;
6035 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006036#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006037#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6039 return 1;
6040 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006041#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306042#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6043 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6044 return 1;
6045 }
6046#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306047#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6048 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6049 return 1;
6050 }
6051#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006053}
6054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006056{
6057 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 psa_status_t status = psa_hash_setup(&operation, alg);
6059 psa_hash_abort(&operation);
6060 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006061}
6062
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306063static psa_status_t psa_key_derivation_set_maximum_capacity(
6064 psa_key_derivation_operation_t *operation,
6065 psa_algorithm_t kdf_alg)
6066{
6067#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6068 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6069 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6070 return PSA_SUCCESS;
6071 }
6072#endif
6073#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
6074 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6075#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05306076 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306077 PSA_KEY_TYPE_AES,
6078 128U,
6079 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306080#else
6081 operation->capacity = SIZE_MAX;
6082#endif
6083 return PSA_SUCCESS;
6084 }
6085#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6086
6087 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6088 * invalid or meaningless but it does not affect this function */
6089 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6090 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306091 if (hash_size == 0) {
6092 return PSA_ERROR_NOT_SUPPORTED;
6093 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306094
6095 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6096 * we might fail later, which is somewhat unfriendly and potentially
6097 * risk-prone. */
6098 psa_status_t status = psa_hash_try_support(hash_alg);
6099 if (status != PSA_SUCCESS) {
6100 return status;
6101 }
6102
6103#if defined(PSA_WANT_ALG_HKDF)
6104 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6105 operation->capacity = 255 * hash_size;
6106 } else
6107#endif
6108#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6109 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6110 operation->capacity = hash_size;
6111 } else
6112#endif
6113#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6114 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6115 operation->capacity = 255 * hash_size;
6116 } else
6117#endif
6118#if defined(PSA_WANT_ALG_TLS12_PRF)
6119 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6120 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6121 operation->capacity = SIZE_MAX;
6122 } else
6123#endif
6124#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6125 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6126 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6127 /* Master Secret is always 48 bytes
6128 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6129 operation->capacity = 48U;
6130 } else
6131#endif
6132#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6133 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6134#if (SIZE_MAX > UINT32_MAX)
6135 operation->capacity = UINT32_MAX * hash_size;
6136#else
6137 operation->capacity = SIZE_MAX;
6138#endif
6139 } else
6140#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6141 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306142 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306143 status = PSA_ERROR_NOT_SUPPORTED;
6144 }
6145 return status;
6146}
6147
Gilles Peskine969c5d62019-01-16 15:53:06 +01006148static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006149 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006151{
Janos Follath5fe19732019-06-20 15:09:30 +01006152 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6153 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006155
Gilles Peskine969c5d62019-01-16 15:53:06 +01006156 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 if (!is_kdf_alg_supported(kdf_alg)) {
6158 return PSA_ERROR_NOT_SUPPORTED;
6159 }
John Durkop07cc04a2020-11-16 22:08:34 -08006160
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306161 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6162 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306163 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006164}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006165
Gilles Peskine449bd832023-01-11 14:50:10 +01006166static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006167{
6168#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 if (alg == PSA_ALG_ECDH) {
6170 return PSA_SUCCESS;
6171 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006172#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006173#if defined(PSA_WANT_ALG_FFDH)
6174 if (alg == PSA_ALG_FFDH) {
6175 return PSA_SUCCESS;
6176 }
6177#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006178 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006180}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006181
6182static int psa_key_derivation_allows_free_form_secret_input(
6183 psa_algorithm_t kdf_alg)
6184{
6185#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6186 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6187 return 0;
6188 }
6189#endif
6190 (void) kdf_alg;
6191 return 1;
6192}
John Durkop07cc04a2020-11-16 22:08:34 -08006193#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006194
Gilles Peskine449bd832023-01-11 14:50:10 +01006195psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6196 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006197{
6198 psa_status_t status;
6199
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 if (operation->alg != 0) {
6201 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006202 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6205 return PSA_ERROR_INVALID_ARGUMENT;
6206 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6207#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6208 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6209 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6210 status = psa_key_agreement_try_support(ka_alg);
6211 if (status != PSA_SUCCESS) {
6212 return status;
6213 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006214 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6215 return PSA_ERROR_INVALID_ARGUMENT;
6216 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6218#else
6219 return PSA_ERROR_NOT_SUPPORTED;
6220#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6221 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6222#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6223 status = psa_key_derivation_setup_kdf(operation, alg);
6224#else
6225 return PSA_ERROR_NOT_SUPPORTED;
6226#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6227 } else {
6228 return PSA_ERROR_INVALID_ARGUMENT;
6229 }
6230
6231 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006232 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 }
6234 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006235}
6236
Przemek Stekiel69c46792022-06-10 12:59:51 +02006237#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006238static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6239 psa_algorithm_t kdf_alg,
6240 psa_key_derivation_step_t step,
6241 const uint8_t *data,
6242 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006243{
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006245 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006247 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006248#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6250 return PSA_ERROR_INVALID_ARGUMENT;
6251 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006252#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 if (hkdf->state != HKDF_STATE_INIT) {
6254 return PSA_ERROR_BAD_STATE;
6255 } else {
6256 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6257 hash_alg,
6258 data, data_length);
6259 if (status != PSA_SUCCESS) {
6260 return status;
6261 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006262 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006264 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006265 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006266#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006268 /* We shouldn't be in different state as HKDF_EXPAND only allows
6269 * two inputs: SECRET (this case) and INFO which does not modify
6270 * the state. It could happen only if the hkdf
6271 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 if (hkdf->state != HKDF_STATE_INIT) {
6273 return PSA_ERROR_BAD_STATE;
6274 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006275
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006276 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6278 return PSA_ERROR_INVALID_ARGUMENT;
6279 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 memcpy(hkdf->prk, data, data_length);
6282 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006283#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006284 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006285 /* HKDF: If no salt was provided, use an empty salt.
6286 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006288#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6290 return PSA_ERROR_BAD_STATE;
6291 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006292#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6294 hash_alg,
6295 NULL, 0);
6296 if (status != PSA_SUCCESS) {
6297 return status;
6298 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006299 hkdf->state = HKDF_STATE_STARTED;
6300 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 if (hkdf->state != HKDF_STATE_STARTED) {
6302 return PSA_ERROR_BAD_STATE;
6303 }
6304 status = psa_mac_update(&hkdf->hmac,
6305 data, data_length);
6306 if (status != PSA_SUCCESS) {
6307 return status;
6308 }
6309 status = psa_mac_sign_finish(&hkdf->hmac,
6310 hkdf->prk,
6311 sizeof(hkdf->prk),
6312 &data_length);
6313 if (status != PSA_SUCCESS) {
6314 return status;
6315 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006316 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006317
Gilles Peskine2b522db2019-04-12 15:11:49 +02006318 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006319 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006320#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006322 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006324 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006326#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006327 {
6328 /* Block 0 is empty, and the next block will be
6329 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006331 }
6332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006334 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006335#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6337 return PSA_ERROR_INVALID_ARGUMENT;
6338 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006339#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006340#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6342 hkdf->state == HKDF_STATE_INIT) {
6343 return PSA_ERROR_BAD_STATE;
6344 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006345#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 if (hkdf->state == HKDF_STATE_OUTPUT) {
6347 return PSA_ERROR_BAD_STATE;
6348 }
6349 if (hkdf->info_set) {
6350 return PSA_ERROR_BAD_STATE;
6351 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006352 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 if (data_length != 0) {
6354 hkdf->info = mbedtls_calloc(1, data_length);
6355 if (hkdf->info == NULL) {
6356 return PSA_ERROR_INSUFFICIENT_MEMORY;
6357 }
6358 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006359 }
6360 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006362 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006364 }
6365}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006366#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006367
John Durkop07cc04a2020-11-16 22:08:34 -08006368#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6369 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006370static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6371 const uint8_t *data,
6372 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006373{
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6375 return PSA_ERROR_BAD_STATE;
6376 }
Janos Follathf08e2652019-06-13 09:05:41 +01006377
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 if (data_length != 0) {
6379 prf->seed = mbedtls_calloc(1, data_length);
6380 if (prf->seed == NULL) {
6381 return PSA_ERROR_INSUFFICIENT_MEMORY;
6382 }
Janos Follathf08e2652019-06-13 09:05:41 +01006383
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006385 prf->seed_length = data_length;
6386 }
Janos Follathf08e2652019-06-13 09:05:41 +01006387
Gilles Peskine43f958b2020-12-13 14:55:14 +01006388 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006389
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006391}
6392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6394 const uint8_t *data,
6395 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006396{
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6398 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6399 return PSA_ERROR_BAD_STATE;
6400 }
Janos Follath81550542019-06-13 14:26:34 +01006401
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 if (data_length != 0) {
6403 prf->secret = mbedtls_calloc(1, data_length);
6404 if (prf->secret == NULL) {
6405 return PSA_ERROR_INSUFFICIENT_MEMORY;
6406 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006409 prf->secret_length = data_length;
6410 }
Janos Follath81550542019-06-13 14:26:34 +01006411
Gilles Peskine43f958b2020-12-13 14:55:14 +01006412 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006413
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006415}
6416
Gilles Peskine449bd832023-01-11 14:50:10 +01006417static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6418 const uint8_t *data,
6419 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006420{
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6422 return PSA_ERROR_BAD_STATE;
6423 }
Janos Follath63028dd2019-06-13 09:15:47 +01006424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 if (data_length != 0) {
6426 prf->label = mbedtls_calloc(1, data_length);
6427 if (prf->label == NULL) {
6428 return PSA_ERROR_INSUFFICIENT_MEMORY;
6429 }
Janos Follath63028dd2019-06-13 09:15:47 +01006430
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006432 prf->label_length = data_length;
6433 }
Janos Follath63028dd2019-06-13 09:15:47 +01006434
Gilles Peskine43f958b2020-12-13 14:55:14 +01006435 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006436
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006438}
6439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6441 psa_key_derivation_step_t step,
6442 const uint8_t *data,
6443 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006444{
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006446 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006448 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006450 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006452 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006454 }
6455}
John Durkop07cc04a2020-11-16 22:08:34 -08006456#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6457 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6458
6459#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6460static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6461 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006462 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006464{
6465 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6467 4 + data_length + prf->other_secret_length :
6468 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006469
Gilles Peskine449bd832023-01-11 14:50:10 +01006470 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6471 return PSA_ERROR_INVALID_ARGUMENT;
6472 }
John Durkop07cc04a2020-11-16 22:08:34 -08006473
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 uint8_t *pms = mbedtls_calloc(1, pms_len);
6475 if (pms == NULL) {
6476 return PSA_ERROR_INSUFFICIENT_MEMORY;
6477 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006478 uint8_t *cur = pms;
6479
6480 /* pure-PSK:
6481 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006482 *
6483 * The premaster secret is formed as follows: if the PSK is N octets
6484 * long, concatenate a uint16 with the value N, N zero octets, a second
6485 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006486 *
6487 * mixed-PSK:
6488 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6489 * follows: concatenate a uint16 with the length of the other secret,
6490 * the other secret itself, uint16 with the length of PSK, and the
6491 * PSK itself.
6492 * For details please check:
6493 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6494 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6495 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006496 */
6497
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6499 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6500 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6501 if (prf->other_secret_length != 0) {
6502 memcpy(cur, prf->other_secret, prf->other_secret_length);
6503 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006504 cur += prf->other_secret_length;
6505 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 } else {
6507 *cur++ = MBEDTLS_BYTE_1(data_length);
6508 *cur++ = MBEDTLS_BYTE_0(data_length);
6509 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006510 cur += data_length;
6511 }
6512
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 *cur++ = MBEDTLS_BYTE_1(data_length);
6514 *cur++ = MBEDTLS_BYTE_0(data_length);
6515 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006516 cur += data_length;
6517
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006518 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006519
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006520 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006522}
Janos Follath6660f0e2019-06-17 08:44:03 +01006523
Przemek Stekiele47201b2022-04-19 13:53:28 +02006524static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6525 psa_tls12_prf_key_derivation_t *prf,
6526 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006528{
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6530 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006531 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006532
6533 if (data_length != 0) {
6534 prf->other_secret = mbedtls_calloc(1, data_length);
6535 if (prf->other_secret == NULL) {
6536 return PSA_ERROR_INSUFFICIENT_MEMORY;
6537 }
6538
6539 memcpy(prf->other_secret, data, data_length);
6540 prf->other_secret_length = data_length;
6541 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006542 prf->other_secret_length = 0;
6543 }
6544
6545 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006548}
6549
Janos Follath6660f0e2019-06-17 08:44:03 +01006550static psa_status_t psa_tls12_prf_psk_to_ms_input(
6551 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006552 psa_key_derivation_step_t step,
6553 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006555{
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006557 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 return psa_tls12_prf_psk_to_ms_set_key(prf,
6559 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006560 break;
6561 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6563 data,
6564 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006565 break;
6566 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006568 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006569
Przemek Stekiele47201b2022-04-19 13:53:28 +02006570 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006571}
John Durkop07cc04a2020-11-16 22:08:34 -08006572#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006573
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006574#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6575static psa_status_t psa_tls12_ecjpake_to_pms_input(
6576 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006577 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006578 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006580{
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6582 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6583 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006584 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006585
6586 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 if (data[0] != 0x04) {
6588 return PSA_ERROR_INVALID_ARGUMENT;
6589 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006590
6591 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006593
Gilles Peskine449bd832023-01-11 14:50:10 +01006594 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006595}
6596#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306597
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306598#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306599static psa_status_t psa_pbkdf2_set_input_cost(
6600 psa_pbkdf2_key_derivation_t *pbkdf2,
6601 psa_key_derivation_step_t step,
6602 uint64_t data)
6603{
6604 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6605 return PSA_ERROR_INVALID_ARGUMENT;
6606 }
6607
6608 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6609 return PSA_ERROR_BAD_STATE;
6610 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306611
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306612 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306613 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306614 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306615
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306616 if (data == 0) {
6617 return PSA_ERROR_INVALID_ARGUMENT;
6618 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306619
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306620 pbkdf2->input_cost = data;
6621 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6622
6623 return PSA_SUCCESS;
6624}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306625
6626static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6627 const uint8_t *data,
6628 size_t data_length)
6629{
Gilles Peskineead17662023-08-20 22:02:51 +02006630 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6631 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6632 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6633 /* Appending to existing salt. No state change. */
6634 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306635 return PSA_ERROR_BAD_STATE;
6636 }
6637
Gilles Peskineca57d782023-07-20 19:08:06 +02006638 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006639 /* Appending an empty string, nothing to do. */
6640 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306641 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306642
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306643 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6644 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306645 return PSA_ERROR_INSUFFICIENT_MEMORY;
6646 }
6647
Gilles Peskineead17662023-08-20 22:02:51 +02006648 if (pbkdf2->salt_length != 0) {
6649 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6650 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306651 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306652 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306653 mbedtls_free(pbkdf2->salt);
6654 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306655 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306656 return PSA_SUCCESS;
6657}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306658
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306659#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306660static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306661 const uint8_t *input,
6662 size_t input_len,
6663 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306664 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306665{
6666 psa_status_t status = PSA_SUCCESS;
6667 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6668 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306669 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306670 } else {
6671 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306672 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306673 }
6674 return status;
6675}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306676#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306677
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306678#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306679static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6680 size_t input_len,
6681 uint8_t *output,
6682 size_t *output_len)
6683{
6684 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306685 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306686 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306687 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306688 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6689 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6690 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306691 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306692 * mac_size as the driver function sets mac_output_length = mac_size
6693 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306694 status = psa_driver_wrapper_mac_compute(&attributes,
6695 zeros, sizeof(zeros),
6696 PSA_ALG_CMAC, input, input_len,
6697 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306698 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6699 128U,
6700 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306701 output_len);
6702 } else {
6703 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306704 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306705 }
6706 return status;
6707}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306708#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306709
6710static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306711 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306712 const uint8_t *data,
6713 size_t data_length)
6714{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306715 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306716 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6717 return PSA_ERROR_BAD_STATE;
6718 }
6719
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306720#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306721 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6722 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6723 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6724 pbkdf2->password,
6725 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306726 } else
6727#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6728#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6729 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306730 status = psa_pbkdf2_cmac_set_password(data, data_length,
6731 pbkdf2->password,
6732 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306733 } else
6734#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6735 {
6736 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306737 }
6738
6739 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6740
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306741 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306742}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306743
6744static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306745 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306746 psa_key_derivation_step_t step,
6747 const uint8_t *data,
6748 size_t data_length)
6749{
6750 switch (step) {
6751 case PSA_KEY_DERIVATION_INPUT_SALT:
6752 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6753 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306754 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306755 default:
6756 return PSA_ERROR_INVALID_ARGUMENT;
6757 }
6758}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306759#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306760
Gilles Peskineb8965192019-09-24 16:21:10 +02006761/** Check whether the given key type is acceptable for the given
6762 * input step of a key derivation.
6763 *
6764 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6765 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6766 * Both secret and non-secret inputs can alternatively have the type
6767 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6768 * that the input was passed as a buffer rather than via a key object.
6769 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006770static int psa_key_derivation_check_input_type(
6771 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006772 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006773{
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006775 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006776 if (key_type == PSA_KEY_TYPE_DERIVE) {
6777 return PSA_SUCCESS;
6778 }
6779 if (key_type == PSA_KEY_TYPE_NONE) {
6780 return PSA_SUCCESS;
6781 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006782 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006783 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006784 if (key_type == PSA_KEY_TYPE_DERIVE) {
6785 return PSA_SUCCESS;
6786 }
6787 if (key_type == PSA_KEY_TYPE_NONE) {
6788 return PSA_SUCCESS;
6789 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006790 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006791 case PSA_KEY_DERIVATION_INPUT_LABEL:
6792 case PSA_KEY_DERIVATION_INPUT_SALT:
6793 case PSA_KEY_DERIVATION_INPUT_INFO:
6794 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6796 return PSA_SUCCESS;
6797 }
6798 if (key_type == PSA_KEY_TYPE_NONE) {
6799 return PSA_SUCCESS;
6800 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006801 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306802 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6803 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6804 return PSA_SUCCESS;
6805 }
6806 if (key_type == PSA_KEY_TYPE_DERIVE) {
6807 return PSA_SUCCESS;
6808 }
6809 if (key_type == PSA_KEY_TYPE_NONE) {
6810 return PSA_SUCCESS;
6811 }
6812 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006813 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006814 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006815}
6816
Janos Follathb80a94e2019-06-12 15:54:46 +01006817static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006818 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006819 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006820 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006821 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006823{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006824 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006826
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 status = psa_key_derivation_check_input_type(step, key_type);
6828 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006829 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006831
Przemek Stekiel69c46792022-06-10 12:59:51 +02006832#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006833 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6834 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6835 step, data, data_length);
6836 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006837#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006838#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6840 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6841 step, data, data_length);
6842 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006843#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6844#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6846 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6847 step, data, data_length);
6848 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006849#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006850#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006852 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6854 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006855#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306856#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306857 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306858 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6859 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306860 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306861#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006862 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006863 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006864 (void) data;
6865 (void) data_length;
6866 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006868 }
6869
Gilles Peskine224b0d62019-09-23 18:13:17 +02006870exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 if (status != PSA_SUCCESS) {
6872 psa_key_derivation_abort(operation);
6873 }
6874 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006875}
6876
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306877static psa_status_t psa_key_derivation_input_integer_internal(
6878 psa_key_derivation_operation_t *operation,
6879 psa_key_derivation_step_t step,
6880 uint64_t value)
6881{
6882 psa_status_t status;
6883 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6884
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306885#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306886 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306887 status = psa_pbkdf2_set_input_cost(
6888 &operation->ctx.pbkdf2, step, value);
6889 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306890#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306891 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306892 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306893 (void) value;
6894 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306895 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306896 }
6897
6898 if (status != PSA_SUCCESS) {
6899 psa_key_derivation_abort(operation);
6900 }
6901 return status;
6902}
6903
Janos Follath51f4a0f2019-06-14 11:35:55 +01006904psa_status_t psa_key_derivation_input_bytes(
6905 psa_key_derivation_operation_t *operation,
6906 psa_key_derivation_step_t step,
6907 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006909{
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 return psa_key_derivation_input_internal(operation, step,
6911 PSA_KEY_TYPE_NONE,
6912 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006913}
6914
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306915psa_status_t psa_key_derivation_input_integer(
6916 psa_key_derivation_operation_t *operation,
6917 psa_key_derivation_step_t step,
6918 uint64_t value)
6919{
6920 return psa_key_derivation_input_integer_internal(operation, step, value);
6921}
6922
Janos Follath51f4a0f2019-06-14 11:35:55 +01006923psa_status_t psa_key_derivation_input_key(
6924 psa_key_derivation_operation_t *operation,
6925 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006926 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006927{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006928 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006929 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006930 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006931
Ronald Cron5c522922020-11-14 16:35:34 +01006932 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006933 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6934 if (status != PSA_SUCCESS) {
6935 psa_key_derivation_abort(operation);
6936 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006937 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006938
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306939 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6940 * permission to output to a key object. */
6941 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6942 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006943 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006944 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006945
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 status = psa_key_derivation_input_internal(operation,
6947 step, slot->attr.type,
6948 slot->key.data,
6949 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006950
Ryan Everett5ac6fa72024-02-14 17:11:36 +00006951 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006952
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006954}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006955
6956
6957
6958/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006959/* Key agreement */
6960/****************************************************************/
6961
Gilles Peskine449bd832023-01-11 14:50:10 +01006962psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6963 const uint8_t *key_buffer,
6964 size_t key_buffer_size,
6965 psa_algorithm_t alg,
6966 const uint8_t *peer_key,
6967 size_t peer_key_length,
6968 uint8_t *shared_secret,
6969 size_t shared_secret_size,
6970 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006971{
Gilles Peskine449bd832023-01-11 14:50:10 +01006972 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006973#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006974 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6976 key_buffer_size, alg,
6977 peer_key, peer_key_length,
6978 shared_secret,
6979 shared_secret_size,
6980 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006981#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006982
6983#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6984 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006985 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006986 peer_key,
6987 peer_key_length,
6988 key_buffer,
6989 key_buffer_size,
6990 shared_secret,
6991 shared_secret_size,
6992 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006993#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6994
Gilles Peskine01d718c2018-09-18 12:01:02 +02006995 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006996 (void) attributes;
6997 (void) key_buffer;
6998 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006999 (void) peer_key;
7000 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007001 (void) shared_secret;
7002 (void) shared_secret_size;
7003 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007005 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007006}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007007
Aditya Deshpande17845b82022-10-13 17:21:01 +01007008/** Internal function for raw key agreement
7009 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007010 * to the driver's implementation if a driver is present.
7011 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007012 * (psa_key_agreement_raw_builtin).
7013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007014static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7015 psa_key_slot_t *private_key,
7016 const uint8_t *peer_key,
7017 size_t peer_key_length,
7018 uint8_t *shared_secret,
7019 size_t shared_secret_size,
7020 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007021{
Gilles Peskine449bd832023-01-11 14:50:10 +01007022 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7023 return PSA_ERROR_NOT_SUPPORTED;
7024 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007025
7026 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007028 };
7029
Gilles Peskine449bd832023-01-11 14:50:10 +01007030 return psa_driver_wrapper_key_agreement(&attributes,
7031 private_key->key.data,
7032 private_key->key.bytes, alg,
7033 peer_key, peer_key_length,
7034 shared_secret,
7035 shared_secret_size,
7036 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007037}
7038
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007039/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007040 * to potentially free embedded data structures and wipe confidential data.
7041 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007042static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7043 psa_key_derivation_step_t step,
7044 psa_key_slot_t *private_key,
7045 const uint8_t *peer_key,
7046 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007047{
7048 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007049 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007050 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007051 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007052
7053 /* Step 1: run the secret agreement algorithm to generate the shared
7054 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007055 status = psa_key_agreement_raw_internal(ka_alg,
7056 private_key,
7057 peer_key, peer_key_length,
7058 shared_secret,
7059 sizeof(shared_secret),
7060 &shared_secret_length);
7061 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007062 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007064
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007065 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007066 * the shared secret. A shared secret is permitted wherever a key
7067 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007068 status = psa_key_derivation_input_internal(operation, step,
7069 PSA_KEY_TYPE_DERIVE,
7070 shared_secret,
7071 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007072exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7074 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007075}
7076
Gilles Peskine449bd832023-01-11 14:50:10 +01007077psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7078 psa_key_derivation_step_t step,
7079 mbedtls_svc_key_id_t private_key,
7080 const uint8_t *peer_key,
7081 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007082{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007083 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007084 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007085 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007086
Gilles Peskine449bd832023-01-11 14:50:10 +01007087 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7088 return PSA_ERROR_INVALID_ARGUMENT;
7089 }
Ronald Cron5c522922020-11-14 16:35:34 +01007090 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7092 if (status != PSA_SUCCESS) {
7093 return status;
7094 }
7095 status = psa_key_agreement_internal(operation, step,
7096 slot,
7097 peer_key, peer_key_length);
7098 if (status != PSA_SUCCESS) {
7099 psa_key_derivation_abort(operation);
7100 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007101 /* If a private key has been added as SECRET, we allow the derived
7102 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007104 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007106 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007107
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007108 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007109
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007111}
7112
Gilles Peskine449bd832023-01-11 14:50:10 +01007113psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7114 mbedtls_svc_key_id_t private_key,
7115 const uint8_t *peer_key,
7116 size_t peer_key_length,
7117 uint8_t *output,
7118 size_t output_size,
7119 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007120{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007121 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007122 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007123 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007124 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007125
Gilles Peskine449bd832023-01-11 14:50:10 +01007126 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007127 status = PSA_ERROR_INVALID_ARGUMENT;
7128 goto exit;
7129 }
Ronald Cron5c522922020-11-14 16:35:34 +01007130 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7132 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007133 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007135
Gilles Peskine3e561302022-04-14 00:17:15 +02007136 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7137 * for the output size. The PSA specification only guarantees that this
7138 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7139 * but it might be nice to allow smaller buffers if the output fits.
7140 * At the time of writing this comment, with only ECDH implemented,
7141 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7142 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7143 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007144 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007145 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7146 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007147 status = PSA_ERROR_BUFFER_TOO_SMALL;
7148 goto exit;
7149 }
7150
Gilles Peskine449bd832023-01-11 14:50:10 +01007151 status = psa_key_agreement_raw_internal(alg, slot,
7152 peer_key, peer_key_length,
7153 output, output_size,
7154 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007155
7156exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007158 /* If an error happens and is not handled properly, the output
7159 * may be used as a key to protect sensitive data. Arrange for such
7160 * a key to be random, which is likely to result in decryption or
7161 * verification errors. This is better than filling the buffer with
7162 * some constant data such as zeros, which would result in the data
7163 * being protected with a reproducible, easily knowable key.
7164 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007165 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007166 *output_length = output_size;
7167 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007168
Ryan Everetta103ec92024-01-31 13:59:57 +00007169 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007170
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007172}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007173
7174
Gilles Peskine30524eb2020-11-13 17:02:26 +01007175
Gilles Peskine86a440b2018-11-12 18:39:40 +01007176/****************************************************************/
7177/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007178/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007179
Gilles Peskine672a7712023-04-28 21:00:28 +02007180#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7181#include "entropy_poll.h"
7182#endif
7183
Gilles Peskine30524eb2020-11-13 17:02:26 +01007184/** Initialize the PSA random generator.
7185 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007186static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007187{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007188#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007190#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7191
Gilles Peskine30524eb2020-11-13 17:02:26 +01007192 /* Set default configuration if
7193 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007195 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 }
7197 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007198 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007199 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007200
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007202#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7203 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7204 /* The PSA entropy injection feature depends on using NV seed as an entropy
7205 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007206 mbedtls_entropy_add_source(&rng->entropy,
7207 mbedtls_nv_seed_poll, NULL,
7208 MBEDTLS_ENTROPY_BLOCK_SIZE,
7209 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007210#endif
7211
Gilles Peskine449bd832023-01-11 14:50:10 +01007212 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007213#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007214}
7215
7216/** Deinitialize the PSA random generator.
7217 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007218static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007219{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007220#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007221 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007222#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti83e0de82023-11-24 12:13:05 +01007223 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7224 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007225#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007226}
7227
7228/** Seed the PSA random generator.
7229 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007230static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007231{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007232#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7233 /* Do nothing: the external RNG seeds itself. */
7234 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007235 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007236#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007237 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007238 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7239 drbg_seed, sizeof(drbg_seed) - 1);
7240 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007241#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007242}
7243
Gilles Peskine449bd832023-01-11 14:50:10 +01007244psa_status_t psa_generate_random(uint8_t *output,
7245 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007246{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007247 GUARD_MODULE_INITIALIZED;
7248
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007249#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7250
7251 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7253 output, output_size,
7254 &output_length);
7255 if (status != PSA_SUCCESS) {
7256 return status;
7257 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007258 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007259 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 if (output_length != output_size) {
7261 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7262 }
7263 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007264
7265#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7266
Gilles Peskine449bd832023-01-11 14:50:10 +01007267 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007268 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7270 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7271 output_size);
7272 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7273 output, request_size);
7274 if (ret != 0) {
7275 return mbedtls_to_psa_error(ret);
7276 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007277 output_size -= request_size;
7278 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007279 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007281#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007282}
7283
Gilles Peskine8814fc42020-12-14 15:33:44 +01007284/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007285 *
7286 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7287 * `psa_generate_random(...)`. The state parameter is ignored since the
7288 * PSA API doesn't support passing an explicit state.
7289 *
7290 * In the non-external case, psa_generate_random() calls an
7291 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7292 * and semantics as mbedtls_psa_get_random(). As an optimization,
7293 * instead of doing this back-and-forth between the PSA API and the
7294 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7295 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007296 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007297#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7298int mbedtls_psa_get_random(void *p_rng,
7299 unsigned char *output,
7300 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007301{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007302 /* This function takes a pointer to the RNG state because that's what
7303 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7304 * its own state internally and doesn't let the caller access that state.
7305 * So we just ignore the state parameter, and in practice we'll pass
7306 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007307 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 psa_status_t status = psa_generate_random(output, output_size);
7309 if (status == PSA_SUCCESS) {
7310 return 0;
7311 } else {
7312 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7313 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007314}
7315#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7316
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007317#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007318psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7319 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007320{
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 if (global_data.initialized) {
7322 return PSA_ERROR_NOT_PERMITTED;
7323 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007324
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7326 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7327 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7328 return PSA_ERROR_INVALID_ARGUMENT;
7329 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007330
Gilles Peskine449bd832023-01-11 14:50:10 +01007331 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007332}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007333#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007334
Ronald Cron3772afe2021-02-08 16:10:05 +01007335/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007336 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007337 * \param type The key type
7338 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007339 *
7340 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007341 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007342 * \retval #PSA_ERROR_INVALID_ARGUMENT
7343 * The size in bits of the key is not valid.
7344 * \retval #PSA_ERROR_NOT_SUPPORTED
7345 * The type and/or the size in bits of the key or the combination of
7346 * the two is not supported.
7347 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007348static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007350{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007351 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007352
Gilles Peskine449bd832023-01-11 14:50:10 +01007353 if (key_type_is_raw_bytes(type)) {
7354 status = psa_validate_unstructured_key_bit_size(type, bits);
7355 if (status != PSA_SUCCESS) {
7356 return status;
7357 }
7358 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007359#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7361 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7362 return PSA_ERROR_NOT_SUPPORTED;
7363 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007364 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007365 return PSA_ERROR_NOT_SUPPORTED;
7366 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007367
Ronald Cron01b2aba2020-10-05 09:42:02 +02007368 /* Accept only byte-aligned keys, for the same reasons as
7369 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007370 if (bits % 8 != 0) {
7371 return PSA_ERROR_NOT_SUPPORTED;
7372 }
7373 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007374#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007375
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007376#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007378 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007379 return PSA_SUCCESS;
7380 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007381#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007382
Valerio Settia55f0422023-07-10 15:34:41 +02007383#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007384 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007385 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007386 return PSA_ERROR_NOT_SUPPORTED;
7387 }
7388 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007389#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007390 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007392 }
7393
Gilles Peskine449bd832023-01-11 14:50:10 +01007394 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007395}
7396
Ronald Cron55ed0592020-10-05 10:30:40 +02007397psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007398 const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007399 const psa_key_production_parameters_t *params, size_t params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007400 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007401{
7402 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007403 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007404
Gilles Peskine7a18f962024-02-12 16:48:11 +01007405 /* Only used for RSA */
Gilles Peskine092ce512024-02-20 12:31:24 +01007406 (void) params;
7407 (void) params_data_length;
Gilles Peskine7a18f962024-02-12 16:48:11 +01007408
Gilles Peskine449bd832023-01-11 14:50:10 +01007409 if (key_type_is_raw_bytes(type)) {
7410 status = psa_generate_random(key_buffer, key_buffer_size);
7411 if (status != PSA_SUCCESS) {
7412 return status;
7413 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007414
David Brown12ca5032021-02-11 11:02:00 -07007415#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007416 if (type == PSA_KEY_TYPE_DES) {
7417 psa_des_set_key_parity(key_buffer, key_buffer_size);
7418 }
David Brown8107e312021-02-12 08:08:46 -07007419#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007421
Valerio Setti76df8c12023-07-11 14:11:28 +02007422#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskinee22f6a92024-02-26 15:45:33 +01007424 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskine4c32b692024-02-16 00:11:09 +01007425 params, params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007426 key_buffer,
7427 key_buffer_size,
7428 key_buffer_length);
7429 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007430#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007431
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007432#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7434 return mbedtls_psa_ecp_generate_key(attributes,
7435 key_buffer,
7436 key_buffer_size,
7437 key_buffer_length);
7438 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007439#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007440
Valerio Settia55f0422023-07-10 15:34:41 +02007441#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007442 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7443 return mbedtls_psa_ffdh_generate_key(attributes,
7444 key_buffer,
7445 key_buffer_size,
7446 key_buffer_length);
7447 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007448#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007449 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 (void) key_buffer_length;
7451 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007452 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007453
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007455}
Darryl Green0c6575a2018-11-07 16:05:30 +00007456
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007457psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007458 const psa_key_production_parameters_t *params,
7459 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007460 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007461{
7462 psa_status_t status;
7463 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007464 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007465 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007466
Ronald Cron81709fc2020-11-14 12:10:32 +01007467 *key = MBEDTLS_SVC_KEY_ID_INIT;
7468
Gilles Peskine0f84d622019-09-12 19:03:13 +02007469 /* Reject any attempt to create a zero-length key so that we don't
7470 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 if (psa_get_key_bits(attributes) == 0) {
7472 return PSA_ERROR_INVALID_ARGUMENT;
7473 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007474
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007475 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7477 return PSA_ERROR_INVALID_ARGUMENT;
7478 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007479
Gilles Peskine7a18f962024-02-12 16:48:11 +01007480#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
7481 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +01007482 if (params->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01007483 return PSA_ERROR_INVALID_ARGUMENT;
7484 }
7485 } else
7486#endif
Gilles Peskine092ce512024-02-20 12:31:24 +01007487 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007488 return PSA_ERROR_INVALID_ARGUMENT;
7489 }
7490
Gilles Peskine449bd832023-01-11 14:50:10 +01007491 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7492 &slot, &driver);
7493 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007494 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007495 }
Gilles Peskine11792082019-08-06 18:36:36 +02007496
Ronald Cron977c2472020-10-13 08:32:21 +02007497 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307498 * storage ( thus not in the case of generating a key in a secure element
7499 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7500 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 if (slot->key.data == NULL) {
7502 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7503 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007504 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007505 attributes->core.type, attributes->core.bits);
7506 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007507 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007509
7510 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007511 attributes->core.type,
7512 attributes->core.bits);
7513 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007514 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007515 attributes, &key_buffer_size);
7516 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007517 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007518 }
Ronald Cron977c2472020-10-13 08:32:21 +02007519 }
Ronald Cron977c2472020-10-13 08:32:21 +02007520
Gilles Peskine449bd832023-01-11 14:50:10 +01007521 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7522 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007523 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007524 }
Ronald Cron977c2472020-10-13 08:32:21 +02007525 }
7526
Gilles Peskine449bd832023-01-11 14:50:10 +01007527 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007528 params, params_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01007529 slot->key.data, slot->key.bytes,
7530 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 if (status != PSA_SUCCESS) {
7532 psa_remove_key_data_from_memory(slot);
7533 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007534
Gilles Peskine11792082019-08-06 18:36:36 +02007535exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 if (status == PSA_SUCCESS) {
7537 status = psa_finish_key_creation(slot, driver, key);
7538 }
7539 if (status != PSA_SUCCESS) {
7540 psa_fail_key_creation(slot, driver);
7541 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007542
Gilles Peskine449bd832023-01-11 14:50:10 +01007543 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007544}
7545
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007546psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7547 mbedtls_svc_key_id_t *key)
7548{
7549 return psa_generate_key_ext(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007550 &default_production_parameters, 0,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007551 key);
7552}
7553
Gilles Peskinee59236f2018-01-27 23:32:46 +01007554/****************************************************************/
7555/* Module setup */
7556/****************************************************************/
7557
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007558#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007559psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007560 void (* entropy_init)(mbedtls_entropy_context *ctx),
7561 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007562{
Gilles Peskine449bd832023-01-11 14:50:10 +01007563 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7564 return PSA_ERROR_BAD_STATE;
7565 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007566 global_data.rng.entropy_init = entropy_init;
7567 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007568 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007569}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007570#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007571
Gilles Peskine449bd832023-01-11 14:50:10 +01007572void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007573{
Gilles Peskine449bd832023-01-11 14:50:10 +01007574 psa_wipe_all_key_slots();
Valerio Setti83e0de82023-11-24 12:13:05 +01007575 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7576 mbedtls_psa_random_free(&global_data.rng);
7577 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007578 /* Wipe all remaining data, including configuration.
7579 * In particular, this sets all state indicator to the value
7580 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007582
7583 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007584 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007585}
7586
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007587#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7588/** Recover a transaction that was interrupted by a power failure.
7589 *
7590 * This function is called during initialization, before psa_crypto_init()
7591 * returns. If this function returns a failure status, the initialization
7592 * fails.
7593 */
7594static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007596{
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007598 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7599 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007600 /* TODO - fall through to the failure case until this
7601 * is implemented.
7602 * https://github.com/ARMmbed/mbed-crypto/issues/218
7603 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007604 default:
7605 /* We found an unsupported transaction in the storage.
7606 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007608 }
7609}
7610#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7611
Gilles Peskine449bd832023-01-11 14:50:10 +01007612psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007613{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007614 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007615
Gilles Peskinec6b69072018-11-20 21:42:52 +01007616 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 if (global_data.initialized != 0) {
7618 return PSA_SUCCESS;
7619 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007620
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007621 /* Init drivers */
7622 status = psa_driver_wrapper_init();
7623 if (status != PSA_SUCCESS) {
7624 goto exit;
7625 }
7626 global_data.drivers_initialized = 1;
7627
Valerio Setti402cfba2023-11-13 10:24:32 +01007628 status = psa_initialize_key_slots();
7629 if (status != PSA_SUCCESS) {
7630 goto exit;
7631 }
7632
Gilles Peskine30524eb2020-11-13 17:02:26 +01007633 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007634 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007635 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007636 status = mbedtls_psa_random_seed(&global_data.rng);
7637 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007638 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007640 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007641
Gilles Peskine4b734222019-07-24 15:56:31 +02007642#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 status = psa_crypto_load_transaction();
7644 if (status == PSA_SUCCESS) {
7645 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7646 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007647 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007648 }
7649 status = psa_crypto_stop_transaction();
7650 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007651 /* There's no transaction to complete. It's all good. */
7652 status = PSA_SUCCESS;
7653 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007654#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007655
Gilles Peskinec6b69072018-11-20 21:42:52 +01007656 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007657 global_data.initialized = 1;
7658
7659exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007660 if (status != PSA_SUCCESS) {
7661 mbedtls_psa_crypto_free();
7662 }
7663 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007664}
7665
Yanray Wangffc3c482023-07-11 12:01:04 +08007666#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007667psa_status_t psa_crypto_driver_pake_get_password_len(
7668 const psa_crypto_driver_pake_inputs_t *inputs,
7669 size_t *password_len)
7670{
7671 if (inputs->password_len == 0) {
7672 return PSA_ERROR_BAD_STATE;
7673 }
7674
7675 *password_len = inputs->password_len;
7676
7677 return PSA_SUCCESS;
7678}
7679
7680psa_status_t psa_crypto_driver_pake_get_password(
7681 const psa_crypto_driver_pake_inputs_t *inputs,
7682 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7683{
7684 if (inputs->password_len == 0) {
7685 return PSA_ERROR_BAD_STATE;
7686 }
7687
7688 if (buffer_size < inputs->password_len) {
7689 return PSA_ERROR_BUFFER_TOO_SMALL;
7690 }
7691
7692 memcpy(buffer, inputs->password, inputs->password_len);
7693 *buffer_length = inputs->password_len;
7694
7695 return PSA_SUCCESS;
7696}
7697
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007698psa_status_t psa_crypto_driver_pake_get_user_len(
7699 const psa_crypto_driver_pake_inputs_t *inputs,
7700 size_t *user_len)
7701{
7702 if (inputs->user_len == 0) {
7703 return PSA_ERROR_BAD_STATE;
7704 }
7705
7706 *user_len = inputs->user_len;
7707
7708 return PSA_SUCCESS;
7709}
7710
7711psa_status_t psa_crypto_driver_pake_get_user(
7712 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007713 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007714{
7715 if (inputs->user_len == 0) {
7716 return PSA_ERROR_BAD_STATE;
7717 }
7718
Przemek Stekielc0e62502023-03-14 11:49:36 +01007719 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007720 return PSA_ERROR_BUFFER_TOO_SMALL;
7721 }
7722
Przemek Stekielc0e62502023-03-14 11:49:36 +01007723 memcpy(user_id, inputs->user, inputs->user_len);
7724 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007725
7726 return PSA_SUCCESS;
7727}
7728
7729psa_status_t psa_crypto_driver_pake_get_peer_len(
7730 const psa_crypto_driver_pake_inputs_t *inputs,
7731 size_t *peer_len)
7732{
7733 if (inputs->peer_len == 0) {
7734 return PSA_ERROR_BAD_STATE;
7735 }
7736
7737 *peer_len = inputs->peer_len;
7738
7739 return PSA_SUCCESS;
7740}
7741
7742psa_status_t psa_crypto_driver_pake_get_peer(
7743 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007744 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007745{
7746 if (inputs->peer_len == 0) {
7747 return PSA_ERROR_BAD_STATE;
7748 }
7749
Przemek Stekielc0e62502023-03-14 11:49:36 +01007750 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007751 return PSA_ERROR_BUFFER_TOO_SMALL;
7752 }
7753
Przemek Stekielc0e62502023-03-14 11:49:36 +01007754 memcpy(peer_id, inputs->peer, inputs->peer_len);
7755 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007756
7757 return PSA_SUCCESS;
7758}
7759
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007760psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7761 const psa_crypto_driver_pake_inputs_t *inputs,
7762 psa_pake_cipher_suite_t *cipher_suite)
7763{
7764 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7765 return PSA_ERROR_BAD_STATE;
7766 }
7767
7768 *cipher_suite = inputs->cipher_suite;
7769
7770 return PSA_SUCCESS;
7771}
7772
Neil Armstronga7d08c32022-06-01 18:21:20 +02007773psa_status_t psa_pake_setup(
7774 psa_pake_operation_t *operation,
7775 const psa_pake_cipher_suite_t *cipher_suite)
7776{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007777 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007778
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007779 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007780 status = PSA_ERROR_BAD_STATE;
7781 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007782 }
7783
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007784 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007785 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007786 status = PSA_ERROR_INVALID_ARGUMENT;
7787 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007788 }
7789
Przemek Stekiel51eac532022-12-07 11:04:51 +01007790 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7791
Przemek Stekiele12ed362022-12-21 12:54:46 +01007792 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007793 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7794 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007795 operation->data.inputs.cipher_suite = *cipher_suite;
7796
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007797#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007798 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007799 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007800 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007801
David Horstmann16f01512023-06-14 17:21:07 +01007802 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007803 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007804 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007805#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007806 {
7807 status = PSA_ERROR_NOT_SUPPORTED;
7808 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007809 }
7810
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007811 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7812
Przemek Stekiel51eac532022-12-07 11:04:51 +01007813 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007814exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007815 psa_pake_abort(operation);
7816 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007817}
7818
7819psa_status_t psa_pake_set_password_key(
7820 psa_pake_operation_t *operation,
7821 mbedtls_svc_key_id_t password)
7822{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007823 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007824 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7825 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007826 psa_key_attributes_t attributes;
7827 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007828
Przemek Stekiel51eac532022-12-07 11:04:51 +01007829 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007830 status = PSA_ERROR_BAD_STATE;
7831 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007832 }
7833
Przemek Stekiel6c764412022-11-22 14:05:12 +01007834 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7835 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007836 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007837 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007838 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007839 }
7840
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007841 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007842 .core = slot->attr
7843 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007844
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007845 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007846
Przemek Stekiel51eac532022-12-07 11:04:51 +01007847 if (type != PSA_KEY_TYPE_PASSWORD &&
7848 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7849 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007850 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007851 }
7852
Przemek Stekiel51eac532022-12-07 11:04:51 +01007853 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7854 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007855 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007856 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007857 }
7858
7859 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7860 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007861 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007862exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007863 if (status != PSA_SUCCESS) {
7864 psa_pake_abort(operation);
7865 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00007866 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007867 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007868}
7869
7870psa_status_t psa_pake_set_user(
7871 psa_pake_operation_t *operation,
7872 const uint8_t *user_id,
7873 size_t user_id_len)
7874{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007875 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007876
7877 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007878 status = PSA_ERROR_BAD_STATE;
7879 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007880 }
7881
Przemek Stekiel51eac532022-12-07 11:04:51 +01007882 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007883 status = PSA_ERROR_INVALID_ARGUMENT;
7884 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007885 }
7886
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007887 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007888 status = PSA_ERROR_BAD_STATE;
7889 goto exit;
7890 }
7891
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007892 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7893 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007894 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7895 goto exit;
7896 }
7897
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007898 memcpy(operation->data.inputs.user, user_id, user_id_len);
7899 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007900
7901 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007902exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007903 psa_pake_abort(operation);
7904 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007905}
7906
7907psa_status_t psa_pake_set_peer(
7908 psa_pake_operation_t *operation,
7909 const uint8_t *peer_id,
7910 size_t peer_id_len)
7911{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007912 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007913
7914 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007915 status = PSA_ERROR_BAD_STATE;
7916 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007917 }
7918
Przemek Stekiel51eac532022-12-07 11:04:51 +01007919 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007920 status = PSA_ERROR_INVALID_ARGUMENT;
7921 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007922 }
7923
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007924 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007925 status = PSA_ERROR_BAD_STATE;
7926 goto exit;
7927 }
7928
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007929 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7930 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007931 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7932 goto exit;
7933 }
7934
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007935 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7936 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007937
7938 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007939exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007940 psa_pake_abort(operation);
7941 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007942}
7943
7944psa_status_t psa_pake_set_role(
7945 psa_pake_operation_t *operation,
7946 psa_pake_role_t role)
7947{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007948 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007949
Przemek Stekiel51eac532022-12-07 11:04:51 +01007950 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007951 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007952 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007953 }
7954
Przemek Stekiel09104b82023-03-06 14:11:51 +01007955 switch (operation->alg) {
7956#if defined(PSA_WANT_ALG_JPAKE)
7957 case PSA_ALG_JPAKE:
7958 if (role == PSA_PAKE_ROLE_NONE) {
7959 return PSA_SUCCESS;
7960 }
7961 status = PSA_ERROR_INVALID_ARGUMENT;
7962 break;
7963#endif
7964 default:
7965 (void) role;
7966 status = PSA_ERROR_NOT_SUPPORTED;
7967 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007968 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007969exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007970 psa_pake_abort(operation);
7971 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007972}
7973
David Horstmanne7f21e62023-05-12 18:17:21 +01007974/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007975#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007976static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007977 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007978{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007979 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007980 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007981 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007982
David Horstmann024e5c52023-06-14 15:48:21 +01007983 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007984 is_x1 = (stage->outputs < 1);
7985 } else {
7986 is_x1 = (stage->inputs < 1);
7987 }
7988
David Horstmann74a3d8c2023-06-14 18:28:19 +01007989 key_share_step = is_x1 ?
7990 PSA_JPAKE_X1_STEP_KEY_SHARE :
7991 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007992 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007993 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7994 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7995 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7996 } else {
7997 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007998 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01007999 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008000}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008001#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008002
Przemek Stekiel2797d372022-12-22 11:19:22 +01008003static psa_status_t psa_pake_complete_inputs(
8004 psa_pake_operation_t *operation)
8005{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008006 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008007 /* Create copy of the inputs on stack as inputs share memory
8008 with the driver context which will be setup by the driver. */
8009 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008010
Przemek Stekielfde11282023-03-13 16:06:09 +01008011 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008012 return PSA_ERROR_BAD_STATE;
8013 }
8014
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008015 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008016 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8017 return PSA_ERROR_BAD_STATE;
8018 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008019 }
8020
Przemek Stekiel18620a32023-01-17 16:34:52 +01008021 /* Clear driver context */
8022 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8023
8024 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008025
8026 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008027 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008028
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008029 /* User and peer are translated to role. */
8030 mbedtls_free(inputs.user);
8031 mbedtls_free(inputs.peer);
8032
Przemek Stekiel2797d372022-12-22 11:19:22 +01008033 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008034#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008035 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008036 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008037 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008038#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008039 {
8040 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008041 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008042 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008043 return status;
8044}
8045
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008046#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008047static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008048 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008049 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008050 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008051{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008052 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8053 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8054 step != PSA_PAKE_STEP_ZK_PROOF) {
8055 return PSA_ERROR_INVALID_ARGUMENT;
8056 }
8057
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008058 psa_jpake_computation_stage_t *computation_stage =
8059 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008060
David Horstmann5da95602023-06-08 15:37:12 +01008061 if (computation_stage->round != PSA_JPAKE_FIRST &&
8062 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008063 return PSA_ERROR_BAD_STATE;
8064 }
8065
David Horstmanne7f21e62023-05-12 18:17:21 +01008066 /* Check that the step we are given is the one we were expecting */
8067 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008068 return PSA_ERROR_BAD_STATE;
8069 }
8070
David Horstmanne7f21e62023-05-12 18:17:21 +01008071 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8072 computation_stage->inputs == 0 &&
8073 computation_stage->outputs == 0) {
8074 /* Start of the round, so function decides whether we are inputting
8075 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008076 computation_stage->io_mode = io_mode;
8077 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008078 /* Middle of the round so the mode we are in must match the function
8079 * called by the user */
8080 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008081 }
8082
8083 return PSA_SUCCESS;
8084}
8085
David Horstmanne7f21e62023-05-12 18:17:21 +01008086static psa_status_t psa_jpake_epilogue(
8087 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008088 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008089{
David Horstmanne7f21e62023-05-12 18:17:21 +01008090 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008091 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008092
David Horstmanne7f21e62023-05-12 18:17:21 +01008093 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8094 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008095 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008096 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008097 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008098 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008099 }
8100 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008101 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008102 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008103 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008104 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008105 }
8106 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008107 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8108 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008109 /* End of a round, move to the next round */
8110 stage->inputs = 0;
8111 stage->outputs = 0;
8112 stage->round++;
8113 }
8114 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008115 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008116 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008117 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008118 return PSA_SUCCESS;
8119}
David Horstmanne7f21e62023-05-12 18:17:21 +01008120
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008121#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008122
Neil Armstronga7d08c32022-06-01 18:21:20 +02008123psa_status_t psa_pake_output(
8124 psa_pake_operation_t *operation,
8125 psa_pake_step_t step,
8126 uint8_t *output,
8127 size_t output_size,
8128 size_t *output_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 Stekielf62b3bb2023-01-31 19:51:24 +01008132 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008133
8134 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008135 status = psa_pake_complete_inputs(operation);
8136 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008137 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008138 }
8139 }
8140
8141 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008142 status = PSA_ERROR_BAD_STATE;
8143 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008144 }
8145
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008146 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008147 status = PSA_ERROR_INVALID_ARGUMENT;
8148 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008149 }
8150
Przemek Stekiele12ed362022-12-21 12:54:46 +01008151 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008152#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008153 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008154 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008155 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008156 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008157 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008158 driver_step = convert_jpake_computation_stage_to_driver_step(
8159 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008160 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008161#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008162 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008163 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008164 status = PSA_ERROR_NOT_SUPPORTED;
8165 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008166 }
8167
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008168 status = psa_driver_wrapper_pake_output(operation, driver_step,
8169 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008170
8171 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008172 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008173 }
8174
8175 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008176#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008177 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008178 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008179 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008180 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008181 }
8182 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008183#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008184 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008185 status = PSA_ERROR_NOT_SUPPORTED;
8186 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008187 }
8188
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008189 return PSA_SUCCESS;
8190exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008191 psa_pake_abort(operation);
8192 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008193}
8194
8195psa_status_t psa_pake_input(
8196 psa_pake_operation_t *operation,
8197 psa_pake_step_t step,
8198 const uint8_t *input,
8199 size_t input_length)
8200{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008201 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008202 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008203 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8204 operation->primitive,
8205 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008206
8207 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008208 status = psa_pake_complete_inputs(operation);
8209 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008210 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008211 }
8212 }
8213
8214 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008215 status = PSA_ERROR_BAD_STATE;
8216 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008217 }
8218
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008219 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008220 status = PSA_ERROR_INVALID_ARGUMENT;
8221 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008222 }
8223
Przemek Stekiele12ed362022-12-21 12:54:46 +01008224 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008225#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008226 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008227 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008228 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008229 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008230 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008231 driver_step = convert_jpake_computation_stage_to_driver_step(
8232 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008233 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008234#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008235 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008236 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008237 status = PSA_ERROR_NOT_SUPPORTED;
8238 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008239 }
8240
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008241 status = psa_driver_wrapper_pake_input(operation, driver_step,
8242 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008243
8244 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008245 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008246 }
8247
8248 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008249#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008250 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008251 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008252 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008253 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008254 }
8255 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008256#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008257 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008258 status = PSA_ERROR_NOT_SUPPORTED;
8259 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008260 }
8261
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008262 return PSA_SUCCESS;
8263exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008264 psa_pake_abort(operation);
8265 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008266}
8267
8268psa_status_t psa_pake_get_implicit_key(
8269 psa_pake_operation_t *operation,
8270 psa_key_derivation_operation_t *output)
8271{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008272 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008273 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008274 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008275 size_t shared_key_len = 0;
8276
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008277 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008278 status = PSA_ERROR_BAD_STATE;
8279 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008280 }
8281
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008282#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008283 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008284 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008285 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008286 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008287 status = PSA_ERROR_BAD_STATE;
8288 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008289 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008290 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008291#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008292 {
8293 status = PSA_ERROR_NOT_SUPPORTED;
8294 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008295 }
8296
Przemek Stekiel0c781802022-11-29 14:53:13 +01008297 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8298 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008299 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008300 &shared_key_len);
8301
8302 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008303 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008304 }
8305
8306 status = psa_key_derivation_input_bytes(output,
8307 PSA_KEY_DERIVATION_INPUT_SECRET,
8308 shared_key,
8309 shared_key_len);
8310
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008311 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008312exit:
8313 abort_status = psa_pake_abort(operation);
8314 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008315}
8316
8317psa_status_t psa_pake_abort(
8318 psa_pake_operation_t *operation)
8319{
Przemek Stekield69dca92023-01-31 19:59:20 +01008320 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008321
Przemek Stekield69dca92023-01-31 19:59:20 +01008322 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008323 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008324 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008325
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008326 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8327 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008328 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008329 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008330 }
8331 if (operation->data.inputs.user != NULL) {
8332 mbedtls_free(operation->data.inputs.user);
8333 }
8334 if (operation->data.inputs.peer != NULL) {
8335 mbedtls_free(operation->data.inputs.peer);
8336 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008337 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008338 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008339
Przemek Stekield69dca92023-01-31 19:59:20 +01008340 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008341}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008342#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008343
Gilles Peskinee59236f2018-01-27 23:32:46 +01008344#endif /* MBEDTLS_PSA_CRYPTO_C */