blob: 91f5f161623744fd7379e1b0e21f5f09534143c1 [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;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100571 psa_key_type_t type = attributes->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 Peskine2f107ae2024-02-28 01:26:46 +0100581 status = psa_validate_unstructured_key_bit_size(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 *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 Peskine7fad3ef2024-02-28 01:08:27 +01001229 *attributes = slot->attr;
1230 attributes->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 Peskine972539c2024-02-28 01:49:45 +01001248 if (attributes->has_slot_number) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001249 *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{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001278 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001279
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
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001327 status = psa_driver_wrapper_export_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 slot->key.data, slot->key.bytes,
1329 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001330
Ryan Everetta103ec92024-01-31 13:59:57 +00001331 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001334}
1335
Ronald Cron7285cda2020-11-26 14:46:37 +01001336psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001337 const psa_key_attributes_t *attributes,
1338 const uint8_t *key_buffer,
1339 size_t key_buffer_size,
1340 uint8_t *data,
1341 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001343{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001344 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001345
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001346 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001347 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1348 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001349 /* Exporting public -> public */
1350 return psa_export_key_buffer_internal(
1351 key_buffer, key_buffer_size,
1352 data, data_size, data_length);
1353 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001354#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001355 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1356 return mbedtls_psa_rsa_export_public_key(attributes,
1357 key_buffer,
1358 key_buffer_size,
1359 data,
1360 data_size,
1361 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001362#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001363 /* We don't know how to convert a private RSA key to public. */
1364 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001365#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001366 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001367 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001368#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001369 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1370 return mbedtls_psa_ecp_export_public_key(attributes,
1371 key_buffer,
1372 key_buffer_size,
1373 data,
1374 data_size,
1375 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001376#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001377 /* We don't know how to convert a private ECC key to public */
1378 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001379#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001380 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001381 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001382#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001383 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001384 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001385 key_buffer,
1386 key_buffer_size,
1387 data, data_size,
1388 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001389#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001390 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001391#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001392 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001394 (void) key_buffer;
1395 (void) key_buffer_size;
1396 (void) data;
1397 (void) data_size;
1398 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001400 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001401}
Ronald Crond18b5f82020-11-25 16:46:05 +01001402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1404 uint8_t *data,
1405 size_t data_size,
1406 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001407{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001408 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001409 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001410 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001411
Ronald Crone907e552021-01-18 13:22:38 +01001412 /* Reject a zero-length output buffer now, since this can never be a
1413 * valid key representation. This way we know that data must be a valid
1414 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if (data_size == 0) {
1416 return PSA_ERROR_BUFFER_TOO_SMALL;
1417 }
Ronald Crone907e552021-01-18 13:22:38 +01001418
Darryl Greendd8fb772018-11-07 16:00:44 +00001419 /* Set the key to empty now, so that even when there are errors, we always
1420 * set data_length to a value between 0 and data_size. On error, setting
1421 * the key to empty is a good choice because an empty key representation is
1422 * unlikely to be accepted anywhere. */
1423 *data_length = 0;
1424
1425 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1427 if (status != PSA_SUCCESS) {
1428 return status;
1429 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1432 status = PSA_ERROR_INVALID_ARGUMENT;
1433 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001434 }
1435
Ronald Cron67227982020-11-26 15:16:05 +01001436 status = psa_driver_wrapper_export_public_key(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001437 &slot->attr, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001439
1440exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001441 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001444}
1445
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001446MBEDTLS_STATIC_ASSERT(
1447 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1448 "One or more key attribute flag is listed as both external-only and dual-use")
1449MBEDTLS_STATIC_ASSERT(
1450 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1451 "One or more key attribute flag is listed as both internal-only and dual-use")
1452MBEDTLS_STATIC_ASSERT(
1453 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1454 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001455
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001456/** Validate that a key policy is internally well-formed.
1457 *
1458 * This function only rejects invalid policies. It does not validate the
1459 * consistency of the policy with respect to other attributes of the key
1460 * such as the key type.
1461 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001463{
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1465 PSA_KEY_USAGE_COPY |
1466 PSA_KEY_USAGE_ENCRYPT |
1467 PSA_KEY_USAGE_DECRYPT |
1468 PSA_KEY_USAGE_SIGN_MESSAGE |
1469 PSA_KEY_USAGE_VERIFY_MESSAGE |
1470 PSA_KEY_USAGE_SIGN_HASH |
1471 PSA_KEY_USAGE_VERIFY_HASH |
1472 PSA_KEY_USAGE_VERIFY_DERIVATION |
1473 PSA_KEY_USAGE_DERIVE)) != 0) {
1474 return PSA_ERROR_INVALID_ARGUMENT;
1475 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001478}
1479
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001480/** Validate the internal consistency of key attributes.
1481 *
1482 * This function only rejects invalid attribute values. If does not
1483 * validate the consistency of the attributes with any key data that may
1484 * be involved in the creation of the key.
1485 *
1486 * Call this function early in the key creation process.
1487 *
1488 * \param[in] attributes Key attributes for the new key.
1489 * \param[out] p_drv On any return, the driver for the key, if any.
1490 * NULL for a transparent key.
1491 *
1492 */
1493static psa_status_t psa_validate_key_attributes(
1494 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001496{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001497 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1499 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 status = psa_validate_key_location(lifetime, p_drv);
1502 if (status != PSA_SUCCESS) {
1503 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001504 }
1505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 status = psa_validate_key_persistence(lifetime);
1507 if (status != PSA_SUCCESS) {
1508 return status;
1509 }
1510
1511 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1512 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1513 return PSA_ERROR_INVALID_ARGUMENT;
1514 }
1515 } else {
1516 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1517 return PSA_ERROR_INVALID_ARGUMENT;
1518 }
1519 }
1520
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001521 status = psa_validate_key_policy(&attributes->policy);
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 if (status != PSA_SUCCESS) {
1523 return status;
1524 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001525
1526 /* Refuse to create overly large keys.
1527 * Note that this doesn't trigger on import if the attributes don't
1528 * explicitly specify a size (so psa_get_key_bits returns 0), so
1529 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1531 return PSA_ERROR_NOT_SUPPORTED;
1532 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001533
Gilles Peskine91e8c332019-08-02 19:19:39 +02001534 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001535 if (attributes->flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1537 return PSA_ERROR_INVALID_ARGUMENT;
1538 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001539
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001541}
1542
Gilles Peskine4747d192019-04-17 15:05:45 +02001543/** Prepare a key slot to receive key material.
1544 *
1545 * This function allocates a key slot and sets its metadata.
1546 *
1547 * If this function fails, call psa_fail_key_creation().
1548 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001549 * This function is intended to be used as follows:
1550 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001551 * it with the specified attributes, and in case of a volatile key assign it
1552 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001553 * -# Populate the slot with the key material.
1554 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1555 * In case of failure at any step, stop the sequence and call
1556 * psa_fail_key_creation().
1557 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001558 * On success, the key slot's state is PSA_SLOT_FILLING.
1559 * It is the responsibility of the caller to change the slot's state to
1560 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001561 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001562 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001563 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001564 * \param[out] p_slot On success, a pointer to the prepared slot.
1565 * \param[out] p_drv On any return, the driver for the key, if any.
1566 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001567 *
1568 * \retval #PSA_SUCCESS
1569 * The key slot is ready to receive key material.
1570 * \return If this function fails, the key slot is an invalid state.
1571 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001572 */
1573static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001574 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001575 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001576 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001578{
1579 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001580 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001581 psa_key_slot_t *slot;
1582
Gilles Peskinedf179142019-07-15 22:02:14 +02001583 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001584 *p_drv = NULL;
1585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 status = psa_validate_key_attributes(attributes, p_drv);
1587 if (status != PSA_SUCCESS) {
1588 return status;
1589 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001590
Ryan Everett3d8118d2024-01-30 16:58:47 +00001591#if defined(MBEDTLS_THREADING_C)
1592 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1593 &mbedtls_threading_key_slot_mutex));
1594#endif
Ryan Everettb69118e2024-01-02 15:54:32 +00001595 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001596#if defined(MBEDTLS_THREADING_C)
1597 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1598 &mbedtls_threading_key_slot_mutex));
1599#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if (status != PSA_SUCCESS) {
1601 return status;
1602 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001603 slot = *p_slot;
1604
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001605 /* We're storing the declared bit-size of the key. It's up to each
1606 * creation mechanism to verify that this information is correct.
1607 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001608 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001609 * is optional (import, copy). In case of a volatile key, assign it the
1610 * volatile key identifier associated to the slot returned to contain its
1611 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001612
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001613 slot->attr = *attributes;
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001615#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1616 slot->attr.id = volatile_key_id;
1617#else
1618 slot->attr.id.key_id = volatile_key_id;
1619#endif
1620 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001621
Gilles Peskine91e8c332019-08-02 19:19:39 +02001622 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001623 * external-only flags, query `attributes`. Thanks to the check
1624 * in psa_validate_key_attributes(), this leaves the dual-use
Ryan Everettb69118e2024-01-02 15:54:32 +00001625 * flags and any internal flag that psa_reserve_free_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001626 * may have set. */
1627 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001628
Gilles Peskinecbaff462019-07-12 23:46:04 +02001629#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001630 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001631 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001632 * create the key file in internal storage, create the
1633 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001634 * persistent data. This is done by starting a transaction that will
1635 * encompass these three actions.
1636 * For registering a volatile key, we just need to find an appropriate
1637 * slot number inside the SE. Since the key is designated volatile, creating
1638 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001639 /* The first thing to do is to find a slot number for the new key.
1640 * We save the slot number in persistent storage as part of the
1641 * transaction data. It will be needed to recover if the power
1642 * fails during the key creation process, to clean up on the secure
1643 * element side after restarting. Obtaining a slot number from the
1644 * secure element driver updates its persistent state, but we do not yet
1645 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001646 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001648 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1650 &slot_number);
1651 if (status != PSA_SUCCESS) {
1652 return status;
1653 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001654
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001655 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001657 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001658 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001659 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 status = psa_crypto_save_transaction();
1661 if (status != PSA_SUCCESS) {
1662 (void) psa_crypto_stop_transaction();
1663 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001664 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001665 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001666
1667 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001669 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001672 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001674 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001675#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001678}
Gilles Peskine4747d192019-04-17 15:05:45 +02001679
1680/** Finalize the creation of a key once its key material has been set.
1681 *
1682 * This entails writing the key to persistent storage.
1683 *
1684 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001685 * See the documentation of psa_start_key_creation() for the intended use
1686 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001687 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001688 * If the finalization succeeds, the function sets the key slot's state to
1689 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1690 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001691 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001692 * \param[in,out] slot Pointer to the slot with key material.
1693 * \param[in] driver The secure element driver for the key,
1694 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001695 * \param[out] key On success, identifier of the key. Note that the
1696 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001697 *
1698 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001699 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001700 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1701 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1702 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1703 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1704 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1705 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001706 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001707 * \return If this function fails, the key slot is an invalid state.
1708 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001709 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001710static psa_status_t psa_finish_key_creation(
1711 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001712 psa_se_drv_table_entry_t *driver,
1713 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001714{
1715 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001716 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001717 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001718
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001719#if defined(MBEDTLS_THREADING_C)
1720 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1721 &mbedtls_threading_key_slot_mutex));
1722#endif
1723
Gilles Peskine4747d192019-04-17 15:05:45 +02001724#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001726#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001728 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001729 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001731
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001732 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1733 sizeof(data.slot_number),
1734 "Slot number size does not match psa_se_key_data_storage_t");
1735
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1737 status = psa_save_persistent_key(&slot->attr,
1738 (uint8_t *) &data,
1739 sizeof(data));
1740 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001741#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1742 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001743 /* Key material is saved in export representation in the slot, so
1744 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 status = psa_save_persistent_key(&slot->attr,
1746 slot->key.data,
1747 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001748 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001749 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001750#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1751
Gilles Peskinecbaff462019-07-12 23:46:04 +02001752#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001753 /* Finish the transaction for a key creation. This does not
1754 * happen when registering an existing key. Detect this case
1755 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001756 * creation of a persistent key in a secure element requires a transaction,
1757 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (driver != NULL &&
1759 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1760 status = psa_save_se_persistent_data(driver);
1761 if (status != PSA_SUCCESS) {
1762 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001763
1764#if defined(MBEDTLS_THREADING_C)
1765 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1766 &mbedtls_threading_key_slot_mutex));
1767#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001769 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001771 }
1772#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001775 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001776 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1777 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001779 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001781 }
Ronald Cron50972942020-11-14 11:28:25 +01001782
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001783#if defined(MBEDTLS_THREADING_C)
1784 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1785 &mbedtls_threading_key_slot_mutex));
1786#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001788}
1789
1790/** Abort the creation of a key.
1791 *
1792 * You may call this function after calling psa_start_key_creation(),
1793 * or after psa_finish_key_creation() fails. In other circumstances, this
1794 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001795 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001796 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001797 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001798 * \param[in,out] slot Pointer to the slot with key material.
1799 * \param[in] driver The secure element driver for the key,
1800 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001801 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001802static void psa_fail_key_creation(psa_key_slot_t *slot,
1803 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001804{
Gilles Peskine011e4282019-06-26 18:34:38 +02001805 (void) driver;
1806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001808 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001810
Ryan Everettb7101442024-01-23 20:09:49 +00001811#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00001812 /* If the lock operation fails we still wipe the slot.
1813 * Operations will no longer work after a failed lock,
1814 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00001815 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
1816#endif
1817
Gilles Peskine011e4282019-06-26 18:34:38 +02001818#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001819 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001820 * element, and the failure happened later (when saving metadata
1821 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001822 * element.
1823 * https://github.com/ARMmbed/mbed-crypto/issues/217
1824 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001825
Gilles Peskined7729582019-08-05 15:55:54 +02001826 /* Abort the ongoing transaction if any (there may not be one if
1827 * the creation process failed before starting one, or if the
1828 * key creation is a registration of a key in a secure element).
1829 * Earlier functions must already have done what it takes to undo any
1830 * partial creation. All that's left is to update the transaction data
1831 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001833#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00001836
1837#if defined(MBEDTLS_THREADING_C)
1838 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
1839#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02001840}
1841
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001842/** Validate optional attributes during key creation.
1843 *
1844 * Some key attributes are optional during key creation. If they are
1845 * specified in the attributes structure, check that they are consistent
1846 * with the data in the slot.
1847 *
1848 * This function should be called near the end of key creation, after
1849 * the slot in memory is fully populated but before saving persistent data.
1850 */
1851static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001852 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001854{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001855 if (attributes->type != 0) {
1856 if (attributes->type != slot->attr.type) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 return PSA_ERROR_INVALID_ARGUMENT;
1858 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001859 }
1860
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001861 if (attributes->bits != 0) {
1862 if (attributes->bits != slot->attr.bits) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 return PSA_ERROR_INVALID_ARGUMENT;
1864 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001865 }
1866
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001868}
1869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1871 const uint8_t *data,
1872 size_t data_length,
1873 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001874{
1875 psa_status_t status;
1876 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001877 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001878 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301879 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001880
Ronald Cron81709fc2020-11-14 12:10:32 +01001881 *key = MBEDTLS_SVC_KEY_ID_INIT;
1882
Gilles Peskine0f84d622019-09-12 19:03:13 +02001883 /* Reject zero-length symmetric keys (including raw data key objects).
1884 * This also rejects any key which might be encoded as an empty string,
1885 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 if (data_length == 0) {
1887 return PSA_ERROR_INVALID_ARGUMENT;
1888 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001889
Archana449608b2021-09-08 15:36:05 +05301890 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 if (data_length > SIZE_MAX / 8) {
1892 return PSA_ERROR_NOT_SUPPORTED;
1893 }
Archana6ed4bda2021-08-04 10:47:15 +05301894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1896 &slot, &driver);
1897 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001900
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001901 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301902 * storage ( thus not in the case of importing a key in a secure element
1903 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1904 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001906 if (psa_key_lifetime_is_external(attributes->lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301907 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 attributes, data, data_length, &storage_size);
1909 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301910 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 }
Archanad8a83dc2021-06-14 10:04:16 +05301912 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 status = psa_allocate_buffer_to_slot(slot, storage_size);
1914 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001915 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001917 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001918
1919 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 status = psa_driver_wrapper_import_key(attributes,
1921 data, data_length,
1922 slot->key.data,
1923 slot->key.bytes,
1924 &slot->key.bytes, &bits);
1925 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001926 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001928
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001930 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001932 status = PSA_ERROR_INVALID_ARGUMENT;
1933 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001934 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001935
Archana6ed4bda2021-08-04 10:47:15 +05301936 /* Enforce a size limit, and in particular ensure that the bit
1937 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301939 status = PSA_ERROR_NOT_SUPPORTED;
1940 goto exit;
1941 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 status = psa_validate_optional_attributes(slot, attributes);
1943 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001944 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001948exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (status != PSA_SUCCESS) {
1950 psa_fail_key_creation(slot, driver);
1951 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001954}
1955
Gilles Peskined7729582019-08-05 15:55:54 +02001956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1957psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02001959{
1960 psa_status_t status;
1961 psa_key_slot_t *slot = NULL;
1962 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001963 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001964
1965 /* Leaving attributes unspecified is not currently supported.
1966 * It could make sense to query the key type and size from the
1967 * secure element, but not all secure elements support this
1968 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
1970 return PSA_ERROR_NOT_SUPPORTED;
1971 }
1972 if (psa_get_key_bits(attributes) == 0) {
1973 return PSA_ERROR_NOT_SUPPORTED;
1974 }
Gilles Peskined7729582019-08-05 15:55:54 +02001975
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
1977 &slot, &driver);
1978 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02001979 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 }
Gilles Peskined7729582019-08-05 15:55:54 +02001981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02001983
1984exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 if (status != PSA_SUCCESS) {
1986 psa_fail_key_creation(slot, driver);
1987 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001988
Gilles Peskined7729582019-08-05 15:55:54 +02001989 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 psa_close_key(key);
1991 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02001992}
1993#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
1996 const psa_key_attributes_t *specified_attributes,
1997 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01001998{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001999 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002000 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002001 psa_key_slot_t *source_slot = NULL;
2002 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002003 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002004 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302005 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002006
Ronald Cron81709fc2020-11-14 12:10:32 +01002007 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2008
Archana8a180362021-07-05 02:18:48 +05302009 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2011 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002012 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 status = psa_validate_optional_attributes(source_slot,
2016 specified_attributes);
2017 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002018 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002020
Archana9d17bf42021-09-10 06:22:44 +05302021 /* The target key type and number of bits have been validated by
2022 * psa_validate_optional_attributes() to be either equal to zero or
2023 * equal to the ones of the source key. So it is safe to inherit
2024 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302025 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002026 actual_attributes.bits = source_slot->attr.bits;
2027 actual_attributes.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302028
2029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 status = psa_restrict_key_policy(source_slot->attr.type,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002031 &actual_attributes.policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 &source_slot->attr.policy);
2033 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002034 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2038 &target_slot, &driver);
2039 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002040 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 }
2042 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2043 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302044 /*
Archana9d17bf42021-09-10 06:22:44 +05302045 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302046 * the source key would need to be exported as plaintext and re-imported
2047 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302048 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302049 * appropriate API invocations from the application, if needed.
2050 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002051 status = PSA_ERROR_NOT_SUPPORTED;
2052 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002053 }
Archana8a180362021-07-05 02:18:48 +05302054 /*
2055 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302056 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302057 * - For opaque keys this translates to an invocation of the drivers'
2058 * copy_key entry point through the dispatch layer.
2059 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002060 if (psa_key_lifetime_is_external(actual_attributes.lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2062 &storage_size);
2063 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302064 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 }
Archana374fe5b2021-09-08 15:50:28 +05302066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2068 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302069 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 }
Archana374fe5b2021-09-08 15:50:28 +05302071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 status = psa_driver_wrapper_copy_key(&actual_attributes,
2073 source_slot->key.data,
2074 source_slot->key.bytes,
2075 target_slot->key.data,
2076 target_slot->key.bytes,
2077 &target_slot->key.bytes);
2078 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302079 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 }
2081 } else {
2082 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302083 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 source_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 }
Archana8a180362021-07-05 02:18:48 +05302088 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002090exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (status != PSA_SUCCESS) {
2092 psa_fail_key_creation(target_slot, driver);
2093 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002094
Ryan Everetta103ec92024-01-31 13:59:57 +00002095 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002098}
2099
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002100
2101
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002102/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002103/* Message digests */
2104/****************************************************************/
2105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002107{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002108 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 if (operation->id == 0) {
2110 return PSA_SUCCESS;
2111 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002114 operation->id = 0;
2115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002117}
2118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2120 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002121{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002122 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002123
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002124 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002126 status = PSA_ERROR_BAD_STATE;
2127 goto exit;
2128 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002131 status = PSA_ERROR_INVALID_ARGUMENT;
2132 goto exit;
2133 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002134
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002135 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2136 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002140
2141exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 if (status != PSA_SUCCESS) {
2143 psa_hash_abort(operation);
2144 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002145
2146 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002147}
2148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2150 const uint8_t *input,
2151 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002152{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002153 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002156 status = PSA_ERROR_BAD_STATE;
2157 goto exit;
2158 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002159
Steven Cooremanc8288352021-03-04 14:02:19 +01002160 /* Don't require hash implementations to behave correctly on a
2161 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 if (input_length == 0) {
2163 return PSA_SUCCESS;
2164 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002167
2168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (status != PSA_SUCCESS) {
2170 psa_hash_abort(operation);
2171 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002174}
2175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2177 uint8_t *hash,
2178 size_t hash_size,
2179 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002180{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002181 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 if (operation->id == 0) {
2183 return PSA_ERROR_BAD_STATE;
2184 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002185
Steven Cooreman1e582352021-02-18 17:24:37 +01002186 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 operation, hash, hash_size, hash_length);
2188 psa_hash_abort(operation);
2189 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002190}
2191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2193 const uint8_t *hash,
2194 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002195{
Ronald Cron69a63422021-10-18 09:47:58 +02002196 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002197 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002198 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 operation,
2200 actual_hash, sizeof(actual_hash),
2201 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002204 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002208 status = PSA_ERROR_INVALID_SIGNATURE;
2209 goto exit;
2210 }
2211
Dave Rodgman78701152023-08-29 14:20:18 +01002212 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002213 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002215
2216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2218 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002219 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002223}
2224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225psa_status_t psa_hash_compute(psa_algorithm_t alg,
2226 const uint8_t *input, size_t input_length,
2227 uint8_t *hash, size_t hash_size,
2228 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002229{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002230 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 if (!PSA_ALG_IS_HASH(alg)) {
2232 return PSA_ERROR_INVALID_ARGUMENT;
2233 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2236 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002237}
2238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239psa_status_t psa_hash_compare(psa_algorithm_t alg,
2240 const uint8_t *input, size_t input_length,
2241 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002242{
Ronald Cron69a63422021-10-18 09:47:58 +02002243 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002244 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if (!PSA_ALG_IS_HASH(alg)) {
2247 return PSA_ERROR_INVALID_ARGUMENT;
2248 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002249
Steven Cooreman1e582352021-02-18 17:24:37 +01002250 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 alg, input, input_length,
2252 actual_hash, sizeof(actual_hash),
2253 &actual_hash_length);
2254 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002255 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 }
2257 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002258 status = PSA_ERROR_INVALID_SIGNATURE;
2259 goto exit;
2260 }
Dave Rodgman78701152023-08-29 14:20:18 +01002261 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002262 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002264
2265exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2267 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002268}
2269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2271 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002272{
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (source_operation->id == 0 ||
2274 target_operation->id != 0) {
2275 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002276 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2279 target_operation);
2280 if (status != PSA_SUCCESS) {
2281 psa_hash_abort(target_operation);
2282 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002285}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286
2287
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002289/* MAC */
2290/****************************************************************/
2291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002293{
Steven Cooremane6804192021-03-19 18:28:56 +01002294 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 if (operation->id == 0) {
2296 return PSA_SUCCESS;
2297 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002300 operation->mac_size = 0;
2301 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002302 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002305}
2306
Ronald Cron2dff3b22021-06-17 16:33:22 +02002307static psa_status_t psa_mac_finalize_alg_and_key_validation(
2308 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002309 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002311{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002312 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 psa_key_type_t key_type = psa_get_key_type(attributes);
2314 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if (!PSA_ALG_IS_MAC(alg)) {
2317 return PSA_ERROR_INVALID_ARGUMENT;
2318 }
Steven Cooremane6804192021-03-19 18:28:56 +01002319
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002320 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 status = psa_mac_key_can_do(alg, key_type);
2322 if (status != PSA_SUCCESS) {
2323 return status;
2324 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002325
Steven Cooremand1a68f12021-05-10 11:13:41 +02002326 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002330 /* A very short MAC is too short for security since it can be
2331 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2332 * so we make this our minimum, even though 32 bits is still
2333 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002335 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2338 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002339 /* It's impossible to "truncate" to a larger length than the full length
2340 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002342 }
2343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002345 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2346 * that is disabled in the compile-time configuration. The result can
2347 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2348 * configuration into account. In this case, force a return of
2349 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2350 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2351 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2352 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2353 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002355 }
2356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002358}
2359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2361 mbedtls_svc_key_id_t key,
2362 psa_algorithm_t alg,
2363 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002364{
2365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2366 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002367 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002368
2369 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002371 status = PSA_ERROR_BAD_STATE;
2372 goto exit;
2373 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374
2375 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 key,
2377 &slot,
2378 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2379 alg);
2380 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002381 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002383
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002384 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 &operation->mac_size);
2386 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002387 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002389
2390 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002391 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 if (is_sign) {
2393 status = psa_driver_wrapper_mac_sign_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002394 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 slot->key.data,
2396 slot->key.bytes,
2397 alg);
2398 } else {
2399 status = psa_driver_wrapper_mac_verify_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002400 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 slot->key.data,
2402 slot->key.bytes,
2403 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002404 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002405
Gilles Peskinefbfac682018-07-08 20:51:54 +02002406exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (status != PSA_SUCCESS) {
2408 psa_mac_abort(operation);
2409 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002410
Ryan Everettfb9857f2024-02-14 12:16:41 +00002411 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002414}
2415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2417 mbedtls_svc_key_id_t key,
2418 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002419{
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002421}
2422
Gilles Peskine449bd832023-01-11 14:50:10 +01002423psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2424 mbedtls_svc_key_id_t key,
2425 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002426{
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002428}
2429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2431 const uint8_t *input,
2432 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433{
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 if (operation->id == 0) {
2435 return PSA_ERROR_BAD_STATE;
2436 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002437
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002438 /* Don't require hash implementations to behave correctly on a
2439 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 if (input_length == 0) {
2441 return PSA_SUCCESS;
2442 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2445 input, input_length);
2446 if (status != PSA_SUCCESS) {
2447 psa_mac_abort(operation);
2448 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451}
2452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2454 uint8_t *mac,
2455 size_t mac_size,
2456 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002457{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002458 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2459 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002462 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002463 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002464 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002467 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002468 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002469 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002470
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002471 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2472 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 if (operation->mac_size == 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 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002479 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002480 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002481 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 status = psa_driver_wrapper_mac_sign_finish(operation,
2484 mac, operation->mac_size,
2485 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002486
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002487exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002488 /* In case of success, set the potential excess room in the output buffer
2489 * to an invalid value, to avoid potentially leaking a longer MAC.
2490 * In case of error, set the output length and content to a safe default,
2491 * such that in case the caller misses an error check, the output would be
2492 * an unachievable MAC.
2493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002495 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002496 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002497 }
mohammad16036df908f2018-04-02 08:34:15 -07002498
Paul Elliottdc42ca82023-02-24 18:11:59 +00002499 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002500
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002504}
2505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2507 const uint8_t *mac,
2508 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002510 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2511 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002514 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002515 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002516 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002519 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002520 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002521 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002524 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002525 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002526 }
2527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 status = psa_driver_wrapper_mac_verify_finish(operation,
2529 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002530
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002531exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535}
2536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2538 psa_algorithm_t alg,
2539 const uint8_t *input,
2540 size_t input_length,
2541 uint8_t *mac,
2542 size_t mac_size,
2543 size_t *mac_length,
2544 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002545{
2546 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002547 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2548 psa_key_slot_t *slot;
2549 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550
Ronald Cron51131b52021-06-17 17:17:20 +02002551 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 key,
2553 &slot,
2554 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2555 alg);
2556 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002557 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002559
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002560 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 &operation_mac_size);
2562 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002563 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002567 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002568 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002569 }
2570
2571 status = psa_driver_wrapper_mac_compute(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002572 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 slot->key.data, slot->key.bytes,
2574 alg,
2575 input, input_length,
2576 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002577
2578exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002579 /* In case of success, set the potential excess room in the output buffer
2580 * to an invalid value, to avoid potentially leaking a longer MAC.
2581 * In case of error, set the output length and content to a safe default,
2582 * such that in case the caller misses an error check, the output would be
2583 * an unachievable MAC.
2584 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002586 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002587 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002588 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002589
2590 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002591
Ryan Everetta103ec92024-01-31 13:59:57 +00002592 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002595}
2596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002598 psa_algorithm_t alg,
2599 const uint8_t *input,
2600 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 uint8_t *mac,
2602 size_t mac_size,
2603 size_t *mac_length)
2604{
2605 return psa_mac_compute_internal(key, alg,
2606 input, input_length,
2607 mac, mac_size, mac_length, 1);
2608}
2609
2610psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2611 psa_algorithm_t alg,
2612 const uint8_t *input,
2613 size_t input_length,
2614 const uint8_t *mac,
2615 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002616{
2617 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002618 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2619 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 status = psa_mac_compute_internal(key, alg,
2622 input, input_length,
2623 actual_mac, sizeof(actual_mac),
2624 &actual_mac_length, 0);
2625 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002626 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002630 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002631 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002632 }
Dave Rodgman78701152023-08-29 14:20:18 +01002633 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002634 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002635 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002636 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002637
2638exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002642}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002643
Gilles Peskinee59236f2018-01-27 23:32:46 +01002644/****************************************************************/
2645/* Asymmetric cryptography */
2646/****************************************************************/
2647
Gilles Peskine449bd832023-01-11 14:50:10 +01002648static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2649 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002650{
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 if (input_is_message) {
2652 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2653 return PSA_ERROR_INVALID_ARGUMENT;
2654 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002655
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2657 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2658 return PSA_ERROR_INVALID_ARGUMENT;
2659 }
2660 }
2661 } else {
2662 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2663 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002664 }
2665 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002668}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2671 int input_is_message,
2672 psa_algorithm_t alg,
2673 const uint8_t *input,
2674 size_t input_length,
2675 uint8_t *signature,
2676 size_t signature_size,
2677 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002678{
2679 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2680 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2681 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002682
2683 *signature_length = 0;
2684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 status = psa_sign_verify_check_alg(input_is_message, alg);
2686 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002687 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002689
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002690 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002691 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002692 * buffer can in principle be empty since it doesn't actually have
2693 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if (signature_size == 0) {
2695 return PSA_ERROR_BUFFER_TOO_SMALL;
2696 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002697
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002698 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 key, &slot,
2700 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2701 PSA_KEY_USAGE_SIGN_HASH,
2702 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002703
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002705 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002709 status = PSA_ERROR_INVALID_ARGUMENT;
2710 goto exit;
2711 }
2712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002714 status = psa_driver_wrapper_sign_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002715 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002716 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 signature, signature_size, signature_length);
2718 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002719
2720 status = psa_driver_wrapper_sign_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002721 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002722 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002724 }
2725
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002726
2727exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002728 psa_wipe_tag_output_buffer(signature, status, signature_size,
2729 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002730
Ryan Everetta103ec92024-01-31 13:59:57 +00002731 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002734}
2735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2737 int input_is_message,
2738 psa_algorithm_t alg,
2739 const uint8_t *input,
2740 size_t input_length,
2741 const uint8_t *signature,
2742 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002743{
2744 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2745 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2746 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002747
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 status = psa_sign_verify_check_alg(input_is_message, alg);
2749 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002750 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002752
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002753 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 key, &slot,
2755 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2756 PSA_KEY_USAGE_VERIFY_HASH,
2757 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 if (status != PSA_SUCCESS) {
2760 return status;
2761 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002762
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002764 status = psa_driver_wrapper_verify_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002765 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002766 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 signature, signature_length);
2768 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002769 status = psa_driver_wrapper_verify_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002770 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002771 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002773 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002774
Ryan Everetta103ec92024-01-31 13:59:57 +00002775 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002778
2779}
2780
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002781psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002782 const psa_key_attributes_t *attributes,
2783 const uint8_t *key_buffer,
2784 size_t key_buffer_size,
2785 psa_algorithm_t alg,
2786 const uint8_t *input,
2787 size_t input_length,
2788 uint8_t *signature,
2789 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002791{
2792 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002793
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002795 size_t hash_length;
2796 uint8_t hash[PSA_HASH_MAX_SIZE];
2797
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002798 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 PSA_ALG_SIGN_GET_HASH(alg),
2800 input, input_length,
2801 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002804 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002806
2807 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 attributes, key_buffer, key_buffer_size,
2809 alg, hash, hash_length,
2810 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002811 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002812
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002814}
2815
Gilles Peskine449bd832023-01-11 14:50:10 +01002816psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2817 psa_algorithm_t alg,
2818 const uint8_t *input,
2819 size_t input_length,
2820 uint8_t *signature,
2821 size_t signature_size,
2822 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002823{
2824 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002825 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002827}
2828
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002829psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002830 const psa_key_attributes_t *attributes,
2831 const uint8_t *key_buffer,
2832 size_t key_buffer_size,
2833 psa_algorithm_t alg,
2834 const uint8_t *input,
2835 size_t input_length,
2836 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002838{
2839 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002840
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002842 size_t hash_length;
2843 uint8_t hash[PSA_HASH_MAX_SIZE];
2844
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002845 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 PSA_ALG_SIGN_GET_HASH(alg),
2847 input, input_length,
2848 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002851 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002853
2854 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 attributes, key_buffer, key_buffer_size,
2856 alg, hash, hash_length,
2857 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002858 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002861}
2862
Gilles Peskine449bd832023-01-11 14:50:10 +01002863psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2864 psa_algorithm_t alg,
2865 const uint8_t *input,
2866 size_t input_length,
2867 const uint8_t *signature,
2868 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002869{
2870 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002871 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002873}
2874
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002875psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002876 const psa_key_attributes_t *attributes,
2877 const uint8_t *key_buffer, size_t key_buffer_size,
2878 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002880{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002881 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2883 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002884#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2886 return mbedtls_psa_rsa_sign_hash(
2887 attributes,
2888 key_buffer, key_buffer_size,
2889 alg, hash, hash_length,
2890 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002891#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2892 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 } else {
2894 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002895 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002896 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002898#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2900 return mbedtls_psa_ecdsa_sign_hash(
2901 attributes,
2902 key_buffer, key_buffer_size,
2903 alg, hash, hash_length,
2904 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002905#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2906 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 } else {
2908 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002909 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002910 }
Ronald Cron4501c982021-03-19 20:09:32 +01002911
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 (void) key_buffer;
2913 (void) key_buffer_size;
2914 (void) hash;
2915 (void) hash_length;
2916 (void) signature;
2917 (void) signature_size;
2918 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002919
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002921}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2924 psa_algorithm_t alg,
2925 const uint8_t *hash,
2926 size_t hash_length,
2927 uint8_t *signature,
2928 size_t signature_size,
2929 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002930{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002931 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002932 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002934}
2935
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002936psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002937 const psa_key_attributes_t *attributes,
2938 const uint8_t *key_buffer, size_t key_buffer_size,
2939 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002941{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002942 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2944 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002945#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2947 return mbedtls_psa_rsa_verify_hash(
2948 attributes,
2949 key_buffer, key_buffer_size,
2950 alg, hash, hash_length,
2951 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002952#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2953 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 } else {
2955 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002956 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002957 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002959#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2961 return mbedtls_psa_ecdsa_verify_hash(
2962 attributes,
2963 key_buffer, key_buffer_size,
2964 alg, hash, hash_length,
2965 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002966#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2967 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 } else {
2969 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002970 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002971 }
Ronald Cron4501c982021-03-19 20:09:32 +01002972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 (void) key_buffer;
2974 (void) key_buffer_size;
2975 (void) hash;
2976 (void) hash_length;
2977 (void) signature;
2978 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002981}
2982
Gilles Peskine449bd832023-01-11 14:50:10 +01002983psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
2984 psa_algorithm_t alg,
2985 const uint8_t *hash,
2986 size_t hash_length,
2987 const uint8_t *signature,
2988 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002989{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002990 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002991 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002993}
2994
Gilles Peskine449bd832023-01-11 14:50:10 +01002995psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
2996 psa_algorithm_t alg,
2997 const uint8_t *input,
2998 size_t input_length,
2999 const uint8_t *salt,
3000 size_t salt_length,
3001 uint8_t *output,
3002 size_t output_size,
3003 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003004{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003005 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003006 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003007 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003008
Darryl Green5cc689a2018-07-24 15:34:10 +01003009 (void) input;
3010 (void) input_length;
3011 (void) salt;
3012 (void) output;
3013 (void) output_size;
3014
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003015 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003016
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3018 return PSA_ERROR_INVALID_ARGUMENT;
3019 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003020
Valerio Setti5bb454a2024-01-15 10:43:16 +01003021 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3023 if (status != PSA_SUCCESS) {
3024 return status;
3025 }
3026 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3027 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003028 status = PSA_ERROR_INVALID_ARGUMENT;
3029 goto exit;
3030 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003031
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003032 status = psa_driver_wrapper_asymmetric_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003033 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003034 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003036exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003037 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003038
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003040}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003041
Gilles Peskine449bd832023-01-11 14:50:10 +01003042psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3043 psa_algorithm_t alg,
3044 const uint8_t *input,
3045 size_t input_length,
3046 const uint8_t *salt,
3047 size_t salt_length,
3048 uint8_t *output,
3049 size_t output_size,
3050 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003051{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003052 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003053 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003054 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003055
Darryl Green5cc689a2018-07-24 15:34:10 +01003056 (void) input;
3057 (void) input_length;
3058 (void) salt;
3059 (void) output;
3060 (void) output_size;
3061
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003062 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003063
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3065 return PSA_ERROR_INVALID_ARGUMENT;
3066 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003067
Valerio Setti5bb454a2024-01-15 10:43:16 +01003068 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3070 if (status != PSA_SUCCESS) {
3071 return status;
3072 }
3073 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003074 status = PSA_ERROR_INVALID_ARGUMENT;
3075 goto exit;
3076 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003077
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003078 status = psa_driver_wrapper_asymmetric_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003079 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003080 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003082
3083exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003084 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003087}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003088
Paul Elliott9fe12f62022-11-30 19:16:02 +00003089/****************************************************************/
3090/* Asymmetric interruptible cryptography */
3091/****************************************************************/
3092
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003093static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3094
Paul Elliott9fe12f62022-11-30 19:16:02 +00003095void psa_interruptible_set_max_ops(uint32_t max_ops)
3096{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003097 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003098}
3099
3100uint32_t psa_interruptible_get_max_ops(void)
3101{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003102 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003103}
3104
Paul Elliott9fe12f62022-11-30 19:16:02 +00003105uint32_t psa_sign_hash_get_num_ops(
3106 const psa_sign_hash_interruptible_operation_t *operation)
3107{
Paul Elliott296ede92022-12-15 17:00:30 +00003108 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003109}
3110
3111uint32_t psa_verify_hash_get_num_ops(
3112 const psa_verify_hash_interruptible_operation_t *operation)
3113{
Paul Elliott296ede92022-12-15 17:00:30 +00003114 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003115}
3116
Paul Elliott59ad9452022-12-18 15:09:02 +00003117static psa_status_t psa_sign_hash_abort_internal(
3118 psa_sign_hash_interruptible_operation_t *operation)
3119{
3120 if (operation->id == 0) {
3121 /* The object has (apparently) been initialized but it is not (yet)
3122 * in use. It's ok to call abort on such an object, and there's
3123 * nothing to do. */
3124 return PSA_SUCCESS;
3125 }
3126
3127 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3128
3129 status = psa_driver_wrapper_sign_hash_abort(operation);
3130
3131 operation->id = 0;
3132
Paul Elliotte6145dc2023-02-07 12:51:21 +00003133 /* Do not clear either the error_occurred or num_ops elements here as they
3134 * only want to be cleared by the application calling abort, not by abort
3135 * being called at completion of an operation. */
3136
Paul Elliott59ad9452022-12-18 15:09:02 +00003137 return status;
3138}
3139
Paul Elliott9fe12f62022-11-30 19:16:02 +00003140psa_status_t psa_sign_hash_start(
3141 psa_sign_hash_interruptible_operation_t *operation,
3142 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3143 const uint8_t *hash, size_t hash_length)
3144{
3145 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3146 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3147 psa_key_slot_t *slot;
3148
Paul Elliottc9774412023-02-06 15:14:07 +00003149 /* Check that start has not been previously called, or operation has not
3150 * previously errored. */
3151 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003152 return PSA_ERROR_BAD_STATE;
3153 }
3154
Paul Elliott9fe12f62022-11-30 19:16:02 +00003155 status = psa_sign_verify_check_alg(0, alg);
3156 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003157 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003158 return status;
3159 }
3160
3161 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3162 PSA_KEY_USAGE_SIGN_HASH,
3163 alg);
3164
3165 if (status != PSA_SUCCESS) {
3166 goto exit;
3167 }
3168
3169 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3170 status = PSA_ERROR_INVALID_ARGUMENT;
3171 goto exit;
3172 }
3173
Paul Elliott296ede92022-12-15 17:00:30 +00003174 /* Ensure ops count gets reset, in case of operation re-use. */
3175 operation->num_ops = 0;
3176
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003177 status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003178 slot->key.data,
3179 slot->key.bytes, alg,
3180 hash, hash_length);
3181exit:
3182
3183 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003184 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003185 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003186 }
3187
Ryan Everettdcc03d52024-02-14 15:44:13 +00003188 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003189
Paul Elliottc9774412023-02-06 15:14:07 +00003190 if (unlock_status != PSA_SUCCESS) {
3191 operation->error_occurred = 1;
3192 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003193
Paul Elliottc9774412023-02-06 15:14:07 +00003194 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003195}
3196
3197
3198psa_status_t psa_sign_hash_complete(
3199 psa_sign_hash_interruptible_operation_t *operation,
3200 uint8_t *signature, size_t signature_size,
3201 size_t *signature_length)
3202{
3203 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3204
3205 *signature_length = 0;
3206
Paul Elliottc9774412023-02-06 15:14:07 +00003207 /* Check that start has been called first, and that operation has not
3208 * previously errored. */
3209 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003210 status = PSA_ERROR_BAD_STATE;
3211 goto exit;
3212 }
3213
Paul Elliott096abc42023-02-03 18:33:23 +00003214 /* Immediately reject a zero-length signature buffer. This guarantees that
3215 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003216 if (signature_size == 0) {
3217 status = PSA_ERROR_BUFFER_TOO_SMALL;
3218 goto exit;
3219 }
3220
3221 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3222 signature_size,
3223 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003224
Paul Elliott296ede92022-12-15 17:00:30 +00003225 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003226 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003227
Paul Elliottebe225c2023-02-10 14:32:53 +00003228exit:
3229
Paul Elliott59200a22023-02-21 15:07:40 +00003230 psa_wipe_tag_output_buffer(signature, status, signature_size,
3231 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003232
Paul Elliott53bb3122023-02-10 14:22:22 +00003233 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003234 if (status != PSA_SUCCESS) {
3235 operation->error_occurred = 1;
3236 }
3237
Paul Elliott59ad9452022-12-18 15:09:02 +00003238 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003239 }
3240
3241 return status;
3242}
3243
3244psa_status_t psa_sign_hash_abort(
3245 psa_sign_hash_interruptible_operation_t *operation)
3246{
Paul Elliott59ad9452022-12-18 15:09:02 +00003247 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3248
3249 status = psa_sign_hash_abort_internal(operation);
3250
3251 /* We clear the number of ops done here, so that it is not cleared when
3252 * the operation fails or succeeds, only on manual abort. */
3253 operation->num_ops = 0;
3254
Paul Elliottc9774412023-02-06 15:14:07 +00003255 /* Likewise, failure state. */
3256 operation->error_occurred = 0;
3257
Paul Elliott59ad9452022-12-18 15:09:02 +00003258 return status;
3259}
3260
3261static psa_status_t psa_verify_hash_abort_internal(
3262 psa_verify_hash_interruptible_operation_t *operation)
3263{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003264 if (operation->id == 0) {
3265 /* The object has (apparently) been initialized but it is not (yet)
3266 * in use. It's ok to call abort on such an object, and there's
3267 * nothing to do. */
3268 return PSA_SUCCESS;
3269 }
3270
Paul Elliott59ad9452022-12-18 15:09:02 +00003271 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3272
3273 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003274
3275 operation->id = 0;
3276
Paul Elliotte6145dc2023-02-07 12:51:21 +00003277 /* Do not clear either the error_occurred or num_ops elements here as they
3278 * only want to be cleared by the application calling abort, not by abort
3279 * being called at completion of an operation. */
3280
Paul Elliott59ad9452022-12-18 15:09:02 +00003281 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003282}
3283
3284psa_status_t psa_verify_hash_start(
3285 psa_verify_hash_interruptible_operation_t *operation,
3286 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3287 const uint8_t *hash, size_t hash_length,
3288 const uint8_t *signature, size_t signature_length)
3289{
3290 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3291 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3292 psa_key_slot_t *slot;
3293
Paul Elliottc9774412023-02-06 15:14:07 +00003294 /* Check that start has not been previously called, or operation has not
3295 * previously errored. */
3296 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003297 return PSA_ERROR_BAD_STATE;
3298 }
3299
3300 status = psa_sign_verify_check_alg(0, alg);
3301 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003302 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003303 return status;
3304 }
3305
3306 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3307 PSA_KEY_USAGE_VERIFY_HASH,
3308 alg);
3309
3310 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003311 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003312 return status;
3313 }
3314
Paul Elliott296ede92022-12-15 17:00:30 +00003315 /* Ensure ops count gets reset, in case of operation re-use. */
3316 operation->num_ops = 0;
3317
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003318 status = psa_driver_wrapper_verify_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003319 slot->key.data,
3320 slot->key.bytes,
3321 alg, hash, hash_length,
3322 signature, signature_length);
3323
3324 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003325 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003326 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003327 }
3328
Ryan Everett291267f2024-02-14 15:59:15 +00003329 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003330
Paul Elliottc9774412023-02-06 15:14:07 +00003331 if (unlock_status != PSA_SUCCESS) {
3332 operation->error_occurred = 1;
3333 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003334
Paul Elliottc9774412023-02-06 15:14:07 +00003335 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003336}
3337
3338psa_status_t psa_verify_hash_complete(
3339 psa_verify_hash_interruptible_operation_t *operation)
3340{
3341 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3342
Paul Elliottc9774412023-02-06 15:14:07 +00003343 /* Check that start has been called first, and that operation has not
3344 * previously errored. */
3345 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003346 status = PSA_ERROR_BAD_STATE;
3347 goto exit;
3348 }
3349
3350 status = psa_driver_wrapper_verify_hash_complete(operation);
3351
Paul Elliott296ede92022-12-15 17:00:30 +00003352 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003353 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003354 operation);
3355
Paul Elliottebe225c2023-02-10 14:32:53 +00003356exit:
3357
Paul Elliott9fe12f62022-11-30 19:16:02 +00003358 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003359 if (status != PSA_SUCCESS) {
3360 operation->error_occurred = 1;
3361 }
3362
Paul Elliott59ad9452022-12-18 15:09:02 +00003363 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003364 }
3365
3366 return status;
3367}
3368
3369psa_status_t psa_verify_hash_abort(
3370 psa_verify_hash_interruptible_operation_t *operation)
3371{
Paul Elliott59ad9452022-12-18 15:09:02 +00003372 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003373
Paul Elliott59ad9452022-12-18 15:09:02 +00003374 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003375
Paul Elliott59ad9452022-12-18 15:09:02 +00003376 /* We clear the number of ops done here, so that it is not cleared when
3377 * the operation fails or succeeds, only on manual abort. */
3378 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003379
Paul Elliottc9774412023-02-06 15:14:07 +00003380 /* Likewise, failure state. */
3381 operation->error_occurred = 0;
3382
Paul Elliott59ad9452022-12-18 15:09:02 +00003383 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003384}
3385
Paul Elliott588f8ed2022-12-02 18:10:26 +00003386/****************************************************************/
3387/* Asymmetric interruptible cryptography internal */
3388/* implementations */
3389/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003390
Paul Elliott588f8ed2022-12-02 18:10:26 +00003391void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3392{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003393
Paul Elliott588f8ed2022-12-02 18:10:26 +00003394#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3395 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3396 defined(MBEDTLS_ECP_RESTARTABLE)
3397
3398 /* Internal implementation uses zero to indicate infinite number max ops,
3399 * therefore avoid this value, and set to minimum possible. */
3400 if (max_ops == 0) {
3401 max_ops = 1;
3402 }
3403
Paul Elliott588f8ed2022-12-02 18:10:26 +00003404 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003405#else
3406 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003407#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3408 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3409 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3410}
3411
Paul Elliott588f8ed2022-12-02 18:10:26 +00003412uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003413 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003414{
3415#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3416 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3417 defined(MBEDTLS_ECP_RESTARTABLE)
3418
Paul Elliott93d9ca82023-02-15 18:14:21 +00003419 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003420#else
3421 (void) operation;
3422 return 0;
3423#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3424 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3425 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3426}
3427
3428uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003429 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003430{
3431 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3432 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3433 defined(MBEDTLS_ECP_RESTARTABLE)
3434
Paul Elliott93d9ca82023-02-15 18:14:21 +00003435 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003436#else
3437 (void) operation;
3438 return 0;
3439#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3440 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3441 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3442}
3443
3444psa_status_t mbedtls_psa_sign_hash_start(
3445 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3446 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3447 size_t key_buffer_size, psa_algorithm_t alg,
3448 const uint8_t *hash, size_t hash_length)
3449{
3450 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003451 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003452
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003453 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003454 return PSA_ERROR_NOT_SUPPORTED;
3455 }
3456
3457 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003458 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003459 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003460
3461#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003462 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3463 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003464
Paul Elliotta1c94092023-02-15 16:38:04 +00003465 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3466
Paul Elliott93d9ca82023-02-15 18:14:21 +00003467 /* Ensure num_ops is zero'ed in case of context re-use. */
3468 operation->num_ops = 0;
3469
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003470 status = mbedtls_psa_ecp_load_representation(attributes->type,
3471 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003472 key_buffer,
3473 key_buffer_size,
3474 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003475
Paul Elliott068fe072023-01-16 13:59:15 +00003476 if (status != PSA_SUCCESS) {
3477 return status;
3478 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003479
Paul Elliott1bc59df2023-02-05 13:41:57 +00003480 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003481 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003482
Paul Elliott068fe072023-01-16 13:59:15 +00003483 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003484 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003485 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003486
Paul Elliott0290a762023-02-09 14:30:24 +00003487 /* We only need to store the same length of hash as the private key size
3488 * here, it would be truncated by the internal implementation anyway. */
3489 required_hash_length = (hash_length < operation->coordinate_bytes ?
3490 hash_length : operation->coordinate_bytes);
3491
Paul Elliottba70ad42023-02-15 18:23:53 +00003492 if (required_hash_length > sizeof(operation->hash)) {
3493 /* Shouldn't happen, but better safe than sorry. */
3494 return PSA_ERROR_CORRUPTION_DETECTED;
3495 }
3496
Paul Elliott0290a762023-02-09 14:30:24 +00003497 memcpy(operation->hash, hash, required_hash_length);
3498 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003499
3500 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003501
3502#else
Paul Elliott068fe072023-01-16 13:59:15 +00003503 (void) operation;
3504 (void) key_buffer;
3505 (void) key_buffer_size;
3506 (void) alg;
3507 (void) hash;
3508 (void) hash_length;
3509 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003510 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003511
Paul Elliott068fe072023-01-16 13:59:15 +00003512 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003513#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3514 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3515 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003516}
3517
3518psa_status_t mbedtls_psa_sign_hash_complete(
3519 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3520 uint8_t *signature, size_t signature_size,
3521 size_t *signature_length)
3522{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003523#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3524 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3525 defined(MBEDTLS_ECP_RESTARTABLE)
3526
Paul Elliotta1c94092023-02-15 16:38:04 +00003527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003528 mbedtls_mpi r;
3529 mbedtls_mpi s;
3530
Paul Elliott46845252023-02-03 14:59:11 +00003531 mbedtls_mpi_init(&r);
3532 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003533
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003534 /* Ensure max_ops is set to the current value (or default). */
3535 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3536
Paul Elliotta1c94092023-02-15 16:38:04 +00003537 if (signature_size < 2 * operation->coordinate_bytes) {
3538 status = PSA_ERROR_BUFFER_TOO_SMALL;
3539 goto exit;
3540 }
3541
Paul Elliott588f8ed2022-12-02 18:10:26 +00003542 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003543
Paul Elliott588f8ed2022-12-02 18:10:26 +00003544#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3545 status = mbedtls_to_psa_error(
3546 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003547 &r,
3548 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003549 &operation->ctx->d,
3550 operation->hash,
3551 operation->hash_length,
3552 operation->md_alg,
3553 mbedtls_psa_get_random,
3554 MBEDTLS_PSA_RANDOM_STATE,
3555 &operation->restart_ctx));
3556#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003557 status = PSA_ERROR_NOT_SUPPORTED;
3558 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003559#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3560 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003561 status = mbedtls_to_psa_error(
3562 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003563 &r,
3564 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003565 &operation->ctx->d,
3566 operation->hash,
3567 operation->hash_length,
3568 mbedtls_psa_get_random,
3569 MBEDTLS_PSA_RANDOM_STATE,
3570 mbedtls_psa_get_random,
3571 MBEDTLS_PSA_RANDOM_STATE,
3572 &operation->restart_ctx));
3573 }
3574
Paul Elliottf8e5b562023-02-19 18:43:45 +00003575 /* Hide the fact that the restart context only holds a delta of number of
3576 * ops done during the last operation, not an absolute value. */
3577 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3578
Paul Elliott724bd252023-02-08 12:35:08 +00003579 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003580 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003581 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003582 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003583 operation->coordinate_bytes)
3584 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003585
3586 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003587 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003588 }
3589
3590 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003591 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003592 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003593 operation->coordinate_bytes,
3594 operation->coordinate_bytes)
3595 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003596
3597 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003598 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003599 }
3600
Paul Elliott1bc59df2023-02-05 13:41:57 +00003601 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003602
Paul Elliott724bd252023-02-08 12:35:08 +00003603 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003604 }
Paul Elliott724bd252023-02-08 12:35:08 +00003605
3606exit:
3607
3608 mbedtls_mpi_free(&r);
3609 mbedtls_mpi_free(&s);
3610 return status;
3611
Paul Elliott588f8ed2022-12-02 18:10:26 +00003612 #else
3613
3614 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003615 (void) signature;
3616 (void) signature_size;
3617 (void) signature_length;
3618
3619 return PSA_ERROR_NOT_SUPPORTED;
3620
3621#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3622 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3623 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3624}
3625
3626psa_status_t mbedtls_psa_sign_hash_abort(
3627 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3628{
3629
3630#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3631 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3632 defined(MBEDTLS_ECP_RESTARTABLE)
3633
3634 if (operation->ctx) {
3635 mbedtls_ecdsa_free(operation->ctx);
3636 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003637 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638 }
3639
3640 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3641
Paul Elliott93d9ca82023-02-15 18:14:21 +00003642 operation->num_ops = 0;
3643
Paul Elliott588f8ed2022-12-02 18:10:26 +00003644 return PSA_SUCCESS;
3645
3646#else
3647
3648 (void) operation;
3649
3650 return PSA_ERROR_NOT_SUPPORTED;
3651
3652#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3653 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3654 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3655}
3656
3657psa_status_t mbedtls_psa_verify_hash_start(
3658 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3659 const psa_key_attributes_t *attributes,
3660 const uint8_t *key_buffer, size_t key_buffer_size,
3661 psa_algorithm_t alg,
3662 const uint8_t *hash, size_t hash_length,
3663 const uint8_t *signature, size_t signature_length)
3664{
3665 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003666 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003667 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003668
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003669 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003670 return PSA_ERROR_NOT_SUPPORTED;
3671 }
3672
3673 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003674 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003675 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003676
3677#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003678 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3679 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003680
Paul Elliotta1c94092023-02-15 16:38:04 +00003681 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3682 mbedtls_mpi_init(&operation->r);
3683 mbedtls_mpi_init(&operation->s);
3684
Paul Elliott93d9ca82023-02-15 18:14:21 +00003685 /* Ensure num_ops is zero'ed in case of context re-use. */
3686 operation->num_ops = 0;
3687
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003688 status = mbedtls_psa_ecp_load_representation(attributes->type,
3689 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003690 key_buffer,
3691 key_buffer_size,
3692 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003693
Paul Elliott068fe072023-01-16 13:59:15 +00003694 if (status != PSA_SUCCESS) {
3695 return status;
3696 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003697
Paul Elliottc569fc22023-02-10 13:02:54 +00003698 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003699
Paul Elliott1bc59df2023-02-05 13:41:57 +00003700 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003701 return PSA_ERROR_INVALID_SIGNATURE;
3702 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003703
Paul Elliott068fe072023-01-16 13:59:15 +00003704 status = mbedtls_to_psa_error(
3705 mbedtls_mpi_read_binary(&operation->r,
3706 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003707 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003708
Paul Elliott068fe072023-01-16 13:59:15 +00003709 if (status != PSA_SUCCESS) {
3710 return status;
3711 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712
Paul Elliott068fe072023-01-16 13:59:15 +00003713 status = mbedtls_to_psa_error(
3714 mbedtls_mpi_read_binary(&operation->s,
3715 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003716 coordinate_bytes,
3717 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003718
Paul Elliott068fe072023-01-16 13:59:15 +00003719 if (status != PSA_SUCCESS) {
3720 return status;
3721 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722
Paul Elliott2c9843f2023-02-15 17:32:42 +00003723 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003724
Paul Elliott2c9843f2023-02-15 17:32:42 +00003725 if (status != PSA_SUCCESS) {
3726 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003727 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003728
Paul Elliott0290a762023-02-09 14:30:24 +00003729 /* We only need to store the same length of hash as the private key size
3730 * here, it would be truncated by the internal implementation anyway. */
3731 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3732 coordinate_bytes);
3733
Paul Elliottba70ad42023-02-15 18:23:53 +00003734 if (required_hash_length > sizeof(operation->hash)) {
3735 /* Shouldn't happen, but better safe than sorry. */
3736 return PSA_ERROR_CORRUPTION_DETECTED;
3737 }
3738
Paul Elliott0290a762023-02-09 14:30:24 +00003739 memcpy(operation->hash, hash, required_hash_length);
3740 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003741
3742 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003743#else
Paul Elliott068fe072023-01-16 13:59:15 +00003744 (void) operation;
3745 (void) key_buffer;
3746 (void) key_buffer_size;
3747 (void) alg;
3748 (void) hash;
3749 (void) hash_length;
3750 (void) signature;
3751 (void) signature_length;
3752 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003753 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003754 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003755
Paul Elliott068fe072023-01-16 13:59:15 +00003756 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003757#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3758 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3759 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003760}
3761
3762psa_status_t mbedtls_psa_verify_hash_complete(
3763 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3764{
3765
3766#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3767 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3768 defined(MBEDTLS_ECP_RESTARTABLE)
3769
Paul Elliottf8e5b562023-02-19 18:43:45 +00003770 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3771
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003772 /* Ensure max_ops is set to the current value (or default). */
3773 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3774
Paul Elliottf8e5b562023-02-19 18:43:45 +00003775 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003776 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3777 operation->hash,
3778 operation->hash_length,
3779 &operation->ctx->Q,
3780 &operation->r,
3781 &operation->s,
3782 &operation->restart_ctx));
3783
Paul Elliottf8e5b562023-02-19 18:43:45 +00003784 /* Hide the fact that the restart context only holds a delta of number of
3785 * ops done during the last operation, not an absolute value. */
3786 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3787
3788 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003789#else
3790 (void) operation;
3791
3792 return PSA_ERROR_NOT_SUPPORTED;
3793
3794#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3795 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3796 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3797}
3798
3799psa_status_t mbedtls_psa_verify_hash_abort(
3800 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3801{
3802
3803#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3804 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3805 defined(MBEDTLS_ECP_RESTARTABLE)
3806
3807 if (operation->ctx) {
3808 mbedtls_ecdsa_free(operation->ctx);
3809 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003810 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003811 }
3812
3813 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3814
Paul Elliott93d9ca82023-02-15 18:14:21 +00003815 operation->num_ops = 0;
3816
Paul Elliott588f8ed2022-12-02 18:10:26 +00003817 mbedtls_mpi_free(&operation->r);
3818 mbedtls_mpi_free(&operation->s);
3819
3820 return PSA_SUCCESS;
3821
3822#else
3823 (void) operation;
3824
3825 return PSA_ERROR_NOT_SUPPORTED;
3826
3827#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3828 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3829 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3830}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003831
mohammad1603503973b2018-03-12 15:59:30 +02003832/****************************************************************/
3833/* Symmetric cryptography */
3834/****************************************************************/
3835
Gilles Peskine449bd832023-01-11 14:50:10 +01003836static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3837 mbedtls_svc_key_id_t key,
3838 psa_algorithm_t alg,
3839 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003840{
3841 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3842 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003843 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003844 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3845 PSA_KEY_USAGE_ENCRYPT :
3846 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003847
3848 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003849 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003850 status = PSA_ERROR_BAD_STATE;
3851 goto exit;
3852 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003853
Gilles Peskine449bd832023-01-11 14:50:10 +01003854 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003855 status = PSA_ERROR_INVALID_ARGUMENT;
3856 goto exit;
3857 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003858
Gilles Peskine449bd832023-01-11 14:50:10 +01003859 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3860 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003861 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003862 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003863
Ronald Cron49fafa92021-03-10 08:34:23 +01003864 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003865 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003866 * so we only set it (in the driver wrapper) after resources have been
3867 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003868 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003869 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003870 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003871 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003872 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003873 }
3874 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003875
3876 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003877 if (cipher_operation == MBEDTLS_ENCRYPT) {
3878 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003879 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003880 slot->key.data,
3881 slot->key.bytes,
3882 alg);
3883 } else {
3884 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003885 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003886 slot->key.data,
3887 slot->key.bytes,
3888 alg);
3889 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003890
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003891exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003892 if (status != PSA_SUCCESS) {
3893 psa_cipher_abort(operation);
3894 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003895
Ryan Everettc0053cc2024-02-14 16:27:13 +00003896 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003897
Gilles Peskine449bd832023-01-11 14:50:10 +01003898 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003899}
3900
Gilles Peskine449bd832023-01-11 14:50:10 +01003901psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3902 mbedtls_svc_key_id_t key,
3903 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003904{
Gilles Peskine449bd832023-01-11 14:50:10 +01003905 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003906}
3907
Gilles Peskine449bd832023-01-11 14:50:10 +01003908psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3909 mbedtls_svc_key_id_t key,
3910 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003911{
Gilles Peskine449bd832023-01-11 14:50:10 +01003912 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003913}
3914
Gilles Peskine449bd832023-01-11 14:50:10 +01003915psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3916 uint8_t *iv,
3917 size_t iv_size,
3918 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003919{
Ronald Cron6d051732020-10-01 14:10:20 +02003920 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003921 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07003922 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003923
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003925 status = PSA_ERROR_BAD_STATE;
3926 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003927 }
3928
Gilles Peskine449bd832023-01-11 14:50:10 +01003929 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003930 status = PSA_ERROR_BAD_STATE;
3931 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003932 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003933
Ronald Cron2fb90522021-07-05 12:31:44 +02003934 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003936 status = PSA_ERROR_BUFFER_TOO_SMALL;
3937 goto exit;
3938 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003939
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02003941 status = PSA_ERROR_GENERIC_ERROR;
3942 goto exit;
3943 }
3944
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 status = psa_generate_random(local_iv, default_iv_length);
3946 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003947 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 }
Ronald Cron5618a392021-03-26 09:52:26 +01003949
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 status = psa_driver_wrapper_cipher_set_iv(operation,
3951 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003952
3953exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 if (status == PSA_SUCCESS) {
3955 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02003956 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003957 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02003959 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02003961 }
Ronald Cron6d051732020-10-01 14:10:20 +02003962
Gilles Peskine449bd832023-01-11 14:50:10 +01003963 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003964}
3965
Gilles Peskine449bd832023-01-11 14:50:10 +01003966psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3967 const uint8_t *iv,
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 Cronc45b4af2020-09-29 16:18:05 +02003971
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003973 status = PSA_ERROR_BAD_STATE;
3974 goto exit;
3975 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003976
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003978 status = PSA_ERROR_BAD_STATE;
3979 goto exit;
3980 }
Ronald Crona0d68172021-03-26 10:15:08 +01003981
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003983 status = PSA_ERROR_INVALID_ARGUMENT;
3984 goto exit;
3985 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003986
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 status = psa_driver_wrapper_cipher_set_iv(operation,
3988 iv,
3989 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003990
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003991exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003993 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 } else {
3995 psa_cipher_abort(operation);
3996 }
3997 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003998}
3999
Gilles Peskine449bd832023-01-11 14:50:10 +01004000psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4001 const uint8_t *input,
4002 size_t input_length,
4003 uint8_t *output,
4004 size_t output_size,
4005 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004006{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004007 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004008
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004010 status = PSA_ERROR_BAD_STATE;
4011 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004012 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004013
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004015 status = PSA_ERROR_BAD_STATE;
4016 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004017 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004018
Gilles Peskine449bd832023-01-11 14:50:10 +01004019 status = psa_driver_wrapper_cipher_update(operation,
4020 input,
4021 input_length,
4022 output,
4023 output_size,
4024 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004025
4026exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 if (status != PSA_SUCCESS) {
4028 psa_cipher_abort(operation);
4029 }
Ronald Cron6d051732020-10-01 14:10:20 +02004030
Gilles Peskine449bd832023-01-11 14:50:10 +01004031 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004032}
4033
Gilles Peskine449bd832023-01-11 14:50:10 +01004034psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4035 uint8_t *output,
4036 size_t output_size,
4037 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004038{
David Saadab4ecc272019-02-14 13:48:10 +02004039 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004040
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004042 status = PSA_ERROR_BAD_STATE;
4043 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004044 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004045
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004047 status = PSA_ERROR_BAD_STATE;
4048 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004049 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004050
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 status = psa_driver_wrapper_cipher_finish(operation,
4052 output,
4053 output_size,
4054 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004055
4056exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 if (status == PSA_SUCCESS) {
4058 return psa_cipher_abort(operation);
4059 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004060 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004061 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004062
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004064 }
mohammad1603503973b2018-03-12 15:59:30 +02004065}
4066
Gilles Peskine449bd832023-01-11 14:50:10 +01004067psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004068{
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004070 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004071 * in use. It's ok to call abort on such an object, and there's
4072 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004074 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004075
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004077
Ronald Cron49fafa92021-03-10 08:34:23 +01004078 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004079 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004080 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004081
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004083}
4084
Gilles Peskine449bd832023-01-11 14:50:10 +01004085psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4086 psa_algorithm_t alg,
4087 const uint8_t *input,
4088 size_t input_length,
4089 uint8_t *output,
4090 size_t output_size,
4091 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004092{
4093 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004094 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004095 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004096 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4097 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004098
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004100 status = PSA_ERROR_INVALID_ARGUMENT;
4101 goto exit;
4102 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4105 PSA_KEY_USAGE_ENCRYPT,
4106 alg);
4107 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004108 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4112 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004113 status = PSA_ERROR_GENERIC_ERROR;
4114 goto exit;
4115 }
4116
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 if (default_iv_length > 0) {
4118 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004119 status = PSA_ERROR_BUFFER_TOO_SMALL;
4120 goto exit;
4121 }
4122
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 status = psa_generate_random(local_iv, default_iv_length);
4124 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004125 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004127 }
4128
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004129 status = psa_driver_wrapper_cipher_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004130 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004131 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004132 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004133 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004134
4135exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004136 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004138 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004139 }
Ronald Cron23919522021-07-08 19:00:07 +02004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 if (status == PSA_SUCCESS) {
4142 if (default_iv_length > 0) {
4143 memcpy(output, local_iv, default_iv_length);
4144 }
4145 *output_length += default_iv_length;
4146 } else {
4147 *output_length = 0;
4148 }
4149
4150 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004151}
4152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4154 psa_algorithm_t alg,
4155 const uint8_t *input,
4156 size_t input_length,
4157 uint8_t *output,
4158 size_t output_size,
4159 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004160{
4161 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004162 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004163 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004166 status = PSA_ERROR_INVALID_ARGUMENT;
4167 goto exit;
4168 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4171 PSA_KEY_USAGE_DECRYPT,
4172 alg);
4173 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004176
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4178 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004179 status = PSA_ERROR_INVALID_ARGUMENT;
4180 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004182 status = PSA_ERROR_INVALID_ARGUMENT;
4183 goto exit;
4184 }
4185
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004186 status = psa_driver_wrapper_cipher_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004187 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004188 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004190
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004191exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004192 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004194 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004196
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004198 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 }
Ronald Cron23919522021-07-08 19:00:07 +02004200
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004202}
4203
4204
mohammad16035955c982018-04-26 00:53:03 +03004205/****************************************************************/
4206/* AEAD */
4207/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004208
Paul Elliottbaff51c2021-09-28 17:44:45 +01004209/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004210static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004211{
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004213}
4214
4215/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004216static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4217 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004218{
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004222#if defined(PSA_WANT_ALG_GCM)
4223 case PSA_ALG_GCM:
4224 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 * arbitrarily large nonces. Please note that we do not generally
4226 * recommend the usage of nonces of greater length than
4227 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4228 * size, which can then lead to collisions if you encrypt a very
4229 * large number of messages.*/
4230 if (nonce_length != 0) {
4231 return PSA_SUCCESS;
4232 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004233 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004234#endif /* PSA_WANT_ALG_GCM */
4235#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004236 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 if (nonce_length >= 7 && nonce_length <= 13) {
4238 return PSA_SUCCESS;
4239 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004240 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004241#endif /* PSA_WANT_ALG_CCM */
4242#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004243 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004244 if (nonce_length == 12) {
4245 return PSA_SUCCESS;
4246 } else if (nonce_length == 8) {
4247 return PSA_ERROR_NOT_SUPPORTED;
4248 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004249 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004250#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004251 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004252 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004254 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004255
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004257}
4258
Gilles Peskine449bd832023-01-11 14:50:10 +01004259static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004260{
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4262 return PSA_ERROR_INVALID_ARGUMENT;
4263 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004266}
4267
Gilles Peskine449bd832023-01-11 14:50:10 +01004268psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4269 psa_algorithm_t alg,
4270 const uint8_t *nonce,
4271 size_t nonce_length,
4272 const uint8_t *additional_data,
4273 size_t additional_data_length,
4274 const uint8_t *plaintext,
4275 size_t plaintext_length,
4276 uint8_t *ciphertext,
4277 size_t ciphertext_size,
4278 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004279{
Ronald Cron215633c2021-03-16 17:15:37 +01004280 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4281 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004282
mohammad1603f08a5502018-06-03 15:05:47 +03004283 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004284
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 status = psa_aead_check_algorithm(alg);
4286 if (status != PSA_SUCCESS) {
4287 return status;
4288 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004289
Ronald Cron9a986162021-03-26 12:40:07 +01004290 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4292 if (status != PSA_SUCCESS) {
4293 return status;
4294 }
mohammad16035955c982018-04-26 00:53:03 +03004295
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 status = psa_aead_check_nonce_length(alg, nonce_length);
4297 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004298 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004300
Ronald Cronde822812021-03-17 16:08:20 +01004301 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004302 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004303 alg,
4304 nonce, nonce_length,
4305 additional_data, additional_data_length,
4306 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004308
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4310 memset(ciphertext, 0, ciphertext_size);
4311 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004312
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004313exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004314 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004315
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004317}
4318
Gilles Peskine449bd832023-01-11 14:50:10 +01004319psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4320 psa_algorithm_t alg,
4321 const uint8_t *nonce,
4322 size_t nonce_length,
4323 const uint8_t *additional_data,
4324 size_t additional_data_length,
4325 const uint8_t *ciphertext,
4326 size_t ciphertext_length,
4327 uint8_t *plaintext,
4328 size_t plaintext_size,
4329 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004330{
Ronald Cron215633c2021-03-16 17:15:37 +01004331 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4332 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004333
Gilles Peskineee652a32018-06-01 19:23:52 +02004334 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004335
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 status = psa_aead_check_algorithm(alg);
4337 if (status != PSA_SUCCESS) {
4338 return status;
4339 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004340
Ronald Cron9a986162021-03-26 12:40:07 +01004341 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4343 if (status != PSA_SUCCESS) {
4344 return status;
4345 }
mohammad16035955c982018-04-26 00:53:03 +03004346
Gilles Peskine449bd832023-01-11 14:50:10 +01004347 status = psa_aead_check_nonce_length(alg, nonce_length);
4348 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004349 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004351
Ronald Cronde822812021-03-17 16:08:20 +01004352 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004353 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004354 alg,
4355 nonce, nonce_length,
4356 additional_data, additional_data_length,
4357 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 if (status != PSA_SUCCESS && plaintext_size != 0) {
4361 memset(plaintext, 0, plaintext_size);
4362 }
mohammad160360a64d02018-06-03 17:20:42 +03004363
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004364exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004365 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 return status;
mohammad16035955c982018-04-26 00:53:03 +03004368}
4369
Gilles Peskine449bd832023-01-11 14:50:10 +01004370static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4371{
4372 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004373
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004375#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004377 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4379 return PSA_ERROR_INVALID_ARGUMENT;
4380 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004381 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004382#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004383
Przemek Stekiel86679c72022-10-06 17:06:56 +02004384#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004386 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4388 return PSA_ERROR_INVALID_ARGUMENT;
4389 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004390 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004391#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004392
Przemek Stekiel86679c72022-10-06 17:06:56 +02004393#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004395 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 if (tag_len != 16) {
4397 return PSA_ERROR_INVALID_ARGUMENT;
4398 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004399 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004400#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004401
4402 default:
4403 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004405 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004407}
4408
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004409/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004410static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4411 int is_encrypt,
4412 mbedtls_svc_key_id_t key,
4413 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004414{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004415 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4416 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4417 psa_key_slot_t *slot = NULL;
4418 psa_key_usage_t key_usage = 0;
4419
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 status = psa_aead_check_algorithm(alg);
4421 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004422 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004424
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004426 status = PSA_ERROR_BAD_STATE;
4427 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004428 }
4429
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 if (operation->nonce_set || operation->lengths_set ||
4431 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004432 status = PSA_ERROR_BAD_STATE;
4433 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004434 }
4435
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004437 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004439 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004441
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4443 alg);
4444 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004445 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004447
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004449 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004451
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 if (is_encrypt) {
4453 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004454 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 slot->key.data,
4456 slot->key.bytes,
4457 alg);
4458 } else {
4459 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004460 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 slot->key.data,
4462 slot->key.bytes,
4463 alg);
4464 }
4465 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004466 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004468
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004469 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004470
4471exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00004472 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004473
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004475 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004477 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 } else {
4479 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004480 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004481
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004483}
4484
Paul Elliott302ff6b2021-04-20 18:10:30 +01004485/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004486psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4487 mbedtls_svc_key_id_t key,
4488 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004489{
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004491}
4492
4493/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004494psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4495 mbedtls_svc_key_id_t key,
4496 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004497{
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004499}
4500
4501/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004502psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4503 uint8_t *nonce,
4504 size_t nonce_size,
4505 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004506{
4507 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004508 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004509 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004510
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004511 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004514 status = PSA_ERROR_BAD_STATE;
4515 goto exit;
4516 }
4517
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004519 status = PSA_ERROR_BAD_STATE;
4520 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004521 }
4522
Paul Elliotte193ea82021-10-01 13:00:16 +01004523 /* For CCM, this size may not be correct according to the PSA
4524 * specification. The PSA Crypto 1.0.1 specification states:
4525 *
4526 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4527 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4528 *
4529 * However this restriction that L has to be the smallest integer is not
4530 * applied in practice, and it is not implementable here since the
4531 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4533 operation->alg);
4534 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004535 status = PSA_ERROR_BUFFER_TOO_SMALL;
4536 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004537 }
4538
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 status = psa_generate_random(local_nonce, required_nonce_size);
4540 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004541 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004543
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004545
Paul Elliott39dc6b82021-05-11 19:16:09 +01004546exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 if (status == PSA_SUCCESS) {
4548 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004549 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 } else {
4551 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004552 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004553
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004555}
4556
4557/* Set the nonce for a multipart authenticated encryption or decryption
4558 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004559psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4560 const uint8_t *nonce,
4561 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004562{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004563 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004566 status = PSA_ERROR_BAD_STATE;
4567 goto exit;
4568 }
4569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004571 status = PSA_ERROR_BAD_STATE;
4572 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004573 }
4574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4576 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004577 status = PSA_ERROR_INVALID_ARGUMENT;
4578 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004579 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004580
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4582 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004583
Paul Elliott39dc6b82021-05-11 19:16:09 +01004584exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004586 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 } else {
4588 psa_aead_abort(operation);
4589 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004590
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004592}
4593
4594/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004595psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4596 size_t ad_length,
4597 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004598{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004599 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4600
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004602 status = PSA_ERROR_BAD_STATE;
4603 goto exit;
4604 }
4605
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 if (operation->lengths_set || operation->ad_started ||
4607 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004608 status = PSA_ERROR_BAD_STATE;
4609 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004610 }
4611
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004613#if defined(PSA_WANT_ALG_GCM)
4614 case PSA_ALG_GCM:
4615 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 * bits. Without the guard this code will generate warnings on 32bit
4617 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004618#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 if (((uint64_t) ad_length) >> 61 != 0 ||
4620 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004621 status = PSA_ERROR_INVALID_ARGUMENT;
4622 goto exit;
4623 }
Paul Elliott325d3742021-09-27 17:56:28 +01004624#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004625 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004626#endif /* PSA_WANT_ALG_GCM */
4627#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004628 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004630 status = PSA_ERROR_INVALID_ARGUMENT;
4631 goto exit;
4632 }
4633 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004634#endif /* PSA_WANT_ALG_CCM */
4635#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004636 case PSA_ALG_CHACHA20_POLY1305:
4637 /* No length restrictions for ChaChaPoly. */
4638 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004639#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004640 default:
4641 break;
4642 }
Paul Elliott325d3742021-09-27 17:56:28 +01004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4645 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004646
Paul Elliott39dc6b82021-05-11 19:16:09 +01004647exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004649 operation->ad_remaining = ad_length;
4650 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004651 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 } else {
4653 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004654 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004655
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004657}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004658
4659/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004660psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4661 const uint8_t *input,
4662 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004663{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004664 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4665
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004667 status = PSA_ERROR_BAD_STATE;
4668 goto exit;
4669 }
4670
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004672 status = PSA_ERROR_BAD_STATE;
4673 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004674 }
4675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 if (operation->lengths_set) {
4677 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004678 status = PSA_ERROR_INVALID_ARGUMENT;
4679 goto exit;
4680 }
4681
4682 operation->ad_remaining -= input_length;
4683 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004684#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004686 status = PSA_ERROR_BAD_STATE;
4687 goto exit;
4688 }
4689#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004690
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 status = psa_driver_wrapper_aead_update_ad(operation, input,
4692 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004693
Paul Elliott39dc6b82021-05-11 19:16:09 +01004694exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004696 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 } else {
4698 psa_aead_abort(operation);
4699 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004700
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004702}
4703
4704/* Encrypt or decrypt a message fragment in an active multipart AEAD
4705 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004706psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4707 const uint8_t *input,
4708 size_t input_length,
4709 uint8_t *output,
4710 size_t output_size,
4711 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004712{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004713 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004714
4715 *output_length = 0;
4716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004718 status = PSA_ERROR_BAD_STATE;
4719 goto exit;
4720 }
4721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004723 status = PSA_ERROR_BAD_STATE;
4724 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004725 }
4726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004728 /* Additional data length was supplied, but not all the additional
4729 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004731 status = PSA_ERROR_INVALID_ARGUMENT;
4732 goto exit;
4733 }
4734
4735 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004737 status = PSA_ERROR_INVALID_ARGUMENT;
4738 goto exit;
4739 }
4740
4741 operation->body_remaining -= input_length;
4742 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004743#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004745 status = PSA_ERROR_BAD_STATE;
4746 goto exit;
4747 }
4748#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004749
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4751 output, output_size,
4752 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004753
Paul Elliott39dc6b82021-05-11 19:16:09 +01004754exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004756 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 } else {
4758 psa_aead_abort(operation);
4759 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004762}
4763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004765{
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 if (operation->id == 0 || !operation->nonce_set) {
4767 return PSA_ERROR_BAD_STATE;
4768 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4771 operation->body_remaining != 0)) {
4772 return PSA_ERROR_INVALID_ARGUMENT;
4773 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004774
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004776}
4777
Paul Elliott302ff6b2021-04-20 18:10:30 +01004778/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004779psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4780 uint8_t *ciphertext,
4781 size_t ciphertext_size,
4782 size_t *ciphertext_length,
4783 uint8_t *tag,
4784 size_t tag_size,
4785 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004786{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004787 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4788
Paul Elliott302ff6b2021-04-20 18:10:30 +01004789 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004790 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004791
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 status = psa_aead_final_checks(operation);
4793 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004794 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004796
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004798 status = PSA_ERROR_BAD_STATE;
4799 goto exit;
4800 }
4801
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4803 ciphertext_size,
4804 ciphertext_length,
4805 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004806
Paul Elliott39dc6b82021-05-11 19:16:09 +01004807exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004808
4809
Paul Elliottf47b0952021-05-21 18:02:33 +01004810 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004811 * the zero tag size, make sure the tag is set to something implausible.
4812 * Even if the operation succeeds, make sure we clear the rest of the
4813 * buffer to prevent potential leakage of anything previously placed in
4814 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004815 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004818
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004820}
4821
4822/* Finish authenticating and decrypting a message in a multipart AEAD
4823 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004824psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4825 uint8_t *plaintext,
4826 size_t plaintext_size,
4827 size_t *plaintext_length,
4828 const uint8_t *tag,
4829 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004830{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004831 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4832
Paul Elliott302ff6b2021-04-20 18:10:30 +01004833 *plaintext_length = 0;
4834
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 status = psa_aead_final_checks(operation);
4836 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004837 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004839
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004841 status = PSA_ERROR_BAD_STATE;
4842 goto exit;
4843 }
4844
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4846 plaintext_size,
4847 plaintext_length,
4848 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004849
4850exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004854}
4855
4856/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004857psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004858{
4859 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4860
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004862 /* The object has (apparently) been initialized but it is not (yet)
4863 * in use. It's ok to call abort on such an object, and there's
4864 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004866 }
4867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004869
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004873}
4874
Gilles Peskinee59236f2018-01-27 23:32:46 +01004875/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004876/* Generators */
4877/****************************************************************/
4878
Przemek Stekiel69c46792022-06-10 12:59:51 +02004879#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004880 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004881 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05304882 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304883 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08004884#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004885#endif /* At least one builtin KDF */
4886
Przemek Stekiel69c46792022-06-10 12:59:51 +02004887#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004888 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4889 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4890static psa_status_t psa_key_derivation_start_hmac(
4891 psa_mac_operation_t *operation,
4892 psa_algorithm_t hash_alg,
4893 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02004895{
4896 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4899 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4900 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02004901
Steven Cooreman72f736a2021-05-07 14:14:37 +02004902 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02004904
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 status = psa_driver_wrapper_mac_sign_setup(operation,
4906 &attributes,
4907 hmac_key, hmac_key_length,
4908 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02004909
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 psa_reset_key_attributes(&attributes);
4911 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02004912}
4913#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004914
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004915#define HKDF_STATE_INIT 0 /* no input yet */
4916#define HKDF_STATE_STARTED 1 /* got salt */
4917#define HKDF_STATE_KEYED 2 /* got key */
4918#define HKDF_STATE_OUTPUT 3 /* output started */
4919
Gilles Peskinecbe66502019-05-16 16:59:18 +02004920static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004922{
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4924 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4925 } else {
4926 return operation->alg;
4927 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004928}
4929
Gilles Peskine449bd832023-01-11 14:50:10 +01004930psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004931{
4932 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4934 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004935 /* The object has (apparently) been initialized but it is not
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 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004939#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
4941 mbedtls_free(operation->ctx.hkdf.info);
4942 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4943 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004944#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004945#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4946 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4948 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4949 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4950 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004951 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02004953 }
4954
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004956 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01004958 }
4959
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004961 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01004963 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01004964#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004966 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02004968 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01004969#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02004970 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004971
4972 /* We leave the fields Ai and output_block to be erased safely by the
4973 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004975#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4976 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004977#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
4979 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
4980 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
4981 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004982#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304983#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05304984 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304985 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004986 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304987 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304988 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304989
4990 status = PSA_SUCCESS;
4991 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304992#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004993 {
4994 status = PSA_ERROR_BAD_STATE;
4995 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 mbedtls_platform_zeroize(operation, sizeof(*operation));
4997 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004998}
4999
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005000psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005002{
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005004 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005006 }
5007
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005008 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005010}
5011
Gilles Peskine449bd832023-01-11 14:50:10 +01005012psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5013 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005014{
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 if (operation->alg == 0) {
5016 return PSA_ERROR_BAD_STATE;
5017 }
5018 if (capacity > operation->capacity) {
5019 return PSA_ERROR_INVALID_ARGUMENT;
5020 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005021 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005023}
5024
Przemek Stekiel69c46792022-06-10 12:59:51 +02005025#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005026/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005027static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5028 psa_algorithm_t kdf_alg,
5029 uint8_t *output,
5030 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005031{
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5033 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005034 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005035 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005036#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005038#else
5039 const uint8_t last_block = 0xff;
5040#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005041
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 if (hkdf->state < HKDF_STATE_KEYED ||
5043 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005044#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005046#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 )) {
5048 return PSA_ERROR_BAD_STATE;
5049 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005050 hkdf->state = HKDF_STATE_OUTPUT;
5051
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005053 /* Copy what remains of the current block */
5054 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005056 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 }
5058 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005059 output += n;
5060 output_length -= n;
5061 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005063 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005065 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005066 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005067 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005068 * object was corrupted or if this function is called directly
5069 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 if (hkdf->block_number == last_block) {
5071 return PSA_ERROR_BAD_STATE;
5072 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005073
5074 /* We need a new block */
5075 ++hkdf->block_number;
5076 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005077
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5079 hash_alg,
5080 hkdf->prk,
5081 hash_length);
5082 if (status != PSA_SUCCESS) {
5083 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005084 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005085
5086 if (hkdf->block_number != 1) {
5087 status = psa_mac_update(&hkdf->hmac,
5088 hkdf->output_block,
5089 hash_length);
5090 if (status != PSA_SUCCESS) {
5091 return status;
5092 }
5093 }
5094 status = psa_mac_update(&hkdf->hmac,
5095 hkdf->info,
5096 hkdf->info_length);
5097 if (status != PSA_SUCCESS) {
5098 return status;
5099 }
5100 status = psa_mac_update(&hkdf->hmac,
5101 &hkdf->block_number, 1);
5102 if (status != PSA_SUCCESS) {
5103 return status;
5104 }
5105 status = psa_mac_sign_finish(&hkdf->hmac,
5106 hkdf->output_block,
5107 sizeof(hkdf->output_block),
5108 &hmac_output_length);
5109 if (status != PSA_SUCCESS) {
5110 return status;
5111 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005112 }
5113
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005115}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005116#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005117
John Durkop07cc04a2020-11-16 22:08:34 -08005118#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5119 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005120static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5121 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005123{
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5125 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005126 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5127 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005128 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005129
Janos Follath7742fee2019-06-17 12:58:10 +01005130 /* We can't be wanting more output after block 0xff, otherwise
5131 * the capacity check in psa_key_derivation_output_bytes() would have
5132 * prevented this call. It could happen only if the operation
5133 * object was corrupted or if this function is called directly
5134 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 if (tls12_prf->block_number == 0xff) {
5136 return PSA_ERROR_CORRUPTION_DETECTED;
5137 }
Janos Follath7742fee2019-06-17 12:58:10 +01005138
5139 /* We need a new block */
5140 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005141 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005142
5143 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5144 *
5145 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5146 *
5147 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5148 * HMAC_hash(secret, A(2) + seed) +
5149 * HMAC_hash(secret, A(3) + seed) + ...
5150 *
5151 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005152 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005153 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005154 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005155 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005156 * is currently extracted as `output_block` and where i is
5157 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005158 */
5159
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 status = psa_key_derivation_start_hmac(&hmac,
5161 hash_alg,
5162 tls12_prf->secret,
5163 tls12_prf->secret_length);
5164 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005165 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005167
5168 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005170 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5171 * the variable seed and in this instance means it in the context of the
5172 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 status = psa_mac_update(&hmac,
5174 tls12_prf->label,
5175 tls12_prf->label_length);
5176 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005177 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 }
5179 status = psa_mac_update(&hmac,
5180 tls12_prf->seed,
5181 tls12_prf->seed_length);
5182 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005183 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 }
5185 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005186 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5188 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005189 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005191 }
5192
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 status = psa_mac_sign_finish(&hmac,
5194 tls12_prf->Ai, hash_length,
5195 &hmac_output_length);
5196 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005197 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 }
5199 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005200 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005202
5203 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 status = psa_key_derivation_start_hmac(&hmac,
5205 hash_alg,
5206 tls12_prf->secret,
5207 tls12_prf->secret_length);
5208 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005209 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 }
5211 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5212 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005213 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 }
5215 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5216 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005217 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 }
5219 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5220 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005221 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 }
5223 status = psa_mac_sign_finish(&hmac,
5224 tls12_prf->output_block, hash_length,
5225 &hmac_output_length);
5226 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005227 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005229
Janos Follath7742fee2019-06-17 12:58:10 +01005230
5231cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 cleanup_status = psa_mac_abort(&hmac);
5233 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005234 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005238}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005239
Janos Follath844eb0e2019-06-19 12:10:49 +01005240static psa_status_t psa_key_derivation_tls12_prf_read(
5241 psa_tls12_prf_key_derivation_t *tls12_prf,
5242 psa_algorithm_t alg,
5243 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005245{
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5247 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005248 psa_status_t status;
5249 uint8_t offset, length;
5250
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005252 case PSA_TLS12_PRF_STATE_LABEL_SET:
5253 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5254 break;
5255 case PSA_TLS12_PRF_STATE_OUTPUT:
5256 break;
5257 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005259 }
5260
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005262 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 if (tls12_prf->left_in_block == 0) {
5264 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5265 alg);
5266 if (status != PSA_SUCCESS) {
5267 return status;
5268 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005269
5270 continue;
5271 }
5272
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005274 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005276 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005278
5279 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005281 output += length;
5282 output_length -= length;
5283 tls12_prf->left_in_block -= length;
5284 }
5285
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005287}
John Durkop07cc04a2020-11-16 22:08:34 -08005288#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5289 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005290
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005291#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5292static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5293 psa_tls12_ecjpake_to_pms_t *ecjpake,
5294 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005296{
5297 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005298 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 if (output_length != 32) {
5301 return PSA_ERROR_INVALID_ARGUMENT;
5302 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005303
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5305 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5306 &output_size);
5307 if (status != PSA_SUCCESS) {
5308 return status;
5309 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005310
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 if (output_size != output_length) {
5312 return PSA_ERROR_GENERIC_ERROR;
5313 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005314
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005316}
5317#endif
5318
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305319#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305320static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5321 psa_pbkdf2_key_derivation_t *pbkdf2,
5322 psa_algorithm_t prf_alg,
5323 uint8_t prf_output_length,
5324 psa_key_attributes_t *attributes)
5325{
5326 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305327 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305328 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305329 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305330 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305331 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305332 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305333
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305334 mac_operation.is_sign = 1;
5335 mac_operation.mac_size = prf_output_length;
5336 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305337
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305338 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5339 attributes,
5340 pbkdf2->password,
5341 pbkdf2->password_length,
5342 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305343 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305344 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305345 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305346 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5347 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305348 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305349 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305350 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305351 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305352 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305353 }
5354 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5355 &mac_output_length);
5356 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305357 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305358 }
5359
5360 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305361 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305362 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305363 }
5364
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305365 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305366
5367 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305368 /* We are passing prf_output_length as mac_size because the driver
5369 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305370 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305371 status = psa_driver_wrapper_mac_compute(attributes,
5372 pbkdf2->password,
5373 pbkdf2->password_length,
5374 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305375 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305376 &mac_output_length);
5377 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305378 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305379 }
5380
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305381 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305382 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305383
5384cleanup:
5385 /* Zeroise buffers to clear sensitive data from memory. */
5386 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5387 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305388}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305389
5390static psa_status_t psa_key_derivation_pbkdf2_read(
5391 psa_pbkdf2_key_derivation_t *pbkdf2,
5392 psa_algorithm_t kdf_alg,
5393 uint8_t *output,
5394 size_t output_length)
5395{
5396 psa_status_t status;
5397 psa_algorithm_t prf_alg;
5398 uint8_t prf_output_length;
5399 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5400 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5401 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5402
5403 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5404 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5405 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5406 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305407 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5408 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305409 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305410 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305411 } else {
5412 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305413 }
5414
5415 switch (pbkdf2->state) {
5416 case PSA_PBKDF2_STATE_PASSWORD_SET:
5417 /* Initially we need a new block so bytes_used is equal to block size*/
5418 pbkdf2->bytes_used = prf_output_length;
5419 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5420 break;
5421 case PSA_PBKDF2_STATE_OUTPUT:
5422 break;
5423 default:
5424 return PSA_ERROR_BAD_STATE;
5425 }
5426
5427 while (output_length != 0) {
5428 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5429 if (n > output_length) {
5430 n = (uint8_t) output_length;
5431 }
5432 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5433 output += n;
5434 output_length -= n;
5435 pbkdf2->bytes_used += n;
5436
5437 if (output_length == 0) {
5438 break;
5439 }
5440
5441 /* We need a new block */
5442 pbkdf2->bytes_used = 0;
5443 pbkdf2->block_number++;
5444
5445 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5446 prf_output_length,
5447 &attributes);
5448 if (status != PSA_SUCCESS) {
5449 return status;
5450 }
5451 }
5452
5453 return PSA_SUCCESS;
5454}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305455#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305456
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005457psa_status_t psa_key_derivation_output_bytes(
5458 psa_key_derivation_operation_t *operation,
5459 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005461{
5462 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005463 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005464
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005466 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005468 }
5469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005471 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005472 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005473 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005474 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005475 goto exit;
5476 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005478 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005479 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005480 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5481 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005482 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005483 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005485 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005486 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005487
Przemek Stekiel69c46792022-06-10 12:59:51 +02005488#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5490 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5491 output, output_length);
5492 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005493#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005494#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5495 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5497 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5498 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5499 kdf_alg, output,
5500 output_length);
5501 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005502#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5503 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005504#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005506 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5508 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005509#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305510#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305511 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305512 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5513 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305514 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305515#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005516
Gilles Peskineeab56e42018-07-12 17:12:33 +02005517 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005518 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005520 }
5521
5522exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005524 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 * This allows us to differentiate between exhausted operations and
5526 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5527 * operations. */
5528 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005530 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005532 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005534}
5535
David Brown7807bf72021-02-09 16:28:23 -07005536#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005537static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005538{
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 if (data_size >= 8) {
5540 mbedtls_des_key_set_parity(data);
5541 }
5542 if (data_size >= 16) {
5543 mbedtls_des_key_set_parity(data + 8);
5544 }
5545 if (data_size >= 24) {
5546 mbedtls_des_key_set_parity(data + 16);
5547 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005548}
David Brown7807bf72021-02-09 16:28:23 -07005549#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005550
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005551/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 * ECC keys on a Weierstrass elliptic curve require the generation
5553 * of a private key which is an integer
5554 * in the range [1, N - 1], where N is the boundary of the private key domain:
5555 * N is the prime p for Diffie-Hellman, or the order of the
5556 * curve’s base point for ECC.
5557 *
5558 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5559 * This function generates the private key using the following process:
5560 *
5561 * 1. Draw a byte string of length ceiling(m/8) bytes.
5562 * 2. If m is not a multiple of 8, set the most significant
5563 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5564 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5565 * 4. If k > N - 2, discard the result and return to step 1.
5566 * 5. Output k + 1 as the private key.
5567 *
5568 * This method allows compliance to NIST standards, specifically the methods titled
5569 * Key-Pair Generation by Testing Candidates in the following publications:
5570 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5571 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5572 * Diffie-Hellman keys.
5573 *
5574 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5575 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5576 *
5577 * Note: Function allocates memory for *data buffer, so given *data should be
5578 * always NULL.
5579 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005580#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5581#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005582static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005583 psa_key_slot_t *slot,
5584 size_t bits,
5585 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005586 uint8_t **data
5587 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005588{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005589 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005590 mbedtls_mpi k;
5591 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005593 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005594 size_t m;
5595 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005596
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 mbedtls_mpi_init(&k);
5598 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005599
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005600 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005602 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01005603 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005604
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005606 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005607 goto cleanup;
5608 }
5609
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005610 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005612
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005614
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005615 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005616 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005617 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005618
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005619 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005620
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005621 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005623
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005624 /* Note: This function is always called with *data == NULL and it
5625 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 *data = mbedtls_calloc(1, m_bytes);
5627 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005628 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005629 goto cleanup;
5630 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005631
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005633 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005635 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005637
5638 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005640 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005641 * (8 * ceiling(m/8) - m) bits of the first byte in
5642 * the string to zero.
5643 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005645 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005646 }
5647
5648 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 * big-endian byte string.
5650 */
5651 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005652
5653 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005654 * Result of comparison is returned. When it indicates error
5655 * then this function is called again.
5656 */
5657 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005658 }
5659
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005660 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005661 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5662 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005663cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 if (ret != 0) {
5665 status = mbedtls_to_psa_error(ret);
5666 }
5667 if (status != PSA_SUCCESS) {
5668 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005669 *data = NULL;
5670 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 mbedtls_mpi_free(&k);
5672 mbedtls_mpi_free(&diff_N_2);
5673 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005674}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005675
5676/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5677 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5678 *
5679 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5680 * draw a 32-byte string and process it as specified in
5681 * Elliptic Curves for Security [RFC7748] §5.
5682 *
5683 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5684 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5685 *
5686 * Note: Function allocates memory for *data buffer, so given *data should be
5687 * always NULL.
5688 */
5689
5690static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5691 size_t bits,
5692 psa_key_derivation_operation_t *operation,
5693 uint8_t **data
5694 )
5695{
5696 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005697 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005698
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005700 case 255:
5701 output_length = 32;
5702 break;
5703 case 448:
5704 output_length = 56;
5705 break;
5706 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005708 break;
5709 }
5710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 if (*data == NULL) {
5714 return PSA_ERROR_INSUFFICIENT_MEMORY;
5715 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005720 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005722
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005724 case 255:
5725 (*data)[0] &= 248;
5726 (*data)[31] &= 127;
5727 (*data)[31] |= 64;
5728 break;
5729 case 448:
5730 (*data)[0] &= 252;
5731 (*data)[55] |= 128;
5732 break;
5733 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005735 break;
5736 }
5737
5738 return status;
5739}
Valerio Setti86587ab2023-06-27 13:09:30 +02005740#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5741static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5742 psa_key_slot_t *slot, size_t bits,
5743 psa_key_derivation_operation_t *operation, uint8_t **data)
5744{
5745 (void) slot;
5746 (void) bits;
5747 (void) operation;
5748 (void) data;
5749 return PSA_ERROR_NOT_SUPPORTED;
5750}
5751
5752static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5753 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5754{
5755 (void) bits;
5756 (void) operation;
5757 (void) data;
5758 return PSA_ERROR_NOT_SUPPORTED;
5759}
5760#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5761#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005762
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005763static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005764 psa_key_slot_t *slot,
5765 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005767{
5768 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305770 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005771 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5774 return PSA_ERROR_INVALID_ARGUMENT;
5775 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005776
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005777#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005778 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5780 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5781 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005782 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5784 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005785 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 }
5787 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005788 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5790 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005791 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005793 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005794 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005795#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005796 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 if (key_type_is_raw_bytes(slot->attr.type)) {
5798 if (bits % 8 != 0) {
5799 return PSA_ERROR_INVALID_ARGUMENT;
5800 }
5801 data = mbedtls_calloc(1, bytes);
5802 if (data == NULL) {
5803 return PSA_ERROR_INSUFFICIENT_MEMORY;
5804 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005805
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 status = psa_key_derivation_output_bytes(operation, data, bytes);
5807 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005808 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 }
David Brown12ca5032021-02-11 11:02:00 -07005810#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5812 psa_des_set_key_parity(data, bytes);
5813 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005814#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 } else {
5816 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005817 }
Ronald Crondd04d422020-11-28 15:14:42 +01005818
Ronald Cronbf33c932020-11-28 18:06:53 +01005819 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005820
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005821 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005822 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 &storage_size);
5824 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305825 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 }
Archanad8a83dc2021-06-14 10:04:16 +05305827 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 status = psa_allocate_buffer_to_slot(slot, storage_size);
5829 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305830 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 }
Archanad8a83dc2021-06-14 10:04:16 +05305832
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005833 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 data, bytes,
5835 slot->key.data,
5836 slot->key.bytes,
5837 &slot->key.bytes, &bits);
5838 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005839 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005841
5842exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 mbedtls_free(data);
5844 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005845}
5846
Gilles Peskine092ce512024-02-20 12:31:24 +01005847static const psa_key_production_parameters_t default_production_parameters =
5848 PSA_KEY_PRODUCTION_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005849
Gilles Peskine092ce512024-02-20 12:31:24 +01005850int psa_key_production_parameters_are_default(
5851 const psa_key_production_parameters_t *params,
5852 size_t params_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005853{
Gilles Peskine092ce512024-02-20 12:31:24 +01005854 if (params->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005855 return 0;
5856 }
Gilles Peskine092ce512024-02-20 12:31:24 +01005857 if (params_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005858 return 0;
5859 }
5860 return 1;
5861}
5862
5863psa_status_t psa_key_derivation_output_key_ext(
5864 const psa_key_attributes_t *attributes,
5865 psa_key_derivation_operation_t *operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005866 const psa_key_production_parameters_t *params,
5867 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005868 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005869{
5870 psa_status_t status;
5871 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005872 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005873
Ronald Cron81709fc2020-11-14 12:10:32 +01005874 *key = MBEDTLS_SVC_KEY_ID_INIT;
5875
Gilles Peskine0f84d622019-09-12 19:03:13 +02005876 /* Reject any attempt to create a zero-length key so that we don't
5877 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 if (psa_get_key_bits(attributes) == 0) {
5879 return PSA_ERROR_INVALID_ARGUMENT;
5880 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005881
Gilles Peskine092ce512024-02-20 12:31:24 +01005882 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005883 return PSA_ERROR_INVALID_ARGUMENT;
5884 }
5885
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 if (operation->alg == PSA_ALG_NONE) {
5887 return PSA_ERROR_BAD_STATE;
5888 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005889
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 if (!operation->can_output_key) {
5891 return PSA_ERROR_NOT_PERMITTED;
5892 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005893
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5895 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005896#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005897 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005898 /* Deriving a key in a secure element is not implemented yet. */
5899 status = PSA_ERROR_NOT_SUPPORTED;
5900 }
5901#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 if (status == PSA_SUCCESS) {
5903 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005904 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005906 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 if (status == PSA_SUCCESS) {
5908 status = psa_finish_key_creation(slot, driver, key);
5909 }
5910 if (status != PSA_SUCCESS) {
5911 psa_fail_key_creation(slot, driver);
5912 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005913
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005915}
5916
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005917psa_status_t psa_key_derivation_output_key(
5918 const psa_key_attributes_t *attributes,
5919 psa_key_derivation_operation_t *operation,
5920 mbedtls_svc_key_id_t *key)
5921{
Gilles Peskinec81393b2024-02-14 20:51:28 +01005922 return psa_key_derivation_output_key_ext(attributes, operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005923 &default_production_parameters, 0,
Gilles Peskinec81393b2024-02-14 20:51:28 +01005924 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005925}
Gilles Peskineeab56e42018-07-12 17:12:33 +02005926
5927
5928/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005929/* Key derivation */
5930/****************************************************************/
5931
Steven Cooreman932ffb72021-02-15 12:14:32 +01005932#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005933static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005934{
5935#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5937 return 1;
5938 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005939#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005940#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5942 return 1;
5943 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005944#endif
5945#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5947 return 1;
5948 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005949#endif
5950#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5952 return 1;
5953 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005954#endif
5955#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5957 return 1;
5958 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005959#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005960#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5962 return 1;
5963 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005964#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05305965#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5966 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5967 return 1;
5968 }
5969#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05305970#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
5971 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5972 return 1;
5973 }
5974#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005976}
5977
Gilles Peskine449bd832023-01-11 14:50:10 +01005978static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005979{
5980 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 psa_status_t status = psa_hash_setup(&operation, alg);
5982 psa_hash_abort(&operation);
5983 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005984}
5985
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05305986static psa_status_t psa_key_derivation_set_maximum_capacity(
5987 psa_key_derivation_operation_t *operation,
5988 psa_algorithm_t kdf_alg)
5989{
5990#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5991 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5992 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
5993 return PSA_SUCCESS;
5994 }
5995#endif
5996#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
5997 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5998#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05305999 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306000 PSA_KEY_TYPE_AES,
6001 128U,
6002 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306003#else
6004 operation->capacity = SIZE_MAX;
6005#endif
6006 return PSA_SUCCESS;
6007 }
6008#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6009
6010 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6011 * invalid or meaningless but it does not affect this function */
6012 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6013 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306014 if (hash_size == 0) {
6015 return PSA_ERROR_NOT_SUPPORTED;
6016 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306017
6018 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6019 * we might fail later, which is somewhat unfriendly and potentially
6020 * risk-prone. */
6021 psa_status_t status = psa_hash_try_support(hash_alg);
6022 if (status != PSA_SUCCESS) {
6023 return status;
6024 }
6025
6026#if defined(PSA_WANT_ALG_HKDF)
6027 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6028 operation->capacity = 255 * hash_size;
6029 } else
6030#endif
6031#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6032 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6033 operation->capacity = hash_size;
6034 } else
6035#endif
6036#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6037 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6038 operation->capacity = 255 * hash_size;
6039 } else
6040#endif
6041#if defined(PSA_WANT_ALG_TLS12_PRF)
6042 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6043 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6044 operation->capacity = SIZE_MAX;
6045 } else
6046#endif
6047#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6048 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6049 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6050 /* Master Secret is always 48 bytes
6051 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6052 operation->capacity = 48U;
6053 } else
6054#endif
6055#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6056 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6057#if (SIZE_MAX > UINT32_MAX)
6058 operation->capacity = UINT32_MAX * hash_size;
6059#else
6060 operation->capacity = SIZE_MAX;
6061#endif
6062 } else
6063#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6064 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306065 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306066 status = PSA_ERROR_NOT_SUPPORTED;
6067 }
6068 return status;
6069}
6070
Gilles Peskine969c5d62019-01-16 15:53:06 +01006071static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006072 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006074{
Janos Follath5fe19732019-06-20 15:09:30 +01006075 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6076 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006078
Gilles Peskine969c5d62019-01-16 15:53:06 +01006079 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 if (!is_kdf_alg_supported(kdf_alg)) {
6081 return PSA_ERROR_NOT_SUPPORTED;
6082 }
John Durkop07cc04a2020-11-16 22:08:34 -08006083
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306084 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6085 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306086 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006087}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006088
Gilles Peskine449bd832023-01-11 14:50:10 +01006089static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006090{
6091#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 if (alg == PSA_ALG_ECDH) {
6093 return PSA_SUCCESS;
6094 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006095#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006096#if defined(PSA_WANT_ALG_FFDH)
6097 if (alg == PSA_ALG_FFDH) {
6098 return PSA_SUCCESS;
6099 }
6100#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006101 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006103}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006104
6105static int psa_key_derivation_allows_free_form_secret_input(
6106 psa_algorithm_t kdf_alg)
6107{
6108#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6109 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6110 return 0;
6111 }
6112#endif
6113 (void) kdf_alg;
6114 return 1;
6115}
John Durkop07cc04a2020-11-16 22:08:34 -08006116#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006117
Gilles Peskine449bd832023-01-11 14:50:10 +01006118psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6119 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006120{
6121 psa_status_t status;
6122
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 if (operation->alg != 0) {
6124 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006125 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006126
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6128 return PSA_ERROR_INVALID_ARGUMENT;
6129 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6130#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6131 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6132 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6133 status = psa_key_agreement_try_support(ka_alg);
6134 if (status != PSA_SUCCESS) {
6135 return status;
6136 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006137 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6138 return PSA_ERROR_INVALID_ARGUMENT;
6139 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6141#else
6142 return PSA_ERROR_NOT_SUPPORTED;
6143#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6144 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6145#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6146 status = psa_key_derivation_setup_kdf(operation, alg);
6147#else
6148 return PSA_ERROR_NOT_SUPPORTED;
6149#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6150 } else {
6151 return PSA_ERROR_INVALID_ARGUMENT;
6152 }
6153
6154 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006155 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 }
6157 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006158}
6159
Przemek Stekiel69c46792022-06-10 12:59:51 +02006160#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006161static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6162 psa_algorithm_t kdf_alg,
6163 psa_key_derivation_step_t step,
6164 const uint8_t *data,
6165 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006166{
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006168 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006170 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006171#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6173 return PSA_ERROR_INVALID_ARGUMENT;
6174 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006175#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 if (hkdf->state != HKDF_STATE_INIT) {
6177 return PSA_ERROR_BAD_STATE;
6178 } else {
6179 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6180 hash_alg,
6181 data, data_length);
6182 if (status != PSA_SUCCESS) {
6183 return status;
6184 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006185 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006187 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006188 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006189#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006191 /* We shouldn't be in different state as HKDF_EXPAND only allows
6192 * two inputs: SECRET (this case) and INFO which does not modify
6193 * the state. It could happen only if the hkdf
6194 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 if (hkdf->state != HKDF_STATE_INIT) {
6196 return PSA_ERROR_BAD_STATE;
6197 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006198
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006199 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6201 return PSA_ERROR_INVALID_ARGUMENT;
6202 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 memcpy(hkdf->prk, data, data_length);
6205 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006206#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006207 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006208 /* HKDF: If no salt was provided, use an empty salt.
6209 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006211#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6213 return PSA_ERROR_BAD_STATE;
6214 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006215#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6217 hash_alg,
6218 NULL, 0);
6219 if (status != PSA_SUCCESS) {
6220 return status;
6221 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006222 hkdf->state = HKDF_STATE_STARTED;
6223 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 if (hkdf->state != HKDF_STATE_STARTED) {
6225 return PSA_ERROR_BAD_STATE;
6226 }
6227 status = psa_mac_update(&hkdf->hmac,
6228 data, data_length);
6229 if (status != PSA_SUCCESS) {
6230 return status;
6231 }
6232 status = psa_mac_sign_finish(&hkdf->hmac,
6233 hkdf->prk,
6234 sizeof(hkdf->prk),
6235 &data_length);
6236 if (status != PSA_SUCCESS) {
6237 return status;
6238 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006239 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006240
Gilles Peskine2b522db2019-04-12 15:11:49 +02006241 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006242 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006243#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006245 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006247 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006249#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006250 {
6251 /* Block 0 is empty, and the next block will be
6252 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006254 }
6255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006257 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006258#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6260 return PSA_ERROR_INVALID_ARGUMENT;
6261 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006262#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006263#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6265 hkdf->state == HKDF_STATE_INIT) {
6266 return PSA_ERROR_BAD_STATE;
6267 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006268#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 if (hkdf->state == HKDF_STATE_OUTPUT) {
6270 return PSA_ERROR_BAD_STATE;
6271 }
6272 if (hkdf->info_set) {
6273 return PSA_ERROR_BAD_STATE;
6274 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006275 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 if (data_length != 0) {
6277 hkdf->info = mbedtls_calloc(1, data_length);
6278 if (hkdf->info == NULL) {
6279 return PSA_ERROR_INSUFFICIENT_MEMORY;
6280 }
6281 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006282 }
6283 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006285 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006287 }
6288}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006289#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006290
John Durkop07cc04a2020-11-16 22:08:34 -08006291#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6292 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006293static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6294 const uint8_t *data,
6295 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006296{
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6298 return PSA_ERROR_BAD_STATE;
6299 }
Janos Follathf08e2652019-06-13 09:05:41 +01006300
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 if (data_length != 0) {
6302 prf->seed = mbedtls_calloc(1, data_length);
6303 if (prf->seed == NULL) {
6304 return PSA_ERROR_INSUFFICIENT_MEMORY;
6305 }
Janos Follathf08e2652019-06-13 09:05:41 +01006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006308 prf->seed_length = data_length;
6309 }
Janos Follathf08e2652019-06-13 09:05:41 +01006310
Gilles Peskine43f958b2020-12-13 14:55:14 +01006311 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006312
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006314}
6315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6317 const uint8_t *data,
6318 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006319{
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6321 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6322 return PSA_ERROR_BAD_STATE;
6323 }
Janos Follath81550542019-06-13 14:26:34 +01006324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 if (data_length != 0) {
6326 prf->secret = mbedtls_calloc(1, data_length);
6327 if (prf->secret == NULL) {
6328 return PSA_ERROR_INSUFFICIENT_MEMORY;
6329 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006330
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006332 prf->secret_length = data_length;
6333 }
Janos Follath81550542019-06-13 14:26:34 +01006334
Gilles Peskine43f958b2020-12-13 14:55:14 +01006335 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006338}
6339
Gilles Peskine449bd832023-01-11 14:50:10 +01006340static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6341 const uint8_t *data,
6342 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006343{
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6345 return PSA_ERROR_BAD_STATE;
6346 }
Janos Follath63028dd2019-06-13 09:15:47 +01006347
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 if (data_length != 0) {
6349 prf->label = mbedtls_calloc(1, data_length);
6350 if (prf->label == NULL) {
6351 return PSA_ERROR_INSUFFICIENT_MEMORY;
6352 }
Janos Follath63028dd2019-06-13 09:15:47 +01006353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006355 prf->label_length = data_length;
6356 }
Janos Follath63028dd2019-06-13 09:15:47 +01006357
Gilles Peskine43f958b2020-12-13 14:55:14 +01006358 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006361}
6362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6364 psa_key_derivation_step_t step,
6365 const uint8_t *data,
6366 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006367{
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006369 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006371 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006373 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006375 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006377 }
6378}
John Durkop07cc04a2020-11-16 22:08:34 -08006379#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6380 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6381
6382#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6383static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6384 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006385 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006387{
6388 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6390 4 + data_length + prf->other_secret_length :
6391 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6394 return PSA_ERROR_INVALID_ARGUMENT;
6395 }
John Durkop07cc04a2020-11-16 22:08:34 -08006396
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 uint8_t *pms = mbedtls_calloc(1, pms_len);
6398 if (pms == NULL) {
6399 return PSA_ERROR_INSUFFICIENT_MEMORY;
6400 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006401 uint8_t *cur = pms;
6402
6403 /* pure-PSK:
6404 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006405 *
6406 * The premaster secret is formed as follows: if the PSK is N octets
6407 * long, concatenate a uint16 with the value N, N zero octets, a second
6408 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006409 *
6410 * mixed-PSK:
6411 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6412 * follows: concatenate a uint16 with the length of the other secret,
6413 * the other secret itself, uint16 with the length of PSK, and the
6414 * PSK itself.
6415 * For details please check:
6416 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6417 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6418 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006419 */
6420
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6422 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6423 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6424 if (prf->other_secret_length != 0) {
6425 memcpy(cur, prf->other_secret, prf->other_secret_length);
6426 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006427 cur += prf->other_secret_length;
6428 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 } else {
6430 *cur++ = MBEDTLS_BYTE_1(data_length);
6431 *cur++ = MBEDTLS_BYTE_0(data_length);
6432 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006433 cur += data_length;
6434 }
6435
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 *cur++ = MBEDTLS_BYTE_1(data_length);
6437 *cur++ = MBEDTLS_BYTE_0(data_length);
6438 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006439 cur += data_length;
6440
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006441 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006442
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006443 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006445}
Janos Follath6660f0e2019-06-17 08:44:03 +01006446
Przemek Stekiele47201b2022-04-19 13:53:28 +02006447static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6448 psa_tls12_prf_key_derivation_t *prf,
6449 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006451{
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6453 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006454 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006455
6456 if (data_length != 0) {
6457 prf->other_secret = mbedtls_calloc(1, data_length);
6458 if (prf->other_secret == NULL) {
6459 return PSA_ERROR_INSUFFICIENT_MEMORY;
6460 }
6461
6462 memcpy(prf->other_secret, data, data_length);
6463 prf->other_secret_length = data_length;
6464 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006465 prf->other_secret_length = 0;
6466 }
6467
6468 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6469
Gilles Peskine449bd832023-01-11 14:50:10 +01006470 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006471}
6472
Janos Follath6660f0e2019-06-17 08:44:03 +01006473static psa_status_t psa_tls12_prf_psk_to_ms_input(
6474 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006475 psa_key_derivation_step_t step,
6476 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006478{
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006480 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 return psa_tls12_prf_psk_to_ms_set_key(prf,
6482 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006483 break;
6484 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6486 data,
6487 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006488 break;
6489 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006491 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006492
Przemek Stekiele47201b2022-04-19 13:53:28 +02006493 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006494}
John Durkop07cc04a2020-11-16 22:08:34 -08006495#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006496
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006497#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6498static psa_status_t psa_tls12_ecjpake_to_pms_input(
6499 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006500 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006501 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006503{
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6505 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6506 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006507 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006508
6509 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 if (data[0] != 0x04) {
6511 return PSA_ERROR_INVALID_ARGUMENT;
6512 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006513
6514 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006516
Gilles Peskine449bd832023-01-11 14:50:10 +01006517 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006518}
6519#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306520
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306521#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306522static psa_status_t psa_pbkdf2_set_input_cost(
6523 psa_pbkdf2_key_derivation_t *pbkdf2,
6524 psa_key_derivation_step_t step,
6525 uint64_t data)
6526{
6527 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6528 return PSA_ERROR_INVALID_ARGUMENT;
6529 }
6530
6531 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6532 return PSA_ERROR_BAD_STATE;
6533 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306534
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306535 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306536 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306537 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306538
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306539 if (data == 0) {
6540 return PSA_ERROR_INVALID_ARGUMENT;
6541 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306542
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306543 pbkdf2->input_cost = data;
6544 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6545
6546 return PSA_SUCCESS;
6547}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306548
6549static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6550 const uint8_t *data,
6551 size_t data_length)
6552{
Gilles Peskineead17662023-08-20 22:02:51 +02006553 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6554 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6555 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6556 /* Appending to existing salt. No state change. */
6557 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306558 return PSA_ERROR_BAD_STATE;
6559 }
6560
Gilles Peskineca57d782023-07-20 19:08:06 +02006561 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006562 /* Appending an empty string, nothing to do. */
6563 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306564 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306565
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306566 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6567 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306568 return PSA_ERROR_INSUFFICIENT_MEMORY;
6569 }
6570
Gilles Peskineead17662023-08-20 22:02:51 +02006571 if (pbkdf2->salt_length != 0) {
6572 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6573 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306574 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306575 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306576 mbedtls_free(pbkdf2->salt);
6577 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306578 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306579 return PSA_SUCCESS;
6580}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306581
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306582#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306583static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306584 const uint8_t *input,
6585 size_t input_len,
6586 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306587 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306588{
6589 psa_status_t status = PSA_SUCCESS;
6590 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6591 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306592 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306593 } else {
6594 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306595 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306596 }
6597 return status;
6598}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306599#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306600
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306601#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306602static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6603 size_t input_len,
6604 uint8_t *output,
6605 size_t *output_len)
6606{
6607 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306608 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306610 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306611 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6612 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6613 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306614 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306615 * mac_size as the driver function sets mac_output_length = mac_size
6616 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306617 status = psa_driver_wrapper_mac_compute(&attributes,
6618 zeros, sizeof(zeros),
6619 PSA_ALG_CMAC, input, input_len,
6620 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306621 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6622 128U,
6623 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306624 output_len);
6625 } else {
6626 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306627 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306628 }
6629 return status;
6630}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306631#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306632
6633static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306634 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306635 const uint8_t *data,
6636 size_t data_length)
6637{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306638 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306639 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6640 return PSA_ERROR_BAD_STATE;
6641 }
6642
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306643#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306644 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6645 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6646 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6647 pbkdf2->password,
6648 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306649 } else
6650#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6651#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6652 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306653 status = psa_pbkdf2_cmac_set_password(data, data_length,
6654 pbkdf2->password,
6655 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306656 } else
6657#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6658 {
6659 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306660 }
6661
6662 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6663
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306664 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306665}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306666
6667static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306668 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306669 psa_key_derivation_step_t step,
6670 const uint8_t *data,
6671 size_t data_length)
6672{
6673 switch (step) {
6674 case PSA_KEY_DERIVATION_INPUT_SALT:
6675 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6676 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306677 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306678 default:
6679 return PSA_ERROR_INVALID_ARGUMENT;
6680 }
6681}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306682#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306683
Gilles Peskineb8965192019-09-24 16:21:10 +02006684/** Check whether the given key type is acceptable for the given
6685 * input step of a key derivation.
6686 *
6687 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6688 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6689 * Both secret and non-secret inputs can alternatively have the type
6690 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6691 * that the input was passed as a buffer rather than via a key object.
6692 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006693static int psa_key_derivation_check_input_type(
6694 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006696{
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006698 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 if (key_type == PSA_KEY_TYPE_DERIVE) {
6700 return PSA_SUCCESS;
6701 }
6702 if (key_type == PSA_KEY_TYPE_NONE) {
6703 return PSA_SUCCESS;
6704 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006705 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006706 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 if (key_type == PSA_KEY_TYPE_DERIVE) {
6708 return PSA_SUCCESS;
6709 }
6710 if (key_type == PSA_KEY_TYPE_NONE) {
6711 return PSA_SUCCESS;
6712 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006713 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006714 case PSA_KEY_DERIVATION_INPUT_LABEL:
6715 case PSA_KEY_DERIVATION_INPUT_SALT:
6716 case PSA_KEY_DERIVATION_INPUT_INFO:
6717 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6719 return PSA_SUCCESS;
6720 }
6721 if (key_type == PSA_KEY_TYPE_NONE) {
6722 return PSA_SUCCESS;
6723 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006724 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306725 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6726 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6727 return PSA_SUCCESS;
6728 }
6729 if (key_type == PSA_KEY_TYPE_DERIVE) {
6730 return PSA_SUCCESS;
6731 }
6732 if (key_type == PSA_KEY_TYPE_NONE) {
6733 return PSA_SUCCESS;
6734 }
6735 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006736 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006737 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006738}
6739
Janos Follathb80a94e2019-06-12 15:54:46 +01006740static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006741 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006742 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006743 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006744 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006746{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006747 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006749
Gilles Peskine449bd832023-01-11 14:50:10 +01006750 status = psa_key_derivation_check_input_type(step, key_type);
6751 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006752 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006753 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006754
Przemek Stekiel69c46792022-06-10 12:59:51 +02006755#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006756 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6757 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6758 step, data, data_length);
6759 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006760#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006761#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006762 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6763 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6764 step, data, data_length);
6765 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006766#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6767#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6769 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6770 step, data, data_length);
6771 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006772#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006773#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006775 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006776 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6777 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006778#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306779#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306780 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306781 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6782 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306783 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306784#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006785 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006786 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006787 (void) data;
6788 (void) data_length;
6789 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006791 }
6792
Gilles Peskine224b0d62019-09-23 18:13:17 +02006793exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 if (status != PSA_SUCCESS) {
6795 psa_key_derivation_abort(operation);
6796 }
6797 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006798}
6799
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306800static psa_status_t psa_key_derivation_input_integer_internal(
6801 psa_key_derivation_operation_t *operation,
6802 psa_key_derivation_step_t step,
6803 uint64_t value)
6804{
6805 psa_status_t status;
6806 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6807
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306808#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306809 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306810 status = psa_pbkdf2_set_input_cost(
6811 &operation->ctx.pbkdf2, step, value);
6812 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306813#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306814 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306815 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306816 (void) value;
6817 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306818 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306819 }
6820
6821 if (status != PSA_SUCCESS) {
6822 psa_key_derivation_abort(operation);
6823 }
6824 return status;
6825}
6826
Janos Follath51f4a0f2019-06-14 11:35:55 +01006827psa_status_t psa_key_derivation_input_bytes(
6828 psa_key_derivation_operation_t *operation,
6829 psa_key_derivation_step_t step,
6830 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006832{
Gilles Peskine449bd832023-01-11 14:50:10 +01006833 return psa_key_derivation_input_internal(operation, step,
6834 PSA_KEY_TYPE_NONE,
6835 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006836}
6837
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306838psa_status_t psa_key_derivation_input_integer(
6839 psa_key_derivation_operation_t *operation,
6840 psa_key_derivation_step_t step,
6841 uint64_t value)
6842{
6843 return psa_key_derivation_input_integer_internal(operation, step, value);
6844}
6845
Janos Follath51f4a0f2019-06-14 11:35:55 +01006846psa_status_t psa_key_derivation_input_key(
6847 psa_key_derivation_operation_t *operation,
6848 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006849 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006850{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006851 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006852 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006853 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006854
Ronald Cron5c522922020-11-14 16:35:34 +01006855 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6857 if (status != PSA_SUCCESS) {
6858 psa_key_derivation_abort(operation);
6859 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006860 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006861
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306862 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6863 * permission to output to a key object. */
6864 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6865 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006866 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006868
Gilles Peskine449bd832023-01-11 14:50:10 +01006869 status = psa_key_derivation_input_internal(operation,
6870 step, slot->attr.type,
6871 slot->key.data,
6872 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006873
Ryan Everett5ac6fa72024-02-14 17:11:36 +00006874 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006875
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006877}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006878
6879
6880
6881/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006882/* Key agreement */
6883/****************************************************************/
6884
Gilles Peskine449bd832023-01-11 14:50:10 +01006885psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6886 const uint8_t *key_buffer,
6887 size_t key_buffer_size,
6888 psa_algorithm_t alg,
6889 const uint8_t *peer_key,
6890 size_t peer_key_length,
6891 uint8_t *shared_secret,
6892 size_t shared_secret_size,
6893 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006894{
Gilles Peskine449bd832023-01-11 14:50:10 +01006895 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006896#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006897 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6899 key_buffer_size, alg,
6900 peer_key, peer_key_length,
6901 shared_secret,
6902 shared_secret_size,
6903 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006904#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006905
6906#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6907 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006908 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006909 peer_key,
6910 peer_key_length,
6911 key_buffer,
6912 key_buffer_size,
6913 shared_secret,
6914 shared_secret_size,
6915 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006916#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6917
Gilles Peskine01d718c2018-09-18 12:01:02 +02006918 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006919 (void) attributes;
6920 (void) key_buffer;
6921 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006922 (void) peer_key;
6923 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006924 (void) shared_secret;
6925 (void) shared_secret_size;
6926 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006928 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006929}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006930
Aditya Deshpande17845b82022-10-13 17:21:01 +01006931/** Internal function for raw key agreement
6932 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006933 * to the driver's implementation if a driver is present.
6934 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006935 * (psa_key_agreement_raw_builtin).
6936 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006937static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6938 psa_key_slot_t *private_key,
6939 const uint8_t *peer_key,
6940 size_t peer_key_length,
6941 uint8_t *shared_secret,
6942 size_t shared_secret_size,
6943 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006944{
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6946 return PSA_ERROR_NOT_SUPPORTED;
6947 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006948
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006949 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 private_key->key.data,
6951 private_key->key.bytes, alg,
6952 peer_key, peer_key_length,
6953 shared_secret,
6954 shared_secret_size,
6955 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006956}
6957
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006958/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006959 * to potentially free embedded data structures and wipe confidential data.
6960 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006961static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6962 psa_key_derivation_step_t step,
6963 psa_key_slot_t *private_key,
6964 const uint8_t *peer_key,
6965 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006966{
6967 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006968 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006969 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006971
6972 /* Step 1: run the secret agreement algorithm to generate the shared
6973 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006974 status = psa_key_agreement_raw_internal(ka_alg,
6975 private_key,
6976 peer_key, peer_key_length,
6977 shared_secret,
6978 sizeof(shared_secret),
6979 &shared_secret_length);
6980 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006981 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006983
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006984 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006985 * the shared secret. A shared secret is permitted wherever a key
6986 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 status = psa_key_derivation_input_internal(operation, step,
6988 PSA_KEY_TYPE_DERIVE,
6989 shared_secret,
6990 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006991exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006992 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6993 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006994}
6995
Gilles Peskine449bd832023-01-11 14:50:10 +01006996psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6997 psa_key_derivation_step_t step,
6998 mbedtls_svc_key_id_t private_key,
6999 const uint8_t *peer_key,
7000 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007001{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007002 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007003 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007004 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007005
Gilles Peskine449bd832023-01-11 14:50:10 +01007006 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7007 return PSA_ERROR_INVALID_ARGUMENT;
7008 }
Ronald Cron5c522922020-11-14 16:35:34 +01007009 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007010 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7011 if (status != PSA_SUCCESS) {
7012 return status;
7013 }
7014 status = psa_key_agreement_internal(operation, step,
7015 slot,
7016 peer_key, peer_key_length);
7017 if (status != PSA_SUCCESS) {
7018 psa_key_derivation_abort(operation);
7019 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007020 /* If a private key has been added as SECRET, we allow the derived
7021 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007022 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007023 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007025 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007026
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007027 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007028
Gilles Peskine449bd832023-01-11 14:50:10 +01007029 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007030}
7031
Gilles Peskine449bd832023-01-11 14:50:10 +01007032psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7033 mbedtls_svc_key_id_t private_key,
7034 const uint8_t *peer_key,
7035 size_t peer_key_length,
7036 uint8_t *output,
7037 size_t output_size,
7038 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007039{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007040 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007041 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007042 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007043 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007044
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007046 status = PSA_ERROR_INVALID_ARGUMENT;
7047 goto exit;
7048 }
Ronald Cron5c522922020-11-14 16:35:34 +01007049 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7051 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007052 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007054
Gilles Peskine3e561302022-04-14 00:17:15 +02007055 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7056 * for the output size. The PSA specification only guarantees that this
7057 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7058 * but it might be nice to allow smaller buffers if the output fits.
7059 * At the time of writing this comment, with only ECDH implemented,
7060 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7061 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7062 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007063 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7065 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007066 status = PSA_ERROR_BUFFER_TOO_SMALL;
7067 goto exit;
7068 }
7069
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 status = psa_key_agreement_raw_internal(alg, slot,
7071 peer_key, peer_key_length,
7072 output, output_size,
7073 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007074
7075exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007076 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007077 /* If an error happens and is not handled properly, the output
7078 * may be used as a key to protect sensitive data. Arrange for such
7079 * a key to be random, which is likely to result in decryption or
7080 * verification errors. This is better than filling the buffer with
7081 * some constant data such as zeros, which would result in the data
7082 * being protected with a reproducible, easily knowable key.
7083 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007085 *output_length = output_size;
7086 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007087
Ryan Everetta103ec92024-01-31 13:59:57 +00007088 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007089
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007091}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007092
7093
Gilles Peskine30524eb2020-11-13 17:02:26 +01007094
Gilles Peskine86a440b2018-11-12 18:39:40 +01007095/****************************************************************/
7096/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007097/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007098
Gilles Peskine672a7712023-04-28 21:00:28 +02007099#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7100#include "entropy_poll.h"
7101#endif
7102
Gilles Peskine30524eb2020-11-13 17:02:26 +01007103/** Initialize the PSA random generator.
7104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007105static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007106{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007107#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007109#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7110
Gilles Peskine30524eb2020-11-13 17:02:26 +01007111 /* Set default configuration if
7112 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007113 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007114 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007115 }
7116 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007117 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007118 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007119
Gilles Peskine449bd832023-01-11 14:50:10 +01007120 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007121#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7122 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7123 /* The PSA entropy injection feature depends on using NV seed as an entropy
7124 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 mbedtls_entropy_add_source(&rng->entropy,
7126 mbedtls_nv_seed_poll, NULL,
7127 MBEDTLS_ENTROPY_BLOCK_SIZE,
7128 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007129#endif
7130
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007132#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007133}
7134
7135/** Deinitialize the PSA random generator.
7136 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007137static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007138{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007139#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007140 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007141#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti83e0de82023-11-24 12:13:05 +01007142 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7143 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007144#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007145}
7146
7147/** Seed the PSA random generator.
7148 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007149static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007150{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007151#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7152 /* Do nothing: the external RNG seeds itself. */
7153 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007155#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007156 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7158 drbg_seed, sizeof(drbg_seed) - 1);
7159 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007160#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007161}
7162
Gilles Peskine449bd832023-01-11 14:50:10 +01007163psa_status_t psa_generate_random(uint8_t *output,
7164 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007165{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007166 GUARD_MODULE_INITIALIZED;
7167
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007168#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7169
7170 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7172 output, output_size,
7173 &output_length);
7174 if (status != PSA_SUCCESS) {
7175 return status;
7176 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007177 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007178 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 if (output_length != output_size) {
7180 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7181 }
7182 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007183
7184#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7185
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007187 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7189 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7190 output_size);
7191 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7192 output, request_size);
7193 if (ret != 0) {
7194 return mbedtls_to_psa_error(ret);
7195 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007196 output_size -= request_size;
7197 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007198 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007199 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007200#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007201}
7202
Gilles Peskine8814fc42020-12-14 15:33:44 +01007203/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007204 *
7205 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7206 * `psa_generate_random(...)`. The state parameter is ignored since the
7207 * PSA API doesn't support passing an explicit state.
7208 *
7209 * In the non-external case, psa_generate_random() calls an
7210 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7211 * and semantics as mbedtls_psa_get_random(). As an optimization,
7212 * instead of doing this back-and-forth between the PSA API and the
7213 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7214 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007215 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007216#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7217int mbedtls_psa_get_random(void *p_rng,
7218 unsigned char *output,
7219 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007220{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007221 /* This function takes a pointer to the RNG state because that's what
7222 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7223 * its own state internally and doesn't let the caller access that state.
7224 * So we just ignore the state parameter, and in practice we'll pass
7225 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007226 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007227 psa_status_t status = psa_generate_random(output, output_size);
7228 if (status == PSA_SUCCESS) {
7229 return 0;
7230 } else {
7231 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7232 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007233}
7234#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7235
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007236#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007237psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7238 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007239{
Gilles Peskine449bd832023-01-11 14:50:10 +01007240 if (global_data.initialized) {
7241 return PSA_ERROR_NOT_PERMITTED;
7242 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007243
Gilles Peskine449bd832023-01-11 14:50:10 +01007244 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7245 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7246 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7247 return PSA_ERROR_INVALID_ARGUMENT;
7248 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007249
Gilles Peskine449bd832023-01-11 14:50:10 +01007250 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007251}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007252#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007253
Ronald Cron3772afe2021-02-08 16:10:05 +01007254/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007255 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007256 * \param type The key type
7257 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007258 *
7259 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007260 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007261 * \retval #PSA_ERROR_INVALID_ARGUMENT
7262 * The size in bits of the key is not valid.
7263 * \retval #PSA_ERROR_NOT_SUPPORTED
7264 * The type and/or the size in bits of the key or the combination of
7265 * the two is not supported.
7266 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007267static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007269{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007270 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007271
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 if (key_type_is_raw_bytes(type)) {
7273 status = psa_validate_unstructured_key_bit_size(type, bits);
7274 if (status != PSA_SUCCESS) {
7275 return status;
7276 }
7277 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007278#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007279 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7280 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7281 return PSA_ERROR_NOT_SUPPORTED;
7282 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007283 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007284 return PSA_ERROR_NOT_SUPPORTED;
7285 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007286
Ronald Cron01b2aba2020-10-05 09:42:02 +02007287 /* Accept only byte-aligned keys, for the same reasons as
7288 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 if (bits % 8 != 0) {
7290 return PSA_ERROR_NOT_SUPPORTED;
7291 }
7292 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007293#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007294
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007295#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007296 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007297 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007298 return PSA_SUCCESS;
7299 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007300#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007301
Valerio Settia55f0422023-07-10 15:34:41 +02007302#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007303 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007304 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007305 return PSA_ERROR_NOT_SUPPORTED;
7306 }
7307 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007308#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007309 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007311 }
7312
Gilles Peskine449bd832023-01-11 14:50:10 +01007313 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007314}
7315
Ronald Cron55ed0592020-10-05 10:30:40 +02007316psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007317 const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007318 const psa_key_production_parameters_t *params, size_t params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007320{
7321 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007322 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007323
Gilles Peskine7a18f962024-02-12 16:48:11 +01007324 /* Only used for RSA */
Gilles Peskine092ce512024-02-20 12:31:24 +01007325 (void) params;
7326 (void) params_data_length;
Gilles Peskine7a18f962024-02-12 16:48:11 +01007327
Gilles Peskine449bd832023-01-11 14:50:10 +01007328 if (key_type_is_raw_bytes(type)) {
7329 status = psa_generate_random(key_buffer, key_buffer_size);
7330 if (status != PSA_SUCCESS) {
7331 return status;
7332 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007333
David Brown12ca5032021-02-11 11:02:00 -07007334#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 if (type == PSA_KEY_TYPE_DES) {
7336 psa_des_set_key_parity(key_buffer, key_buffer_size);
7337 }
David Brown8107e312021-02-12 08:08:46 -07007338#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007339 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007340
Valerio Setti76df8c12023-07-11 14:11:28 +02007341#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007342 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskinee22f6a92024-02-26 15:45:33 +01007343 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskine4c32b692024-02-16 00:11:09 +01007344 params, params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 key_buffer,
7346 key_buffer_size,
7347 key_buffer_length);
7348 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007349#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007350
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007351#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007352 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7353 return mbedtls_psa_ecp_generate_key(attributes,
7354 key_buffer,
7355 key_buffer_size,
7356 key_buffer_length);
7357 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007358#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007359
Valerio Settia55f0422023-07-10 15:34:41 +02007360#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007361 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7362 return mbedtls_psa_ffdh_generate_key(attributes,
7363 key_buffer,
7364 key_buffer_size,
7365 key_buffer_length);
7366 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007367#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007368 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007369 (void) key_buffer_length;
7370 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007371 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007372
Gilles Peskine449bd832023-01-11 14:50:10 +01007373 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007374}
Darryl Green0c6575a2018-11-07 16:05:30 +00007375
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007376psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007377 const psa_key_production_parameters_t *params,
7378 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007379 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007380{
7381 psa_status_t status;
7382 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007383 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007384 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007385
Ronald Cron81709fc2020-11-14 12:10:32 +01007386 *key = MBEDTLS_SVC_KEY_ID_INIT;
7387
Gilles Peskine0f84d622019-09-12 19:03:13 +02007388 /* Reject any attempt to create a zero-length key so that we don't
7389 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007390 if (psa_get_key_bits(attributes) == 0) {
7391 return PSA_ERROR_INVALID_ARGUMENT;
7392 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007393
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007394 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007395 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 return PSA_ERROR_INVALID_ARGUMENT;
7397 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007398
Gilles Peskine7a18f962024-02-12 16:48:11 +01007399#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007400 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +01007401 if (params->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01007402 return PSA_ERROR_INVALID_ARGUMENT;
7403 }
7404 } else
7405#endif
Gilles Peskine092ce512024-02-20 12:31:24 +01007406 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007407 return PSA_ERROR_INVALID_ARGUMENT;
7408 }
7409
Gilles Peskine449bd832023-01-11 14:50:10 +01007410 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7411 &slot, &driver);
7412 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007413 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007414 }
Gilles Peskine11792082019-08-06 18:36:36 +02007415
Ronald Cron977c2472020-10-13 08:32:21 +02007416 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307417 * storage ( thus not in the case of generating a key in a secure element
7418 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7419 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007421 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01007422 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007423 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007424 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007425 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007426 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007428
7429 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007430 attributes->type,
7431 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007432 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007433 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 attributes, &key_buffer_size);
7435 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007436 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 }
Ronald Cron977c2472020-10-13 08:32:21 +02007438 }
Ronald Cron977c2472020-10-13 08:32:21 +02007439
Gilles Peskine449bd832023-01-11 14:50:10 +01007440 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7441 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007442 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 }
Ronald Cron977c2472020-10-13 08:32:21 +02007444 }
7445
Gilles Peskine449bd832023-01-11 14:50:10 +01007446 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007447 params, params_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01007448 slot->key.data, slot->key.bytes,
7449 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 if (status != PSA_SUCCESS) {
7451 psa_remove_key_data_from_memory(slot);
7452 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007453
Gilles Peskine11792082019-08-06 18:36:36 +02007454exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 if (status == PSA_SUCCESS) {
7456 status = psa_finish_key_creation(slot, driver, key);
7457 }
7458 if (status != PSA_SUCCESS) {
7459 psa_fail_key_creation(slot, driver);
7460 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007461
Gilles Peskine449bd832023-01-11 14:50:10 +01007462 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007463}
7464
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007465psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7466 mbedtls_svc_key_id_t *key)
7467{
7468 return psa_generate_key_ext(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007469 &default_production_parameters, 0,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007470 key);
7471}
7472
Gilles Peskinee59236f2018-01-27 23:32:46 +01007473/****************************************************************/
7474/* Module setup */
7475/****************************************************************/
7476
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007477#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007478psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 void (* entropy_init)(mbedtls_entropy_context *ctx),
7480 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007481{
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7483 return PSA_ERROR_BAD_STATE;
7484 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007485 global_data.rng.entropy_init = entropy_init;
7486 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007487 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007488}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007489#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007490
Gilles Peskine449bd832023-01-11 14:50:10 +01007491void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007492{
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 psa_wipe_all_key_slots();
Valerio Setti83e0de82023-11-24 12:13:05 +01007494 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7495 mbedtls_psa_random_free(&global_data.rng);
7496 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007497 /* Wipe all remaining data, including configuration.
7498 * In particular, this sets all state indicator to the value
7499 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007501
7502 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007504}
7505
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007506#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7507/** Recover a transaction that was interrupted by a power failure.
7508 *
7509 * This function is called during initialization, before psa_crypto_init()
7510 * returns. If this function returns a failure status, the initialization
7511 * fails.
7512 */
7513static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007514 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007515{
Gilles Peskine449bd832023-01-11 14:50:10 +01007516 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007517 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7518 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007519 /* TODO - fall through to the failure case until this
7520 * is implemented.
7521 * https://github.com/ARMmbed/mbed-crypto/issues/218
7522 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007523 default:
7524 /* We found an unsupported transaction in the storage.
7525 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007527 }
7528}
7529#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7530
Gilles Peskine449bd832023-01-11 14:50:10 +01007531psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007532{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007533 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007534
Gilles Peskinec6b69072018-11-20 21:42:52 +01007535 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 if (global_data.initialized != 0) {
7537 return PSA_SUCCESS;
7538 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007539
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007540 /* Init drivers */
7541 status = psa_driver_wrapper_init();
7542 if (status != PSA_SUCCESS) {
7543 goto exit;
7544 }
7545 global_data.drivers_initialized = 1;
7546
Valerio Setti402cfba2023-11-13 10:24:32 +01007547 status = psa_initialize_key_slots();
7548 if (status != PSA_SUCCESS) {
7549 goto exit;
7550 }
7551
Gilles Peskine30524eb2020-11-13 17:02:26 +01007552 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007554 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007555 status = mbedtls_psa_random_seed(&global_data.rng);
7556 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007557 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007559 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007560
Gilles Peskine4b734222019-07-24 15:56:31 +02007561#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 status = psa_crypto_load_transaction();
7563 if (status == PSA_SUCCESS) {
7564 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7565 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007566 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 }
7568 status = psa_crypto_stop_transaction();
7569 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007570 /* There's no transaction to complete. It's all good. */
7571 status = PSA_SUCCESS;
7572 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007573#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007574
Gilles Peskinec6b69072018-11-20 21:42:52 +01007575 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007576 global_data.initialized = 1;
7577
7578exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 if (status != PSA_SUCCESS) {
7580 mbedtls_psa_crypto_free();
7581 }
7582 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007583}
7584
Yanray Wangffc3c482023-07-11 12:01:04 +08007585#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007586psa_status_t psa_crypto_driver_pake_get_password_len(
7587 const psa_crypto_driver_pake_inputs_t *inputs,
7588 size_t *password_len)
7589{
7590 if (inputs->password_len == 0) {
7591 return PSA_ERROR_BAD_STATE;
7592 }
7593
7594 *password_len = inputs->password_len;
7595
7596 return PSA_SUCCESS;
7597}
7598
7599psa_status_t psa_crypto_driver_pake_get_password(
7600 const psa_crypto_driver_pake_inputs_t *inputs,
7601 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7602{
7603 if (inputs->password_len == 0) {
7604 return PSA_ERROR_BAD_STATE;
7605 }
7606
7607 if (buffer_size < inputs->password_len) {
7608 return PSA_ERROR_BUFFER_TOO_SMALL;
7609 }
7610
7611 memcpy(buffer, inputs->password, inputs->password_len);
7612 *buffer_length = inputs->password_len;
7613
7614 return PSA_SUCCESS;
7615}
7616
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007617psa_status_t psa_crypto_driver_pake_get_user_len(
7618 const psa_crypto_driver_pake_inputs_t *inputs,
7619 size_t *user_len)
7620{
7621 if (inputs->user_len == 0) {
7622 return PSA_ERROR_BAD_STATE;
7623 }
7624
7625 *user_len = inputs->user_len;
7626
7627 return PSA_SUCCESS;
7628}
7629
7630psa_status_t psa_crypto_driver_pake_get_user(
7631 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007632 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007633{
7634 if (inputs->user_len == 0) {
7635 return PSA_ERROR_BAD_STATE;
7636 }
7637
Przemek Stekielc0e62502023-03-14 11:49:36 +01007638 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007639 return PSA_ERROR_BUFFER_TOO_SMALL;
7640 }
7641
Przemek Stekielc0e62502023-03-14 11:49:36 +01007642 memcpy(user_id, inputs->user, inputs->user_len);
7643 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007644
7645 return PSA_SUCCESS;
7646}
7647
7648psa_status_t psa_crypto_driver_pake_get_peer_len(
7649 const psa_crypto_driver_pake_inputs_t *inputs,
7650 size_t *peer_len)
7651{
7652 if (inputs->peer_len == 0) {
7653 return PSA_ERROR_BAD_STATE;
7654 }
7655
7656 *peer_len = inputs->peer_len;
7657
7658 return PSA_SUCCESS;
7659}
7660
7661psa_status_t psa_crypto_driver_pake_get_peer(
7662 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007663 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007664{
7665 if (inputs->peer_len == 0) {
7666 return PSA_ERROR_BAD_STATE;
7667 }
7668
Przemek Stekielc0e62502023-03-14 11:49:36 +01007669 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007670 return PSA_ERROR_BUFFER_TOO_SMALL;
7671 }
7672
Przemek Stekielc0e62502023-03-14 11:49:36 +01007673 memcpy(peer_id, inputs->peer, inputs->peer_len);
7674 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007675
7676 return PSA_SUCCESS;
7677}
7678
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007679psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7680 const psa_crypto_driver_pake_inputs_t *inputs,
7681 psa_pake_cipher_suite_t *cipher_suite)
7682{
7683 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7684 return PSA_ERROR_BAD_STATE;
7685 }
7686
7687 *cipher_suite = inputs->cipher_suite;
7688
7689 return PSA_SUCCESS;
7690}
7691
Neil Armstronga7d08c32022-06-01 18:21:20 +02007692psa_status_t psa_pake_setup(
7693 psa_pake_operation_t *operation,
7694 const psa_pake_cipher_suite_t *cipher_suite)
7695{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007697
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007698 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007699 status = PSA_ERROR_BAD_STATE;
7700 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007701 }
7702
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007703 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007704 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007705 status = PSA_ERROR_INVALID_ARGUMENT;
7706 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007707 }
7708
Przemek Stekiel51eac532022-12-07 11:04:51 +01007709 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7710
Przemek Stekiele12ed362022-12-21 12:54:46 +01007711 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007712 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7713 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007714 operation->data.inputs.cipher_suite = *cipher_suite;
7715
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007716#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007717 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007718 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007719 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007720
David Horstmann16f01512023-06-14 17:21:07 +01007721 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007722 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007723 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007724#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007725 {
7726 status = PSA_ERROR_NOT_SUPPORTED;
7727 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007728 }
7729
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007730 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7731
Przemek Stekiel51eac532022-12-07 11:04:51 +01007732 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007733exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007734 psa_pake_abort(operation);
7735 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007736}
7737
7738psa_status_t psa_pake_set_password_key(
7739 psa_pake_operation_t *operation,
7740 mbedtls_svc_key_id_t password)
7741{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007742 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007743 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7744 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007745 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007746
Przemek Stekiel51eac532022-12-07 11:04:51 +01007747 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007748 status = PSA_ERROR_BAD_STATE;
7749 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007750 }
7751
Przemek Stekiel6c764412022-11-22 14:05:12 +01007752 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7753 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007754 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007755 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007756 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007757 }
7758
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007759 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007760
Przemek Stekiel51eac532022-12-07 11:04:51 +01007761 if (type != PSA_KEY_TYPE_PASSWORD &&
7762 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7763 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007764 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007765 }
7766
Przemek Stekiel51eac532022-12-07 11:04:51 +01007767 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7768 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007769 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007770 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007771 }
7772
7773 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7774 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01007775 operation->data.inputs.attributes = slot->attr;
7776
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007777exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007778 if (status != PSA_SUCCESS) {
7779 psa_pake_abort(operation);
7780 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00007781 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007782 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007783}
7784
7785psa_status_t psa_pake_set_user(
7786 psa_pake_operation_t *operation,
7787 const uint8_t *user_id,
7788 size_t user_id_len)
7789{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007790 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007791
7792 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007793 status = PSA_ERROR_BAD_STATE;
7794 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007795 }
7796
Przemek Stekiel51eac532022-12-07 11:04:51 +01007797 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007798 status = PSA_ERROR_INVALID_ARGUMENT;
7799 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007800 }
7801
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007802 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007803 status = PSA_ERROR_BAD_STATE;
7804 goto exit;
7805 }
7806
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007807 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7808 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007809 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7810 goto exit;
7811 }
7812
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007813 memcpy(operation->data.inputs.user, user_id, user_id_len);
7814 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007815
7816 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007817exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007818 psa_pake_abort(operation);
7819 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007820}
7821
7822psa_status_t psa_pake_set_peer(
7823 psa_pake_operation_t *operation,
7824 const uint8_t *peer_id,
7825 size_t peer_id_len)
7826{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007827 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007828
7829 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;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007832 }
7833
Przemek Stekiel51eac532022-12-07 11:04:51 +01007834 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007835 status = PSA_ERROR_INVALID_ARGUMENT;
7836 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007837 }
7838
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007839 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007840 status = PSA_ERROR_BAD_STATE;
7841 goto exit;
7842 }
7843
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007844 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7845 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007846 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7847 goto exit;
7848 }
7849
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007850 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7851 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007852
7853 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007854exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007855 psa_pake_abort(operation);
7856 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007857}
7858
7859psa_status_t psa_pake_set_role(
7860 psa_pake_operation_t *operation,
7861 psa_pake_role_t role)
7862{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007863 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007864
Przemek Stekiel51eac532022-12-07 11:04:51 +01007865 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007866 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007867 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007868 }
7869
Przemek Stekiel09104b82023-03-06 14:11:51 +01007870 switch (operation->alg) {
7871#if defined(PSA_WANT_ALG_JPAKE)
7872 case PSA_ALG_JPAKE:
7873 if (role == PSA_PAKE_ROLE_NONE) {
7874 return PSA_SUCCESS;
7875 }
7876 status = PSA_ERROR_INVALID_ARGUMENT;
7877 break;
7878#endif
7879 default:
7880 (void) role;
7881 status = PSA_ERROR_NOT_SUPPORTED;
7882 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007883 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007884exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007885 psa_pake_abort(operation);
7886 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007887}
7888
David Horstmanne7f21e62023-05-12 18:17:21 +01007889/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007890#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007891static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007892 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007893{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007894 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007895 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007896 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007897
David Horstmann024e5c52023-06-14 15:48:21 +01007898 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007899 is_x1 = (stage->outputs < 1);
7900 } else {
7901 is_x1 = (stage->inputs < 1);
7902 }
7903
David Horstmann74a3d8c2023-06-14 18:28:19 +01007904 key_share_step = is_x1 ?
7905 PSA_JPAKE_X1_STEP_KEY_SHARE :
7906 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007907 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007908 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7909 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7910 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7911 } else {
7912 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007913 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01007914 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01007915}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007916#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007917
Przemek Stekiel2797d372022-12-22 11:19:22 +01007918static psa_status_t psa_pake_complete_inputs(
7919 psa_pake_operation_t *operation)
7920{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007921 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007922 /* Create copy of the inputs on stack as inputs share memory
7923 with the driver context which will be setup by the driver. */
7924 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007925
Przemek Stekielfde11282023-03-13 16:06:09 +01007926 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007927 return PSA_ERROR_BAD_STATE;
7928 }
7929
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007930 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007931 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7932 return PSA_ERROR_BAD_STATE;
7933 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007934 }
7935
Przemek Stekiel18620a32023-01-17 16:34:52 +01007936 /* Clear driver context */
7937 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7938
7939 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007940
7941 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01007942 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007943
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007944 /* User and peer are translated to role. */
7945 mbedtls_free(inputs.user);
7946 mbedtls_free(inputs.peer);
7947
Przemek Stekiel2797d372022-12-22 11:19:22 +01007948 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007949#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007950 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007951 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007952 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007953#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007954 {
7955 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007956 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007957 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007958 return status;
7959}
7960
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007961#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01007962static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01007963 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01007964 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01007965 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007966{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007967 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7968 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7969 step != PSA_PAKE_STEP_ZK_PROOF) {
7970 return PSA_ERROR_INVALID_ARGUMENT;
7971 }
7972
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007973 psa_jpake_computation_stage_t *computation_stage =
7974 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007975
David Horstmann5da95602023-06-08 15:37:12 +01007976 if (computation_stage->round != PSA_JPAKE_FIRST &&
7977 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007978 return PSA_ERROR_BAD_STATE;
7979 }
7980
David Horstmanne7f21e62023-05-12 18:17:21 +01007981 /* Check that the step we are given is the one we were expecting */
7982 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007983 return PSA_ERROR_BAD_STATE;
7984 }
7985
David Horstmanne7f21e62023-05-12 18:17:21 +01007986 if (step == PSA_PAKE_STEP_KEY_SHARE &&
7987 computation_stage->inputs == 0 &&
7988 computation_stage->outputs == 0) {
7989 /* Start of the round, so function decides whether we are inputting
7990 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01007991 computation_stage->io_mode = io_mode;
7992 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007993 /* Middle of the round so the mode we are in must match the function
7994 * called by the user */
7995 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007996 }
7997
7998 return PSA_SUCCESS;
7999}
8000
David Horstmanne7f21e62023-05-12 18:17:21 +01008001static psa_status_t psa_jpake_epilogue(
8002 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008003 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008004{
David Horstmanne7f21e62023-05-12 18:17:21 +01008005 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008006 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008007
David Horstmanne7f21e62023-05-12 18:17:21 +01008008 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8009 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008010 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008011 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008012 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008013 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008014 }
8015 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008016 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008017 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008018 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008019 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008020 }
8021 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008022 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8023 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008024 /* End of a round, move to the next round */
8025 stage->inputs = 0;
8026 stage->outputs = 0;
8027 stage->round++;
8028 }
8029 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008030 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008031 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008032 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008033 return PSA_SUCCESS;
8034}
David Horstmanne7f21e62023-05-12 18:17:21 +01008035
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008036#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008037
Neil Armstronga7d08c32022-06-01 18:21:20 +02008038psa_status_t psa_pake_output(
8039 psa_pake_operation_t *operation,
8040 psa_pake_step_t step,
8041 uint8_t *output,
8042 size_t output_size,
8043 size_t *output_length)
8044{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008045 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008046 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008047 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008048
8049 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008050 status = psa_pake_complete_inputs(operation);
8051 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008052 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008053 }
8054 }
8055
8056 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008057 status = PSA_ERROR_BAD_STATE;
8058 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008059 }
8060
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008061 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008062 status = PSA_ERROR_INVALID_ARGUMENT;
8063 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008064 }
8065
Przemek Stekiele12ed362022-12-21 12:54:46 +01008066 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008067#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008068 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008069 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008070 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008071 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008072 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008073 driver_step = convert_jpake_computation_stage_to_driver_step(
8074 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008075 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008076#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008077 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008078 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008079 status = PSA_ERROR_NOT_SUPPORTED;
8080 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008081 }
8082
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008083 status = psa_driver_wrapper_pake_output(operation, driver_step,
8084 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008085
8086 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008087 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008088 }
8089
8090 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008091#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008092 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008093 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008094 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008095 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008096 }
8097 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008098#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008099 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008100 status = PSA_ERROR_NOT_SUPPORTED;
8101 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008102 }
8103
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008104 return PSA_SUCCESS;
8105exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008106 psa_pake_abort(operation);
8107 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008108}
8109
8110psa_status_t psa_pake_input(
8111 psa_pake_operation_t *operation,
8112 psa_pake_step_t step,
8113 const uint8_t *input,
8114 size_t input_length)
8115{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008116 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008117 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008118 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8119 operation->primitive,
8120 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008121
8122 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008123 status = psa_pake_complete_inputs(operation);
8124 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008125 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008126 }
8127 }
8128
8129 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008130 status = PSA_ERROR_BAD_STATE;
8131 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008132 }
8133
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008134 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008135 status = PSA_ERROR_INVALID_ARGUMENT;
8136 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008137 }
8138
Przemek Stekiele12ed362022-12-21 12:54:46 +01008139 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008140#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008141 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008142 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008143 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008144 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008145 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008146 driver_step = convert_jpake_computation_stage_to_driver_step(
8147 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008148 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008149#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008150 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008151 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008152 status = PSA_ERROR_NOT_SUPPORTED;
8153 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008154 }
8155
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008156 status = psa_driver_wrapper_pake_input(operation, driver_step,
8157 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008158
8159 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008160 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008161 }
8162
8163 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008164#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008165 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008166 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008167 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008168 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008169 }
8170 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008171#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008172 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008173 status = PSA_ERROR_NOT_SUPPORTED;
8174 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008175 }
8176
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008177 return PSA_SUCCESS;
8178exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008179 psa_pake_abort(operation);
8180 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008181}
8182
8183psa_status_t psa_pake_get_implicit_key(
8184 psa_pake_operation_t *operation,
8185 psa_key_derivation_operation_t *output)
8186{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008187 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008188 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008189 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008190 size_t shared_key_len = 0;
8191
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008192 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008193 status = PSA_ERROR_BAD_STATE;
8194 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008195 }
8196
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008197#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008198 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008199 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008200 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008201 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008202 status = PSA_ERROR_BAD_STATE;
8203 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008204 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008205 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008206#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008207 {
8208 status = PSA_ERROR_NOT_SUPPORTED;
8209 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008210 }
8211
Przemek Stekiel0c781802022-11-29 14:53:13 +01008212 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8213 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008214 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008215 &shared_key_len);
8216
8217 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008218 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008219 }
8220
8221 status = psa_key_derivation_input_bytes(output,
8222 PSA_KEY_DERIVATION_INPUT_SECRET,
8223 shared_key,
8224 shared_key_len);
8225
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008226 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008227exit:
8228 abort_status = psa_pake_abort(operation);
8229 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008230}
8231
8232psa_status_t psa_pake_abort(
8233 psa_pake_operation_t *operation)
8234{
Przemek Stekield69dca92023-01-31 19:59:20 +01008235 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008236
Przemek Stekield69dca92023-01-31 19:59:20 +01008237 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008238 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008239 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008240
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008241 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8242 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008243 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008244 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008245 }
8246 if (operation->data.inputs.user != NULL) {
8247 mbedtls_free(operation->data.inputs.user);
8248 }
8249 if (operation->data.inputs.peer != NULL) {
8250 mbedtls_free(operation->data.inputs.peer);
8251 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008252 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008253 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008254
Przemek Stekield69dca92023-01-31 19:59:20 +01008255 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008256}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008257#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008258
Gilles Peskinee59236f2018-01-27 23:32:46 +01008259#endif /* MBEDTLS_PSA_CRYPTO_C */