blob: ec9d115b3b989c6f9317052d78b7f337b8e20992 [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
itayzafrir0adf0fc2018-09-06 16:24:41 +0300104#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if (global_data.initialized == 0) \
106 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300107
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100108int psa_can_do_hash(psa_algorithm_t hash_alg)
109{
110 (void) hash_alg;
111 return global_data.drivers_initialized;
112}
Valerio Settic6f004f2023-12-12 11:27:36 +0100113
Valerio Setti1fff4f22023-12-28 14:19:34 +0100114int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
Valerio Settic6f004f2023-12-12 11:27:36 +0100115{
Valerio Setti1fff4f22023-12-28 14:19:34 +0100116 (void) key_type;
Valerio Settic6f004f2023-12-12 11:27:36 +0100117 (void) cipher_alg;
118 return global_data.drivers_initialized;
119}
120
121
Valerio Settia55f0422023-07-10 15:34:41 +0200122#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200123 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200124 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200125static int psa_is_dh_key_size_valid(size_t bits)
126{
Valerio Setti504a1022024-01-17 15:19:30 +0100127 switch (bits) {
128#if defined(PSA_WANT_DH_RFC7919_2048)
129 case 2048:
130 return 1;
131#endif /* PSA_WANT_DH_RFC7919_2048 */
132#if defined(PSA_WANT_DH_RFC7919_3072)
133 case 3072:
134 return 1;
135#endif /* PSA_WANT_DH_RFC7919_3072 */
136#if defined(PSA_WANT_DH_RFC7919_4096)
137 case 4096:
138 return 1;
139#endif /* PSA_WANT_DH_RFC7919_4096 */
140#if defined(PSA_WANT_DH_RFC7919_6144)
141 case 6144:
142 return 1;
143#endif /* PSA_WANT_DH_RFC7919_6144 */
144#if defined(PSA_WANT_DH_RFC7919_8192)
145 case 8192:
146 return 1;
147#endif /* PSA_WANT_DH_RFC7919_8192 */
148 default:
149 return 0;
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200150 }
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200151}
Valerio Settia55f0422023-07-10 15:34:41 +0200152#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200153 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200154 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100157{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100158 /* Mbed TLS error codes can combine a high-level error code and a
159 * low-level error code. The low-level error usually reflects the
160 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 int low_level_ret = -(-ret & 0x007f);
162 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100163 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100165
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100166#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
168 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100170 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
171 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100172#endif
173
174#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200175 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
176 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
177 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
178 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
179 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200181 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200183 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100185#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200186
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100187#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000188 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100189 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100191#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100192
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100193#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100194 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100198#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100199
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100200#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200201 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100203#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200204
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100205#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200206 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200208 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100210#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200211
Dave Rodgman509b5672023-08-16 19:26:23 +0100212#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100217 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100219 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100221 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100223 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100227#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
230 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100231 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
232 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100233 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
236 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100240#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100241
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100242#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100243 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100245#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100246
Gilles Peskinee59236f2018-01-27 23:32:46 +0100247 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
248 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
249 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100251
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100252#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200255 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100257 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100259#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100260
Gilles Peskine82e57d12020-11-13 21:31:17 +0100261#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100263 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
264 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100265 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100267 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
268 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100270 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100272#endif
273
Dave Rodgmandea266f2023-08-31 11:52:43 +0100274#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100275 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100279 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100281#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100282 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100284#endif
285#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100286
Dave Rodgman509b5672023-08-16 19:26:23 +0100287#if defined(MBEDTLS_BIGNUM_C)
288#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100289 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100291#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100292 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100294 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100296 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100298 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100300 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100302 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100304 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100306#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100307
Dave Rodgman509b5672023-08-16 19:26:23 +0100308#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100311 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
312 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100314#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
315 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100316 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100318#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100319 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
320 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100322 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100324 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
325 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100327 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100329 case MBEDTLS_ERR_PK_INVALID_ALG:
330 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
331 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100333 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200335 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100337#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100338
Gilles Peskineff2d2002019-05-06 15:26:23 +0200339 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200341 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200343
Dave Rodgman509b5672023-08-16 19:26:23 +0100344#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100345 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100349 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100351 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100353 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
354 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100356 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100358 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100360 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100362#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200363
Dave Rodgman1dab4452023-09-01 09:59:51 +0100364#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300365 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300366 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300368 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300370 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300372 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
373 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300375 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100377 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100379
Dave Rodgman509b5672023-08-16 19:26:23 +0100380#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000381 case MBEDTLS_ERR_ECP_IN_PROGRESS:
382 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100383#endif
384#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000385
Janos Follatha13b9052019-11-22 12:48:59 +0000386 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300388
Gilles Peskinee59236f2018-01-27 23:32:46 +0100389 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100391 }
392}
393
Paul Elliottdc42ca82023-02-24 18:11:59 +0000394/**
395 * \brief For output buffers which contain "tags"
396 * (outputs that may be checked for validity like
397 * hashes, MACs and signatures), fill the unused
398 * part of the output buffer (the whole buffer on
399 * error, the trailing part on success) with
400 * something that isn't a valid tag (barring an
401 * attack on the tag and deliberately-crafted
402 * input), in case the caller doesn't check the
403 * return status properly.
404 *
405 * \param output_buffer Pointer to buffer to wipe. May not be NULL
406 * unless \p output_buffer_size is zero.
407 * \param status Status of function called to generate
408 * output_buffer originally
409 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
410 * could be NULL.
411 * \param output_buffer_length Length of data written to output_buffer, must be
412 * less than \p output_buffer_size
413 */
414static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000415 size_t output_buffer_size, size_t output_buffer_length)
416{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000417 size_t offset = 0;
418
419 if (output_buffer_size == 0) {
420 /* If output_buffer_size is 0 then we have nothing to do. We must not
421 call memset because output_buffer may be NULL in this case */
422 return;
423 }
424
425 if (status == PSA_SUCCESS) {
426 offset = output_buffer_length;
427 }
428
429 memset(output_buffer + offset, '!', output_buffer_size - offset);
430}
431
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
434 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200435{
436 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200438 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200439 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200440 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200441 case PSA_KEY_TYPE_PASSWORD:
442 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200443 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100444#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (bits != 128 && bits != 192 && bits != 256) {
447 return PSA_ERROR_INVALID_ARGUMENT;
448 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200449 break;
450#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200451#if defined(PSA_WANT_KEY_TYPE_ARIA)
452 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (bits != 128 && bits != 192 && bits != 256) {
454 return PSA_ERROR_INVALID_ARGUMENT;
455 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200456 break;
457#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100458#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200459 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (bits != 128 && bits != 192 && bits != 256) {
461 return PSA_ERROR_INVALID_ARGUMENT;
462 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200463 break;
464#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100465#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (bits != 64 && bits != 128 && bits != 192) {
468 return PSA_ERROR_INVALID_ARGUMENT;
469 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200470 break;
471#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100472#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200473 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 if (bits != 256) {
475 return PSA_ERROR_INVALID_ARGUMENT;
476 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200477 break;
478#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200479 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if (bits % 8 != 0) {
483 return PSA_ERROR_INVALID_ARGUMENT;
484 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487}
488
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100489/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100490 *
Steven Cooremancd640932021-03-08 12:00:27 +0100491 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
492 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100493 *
494 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100495 * \param[in] key_type The key type of the key to be used with the
496 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100497 *
498 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100499 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100500 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100501 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100502 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100503MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100504 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100506{
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 if (PSA_ALG_IS_HMAC(algorithm)) {
508 if (key_type == PSA_KEY_TYPE_HMAC) {
509 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100510 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100511 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
514 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
515 * key. */
516 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
517 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
518 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
519 * the block length (larger than 1) for block ciphers. */
520 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
521 return PSA_SUCCESS;
522 }
523 }
524 }
525
526 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100527}
528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
530 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200531{
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (slot->key.data != NULL) {
533 return PSA_ERROR_ALREADY_EXISTS;
534 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 slot->key.data = mbedtls_calloc(1, buffer_length);
537 if (slot->key.data == NULL) {
538 return PSA_ERROR_INSUFFICIENT_MEMORY;
539 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200540
Ronald Cronea0f8a62020-11-25 17:52:23 +0100541 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200543}
544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
546 const uint8_t *data,
547 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200548{
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 psa_status_t status = psa_allocate_buffer_to_slot(slot,
550 data_length);
551 if (status != PSA_SUCCESS) {
552 return status;
553 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 memcpy(slot->key.data, data, data_length);
556 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200557}
558
Ronald Crona0fe59f2020-11-29 11:14:53 +0100559psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100560 const psa_key_attributes_t *attributes,
561 const uint8_t *data, size_t data_length,
562 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100564{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100565 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100566 psa_key_type_t type = attributes->type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200568 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if (data_length == 0) {
570 return PSA_ERROR_NOT_SUPPORTED;
571 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 if (key_type_is_raw_bytes(type)) {
574 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200575
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100576 status = psa_validate_unstructured_key_bit_size(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 *bits);
578 if (status != PSA_SUCCESS) {
579 return status;
580 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200581
Ronald Crondd04d422020-11-28 15:14:42 +0100582 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100584 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 return PSA_SUCCESS;
588 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200589#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200590 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100591 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200592 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Valerio Setti504a1022024-01-17 15:19:30 +0100593 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100594 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200595 return mbedtls_psa_ffdh_import_key(attributes,
596 data, data_length,
597 key_buffer, key_buffer_size,
598 key_buffer_length,
599 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100600 }
Valerio Settia55f0422023-07-10 15:34:41 +0200601#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200602 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200603#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
605 if (PSA_KEY_TYPE_IS_ECC(type)) {
606 return mbedtls_psa_ecp_import_key(attributes,
607 data, data_length,
608 key_buffer, key_buffer_size,
609 key_buffer_length,
610 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100611 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200612#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800613 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200614#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
615 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
617 if (PSA_KEY_TYPE_IS_RSA(type)) {
618 return mbedtls_psa_rsa_import_key(attributes,
619 data, data_length,
620 key_buffer, key_buffer_size,
621 key_buffer_length,
622 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200623 }
Valerio Settife478902023-07-25 12:27:19 +0200624#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
625 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800626 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100627 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100630}
631
Gilles Peskinef603c712019-01-19 13:40:11 +0100632/** Calculate the intersection of two algorithm usage policies.
633 *
634 * Return 0 (which allows no operation) on incompatibility.
635 */
636static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100637 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100638 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100640{
Gilles Peskine549ea862019-05-22 11:45:59 +0200641 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if (alg1 == alg2) {
643 return alg1;
644 }
Steven Cooreman03488022021-02-18 12:07:20 +0100645 /* If the policies are from the same hash-and-sign family, check
646 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
648 PSA_ALG_IS_SIGN_HASH(alg2) &&
649 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
650 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
651 return alg2;
652 }
653 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
654 return alg1;
655 }
Steven Cooreman03488022021-02-18 12:07:20 +0100656 }
657 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100658 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100659 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
661 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
662 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
663 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
664 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100665 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100666
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100667 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
669 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
670 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
671 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100672 }
673 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
675 (alg1_len <= alg2_len)) {
676 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100677 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
679 (alg2_len <= alg1_len)) {
680 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100681 }
682 }
683 /* If the policies are from the same MAC family, check whether one
684 * of them is a minimum-MAC-length policy. Calculate the most
685 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
687 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
688 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100689 /* Validate the combination of key type and algorithm. Since the base
690 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
692 return 0;
693 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100694
Steven Cooreman31a876d2021-03-03 20:47:40 +0100695 /* Get the (exact or at-least) output lengths for both sides of the
696 * requested intersection. None of the currently supported algorithms
697 * have an output length dependent on the actual key size, so setting it
698 * to a bogus value of 0 is currently OK.
699 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100700 * Note that for at-least-this-length wildcard algorithms, the output
701 * length is set to the shortest allowed length, which allows us to
702 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
704 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100705 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100706
Steven Cooremana1d83222021-02-25 10:20:29 +0100707 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
709 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
710 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100711 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100712
713 /* If only one is an at-least-this-length policy, the intersection would
714 * be the other (fixed-length) policy as long as said fixed length is
715 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
717 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100718 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
720 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100721 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100722
Steven Cooremancd640932021-03-08 12:00:27 +0100723 /* If none of them are wildcards, check whether they define the same tag
724 * length. This is still possible here when one is default-length and
725 * the other specific-length. Ensure to always return the
726 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (alg1_len == alg2_len) {
728 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
729 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100730 }
731 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100733}
734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735static int psa_key_algorithm_permits(psa_key_type_t key_type,
736 psa_algorithm_t policy_alg,
737 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200738{
Gilles Peskine549ea862019-05-22 11:45:59 +0200739 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 if (requested_alg == policy_alg) {
741 return 1;
742 }
Steven Cooreman03488022021-02-18 12:07:20 +0100743 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
744 * and requested_alg is the same hash-and-sign family with any hash,
745 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
747 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
748 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
749 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100750 }
751 /* If policy_alg is a wildcard AEAD algorithm of the same base as
752 * the requested algorithm, check the requested tag length to be
753 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if (PSA_ALG_IS_AEAD(policy_alg) &&
755 PSA_ALG_IS_AEAD(requested_alg) &&
756 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
757 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
758 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
759 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
760 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100761 }
Steven Cooremancd640932021-03-08 12:00:27 +0100762 /* If policy_alg is a MAC algorithm of the same base as the requested
763 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 if (PSA_ALG_IS_MAC(policy_alg) &&
765 PSA_ALG_IS_MAC(requested_alg) &&
766 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
767 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100768 /* Validate the combination of key type and algorithm. Since the policy
769 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
771 return 0;
772 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100773
Steven Cooreman31a876d2021-03-03 20:47:40 +0100774 /* Get both the requested output length for the algorithm which is to be
775 * verified, and the default output length for the base algorithm.
776 * Note that none of the currently supported algorithms have an output
777 * length dependent on actual key size, so setting it to a bogus value
778 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100779 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100781 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 key_type, 0,
783 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100784
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100785 /* If the policy is default-length, only allow an algorithm with
786 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
788 return requested_output_length == default_output_length;
789 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100790
791 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100792 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
794 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
795 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100796 }
797
Steven Cooremancd640932021-03-08 12:00:27 +0100798 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
799 * check for the requested MAC length to be equal to or longer than the
800 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
802 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
803 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100804 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200805 }
Steven Cooremance48e852020-10-05 16:02:45 +0200806 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200807 * a key derivation with that key agreement should also be allowed. This
808 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
810 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
811 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
812 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200813 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100814 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200816}
817
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100818/** Test whether a policy permits an algorithm.
819 *
820 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100821 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100822 * \note This function requires providing the key type for which the policy is
823 * being validated, since some algorithm policy definitions (e.g. MAC)
824 * have different properties depending on what kind of cipher it is
825 * combined with.
826 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100827 * \retval PSA_SUCCESS When \p alg is a specific algorithm
828 * allowed by the \p policy.
829 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
830 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
831 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100832 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
834 psa_key_type_t key_type,
835 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100836{
Steven Cooremand788fab2021-02-25 11:29:17 +0100837 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (alg == 0) {
839 return PSA_ERROR_INVALID_ARGUMENT;
840 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100841
Steven Cooreman7e39f052021-02-23 14:18:32 +0100842 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if (PSA_ALG_IS_WILDCARD(alg)) {
844 return PSA_ERROR_INVALID_ARGUMENT;
845 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
848 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
849 return PSA_SUCCESS;
850 } else {
851 return PSA_ERROR_NOT_PERMITTED;
852 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100853}
854
Gilles Peskinef603c712019-01-19 13:40:11 +0100855/** Restrict a key policy based on a constraint.
856 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100857 * \note This function requires providing the key type for which the policy is
858 * being restricted, since some algorithm policy definitions (e.g. MAC)
859 * have different properties depending on what kind of cipher it is
860 * combined with.
861 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100862 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100863 * \param[in,out] policy The policy to restrict.
864 * \param[in] constraint The policy constraint to apply.
865 *
866 * \retval #PSA_SUCCESS
867 * \c *policy contains the intersection of the original value of
868 * \c *policy and \c *constraint.
869 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100870 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100871 * \c *policy is unchanged.
872 */
873static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100874 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100875 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100877{
878 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 psa_key_policy_algorithm_intersection(key_type, policy->alg,
880 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200881 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
883 constraint->alg2);
884 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
885 return PSA_ERROR_INVALID_ARGUMENT;
886 }
887 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
888 return PSA_ERROR_INVALID_ARGUMENT;
889 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100890 policy->usage &= constraint->usage;
891 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200892 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100894}
895
Przemek Stekiel061f6942022-11-30 10:51:35 +0100896/** Get the description of a key given its identifier and policy constraints
897 * and lock it.
898 *
899 * The key must have allow all the usage flags set in \p usage. If \p alg is
900 * nonzero, the key must allow operations with this algorithm. If \p alg is
901 * zero, the algorithm is not checked.
902 *
903 * In case of a persistent key, the function loads the description of the key
904 * into a key slot if not already done.
905 *
Ryan Everett098c6652024-01-03 13:03:36 +0000906 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +0000907 * It is the responsibility of the caller to then unregister
908 * once they have finished reading the contents of the slot.
909 * The caller unregisters by calling psa_unregister_read() or
910 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
911 * if and only if the caller already holds the global key slot mutex
912 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
913 * the unregister with mutex lock and unlock operations.
Przemek Stekiel061f6942022-11-30 10:51:35 +0100914 */
915static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100916 mbedtls_svc_key_id_t key,
917 psa_key_slot_t **p_slot,
918 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100920{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200921 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100922 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 status = psa_get_and_lock_key_slot(key, p_slot);
925 if (status != PSA_SUCCESS) {
926 return status;
927 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200928 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100929
930 /* Enforce that usage policy for the key slot contains all the flags
931 * required by the usage parameter. There is one exception: public
932 * keys can always be exported, so we treat public key objects as
933 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100935 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100939 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200940 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100941 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100942
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800943 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if (alg != 0) {
945 status = psa_key_policy_permits(&slot->attr.policy,
946 slot->attr.type,
947 alg);
948 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100949 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100951 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200954
955error:
956 *p_slot = NULL;
Ryan Everettfb792ca2024-01-31 13:40:05 +0000957 psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100960}
Darryl Green940d72c2018-07-13 13:18:51 +0100961
Ronald Cron5c522922020-11-14 16:35:34 +0100962/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200963 *
964 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200965 * available, as opposed to a key in a secure element and/or to be used
966 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200967 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200968 * This is a temporary function that may be used instead of
969 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
970 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200971 *
Ryan Everett098c6652024-01-03 13:03:36 +0000972 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +0000973 * It is the responsibility of the caller to then unregister
974 * once they have finished reading the contents of the slot.
975 * The caller unregisters by calling psa_unregister_read() or
976 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
977 * if and only if the caller already holds the global key slot mutex
978 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
979 * psa_unregister_read() with mutex lock and unlock operations.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200980 */
Ronald Cron5c522922020-11-14 16:35:34 +0100981static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
982 mbedtls_svc_key_id_t key,
983 psa_key_slot_t **p_slot,
984 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200986{
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
988 usage, alg);
989 if (status != PSA_SUCCESS) {
990 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200991 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everettfb792ca2024-01-31 13:40:05 +0000994 psa_unregister_read_under_mutex(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 *p_slot = NULL;
996 return PSA_ERROR_NOT_SUPPORTED;
997 }
998
999 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001000}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001003{
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001005 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001007
Ronald Cronea0f8a62020-11-25 17:52:23 +01001008 slot->key.data = NULL;
1009 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001012}
1013
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001014/** Completely wipe a slot in memory, including its policy.
1015 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001016psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001017{
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 /*
1021 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +00001022 * do our best to report an unexpected amount of registered readers or
1023 * an unexpected state.
1024 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1025 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1027 * function is called as part of the execution of a test suite, the
1028 * execution of the test suite is stopped in error if the assertion fails.
1029 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001030 switch (slot->state) {
1031 case PSA_SLOT_FULL:
1032 /* In this state psa_wipe_key_slot() must only be called if the
1033 * caller is the last reader. */
1034 case PSA_SLOT_PENDING_DELETION:
1035 /* In this state psa_wipe_key_slot() must only be called if the
1036 * caller is the last reader. */
1037 if (slot->registered_readers != 1) {
1038 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1);
1039 status = PSA_ERROR_CORRUPTION_DETECTED;
1040 }
1041 break;
1042 case PSA_SLOT_FILLING:
1043 /* In this state registered_readers must be 0. */
1044 if (slot->registered_readers != 0) {
1045 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0);
1046 status = PSA_ERROR_CORRUPTION_DETECTED;
1047 }
1048 break;
1049 case PSA_SLOT_EMPTY:
1050 /* The slot is already empty, it cannot be wiped. */
1051 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1052 status = PSA_ERROR_CORRUPTION_DETECTED;
1053 break;
1054 default:
1055 /* The slot's state is invalid. */
1056 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001057 }
1058
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001059 /* Multipart operations may still be using the key. This is safe
1060 * because all multipart operation objects are independent from
1061 * the key slot: if they need to access the key after the setup
1062 * phase, they have a copy of the key. Note that this means that
1063 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001064 /* At this point, key material and other type-specific content has
1065 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001066 * zeroize because the metadata is not particularly sensitive.
1067 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 memset(slot, 0, sizeof(*slot));
1069 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001070}
1071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001073{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001074 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001075 psa_status_t status; /* status of the last operation */
1076 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001077#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1078 psa_se_drv_table_entry_t *driver;
1079#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if (mbedtls_svc_key_id_is_null(key)) {
1082 return PSA_SUCCESS;
1083 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001084
Ronald Cronf2911112020-10-29 17:51:10 +01001085 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001086 * Get the description of the key in a key slot, and register to read it.
1087 * In the case of a persistent key, this will load the key description
1088 * from persistent memory if not done yet.
1089 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001090 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001091 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 status = psa_get_and_lock_key_slot(key, &slot);
1093 if (status != PSA_SUCCESS) {
1094 return status;
1095 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001096
Ryan Everettc053d962024-01-25 17:56:32 +00001097#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001098 /* We cannot unlock between setting the state to PENDING_DELETION
1099 * and destroying the key in storage, as otherwise another thread
1100 * could load the key into a new slot and the key will not be
1101 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001102 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1103 &mbedtls_threading_key_slot_mutex));
Ryan Everettd868b742024-03-08 18:35:09 +00001104
1105 if (slot->state == PSA_SLOT_PENDING_DELETION) {
1106 /* Another thread has destroyed the key between us locking the slot
1107 * and us gaining the mutex. Unregister from the slot,
1108 * and report that the key does not exist. */
1109 status = psa_unregister_read(slot);
1110
1111 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1112 &mbedtls_threading_key_slot_mutex));
1113 return (status == PSA_SUCCESS) ? PSA_ERROR_INVALID_HANDLE : status;
1114 }
Ryan Everettc053d962024-01-25 17:56:32 +00001115#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001116 /* Set the key slot containing the key description's state to
1117 * PENDING_DELETION. This stops new operations from registering
1118 * to read the slot. Current readers can safely continue to access
1119 * the key within the slot; the last registered reader will
1120 * automatically wipe the slot when they call psa_unregister_read().
1121 * If the key is persistent, we can now delete the copy of the key
1122 * from memory. If the key is opaque, we require the driver to
1123 * deal with the deletion. */
Ryan Everettd868b742024-03-08 18:35:09 +00001124 overall_status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1125 PSA_SLOT_PENDING_DELETION);
Ryan Everettc053d962024-01-25 17:56:32 +00001126
Ryan Everettd868b742024-03-08 18:35:09 +00001127 if (overall_status != PSA_SUCCESS) {
Ryan Everettc053d962024-01-25 17:56:32 +00001128 goto exit;
1129 }
Ronald Cronf2911112020-10-29 17:51:10 +01001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001132 /* Refuse the destruction of a read-only key (which may or may not work
1133 * if we attempt it, depending on whether the key is merely read-only
1134 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001135 * Just do the best we can, which is to wipe the copy in memory
1136 * (done in this function's cleanup code). */
1137 overall_status = PSA_ERROR_NOT_PERMITTED;
1138 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001139 }
1140
Gilles Peskine354f7672019-07-12 23:46:38 +02001141#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1143 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001144 /* For a key in a secure element, we need to do three things:
1145 * remove the key file in internal storage, destroy the
1146 * key inside the secure element, and update the driver's
1147 * persistent data. Start a transaction that will encompass these
1148 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001150 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001152 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 status = psa_crypto_save_transaction();
1154 if (status != PSA_SUCCESS) {
1155 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001156 /* We should still try to destroy the key in the secure
1157 * element and the key metadata in storage. This is especially
1158 * important if the error is that the storage is full.
1159 * But how to do it exactly without risking an inconsistent
1160 * state after a reset?
1161 * https://github.com/ARMmbed/mbed-crypto/issues/215
1162 */
1163 overall_status = status;
1164 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001165 }
1166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 status = psa_destroy_se_key(driver,
1168 psa_key_slot_get_slot_number(slot));
1169 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001170 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001172 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001173#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1174
Darryl Greend49a4992018-06-18 17:27:26 +01001175#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001177 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001178 * The slot will still hold a copy of the key until the last reader
1179 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 status = psa_destroy_persistent_key(slot->attr.id);
1181 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001182 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 }
Darryl Greend49a4992018-06-18 17:27:26 +01001184 }
1185#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001186
Gilles Peskinefc762652019-07-22 19:30:34 +02001187#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 if (driver != NULL) {
1189 status = psa_save_se_persistent_data(driver);
1190 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001191 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 }
1193 status = psa_crypto_stop_transaction();
1194 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001195 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001197 }
1198#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1199
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001200exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001201 /* Unregister from reading the slot. If we are the last active reader
1202 * then this will wipe the slot. */
1203 status = psa_unregister_read(slot);
1204 /* Prioritize CORRUPTION_DETECTED from unregistering over
1205 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001207 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 }
Ryan Everettc053d962024-01-25 17:56:32 +00001209
1210#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001211 /* Don't overwrite existing errors if the unlock fails. */
1212 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001213 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1214 &mbedtls_threading_key_slot_mutex));
1215#endif
1216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001218}
1219
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001220/** Retrieve all the publicly-accessible attributes of a key.
1221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1223 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001224{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001225 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001226 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1231 if (status != PSA_SUCCESS) {
1232 return status;
1233 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001234
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001235 *attributes = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001236
1237#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1239 psa_set_key_slot_number(attributes,
1240 psa_key_slot_get_slot_number(slot));
1241 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001242#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001243
Gilles Peskine97c0b2f2024-02-16 00:49:46 +01001244 return psa_unregister_read_under_mutex(slot);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001245}
1246
Gilles Peskinec8000c02019-08-02 20:15:51 +02001247#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1248psa_status_t psa_get_key_slot_number(
1249 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001251{
Gilles Peskine972539c2024-02-28 01:49:45 +01001252 if (attributes->has_slot_number) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001253 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 return PSA_SUCCESS;
1255 } else {
1256 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001257 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001258}
1259#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1262 size_t key_buffer_size,
1263 uint8_t *data,
1264 size_t data_size,
1265 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001266{
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 if (key_buffer_size > data_size) {
1268 return PSA_ERROR_BUFFER_TOO_SMALL;
1269 }
1270 memcpy(data, key_buffer, key_buffer_size);
1271 memset(data + key_buffer_size, 0,
1272 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001273 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001275}
1276
Ronald Cron7285cda2020-11-26 14:46:37 +01001277psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001278 const psa_key_attributes_t *attributes,
1279 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001281{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001282 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (key_type_is_raw_bytes(type) ||
1285 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001286 PSA_KEY_TYPE_IS_ECC(type) ||
1287 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 return psa_export_key_buffer_internal(
1289 key_buffer, key_buffer_size,
1290 data, data_size, data_length);
1291 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001292 /* This shouldn't happen in the reference implementation, but
1293 it is valid for a special-purpose implementation to omit
1294 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001296 }
1297}
1298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1300 uint8_t *data,
1301 size_t data_size,
1302 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001303{
1304 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1305 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1306 psa_key_slot_t *slot;
1307
Ronald Crone907e552021-01-18 13:22:38 +01001308 /* Reject a zero-length output buffer now, since this can never be a
1309 * valid key representation. This way we know that data must be a valid
1310 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if (data_size == 0) {
1312 return PSA_ERROR_BUFFER_TOO_SMALL;
1313 }
Ronald Crone907e552021-01-18 13:22:38 +01001314
Ronald Cron9486f9d2020-11-26 13:36:23 +01001315 /* Set the key to empty now, so that even when there are errors, we always
1316 * set data_length to a value between 0 and data_size. On error, setting
1317 * the key to empty is a good choice because an empty key representation is
1318 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001319 *data_length = 0;
1320
Ronald Cron9486f9d2020-11-26 13:36:23 +01001321 /* Export requires the EXPORT flag. There is an exception for public keys,
1322 * which don't require any flag, but
1323 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1326 PSA_KEY_USAGE_EXPORT, 0);
1327 if (status != PSA_SUCCESS) {
1328 return status;
1329 }
mohammad160306e79202018-03-28 13:17:44 +03001330
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001331 status = psa_driver_wrapper_export_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 slot->key.data, slot->key.bytes,
1333 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001334
Ryan Everetta103ec92024-01-31 13:59:57 +00001335 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001338}
1339
Ronald Cron7285cda2020-11-26 14:46:37 +01001340psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001341 const psa_key_attributes_t *attributes,
1342 const uint8_t *key_buffer,
1343 size_t key_buffer_size,
1344 uint8_t *data,
1345 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001347{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001348 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001349
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001350 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001351 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1352 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001353 /* Exporting public -> public */
1354 return psa_export_key_buffer_internal(
1355 key_buffer, key_buffer_size,
1356 data, data_size, data_length);
1357 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001358#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001359 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1360 return mbedtls_psa_rsa_export_public_key(attributes,
1361 key_buffer,
1362 key_buffer_size,
1363 data,
1364 data_size,
1365 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001366#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001367 /* We don't know how to convert a private RSA key to public. */
1368 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001369#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001370 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001371 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001372#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001373 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1374 return mbedtls_psa_ecp_export_public_key(attributes,
1375 key_buffer,
1376 key_buffer_size,
1377 data,
1378 data_size,
1379 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001380#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001381 /* We don't know how to convert a private ECC key to public */
1382 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001383#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001384 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001385 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001386#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001387 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001388 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001389 key_buffer,
1390 key_buffer_size,
1391 data, data_size,
1392 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001393#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001394 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001395#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001396 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001398 (void) key_buffer;
1399 (void) key_buffer_size;
1400 (void) data;
1401 (void) data_size;
1402 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001404 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001405}
Ronald Crond18b5f82020-11-25 16:46:05 +01001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1408 uint8_t *data,
1409 size_t data_size,
1410 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001411{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001412 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001413 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001414 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001415
Ronald Crone907e552021-01-18 13:22:38 +01001416 /* Reject a zero-length output buffer now, since this can never be a
1417 * valid key representation. This way we know that data must be a valid
1418 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if (data_size == 0) {
1420 return PSA_ERROR_BUFFER_TOO_SMALL;
1421 }
Ronald Crone907e552021-01-18 13:22:38 +01001422
Darryl Greendd8fb772018-11-07 16:00:44 +00001423 /* Set the key to empty now, so that even when there are errors, we always
1424 * set data_length to a value between 0 and data_size. On error, setting
1425 * the key to empty is a good choice because an empty key representation is
1426 * unlikely to be accepted anywhere. */
1427 *data_length = 0;
1428
1429 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1431 if (status != PSA_SUCCESS) {
1432 return status;
1433 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1436 status = PSA_ERROR_INVALID_ARGUMENT;
1437 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001438 }
1439
Ronald Cron67227982020-11-26 15:16:05 +01001440 status = psa_driver_wrapper_export_public_key(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001441 &slot->attr, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001443
1444exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001445 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001448}
1449
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001450/** Validate that a key policy is internally well-formed.
1451 *
1452 * This function only rejects invalid policies. It does not validate the
1453 * consistency of the policy with respect to other attributes of the key
1454 * such as the key type.
1455 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001456static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001457{
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1459 PSA_KEY_USAGE_COPY |
1460 PSA_KEY_USAGE_ENCRYPT |
1461 PSA_KEY_USAGE_DECRYPT |
1462 PSA_KEY_USAGE_SIGN_MESSAGE |
1463 PSA_KEY_USAGE_VERIFY_MESSAGE |
1464 PSA_KEY_USAGE_SIGN_HASH |
1465 PSA_KEY_USAGE_VERIFY_HASH |
1466 PSA_KEY_USAGE_VERIFY_DERIVATION |
1467 PSA_KEY_USAGE_DERIVE)) != 0) {
1468 return PSA_ERROR_INVALID_ARGUMENT;
1469 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001472}
1473
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001474/** Validate the internal consistency of key attributes.
1475 *
1476 * This function only rejects invalid attribute values. If does not
1477 * validate the consistency of the attributes with any key data that may
1478 * be involved in the creation of the key.
1479 *
1480 * Call this function early in the key creation process.
1481 *
1482 * \param[in] attributes Key attributes for the new key.
1483 * \param[out] p_drv On any return, the driver for the key, if any.
1484 * NULL for a transparent key.
1485 *
1486 */
1487static psa_status_t psa_validate_key_attributes(
1488 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001490{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001491 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1493 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 status = psa_validate_key_location(lifetime, p_drv);
1496 if (status != PSA_SUCCESS) {
1497 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001498 }
1499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 status = psa_validate_key_persistence(lifetime);
1501 if (status != PSA_SUCCESS) {
1502 return status;
1503 }
1504
1505 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1506 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1507 return PSA_ERROR_INVALID_ARGUMENT;
1508 }
1509 } else {
1510 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1511 return PSA_ERROR_INVALID_ARGUMENT;
1512 }
1513 }
1514
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001515 status = psa_validate_key_policy(&attributes->policy);
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 if (status != PSA_SUCCESS) {
1517 return status;
1518 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001519
1520 /* Refuse to create overly large keys.
1521 * Note that this doesn't trigger on import if the attributes don't
1522 * explicitly specify a size (so psa_get_key_bits returns 0), so
1523 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1525 return PSA_ERROR_NOT_SUPPORTED;
1526 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001529}
1530
Gilles Peskine4747d192019-04-17 15:05:45 +02001531/** Prepare a key slot to receive key material.
1532 *
1533 * This function allocates a key slot and sets its metadata.
1534 *
1535 * If this function fails, call psa_fail_key_creation().
1536 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001537 * This function is intended to be used as follows:
1538 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001539 * it with the specified attributes, and in case of a volatile key assign it
1540 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001541 * -# Populate the slot with the key material.
1542 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1543 * In case of failure at any step, stop the sequence and call
1544 * psa_fail_key_creation().
1545 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001546 * On success, the key slot's state is PSA_SLOT_FILLING.
1547 * It is the responsibility of the caller to change the slot's state to
1548 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001549 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001550 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001551 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001552 * \param[out] p_slot On success, a pointer to the prepared slot.
1553 * \param[out] p_drv On any return, the driver for the key, if any.
1554 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001555 *
1556 * \retval #PSA_SUCCESS
1557 * The key slot is ready to receive key material.
1558 * \return If this function fails, the key slot is an invalid state.
1559 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001560 */
1561static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001562 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001563 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001564 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001566{
1567 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001568 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001569 psa_key_slot_t *slot;
1570
Gilles Peskinedf179142019-07-15 22:02:14 +02001571 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001572 *p_drv = NULL;
1573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 status = psa_validate_key_attributes(attributes, p_drv);
1575 if (status != PSA_SUCCESS) {
1576 return status;
1577 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001578
Ryan Everett3d8118d2024-01-30 16:58:47 +00001579#if defined(MBEDTLS_THREADING_C)
1580 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1581 &mbedtls_threading_key_slot_mutex));
1582#endif
Ryan Everettb69118e2024-01-02 15:54:32 +00001583 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001584#if defined(MBEDTLS_THREADING_C)
1585 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1586 &mbedtls_threading_key_slot_mutex));
1587#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if (status != PSA_SUCCESS) {
1589 return status;
1590 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001591 slot = *p_slot;
1592
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001593 /* We're storing the declared bit-size of the key. It's up to each
1594 * creation mechanism to verify that this information is correct.
1595 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001596 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001597 * is optional (import, copy). In case of a volatile key, assign it the
1598 * volatile key identifier associated to the slot returned to contain its
1599 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001600
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001601 slot->attr = *attributes;
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001603#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1604 slot->attr.id = volatile_key_id;
1605#else
1606 slot->attr.id.key_id = volatile_key_id;
1607#endif
1608 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001609
Gilles Peskinecbaff462019-07-12 23:46:04 +02001610#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001611 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001612 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001613 * create the key file in internal storage, create the
1614 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001615 * persistent data. This is done by starting a transaction that will
1616 * encompass these three actions.
1617 * For registering a volatile key, we just need to find an appropriate
1618 * slot number inside the SE. Since the key is designated volatile, creating
1619 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001620 /* The first thing to do is to find a slot number for the new key.
1621 * We save the slot number in persistent storage as part of the
1622 * transaction data. It will be needed to recover if the power
1623 * fails during the key creation process, to clean up on the secure
1624 * element side after restarting. Obtaining a slot number from the
1625 * secure element driver updates its persistent state, but we do not yet
1626 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001627 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001629 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1631 &slot_number);
1632 if (status != PSA_SUCCESS) {
1633 return status;
1634 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001635
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001636 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001638 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001639 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001640 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 status = psa_crypto_save_transaction();
1642 if (status != PSA_SUCCESS) {
1643 (void) psa_crypto_stop_transaction();
1644 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001645 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001646 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001647
1648 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001650 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001653 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001655 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001656#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001659}
Gilles Peskine4747d192019-04-17 15:05:45 +02001660
1661/** Finalize the creation of a key once its key material has been set.
1662 *
1663 * This entails writing the key to persistent storage.
1664 *
1665 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001666 * See the documentation of psa_start_key_creation() for the intended use
1667 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001668 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001669 * If the finalization succeeds, the function sets the key slot's state to
1670 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1671 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001672 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001673 * \param[in,out] slot Pointer to the slot with key material.
1674 * \param[in] driver The secure element driver for the key,
1675 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001676 * \param[out] key On success, identifier of the key. Note that the
1677 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001678 *
1679 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001680 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001681 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1682 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1683 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1684 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1685 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1686 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001687 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001688 * \return If this function fails, the key slot is an invalid state.
1689 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001690 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001691static psa_status_t psa_finish_key_creation(
1692 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001693 psa_se_drv_table_entry_t *driver,
1694 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001695{
1696 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001697 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001698 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001699
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001700#if defined(MBEDTLS_THREADING_C)
1701 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1702 &mbedtls_threading_key_slot_mutex));
1703#endif
1704
Gilles Peskine4747d192019-04-17 15:05:45 +02001705#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001707#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001709 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001710 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001712
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001713 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1714 sizeof(data.slot_number),
1715 "Slot number size does not match psa_se_key_data_storage_t");
1716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1718 status = psa_save_persistent_key(&slot->attr,
1719 (uint8_t *) &data,
1720 sizeof(data));
1721 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001722#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1723 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001724 /* Key material is saved in export representation in the slot, so
1725 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 status = psa_save_persistent_key(&slot->attr,
1727 slot->key.data,
1728 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001729 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001730 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001731#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1732
Gilles Peskinecbaff462019-07-12 23:46:04 +02001733#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001734 /* Finish the transaction for a key creation. This does not
1735 * happen when registering an existing key. Detect this case
1736 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001737 * creation of a persistent key in a secure element requires a transaction,
1738 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (driver != NULL &&
1740 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1741 status = psa_save_se_persistent_data(driver);
1742 if (status != PSA_SUCCESS) {
1743 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001744
1745#if defined(MBEDTLS_THREADING_C)
1746 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1747 &mbedtls_threading_key_slot_mutex));
1748#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001750 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001752 }
1753#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1754
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001756 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001757 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1758 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001760 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001762 }
Ronald Cron50972942020-11-14 11:28:25 +01001763
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001764#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 Peskine4747d192019-04-17 15:05:45 +02001769}
1770
1771/** Abort the creation of a key.
1772 *
1773 * You may call this function after calling psa_start_key_creation(),
1774 * or after psa_finish_key_creation() fails. In other circumstances, this
1775 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001776 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001777 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001779 * \param[in,out] slot Pointer to the slot with key material.
1780 * \param[in] driver The secure element driver for the key,
1781 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001782 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001783static void psa_fail_key_creation(psa_key_slot_t *slot,
1784 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001785{
Gilles Peskine011e4282019-06-26 18:34:38 +02001786 (void) driver;
1787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001789 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001791
Ryan Everettb7101442024-01-23 20:09:49 +00001792#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00001793 /* If the lock operation fails we still wipe the slot.
1794 * Operations will no longer work after a failed lock,
1795 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00001796 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
1797#endif
1798
Gilles Peskine011e4282019-06-26 18:34:38 +02001799#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001800 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001801 * element, and the failure happened later (when saving metadata
1802 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001803 * element.
1804 * https://github.com/ARMmbed/mbed-crypto/issues/217
1805 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001806
Gilles Peskined7729582019-08-05 15:55:54 +02001807 /* Abort the ongoing transaction if any (there may not be one if
1808 * the creation process failed before starting one, or if the
1809 * key creation is a registration of a key in a secure element).
1810 * Earlier functions must already have done what it takes to undo any
1811 * partial creation. All that's left is to update the transaction data
1812 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001814#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1815
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00001817
1818#if defined(MBEDTLS_THREADING_C)
1819 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
1820#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02001821}
1822
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001823/** Validate optional attributes during key creation.
1824 *
1825 * Some key attributes are optional during key creation. If they are
1826 * specified in the attributes structure, check that they are consistent
1827 * with the data in the slot.
1828 *
1829 * This function should be called near the end of key creation, after
1830 * the slot in memory is fully populated but before saving persistent data.
1831 */
1832static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001833 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001835{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001836 if (attributes->type != 0) {
1837 if (attributes->type != slot->attr.type) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 return PSA_ERROR_INVALID_ARGUMENT;
1839 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001840 }
1841
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001842 if (attributes->bits != 0) {
1843 if (attributes->bits != slot->attr.bits) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 return PSA_ERROR_INVALID_ARGUMENT;
1845 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001846 }
1847
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001849}
1850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1852 const uint8_t *data,
1853 size_t data_length,
1854 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001855{
1856 psa_status_t status;
1857 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001858 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001859 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301860 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001861
Ronald Cron81709fc2020-11-14 12:10:32 +01001862 *key = MBEDTLS_SVC_KEY_ID_INIT;
1863
Gilles Peskine0f84d622019-09-12 19:03:13 +02001864 /* Reject zero-length symmetric keys (including raw data key objects).
1865 * This also rejects any key which might be encoded as an empty string,
1866 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 if (data_length == 0) {
1868 return PSA_ERROR_INVALID_ARGUMENT;
1869 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001870
Archana449608b2021-09-08 15:36:05 +05301871 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 if (data_length > SIZE_MAX / 8) {
1873 return PSA_ERROR_NOT_SUPPORTED;
1874 }
Archana6ed4bda2021-08-04 10:47:15 +05301875
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1877 &slot, &driver);
1878 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001879 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001881
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001882 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301883 * storage ( thus not in the case of importing a key in a secure element
1884 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1885 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001887 if (psa_key_lifetime_is_external(attributes->lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301888 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 attributes, data, data_length, &storage_size);
1890 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301891 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 }
Archanad8a83dc2021-06-14 10:04:16 +05301893 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 status = psa_allocate_buffer_to_slot(slot, storage_size);
1895 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001896 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001898 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001899
1900 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 status = psa_driver_wrapper_import_key(attributes,
1902 data, data_length,
1903 slot->key.data,
1904 slot->key.bytes,
1905 &slot->key.bytes, &bits);
1906 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001907 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001911 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001913 status = PSA_ERROR_INVALID_ARGUMENT;
1914 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001915 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001916
Archana6ed4bda2021-08-04 10:47:15 +05301917 /* Enforce a size limit, and in particular ensure that the bit
1918 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301920 status = PSA_ERROR_NOT_SUPPORTED;
1921 goto exit;
1922 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 status = psa_validate_optional_attributes(slot, attributes);
1924 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001925 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001929exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 if (status != PSA_SUCCESS) {
1931 psa_fail_key_creation(slot, driver);
1932 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001935}
1936
Gilles Peskined7729582019-08-05 15:55:54 +02001937#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1938psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02001940{
1941 psa_status_t status;
1942 psa_key_slot_t *slot = NULL;
1943 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001944 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001945
1946 /* Leaving attributes unspecified is not currently supported.
1947 * It could make sense to query the key type and size from the
1948 * secure element, but not all secure elements support this
1949 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
1951 return PSA_ERROR_NOT_SUPPORTED;
1952 }
1953 if (psa_get_key_bits(attributes) == 0) {
1954 return PSA_ERROR_NOT_SUPPORTED;
1955 }
Gilles Peskined7729582019-08-05 15:55:54 +02001956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
1958 &slot, &driver);
1959 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02001960 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 }
Gilles Peskined7729582019-08-05 15:55:54 +02001962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02001964
1965exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 if (status != PSA_SUCCESS) {
1967 psa_fail_key_creation(slot, driver);
1968 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001969
Gilles Peskined7729582019-08-05 15:55:54 +02001970 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 psa_close_key(key);
1972 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02001973}
1974#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1975
Gilles Peskine449bd832023-01-11 14:50:10 +01001976psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
1977 const psa_key_attributes_t *specified_attributes,
1978 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01001979{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001980 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001981 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01001982 psa_key_slot_t *source_slot = NULL;
1983 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001984 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001985 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05301986 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01001987
Ronald Cron81709fc2020-11-14 12:10:32 +01001988 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
1989
Archana8a180362021-07-05 02:18:48 +05301990 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
1992 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001993 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 status = psa_validate_optional_attributes(source_slot,
1997 specified_attributes);
1998 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001999 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002001
Archana9d17bf42021-09-10 06:22:44 +05302002 /* The target key type and number of bits have been validated by
2003 * psa_validate_optional_attributes() to be either equal to zero or
2004 * equal to the ones of the source key. So it is safe to inherit
2005 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302006 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002007 actual_attributes.bits = source_slot->attr.bits;
2008 actual_attributes.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302009
2010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 status = psa_restrict_key_policy(source_slot->attr.type,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002012 &actual_attributes.policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 &source_slot->attr.policy);
2014 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002015 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2019 &target_slot, &driver);
2020 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002021 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 }
2023 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2024 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302025 /*
Archana9d17bf42021-09-10 06:22:44 +05302026 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302027 * the source key would need to be exported as plaintext and re-imported
2028 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302029 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302030 * appropriate API invocations from the application, if needed.
2031 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002032 status = PSA_ERROR_NOT_SUPPORTED;
2033 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002034 }
Archana8a180362021-07-05 02:18:48 +05302035 /*
2036 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302037 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302038 * - For opaque keys this translates to an invocation of the drivers'
2039 * copy_key entry point through the dispatch layer.
2040 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002041 if (psa_key_lifetime_is_external(actual_attributes.lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2043 &storage_size);
2044 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302045 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 }
Archana374fe5b2021-09-08 15:50:28 +05302047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2049 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302050 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 }
Archana374fe5b2021-09-08 15:50:28 +05302052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 status = psa_driver_wrapper_copy_key(&actual_attributes,
2054 source_slot->key.data,
2055 source_slot->key.bytes,
2056 target_slot->key.data,
2057 target_slot->key.bytes,
2058 &target_slot->key.bytes);
2059 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302060 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 }
2062 } else {
2063 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302064 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 source_slot->key.bytes);
2066 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302067 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 }
Archana8a180362021-07-05 02:18:48 +05302069 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002071exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 if (status != PSA_SUCCESS) {
2073 psa_fail_key_creation(target_slot, driver);
2074 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002075
Ryan Everetta103ec92024-01-31 13:59:57 +00002076 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002079}
2080
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002081
2082
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002083/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002084/* Message digests */
2085/****************************************************************/
2086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002088{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002089 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 if (operation->id == 0) {
2091 return PSA_SUCCESS;
2092 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002095 operation->id = 0;
2096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002098}
2099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2101 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002102{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002103 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002104
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002105 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002107 status = PSA_ERROR_BAD_STATE;
2108 goto exit;
2109 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002112 status = PSA_ERROR_INVALID_ARGUMENT;
2113 goto exit;
2114 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002115
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002116 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2117 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002121
2122exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 if (status != PSA_SUCCESS) {
2124 psa_hash_abort(operation);
2125 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002126
2127 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002128}
2129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2131 const uint8_t *input,
2132 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002133{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002134 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002137 status = PSA_ERROR_BAD_STATE;
2138 goto exit;
2139 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002140
Steven Cooremanc8288352021-03-04 14:02:19 +01002141 /* Don't require hash implementations to behave correctly on a
2142 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 if (input_length == 0) {
2144 return PSA_SUCCESS;
2145 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002148
2149exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 if (status != PSA_SUCCESS) {
2151 psa_hash_abort(operation);
2152 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002155}
2156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2158 uint8_t *hash,
2159 size_t hash_size,
2160 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002161{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002162 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 if (operation->id == 0) {
2164 return PSA_ERROR_BAD_STATE;
2165 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002166
Steven Cooreman1e582352021-02-18 17:24:37 +01002167 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 operation, hash, hash_size, hash_length);
2169 psa_hash_abort(operation);
2170 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002171}
2172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2174 const uint8_t *hash,
2175 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002176{
Ronald Cron69a63422021-10-18 09:47:58 +02002177 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002178 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002179 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 operation,
2181 actual_hash, sizeof(actual_hash),
2182 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002185 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002189 status = PSA_ERROR_INVALID_SIGNATURE;
2190 goto exit;
2191 }
2192
Dave Rodgman78701152023-08-29 14:20:18 +01002193 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002194 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002196
2197exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2199 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002200 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002204}
2205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206psa_status_t psa_hash_compute(psa_algorithm_t alg,
2207 const uint8_t *input, size_t input_length,
2208 uint8_t *hash, size_t hash_size,
2209 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002210{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002211 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 if (!PSA_ALG_IS_HASH(alg)) {
2213 return PSA_ERROR_INVALID_ARGUMENT;
2214 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2217 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002218}
2219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220psa_status_t psa_hash_compare(psa_algorithm_t alg,
2221 const uint8_t *input, size_t input_length,
2222 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002223{
Ronald Cron69a63422021-10-18 09:47:58 +02002224 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002225 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if (!PSA_ALG_IS_HASH(alg)) {
2228 return PSA_ERROR_INVALID_ARGUMENT;
2229 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002230
Steven Cooreman1e582352021-02-18 17:24:37 +01002231 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 alg, input, input_length,
2233 actual_hash, sizeof(actual_hash),
2234 &actual_hash_length);
2235 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002236 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 }
2238 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002239 status = PSA_ERROR_INVALID_SIGNATURE;
2240 goto exit;
2241 }
Dave Rodgman78701152023-08-29 14:20:18 +01002242 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002243 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002245
2246exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2248 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002249}
2250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2252 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002253{
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 if (source_operation->id == 0 ||
2255 target_operation->id != 0) {
2256 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002257 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2260 target_operation);
2261 if (status != PSA_SUCCESS) {
2262 psa_hash_abort(target_operation);
2263 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002266}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002267
2268
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002270/* MAC */
2271/****************************************************************/
2272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002274{
Steven Cooremane6804192021-03-19 18:28:56 +01002275 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 if (operation->id == 0) {
2277 return PSA_SUCCESS;
2278 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002281 operation->mac_size = 0;
2282 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002283 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002286}
2287
Ronald Cron2dff3b22021-06-17 16:33:22 +02002288static psa_status_t psa_mac_finalize_alg_and_key_validation(
2289 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002290 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002292{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002293 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 psa_key_type_t key_type = psa_get_key_type(attributes);
2295 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 if (!PSA_ALG_IS_MAC(alg)) {
2298 return PSA_ERROR_INVALID_ARGUMENT;
2299 }
Steven Cooremane6804192021-03-19 18:28:56 +01002300
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002301 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 status = psa_mac_key_can_do(alg, key_type);
2303 if (status != PSA_SUCCESS) {
2304 return status;
2305 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002306
Steven Cooremand1a68f12021-05-10 11:13:41 +02002307 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002309
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002311 /* A very short MAC is too short for security since it can be
2312 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2313 * so we make this our minimum, even though 32 bits is still
2314 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002316 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2319 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002320 /* It's impossible to "truncate" to a larger length than the full length
2321 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002323 }
2324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002326 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2327 * that is disabled in the compile-time configuration. The result can
2328 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2329 * configuration into account. In this case, force a return of
2330 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2331 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2332 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2333 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2334 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002336 }
2337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002339}
2340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2342 mbedtls_svc_key_id_t key,
2343 psa_algorithm_t alg,
2344 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002345{
2346 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2347 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002348 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002349
2350 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002352 status = PSA_ERROR_BAD_STATE;
2353 goto exit;
2354 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002355
2356 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 key,
2358 &slot,
2359 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2360 alg);
2361 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002362 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002364
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002365 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 &operation->mac_size);
2367 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002368 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002370
2371 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002372 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 if (is_sign) {
2374 status = psa_driver_wrapper_mac_sign_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002375 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 slot->key.data,
2377 slot->key.bytes,
2378 alg);
2379 } else {
2380 status = psa_driver_wrapper_mac_verify_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002381 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 slot->key.data,
2383 slot->key.bytes,
2384 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002385 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002386
Gilles Peskinefbfac682018-07-08 20:51:54 +02002387exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 if (status != PSA_SUCCESS) {
2389 psa_mac_abort(operation);
2390 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002391
Ryan Everettfb9857f2024-02-14 12:16:41 +00002392 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002395}
2396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2398 mbedtls_svc_key_id_t key,
2399 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002400{
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002402}
2403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2405 mbedtls_svc_key_id_t key,
2406 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002407{
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002409}
2410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2412 const uint8_t *input,
2413 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002414{
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 if (operation->id == 0) {
2416 return PSA_ERROR_BAD_STATE;
2417 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002419 /* Don't require hash implementations to behave correctly on a
2420 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 if (input_length == 0) {
2422 return PSA_SUCCESS;
2423 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2426 input, input_length);
2427 if (status != PSA_SUCCESS) {
2428 psa_mac_abort(operation);
2429 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432}
2433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2435 uint8_t *mac,
2436 size_t mac_size,
2437 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002438{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002439 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2440 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002443 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002444 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002445 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002446
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002448 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002449 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002450 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002451
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002452 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2453 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002455 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002456 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002457 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002460 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002461 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002462 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 status = psa_driver_wrapper_mac_sign_finish(operation,
2465 mac, operation->mac_size,
2466 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002467
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002468exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002469 /* In case of success, set the potential excess room in the output buffer
2470 * to an invalid value, to avoid potentially leaking a longer MAC.
2471 * In case of error, set the output length and content to a safe default,
2472 * such that in case the caller misses an error check, the output would be
2473 * an unachievable MAC.
2474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002476 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002477 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002478 }
mohammad16036df908f2018-04-02 08:34:15 -07002479
Paul Elliottdc42ca82023-02-24 18:11:59 +00002480 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002485}
2486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2488 const uint8_t *mac,
2489 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002491 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2492 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002495 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002496 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002497 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002500 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002501 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002502 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002505 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002506 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002507 }
2508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 status = psa_driver_wrapper_mac_verify_finish(operation,
2510 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002511
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002512exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002516}
2517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2519 psa_algorithm_t alg,
2520 const uint8_t *input,
2521 size_t input_length,
2522 uint8_t *mac,
2523 size_t mac_size,
2524 size_t *mac_length,
2525 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002526{
2527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002528 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2529 psa_key_slot_t *slot;
2530 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531
Ronald Cron51131b52021-06-17 17:17:20 +02002532 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 key,
2534 &slot,
2535 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2536 alg);
2537 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002538 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002540
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002541 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 &operation_mac_size);
2543 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002544 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002548 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002549 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002550 }
2551
2552 status = psa_driver_wrapper_mac_compute(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002553 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 slot->key.data, slot->key.bytes,
2555 alg,
2556 input, input_length,
2557 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002558
2559exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002560 /* In case of success, set the potential excess room in the output buffer
2561 * to an invalid value, to avoid potentially leaking a longer MAC.
2562 * In case of error, set the output length and content to a safe default,
2563 * such that in case the caller misses an error check, the output would be
2564 * an unachievable MAC.
2565 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002567 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002568 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002569 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002570
2571 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002572
Ryan Everetta103ec92024-01-31 13:59:57 +00002573 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002576}
2577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002579 psa_algorithm_t alg,
2580 const uint8_t *input,
2581 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 uint8_t *mac,
2583 size_t mac_size,
2584 size_t *mac_length)
2585{
2586 return psa_mac_compute_internal(key, alg,
2587 input, input_length,
2588 mac, mac_size, mac_length, 1);
2589}
2590
2591psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2592 psa_algorithm_t alg,
2593 const uint8_t *input,
2594 size_t input_length,
2595 const uint8_t *mac,
2596 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002597{
2598 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002599 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2600 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 status = psa_mac_compute_internal(key, alg,
2603 input, input_length,
2604 actual_mac, sizeof(actual_mac),
2605 &actual_mac_length, 0);
2606 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002607 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002611 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002612 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002613 }
Dave Rodgman78701152023-08-29 14:20:18 +01002614 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002615 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002616 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002617 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002618
2619exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002623}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002624
Gilles Peskinee59236f2018-01-27 23:32:46 +01002625/****************************************************************/
2626/* Asymmetric cryptography */
2627/****************************************************************/
2628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2630 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002631{
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if (input_is_message) {
2633 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2634 return PSA_ERROR_INVALID_ARGUMENT;
2635 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2638 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2639 return PSA_ERROR_INVALID_ARGUMENT;
2640 }
2641 }
2642 } else {
2643 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2644 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002645 }
2646 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002647
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002649}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002650
Gilles Peskine449bd832023-01-11 14:50:10 +01002651static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2652 int input_is_message,
2653 psa_algorithm_t alg,
2654 const uint8_t *input,
2655 size_t input_length,
2656 uint8_t *signature,
2657 size_t signature_size,
2658 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002659{
2660 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2661 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2662 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002663
2664 *signature_length = 0;
2665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 status = psa_sign_verify_check_alg(input_is_message, alg);
2667 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002668 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002670
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002671 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002672 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002673 * buffer can in principle be empty since it doesn't actually have
2674 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 if (signature_size == 0) {
2676 return PSA_ERROR_BUFFER_TOO_SMALL;
2677 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002678
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002679 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 key, &slot,
2681 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2682 PSA_KEY_USAGE_SIGN_HASH,
2683 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002686 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002690 status = PSA_ERROR_INVALID_ARGUMENT;
2691 goto exit;
2692 }
2693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002695 status = psa_driver_wrapper_sign_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002696 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002697 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 signature, signature_size, signature_length);
2699 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002700
2701 status = psa_driver_wrapper_sign_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002702 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002703 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002705 }
2706
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002707
2708exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002709 psa_wipe_tag_output_buffer(signature, status, signature_size,
2710 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002711
Ryan Everetta103ec92024-01-31 13:59:57 +00002712 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002715}
2716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2718 int input_is_message,
2719 psa_algorithm_t alg,
2720 const uint8_t *input,
2721 size_t input_length,
2722 const uint8_t *signature,
2723 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002724{
2725 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2726 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2727 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 status = psa_sign_verify_check_alg(input_is_message, alg);
2730 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002731 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002733
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002734 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 key, &slot,
2736 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2737 PSA_KEY_USAGE_VERIFY_HASH,
2738 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 if (status != PSA_SUCCESS) {
2741 return status;
2742 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002745 status = psa_driver_wrapper_verify_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002746 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002747 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 signature, signature_length);
2749 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002750 status = psa_driver_wrapper_verify_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002751 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002752 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002754 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002755
Ryan Everetta103ec92024-01-31 13:59:57 +00002756 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002759
2760}
2761
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002762psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002763 const psa_key_attributes_t *attributes,
2764 const uint8_t *key_buffer,
2765 size_t key_buffer_size,
2766 psa_algorithm_t alg,
2767 const uint8_t *input,
2768 size_t input_length,
2769 uint8_t *signature,
2770 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002772{
2773 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002776 size_t hash_length;
2777 uint8_t hash[PSA_HASH_MAX_SIZE];
2778
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002779 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 PSA_ALG_SIGN_GET_HASH(alg),
2781 input, input_length,
2782 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002785 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002787
2788 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 attributes, key_buffer, key_buffer_size,
2790 alg, hash, hash_length,
2791 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002792 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002793
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002795}
2796
Gilles Peskine449bd832023-01-11 14:50:10 +01002797psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2798 psa_algorithm_t alg,
2799 const uint8_t *input,
2800 size_t input_length,
2801 uint8_t *signature,
2802 size_t signature_size,
2803 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002804{
2805 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002806 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002808}
2809
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002810psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002811 const psa_key_attributes_t *attributes,
2812 const uint8_t *key_buffer,
2813 size_t key_buffer_size,
2814 psa_algorithm_t alg,
2815 const uint8_t *input,
2816 size_t input_length,
2817 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002819{
2820 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002821
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002823 size_t hash_length;
2824 uint8_t hash[PSA_HASH_MAX_SIZE];
2825
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002826 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 PSA_ALG_SIGN_GET_HASH(alg),
2828 input, input_length,
2829 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002830
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002832 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002834
2835 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 attributes, key_buffer, key_buffer_size,
2837 alg, hash, hash_length,
2838 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002839 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002840
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002842}
2843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2845 psa_algorithm_t alg,
2846 const uint8_t *input,
2847 size_t input_length,
2848 const uint8_t *signature,
2849 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002850{
2851 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002852 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002854}
2855
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002856psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002857 const psa_key_attributes_t *attributes,
2858 const uint8_t *key_buffer, size_t key_buffer_size,
2859 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002861{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002862 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2864 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002865#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2867 return mbedtls_psa_rsa_sign_hash(
2868 attributes,
2869 key_buffer, key_buffer_size,
2870 alg, hash, hash_length,
2871 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002872#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2873 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 } else {
2875 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002876 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002877 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002879#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2881 return mbedtls_psa_ecdsa_sign_hash(
2882 attributes,
2883 key_buffer, key_buffer_size,
2884 alg, hash, hash_length,
2885 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002886#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2887 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 } else {
2889 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002890 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002891 }
Ronald Cron4501c982021-03-19 20:09:32 +01002892
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 (void) key_buffer;
2894 (void) key_buffer_size;
2895 (void) hash;
2896 (void) hash_length;
2897 (void) signature;
2898 (void) signature_size;
2899 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002900
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002902}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002903
Gilles Peskine449bd832023-01-11 14:50:10 +01002904psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2905 psa_algorithm_t alg,
2906 const uint8_t *hash,
2907 size_t hash_length,
2908 uint8_t *signature,
2909 size_t signature_size,
2910 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002911{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002912 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002913 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002915}
2916
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002917psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002918 const psa_key_attributes_t *attributes,
2919 const uint8_t *key_buffer, size_t key_buffer_size,
2920 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002922{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002923 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2925 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002926#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2928 return mbedtls_psa_rsa_verify_hash(
2929 attributes,
2930 key_buffer, key_buffer_size,
2931 alg, hash, hash_length,
2932 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002933#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2934 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 } else {
2936 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002937 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002938 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002940#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2942 return mbedtls_psa_ecdsa_verify_hash(
2943 attributes,
2944 key_buffer, key_buffer_size,
2945 alg, hash, hash_length,
2946 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002947#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2948 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 } else {
2950 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002951 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002952 }
Ronald Cron4501c982021-03-19 20:09:32 +01002953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 (void) key_buffer;
2955 (void) key_buffer_size;
2956 (void) hash;
2957 (void) hash_length;
2958 (void) signature;
2959 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002960
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002962}
2963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
2965 psa_algorithm_t alg,
2966 const uint8_t *hash,
2967 size_t hash_length,
2968 const uint8_t *signature,
2969 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002970{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002971 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002972 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002974}
2975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
2977 psa_algorithm_t alg,
2978 const uint8_t *input,
2979 size_t input_length,
2980 const uint8_t *salt,
2981 size_t salt_length,
2982 uint8_t *output,
2983 size_t output_size,
2984 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002985{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002986 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002987 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002988 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002989
Darryl Green5cc689a2018-07-24 15:34:10 +01002990 (void) input;
2991 (void) input_length;
2992 (void) salt;
2993 (void) output;
2994 (void) output_size;
2995
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002996 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
2999 return PSA_ERROR_INVALID_ARGUMENT;
3000 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003001
Valerio Setti5bb454a2024-01-15 10:43:16 +01003002 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3004 if (status != PSA_SUCCESS) {
3005 return status;
3006 }
3007 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3008 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003009 status = PSA_ERROR_INVALID_ARGUMENT;
3010 goto exit;
3011 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003012
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003013 status = psa_driver_wrapper_asymmetric_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003014 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003015 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003017exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003018 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003019
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003021}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3024 psa_algorithm_t alg,
3025 const uint8_t *input,
3026 size_t input_length,
3027 const uint8_t *salt,
3028 size_t salt_length,
3029 uint8_t *output,
3030 size_t output_size,
3031 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003032{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003033 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003034 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003035 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003036
Darryl Green5cc689a2018-07-24 15:34:10 +01003037 (void) input;
3038 (void) input_length;
3039 (void) salt;
3040 (void) output;
3041 (void) output_size;
3042
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003043 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3046 return PSA_ERROR_INVALID_ARGUMENT;
3047 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003048
Valerio Setti5bb454a2024-01-15 10:43:16 +01003049 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3051 if (status != PSA_SUCCESS) {
3052 return status;
3053 }
3054 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003055 status = PSA_ERROR_INVALID_ARGUMENT;
3056 goto exit;
3057 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003058
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003059 status = psa_driver_wrapper_asymmetric_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003060 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003061 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003063
3064exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003065 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003066
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003068}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003069
Paul Elliott9fe12f62022-11-30 19:16:02 +00003070/****************************************************************/
3071/* Asymmetric interruptible cryptography */
3072/****************************************************************/
3073
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003074static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3075
Paul Elliott9fe12f62022-11-30 19:16:02 +00003076void psa_interruptible_set_max_ops(uint32_t max_ops)
3077{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003078 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003079}
3080
3081uint32_t psa_interruptible_get_max_ops(void)
3082{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003083 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003084}
3085
Paul Elliott9fe12f62022-11-30 19:16:02 +00003086uint32_t psa_sign_hash_get_num_ops(
3087 const psa_sign_hash_interruptible_operation_t *operation)
3088{
Paul Elliott296ede92022-12-15 17:00:30 +00003089 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003090}
3091
3092uint32_t psa_verify_hash_get_num_ops(
3093 const psa_verify_hash_interruptible_operation_t *operation)
3094{
Paul Elliott296ede92022-12-15 17:00:30 +00003095 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003096}
3097
Paul Elliott59ad9452022-12-18 15:09:02 +00003098static psa_status_t psa_sign_hash_abort_internal(
3099 psa_sign_hash_interruptible_operation_t *operation)
3100{
3101 if (operation->id == 0) {
3102 /* The object has (apparently) been initialized but it is not (yet)
3103 * in use. It's ok to call abort on such an object, and there's
3104 * nothing to do. */
3105 return PSA_SUCCESS;
3106 }
3107
3108 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3109
3110 status = psa_driver_wrapper_sign_hash_abort(operation);
3111
3112 operation->id = 0;
3113
Paul Elliotte6145dc2023-02-07 12:51:21 +00003114 /* Do not clear either the error_occurred or num_ops elements here as they
3115 * only want to be cleared by the application calling abort, not by abort
3116 * being called at completion of an operation. */
3117
Paul Elliott59ad9452022-12-18 15:09:02 +00003118 return status;
3119}
3120
Paul Elliott9fe12f62022-11-30 19:16:02 +00003121psa_status_t psa_sign_hash_start(
3122 psa_sign_hash_interruptible_operation_t *operation,
3123 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3124 const uint8_t *hash, size_t hash_length)
3125{
3126 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3127 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3128 psa_key_slot_t *slot;
3129
Paul Elliottc9774412023-02-06 15:14:07 +00003130 /* Check that start has not been previously called, or operation has not
3131 * previously errored. */
3132 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003133 return PSA_ERROR_BAD_STATE;
3134 }
3135
Paul Elliott9fe12f62022-11-30 19:16:02 +00003136 status = psa_sign_verify_check_alg(0, alg);
3137 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003138 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003139 return status;
3140 }
3141
3142 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3143 PSA_KEY_USAGE_SIGN_HASH,
3144 alg);
3145
3146 if (status != PSA_SUCCESS) {
3147 goto exit;
3148 }
3149
3150 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3151 status = PSA_ERROR_INVALID_ARGUMENT;
3152 goto exit;
3153 }
3154
Paul Elliott296ede92022-12-15 17:00:30 +00003155 /* Ensure ops count gets reset, in case of operation re-use. */
3156 operation->num_ops = 0;
3157
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003158 status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003159 slot->key.data,
3160 slot->key.bytes, alg,
3161 hash, hash_length);
3162exit:
3163
3164 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003165 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003166 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003167 }
3168
Ryan Everettdcc03d52024-02-14 15:44:13 +00003169 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003170
Paul Elliottc9774412023-02-06 15:14:07 +00003171 if (unlock_status != PSA_SUCCESS) {
3172 operation->error_occurred = 1;
3173 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003174
Paul Elliottc9774412023-02-06 15:14:07 +00003175 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003176}
3177
3178
3179psa_status_t psa_sign_hash_complete(
3180 psa_sign_hash_interruptible_operation_t *operation,
3181 uint8_t *signature, size_t signature_size,
3182 size_t *signature_length)
3183{
3184 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3185
3186 *signature_length = 0;
3187
Paul Elliottc9774412023-02-06 15:14:07 +00003188 /* Check that start has been called first, and that operation has not
3189 * previously errored. */
3190 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003191 status = PSA_ERROR_BAD_STATE;
3192 goto exit;
3193 }
3194
Paul Elliott096abc42023-02-03 18:33:23 +00003195 /* Immediately reject a zero-length signature buffer. This guarantees that
3196 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003197 if (signature_size == 0) {
3198 status = PSA_ERROR_BUFFER_TOO_SMALL;
3199 goto exit;
3200 }
3201
3202 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3203 signature_size,
3204 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003205
Paul Elliott296ede92022-12-15 17:00:30 +00003206 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003207 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003208
Paul Elliottebe225c2023-02-10 14:32:53 +00003209exit:
3210
Paul Elliott59200a22023-02-21 15:07:40 +00003211 psa_wipe_tag_output_buffer(signature, status, signature_size,
3212 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003213
Paul Elliott53bb3122023-02-10 14:22:22 +00003214 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003215 if (status != PSA_SUCCESS) {
3216 operation->error_occurred = 1;
3217 }
3218
Paul Elliott59ad9452022-12-18 15:09:02 +00003219 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003220 }
3221
3222 return status;
3223}
3224
3225psa_status_t psa_sign_hash_abort(
3226 psa_sign_hash_interruptible_operation_t *operation)
3227{
Paul Elliott59ad9452022-12-18 15:09:02 +00003228 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3229
3230 status = psa_sign_hash_abort_internal(operation);
3231
3232 /* We clear the number of ops done here, so that it is not cleared when
3233 * the operation fails or succeeds, only on manual abort. */
3234 operation->num_ops = 0;
3235
Paul Elliottc9774412023-02-06 15:14:07 +00003236 /* Likewise, failure state. */
3237 operation->error_occurred = 0;
3238
Paul Elliott59ad9452022-12-18 15:09:02 +00003239 return status;
3240}
3241
3242static psa_status_t psa_verify_hash_abort_internal(
3243 psa_verify_hash_interruptible_operation_t *operation)
3244{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003245 if (operation->id == 0) {
3246 /* The object has (apparently) been initialized but it is not (yet)
3247 * in use. It's ok to call abort on such an object, and there's
3248 * nothing to do. */
3249 return PSA_SUCCESS;
3250 }
3251
Paul Elliott59ad9452022-12-18 15:09:02 +00003252 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3253
3254 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003255
3256 operation->id = 0;
3257
Paul Elliotte6145dc2023-02-07 12:51:21 +00003258 /* Do not clear either the error_occurred or num_ops elements here as they
3259 * only want to be cleared by the application calling abort, not by abort
3260 * being called at completion of an operation. */
3261
Paul Elliott59ad9452022-12-18 15:09:02 +00003262 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003263}
3264
3265psa_status_t psa_verify_hash_start(
3266 psa_verify_hash_interruptible_operation_t *operation,
3267 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3268 const uint8_t *hash, size_t hash_length,
3269 const uint8_t *signature, size_t signature_length)
3270{
3271 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3272 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3273 psa_key_slot_t *slot;
3274
Paul Elliottc9774412023-02-06 15:14:07 +00003275 /* Check that start has not been previously called, or operation has not
3276 * previously errored. */
3277 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003278 return PSA_ERROR_BAD_STATE;
3279 }
3280
3281 status = psa_sign_verify_check_alg(0, alg);
3282 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003283 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003284 return status;
3285 }
3286
3287 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3288 PSA_KEY_USAGE_VERIFY_HASH,
3289 alg);
3290
3291 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003292 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003293 return status;
3294 }
3295
Paul Elliott296ede92022-12-15 17:00:30 +00003296 /* Ensure ops count gets reset, in case of operation re-use. */
3297 operation->num_ops = 0;
3298
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003299 status = psa_driver_wrapper_verify_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003300 slot->key.data,
3301 slot->key.bytes,
3302 alg, hash, hash_length,
3303 signature, signature_length);
3304
3305 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003306 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003307 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003308 }
3309
Ryan Everett291267f2024-02-14 15:59:15 +00003310 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003311
Paul Elliottc9774412023-02-06 15:14:07 +00003312 if (unlock_status != PSA_SUCCESS) {
3313 operation->error_occurred = 1;
3314 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003315
Paul Elliottc9774412023-02-06 15:14:07 +00003316 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003317}
3318
3319psa_status_t psa_verify_hash_complete(
3320 psa_verify_hash_interruptible_operation_t *operation)
3321{
3322 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3323
Paul Elliottc9774412023-02-06 15:14:07 +00003324 /* Check that start has been called first, and that operation has not
3325 * previously errored. */
3326 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003327 status = PSA_ERROR_BAD_STATE;
3328 goto exit;
3329 }
3330
3331 status = psa_driver_wrapper_verify_hash_complete(operation);
3332
Paul Elliott296ede92022-12-15 17:00:30 +00003333 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003334 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003335 operation);
3336
Paul Elliottebe225c2023-02-10 14:32:53 +00003337exit:
3338
Paul Elliott9fe12f62022-11-30 19:16:02 +00003339 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003340 if (status != PSA_SUCCESS) {
3341 operation->error_occurred = 1;
3342 }
3343
Paul Elliott59ad9452022-12-18 15:09:02 +00003344 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003345 }
3346
3347 return status;
3348}
3349
3350psa_status_t psa_verify_hash_abort(
3351 psa_verify_hash_interruptible_operation_t *operation)
3352{
Paul Elliott59ad9452022-12-18 15:09:02 +00003353 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003354
Paul Elliott59ad9452022-12-18 15:09:02 +00003355 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003356
Paul Elliott59ad9452022-12-18 15:09:02 +00003357 /* We clear the number of ops done here, so that it is not cleared when
3358 * the operation fails or succeeds, only on manual abort. */
3359 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003360
Paul Elliottc9774412023-02-06 15:14:07 +00003361 /* Likewise, failure state. */
3362 operation->error_occurred = 0;
3363
Paul Elliott59ad9452022-12-18 15:09:02 +00003364 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003365}
3366
Paul Elliott588f8ed2022-12-02 18:10:26 +00003367/****************************************************************/
3368/* Asymmetric interruptible cryptography internal */
3369/* implementations */
3370/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003371
Paul Elliott588f8ed2022-12-02 18:10:26 +00003372void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3373{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003374
Paul Elliott588f8ed2022-12-02 18:10:26 +00003375#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3376 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3377 defined(MBEDTLS_ECP_RESTARTABLE)
3378
3379 /* Internal implementation uses zero to indicate infinite number max ops,
3380 * therefore avoid this value, and set to minimum possible. */
3381 if (max_ops == 0) {
3382 max_ops = 1;
3383 }
3384
Paul Elliott588f8ed2022-12-02 18:10:26 +00003385 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003386#else
3387 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003388#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3389 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3390 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3391}
3392
Paul Elliott588f8ed2022-12-02 18:10:26 +00003393uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003394 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003395{
3396#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3397 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3398 defined(MBEDTLS_ECP_RESTARTABLE)
3399
Paul Elliott93d9ca82023-02-15 18:14:21 +00003400 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003401#else
3402 (void) operation;
3403 return 0;
3404#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3405 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3406 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3407}
3408
3409uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003410 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003411{
3412 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3413 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3414 defined(MBEDTLS_ECP_RESTARTABLE)
3415
Paul Elliott93d9ca82023-02-15 18:14:21 +00003416 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003417#else
3418 (void) operation;
3419 return 0;
3420#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3421 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3422 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3423}
3424
3425psa_status_t mbedtls_psa_sign_hash_start(
3426 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3427 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3428 size_t key_buffer_size, psa_algorithm_t alg,
3429 const uint8_t *hash, size_t hash_length)
3430{
3431 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003432 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003433
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003434 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003435 return PSA_ERROR_NOT_SUPPORTED;
3436 }
3437
3438 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003439 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003440 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003441
3442#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003443 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3444 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003445
Paul Elliotta1c94092023-02-15 16:38:04 +00003446 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3447
Paul Elliott93d9ca82023-02-15 18:14:21 +00003448 /* Ensure num_ops is zero'ed in case of context re-use. */
3449 operation->num_ops = 0;
3450
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003451 status = mbedtls_psa_ecp_load_representation(attributes->type,
3452 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003453 key_buffer,
3454 key_buffer_size,
3455 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003456
Paul Elliott068fe072023-01-16 13:59:15 +00003457 if (status != PSA_SUCCESS) {
3458 return status;
3459 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003460
Paul Elliott1bc59df2023-02-05 13:41:57 +00003461 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003462 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003463
Paul Elliott068fe072023-01-16 13:59:15 +00003464 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003465 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003466 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003467
Paul Elliott0290a762023-02-09 14:30:24 +00003468 /* We only need to store the same length of hash as the private key size
3469 * here, it would be truncated by the internal implementation anyway. */
3470 required_hash_length = (hash_length < operation->coordinate_bytes ?
3471 hash_length : operation->coordinate_bytes);
3472
Paul Elliottba70ad42023-02-15 18:23:53 +00003473 if (required_hash_length > sizeof(operation->hash)) {
3474 /* Shouldn't happen, but better safe than sorry. */
3475 return PSA_ERROR_CORRUPTION_DETECTED;
3476 }
3477
Paul Elliott0290a762023-02-09 14:30:24 +00003478 memcpy(operation->hash, hash, required_hash_length);
3479 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003480
3481 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003482
3483#else
Paul Elliott068fe072023-01-16 13:59:15 +00003484 (void) operation;
3485 (void) key_buffer;
3486 (void) key_buffer_size;
3487 (void) alg;
3488 (void) hash;
3489 (void) hash_length;
3490 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003491 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003492
Paul Elliott068fe072023-01-16 13:59:15 +00003493 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003494#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3495 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3496 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003497}
3498
3499psa_status_t mbedtls_psa_sign_hash_complete(
3500 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3501 uint8_t *signature, size_t signature_size,
3502 size_t *signature_length)
3503{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003504#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3505 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3506 defined(MBEDTLS_ECP_RESTARTABLE)
3507
Paul Elliotta1c94092023-02-15 16:38:04 +00003508 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003509 mbedtls_mpi r;
3510 mbedtls_mpi s;
3511
Paul Elliott46845252023-02-03 14:59:11 +00003512 mbedtls_mpi_init(&r);
3513 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003514
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003515 /* Ensure max_ops is set to the current value (or default). */
3516 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3517
Paul Elliotta1c94092023-02-15 16:38:04 +00003518 if (signature_size < 2 * operation->coordinate_bytes) {
3519 status = PSA_ERROR_BUFFER_TOO_SMALL;
3520 goto exit;
3521 }
3522
Paul Elliott588f8ed2022-12-02 18:10:26 +00003523 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003524
Paul Elliott588f8ed2022-12-02 18:10:26 +00003525#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3526 status = mbedtls_to_psa_error(
3527 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003528 &r,
3529 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003530 &operation->ctx->d,
3531 operation->hash,
3532 operation->hash_length,
3533 operation->md_alg,
3534 mbedtls_psa_get_random,
3535 MBEDTLS_PSA_RANDOM_STATE,
3536 &operation->restart_ctx));
3537#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003538 status = PSA_ERROR_NOT_SUPPORTED;
3539 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003540#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3541 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003542 status = mbedtls_to_psa_error(
3543 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003544 &r,
3545 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003546 &operation->ctx->d,
3547 operation->hash,
3548 operation->hash_length,
3549 mbedtls_psa_get_random,
3550 MBEDTLS_PSA_RANDOM_STATE,
3551 mbedtls_psa_get_random,
3552 MBEDTLS_PSA_RANDOM_STATE,
3553 &operation->restart_ctx));
3554 }
3555
Paul Elliottf8e5b562023-02-19 18:43:45 +00003556 /* Hide the fact that the restart context only holds a delta of number of
3557 * ops done during the last operation, not an absolute value. */
3558 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3559
Paul Elliott724bd252023-02-08 12:35:08 +00003560 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003561 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003562 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003563 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003564 operation->coordinate_bytes)
3565 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003566
3567 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003568 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003569 }
3570
3571 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003572 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003573 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003574 operation->coordinate_bytes,
3575 operation->coordinate_bytes)
3576 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003577
3578 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003579 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003580 }
3581
Paul Elliott1bc59df2023-02-05 13:41:57 +00003582 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003583
Paul Elliott724bd252023-02-08 12:35:08 +00003584 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003585 }
Paul Elliott724bd252023-02-08 12:35:08 +00003586
3587exit:
3588
3589 mbedtls_mpi_free(&r);
3590 mbedtls_mpi_free(&s);
3591 return status;
3592
Paul Elliott588f8ed2022-12-02 18:10:26 +00003593 #else
3594
3595 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003596 (void) signature;
3597 (void) signature_size;
3598 (void) signature_length;
3599
3600 return PSA_ERROR_NOT_SUPPORTED;
3601
3602#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3603 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3604 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3605}
3606
3607psa_status_t mbedtls_psa_sign_hash_abort(
3608 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3609{
3610
3611#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3612 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3613 defined(MBEDTLS_ECP_RESTARTABLE)
3614
3615 if (operation->ctx) {
3616 mbedtls_ecdsa_free(operation->ctx);
3617 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003618 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003619 }
3620
3621 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3622
Paul Elliott93d9ca82023-02-15 18:14:21 +00003623 operation->num_ops = 0;
3624
Paul Elliott588f8ed2022-12-02 18:10:26 +00003625 return PSA_SUCCESS;
3626
3627#else
3628
3629 (void) operation;
3630
3631 return PSA_ERROR_NOT_SUPPORTED;
3632
3633#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3634 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3635 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3636}
3637
3638psa_status_t mbedtls_psa_verify_hash_start(
3639 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3640 const psa_key_attributes_t *attributes,
3641 const uint8_t *key_buffer, size_t key_buffer_size,
3642 psa_algorithm_t alg,
3643 const uint8_t *hash, size_t hash_length,
3644 const uint8_t *signature, size_t signature_length)
3645{
3646 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003647 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003648 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003649
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003650 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003651 return PSA_ERROR_NOT_SUPPORTED;
3652 }
3653
3654 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003655 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003656 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003657
3658#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003659 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3660 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003661
Paul Elliotta1c94092023-02-15 16:38:04 +00003662 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3663 mbedtls_mpi_init(&operation->r);
3664 mbedtls_mpi_init(&operation->s);
3665
Paul Elliott93d9ca82023-02-15 18:14:21 +00003666 /* Ensure num_ops is zero'ed in case of context re-use. */
3667 operation->num_ops = 0;
3668
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003669 status = mbedtls_psa_ecp_load_representation(attributes->type,
3670 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003671 key_buffer,
3672 key_buffer_size,
3673 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003674
Paul Elliott068fe072023-01-16 13:59:15 +00003675 if (status != PSA_SUCCESS) {
3676 return status;
3677 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003678
Paul Elliottc569fc22023-02-10 13:02:54 +00003679 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003680
Paul Elliott1bc59df2023-02-05 13:41:57 +00003681 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003682 return PSA_ERROR_INVALID_SIGNATURE;
3683 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003684
Paul Elliott068fe072023-01-16 13:59:15 +00003685 status = mbedtls_to_psa_error(
3686 mbedtls_mpi_read_binary(&operation->r,
3687 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003688 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003689
Paul Elliott068fe072023-01-16 13:59:15 +00003690 if (status != PSA_SUCCESS) {
3691 return status;
3692 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003693
Paul Elliott068fe072023-01-16 13:59:15 +00003694 status = mbedtls_to_psa_error(
3695 mbedtls_mpi_read_binary(&operation->s,
3696 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003697 coordinate_bytes,
3698 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003699
Paul Elliott068fe072023-01-16 13:59:15 +00003700 if (status != PSA_SUCCESS) {
3701 return status;
3702 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003703
Paul Elliott2c9843f2023-02-15 17:32:42 +00003704 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003705
Paul Elliott2c9843f2023-02-15 17:32:42 +00003706 if (status != PSA_SUCCESS) {
3707 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003708 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003709
Paul Elliott0290a762023-02-09 14:30:24 +00003710 /* We only need to store the same length of hash as the private key size
3711 * here, it would be truncated by the internal implementation anyway. */
3712 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3713 coordinate_bytes);
3714
Paul Elliottba70ad42023-02-15 18:23:53 +00003715 if (required_hash_length > sizeof(operation->hash)) {
3716 /* Shouldn't happen, but better safe than sorry. */
3717 return PSA_ERROR_CORRUPTION_DETECTED;
3718 }
3719
Paul Elliott0290a762023-02-09 14:30:24 +00003720 memcpy(operation->hash, hash, required_hash_length);
3721 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003722
3723 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003724#else
Paul Elliott068fe072023-01-16 13:59:15 +00003725 (void) operation;
3726 (void) key_buffer;
3727 (void) key_buffer_size;
3728 (void) alg;
3729 (void) hash;
3730 (void) hash_length;
3731 (void) signature;
3732 (void) signature_length;
3733 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003734 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003735 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003736
Paul Elliott068fe072023-01-16 13:59:15 +00003737 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003738#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3739 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3740 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003741}
3742
3743psa_status_t mbedtls_psa_verify_hash_complete(
3744 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3745{
3746
3747#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3748 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3749 defined(MBEDTLS_ECP_RESTARTABLE)
3750
Paul Elliottf8e5b562023-02-19 18:43:45 +00003751 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3752
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003753 /* Ensure max_ops is set to the current value (or default). */
3754 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3755
Paul Elliottf8e5b562023-02-19 18:43:45 +00003756 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003757 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3758 operation->hash,
3759 operation->hash_length,
3760 &operation->ctx->Q,
3761 &operation->r,
3762 &operation->s,
3763 &operation->restart_ctx));
3764
Paul Elliottf8e5b562023-02-19 18:43:45 +00003765 /* Hide the fact that the restart context only holds a delta of number of
3766 * ops done during the last operation, not an absolute value. */
3767 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3768
3769 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770#else
3771 (void) operation;
3772
3773 return PSA_ERROR_NOT_SUPPORTED;
3774
3775#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3776 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3777 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3778}
3779
3780psa_status_t mbedtls_psa_verify_hash_abort(
3781 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3782{
3783
3784#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3785 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3786 defined(MBEDTLS_ECP_RESTARTABLE)
3787
3788 if (operation->ctx) {
3789 mbedtls_ecdsa_free(operation->ctx);
3790 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003791 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003792 }
3793
3794 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3795
Paul Elliott93d9ca82023-02-15 18:14:21 +00003796 operation->num_ops = 0;
3797
Paul Elliott588f8ed2022-12-02 18:10:26 +00003798 mbedtls_mpi_free(&operation->r);
3799 mbedtls_mpi_free(&operation->s);
3800
3801 return PSA_SUCCESS;
3802
3803#else
3804 (void) operation;
3805
3806 return PSA_ERROR_NOT_SUPPORTED;
3807
3808#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3809 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3810 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3811}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003812
mohammad1603503973b2018-03-12 15:59:30 +02003813/****************************************************************/
3814/* Symmetric cryptography */
3815/****************************************************************/
3816
Gilles Peskine449bd832023-01-11 14:50:10 +01003817static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3818 mbedtls_svc_key_id_t key,
3819 psa_algorithm_t alg,
3820 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003821{
3822 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3823 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003824 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003825 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3826 PSA_KEY_USAGE_ENCRYPT :
3827 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003828
3829 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003830 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003831 status = PSA_ERROR_BAD_STATE;
3832 goto exit;
3833 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003834
Gilles Peskine449bd832023-01-11 14:50:10 +01003835 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003836 status = PSA_ERROR_INVALID_ARGUMENT;
3837 goto exit;
3838 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003839
Gilles Peskine449bd832023-01-11 14:50:10 +01003840 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3841 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003842 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003843 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003844
Ronald Cron49fafa92021-03-10 08:34:23 +01003845 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003846 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003847 * so we only set it (in the driver wrapper) after resources have been
3848 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003849 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003850 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003851 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003852 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003853 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003854 }
3855 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003856
3857 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 if (cipher_operation == MBEDTLS_ENCRYPT) {
3859 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003860 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003861 slot->key.data,
3862 slot->key.bytes,
3863 alg);
3864 } else {
3865 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003866 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003867 slot->key.data,
3868 slot->key.bytes,
3869 alg);
3870 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003871
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003872exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003873 if (status != PSA_SUCCESS) {
3874 psa_cipher_abort(operation);
3875 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003876
Ryan Everettc0053cc2024-02-14 16:27:13 +00003877 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003878
Gilles Peskine449bd832023-01-11 14:50:10 +01003879 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003880}
3881
Gilles Peskine449bd832023-01-11 14:50:10 +01003882psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3883 mbedtls_svc_key_id_t key,
3884 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003885{
Gilles Peskine449bd832023-01-11 14:50:10 +01003886 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003887}
3888
Gilles Peskine449bd832023-01-11 14:50:10 +01003889psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3890 mbedtls_svc_key_id_t key,
3891 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003892{
Gilles Peskine449bd832023-01-11 14:50:10 +01003893 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003894}
3895
Gilles Peskine449bd832023-01-11 14:50:10 +01003896psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3897 uint8_t *iv,
3898 size_t iv_size,
3899 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003900{
Ronald Cron6d051732020-10-01 14:10:20 +02003901 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003902 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07003903 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003904
Gilles Peskine449bd832023-01-11 14:50:10 +01003905 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003906 status = PSA_ERROR_BAD_STATE;
3907 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003908 }
3909
Gilles Peskine449bd832023-01-11 14:50:10 +01003910 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003911 status = PSA_ERROR_BAD_STATE;
3912 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003913 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003914
Ronald Cron2fb90522021-07-05 12:31:44 +02003915 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003916 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003917 status = PSA_ERROR_BUFFER_TOO_SMALL;
3918 goto exit;
3919 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003920
Gilles Peskine449bd832023-01-11 14:50:10 +01003921 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02003922 status = PSA_ERROR_GENERIC_ERROR;
3923 goto exit;
3924 }
3925
Gilles Peskine449bd832023-01-11 14:50:10 +01003926 status = psa_generate_random(local_iv, default_iv_length);
3927 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003928 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003929 }
Ronald Cron5618a392021-03-26 09:52:26 +01003930
Gilles Peskine449bd832023-01-11 14:50:10 +01003931 status = psa_driver_wrapper_cipher_set_iv(operation,
3932 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003933
3934exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 if (status == PSA_SUCCESS) {
3936 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02003937 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003938 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02003940 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003941 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02003942 }
Ronald Cron6d051732020-10-01 14:10:20 +02003943
Gilles Peskine449bd832023-01-11 14:50:10 +01003944 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003945}
3946
Gilles Peskine449bd832023-01-11 14:50:10 +01003947psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3948 const uint8_t *iv,
3949 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003950{
Ronald Cron6d051732020-10-01 14:10:20 +02003951 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003952
Gilles Peskine449bd832023-01-11 14:50:10 +01003953 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003954 status = PSA_ERROR_BAD_STATE;
3955 goto exit;
3956 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003957
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003959 status = PSA_ERROR_BAD_STATE;
3960 goto exit;
3961 }
Ronald Crona0d68172021-03-26 10:15:08 +01003962
Gilles Peskine449bd832023-01-11 14:50:10 +01003963 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003964 status = PSA_ERROR_INVALID_ARGUMENT;
3965 goto exit;
3966 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003967
Gilles Peskine449bd832023-01-11 14:50:10 +01003968 status = psa_driver_wrapper_cipher_set_iv(operation,
3969 iv,
3970 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003971
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003972exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003974 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003975 } else {
3976 psa_cipher_abort(operation);
3977 }
3978 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003979}
3980
Gilles Peskine449bd832023-01-11 14:50:10 +01003981psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3982 const uint8_t *input,
3983 size_t input_length,
3984 uint8_t *output,
3985 size_t output_size,
3986 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003987{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003988 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003989
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003991 status = PSA_ERROR_BAD_STATE;
3992 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003993 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003994
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003996 status = PSA_ERROR_BAD_STATE;
3997 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003998 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003999
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 status = psa_driver_wrapper_cipher_update(operation,
4001 input,
4002 input_length,
4003 output,
4004 output_size,
4005 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004006
4007exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 if (status != PSA_SUCCESS) {
4009 psa_cipher_abort(operation);
4010 }
Ronald Cron6d051732020-10-01 14:10:20 +02004011
Gilles Peskine449bd832023-01-11 14:50:10 +01004012 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004013}
4014
Gilles Peskine449bd832023-01-11 14:50:10 +01004015psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4016 uint8_t *output,
4017 size_t output_size,
4018 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004019{
David Saadab4ecc272019-02-14 13:48:10 +02004020 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004021
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004023 status = PSA_ERROR_BAD_STATE;
4024 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004025 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004028 status = PSA_ERROR_BAD_STATE;
4029 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004030 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 status = psa_driver_wrapper_cipher_finish(operation,
4033 output,
4034 output_size,
4035 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004036
4037exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 if (status == PSA_SUCCESS) {
4039 return psa_cipher_abort(operation);
4040 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004041 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004043
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004045 }
mohammad1603503973b2018-03-12 15:59:30 +02004046}
4047
Gilles Peskine449bd832023-01-11 14:50:10 +01004048psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004049{
Gilles Peskine449bd832023-01-11 14:50:10 +01004050 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004051 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004052 * in use. It's ok to call abort on such an object, and there's
4053 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004055 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004058
Ronald Cron49fafa92021-03-10 08:34:23 +01004059 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004060 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004061 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004062
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004064}
4065
Gilles Peskine449bd832023-01-11 14:50:10 +01004066psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4067 psa_algorithm_t alg,
4068 const uint8_t *input,
4069 size_t input_length,
4070 uint8_t *output,
4071 size_t output_size,
4072 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004073{
4074 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004075 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004076 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004077 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4078 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004079
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004081 status = PSA_ERROR_INVALID_ARGUMENT;
4082 goto exit;
4083 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004084
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4086 PSA_KEY_USAGE_ENCRYPT,
4087 alg);
4088 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004089 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004091
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4093 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004094 status = PSA_ERROR_GENERIC_ERROR;
4095 goto exit;
4096 }
4097
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 if (default_iv_length > 0) {
4099 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004100 status = PSA_ERROR_BUFFER_TOO_SMALL;
4101 goto exit;
4102 }
4103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 status = psa_generate_random(local_iv, default_iv_length);
4105 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004106 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004108 }
4109
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004110 status = psa_driver_wrapper_cipher_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004111 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004112 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004113 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004115
4116exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004117 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004119 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004120 }
Ronald Cron23919522021-07-08 19:00:07 +02004121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 if (status == PSA_SUCCESS) {
4123 if (default_iv_length > 0) {
4124 memcpy(output, local_iv, default_iv_length);
4125 }
4126 *output_length += default_iv_length;
4127 } else {
4128 *output_length = 0;
4129 }
4130
4131 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004132}
4133
Gilles Peskine449bd832023-01-11 14:50:10 +01004134psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4135 psa_algorithm_t alg,
4136 const uint8_t *input,
4137 size_t input_length,
4138 uint8_t *output,
4139 size_t output_size,
4140 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004141{
4142 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004143 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004144 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004145
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004147 status = PSA_ERROR_INVALID_ARGUMENT;
4148 goto exit;
4149 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004150
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4152 PSA_KEY_USAGE_DECRYPT,
4153 alg);
4154 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004155 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004157
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4159 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004160 status = PSA_ERROR_INVALID_ARGUMENT;
4161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004163 status = PSA_ERROR_INVALID_ARGUMENT;
4164 goto exit;
4165 }
4166
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004167 status = psa_driver_wrapper_cipher_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004168 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004169 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004171
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004172exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004173 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004175 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004177
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004179 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 }
Ronald Cron23919522021-07-08 19:00:07 +02004181
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004183}
4184
4185
mohammad16035955c982018-04-26 00:53:03 +03004186/****************************************************************/
4187/* AEAD */
4188/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004189
Paul Elliottbaff51c2021-09-28 17:44:45 +01004190/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004191static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004192{
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004194}
4195
4196/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004197static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4198 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004199{
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004201
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004203#if defined(PSA_WANT_ALG_GCM)
4204 case PSA_ALG_GCM:
4205 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 * arbitrarily large nonces. Please note that we do not generally
4207 * recommend the usage of nonces of greater length than
4208 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4209 * size, which can then lead to collisions if you encrypt a very
4210 * large number of messages.*/
4211 if (nonce_length != 0) {
4212 return PSA_SUCCESS;
4213 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004214 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004215#endif /* PSA_WANT_ALG_GCM */
4216#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004217 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 if (nonce_length >= 7 && nonce_length <= 13) {
4219 return PSA_SUCCESS;
4220 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004221 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004222#endif /* PSA_WANT_ALG_CCM */
4223#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004224 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 if (nonce_length == 12) {
4226 return PSA_SUCCESS;
4227 } else if (nonce_length == 8) {
4228 return PSA_ERROR_NOT_SUPPORTED;
4229 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004230 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004231#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004232 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004233 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004235 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004236
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004238}
4239
Gilles Peskine449bd832023-01-11 14:50:10 +01004240static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004241{
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4243 return PSA_ERROR_INVALID_ARGUMENT;
4244 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004245
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004247}
4248
Gilles Peskine449bd832023-01-11 14:50:10 +01004249psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4250 psa_algorithm_t alg,
4251 const uint8_t *nonce,
4252 size_t nonce_length,
4253 const uint8_t *additional_data,
4254 size_t additional_data_length,
4255 const uint8_t *plaintext,
4256 size_t plaintext_length,
4257 uint8_t *ciphertext,
4258 size_t ciphertext_size,
4259 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004260{
Ronald Cron215633c2021-03-16 17:15:37 +01004261 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4262 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004263
mohammad1603f08a5502018-06-03 15:05:47 +03004264 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004265
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 status = psa_aead_check_algorithm(alg);
4267 if (status != PSA_SUCCESS) {
4268 return status;
4269 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004270
Ronald Cron9a986162021-03-26 12:40:07 +01004271 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4273 if (status != PSA_SUCCESS) {
4274 return status;
4275 }
mohammad16035955c982018-04-26 00:53:03 +03004276
Gilles Peskine449bd832023-01-11 14:50:10 +01004277 status = psa_aead_check_nonce_length(alg, nonce_length);
4278 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004279 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004281
Ronald Cronde822812021-03-17 16:08:20 +01004282 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004283 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004284 alg,
4285 nonce, nonce_length,
4286 additional_data, additional_data_length,
4287 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004288 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004289
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4291 memset(ciphertext, 0, ciphertext_size);
4292 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004293
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004294exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004295 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004298}
4299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4301 psa_algorithm_t alg,
4302 const uint8_t *nonce,
4303 size_t nonce_length,
4304 const uint8_t *additional_data,
4305 size_t additional_data_length,
4306 const uint8_t *ciphertext,
4307 size_t ciphertext_length,
4308 uint8_t *plaintext,
4309 size_t plaintext_size,
4310 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004311{
Ronald Cron215633c2021-03-16 17:15:37 +01004312 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4313 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004314
Gilles Peskineee652a32018-06-01 19:23:52 +02004315 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004316
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 status = psa_aead_check_algorithm(alg);
4318 if (status != PSA_SUCCESS) {
4319 return status;
4320 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004321
Ronald Cron9a986162021-03-26 12:40:07 +01004322 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4324 if (status != PSA_SUCCESS) {
4325 return status;
4326 }
mohammad16035955c982018-04-26 00:53:03 +03004327
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 status = psa_aead_check_nonce_length(alg, nonce_length);
4329 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004330 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004332
Ronald Cronde822812021-03-17 16:08:20 +01004333 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004334 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004335 alg,
4336 nonce, nonce_length,
4337 additional_data, additional_data_length,
4338 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004340
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 if (status != PSA_SUCCESS && plaintext_size != 0) {
4342 memset(plaintext, 0, plaintext_size);
4343 }
mohammad160360a64d02018-06-03 17:20:42 +03004344
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004345exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004346 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004347
Gilles Peskine449bd832023-01-11 14:50:10 +01004348 return status;
mohammad16035955c982018-04-26 00:53:03 +03004349}
4350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4352{
4353 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004354
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004356#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004358 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4360 return PSA_ERROR_INVALID_ARGUMENT;
4361 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004362 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004363#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004364
Przemek Stekiel86679c72022-10-06 17:06:56 +02004365#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004367 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4369 return PSA_ERROR_INVALID_ARGUMENT;
4370 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004371 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004372#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004373
Przemek Stekiel86679c72022-10-06 17:06:56 +02004374#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004376 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 if (tag_len != 16) {
4378 return PSA_ERROR_INVALID_ARGUMENT;
4379 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004380 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004381#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004382
4383 default:
4384 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004386 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004388}
4389
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004390/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004391static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4392 int is_encrypt,
4393 mbedtls_svc_key_id_t key,
4394 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004395{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004396 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4397 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4398 psa_key_slot_t *slot = NULL;
4399 psa_key_usage_t key_usage = 0;
4400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 status = psa_aead_check_algorithm(alg);
4402 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004403 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004407 status = PSA_ERROR_BAD_STATE;
4408 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004409 }
4410
Gilles Peskine449bd832023-01-11 14:50:10 +01004411 if (operation->nonce_set || operation->lengths_set ||
4412 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004413 status = PSA_ERROR_BAD_STATE;
4414 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004415 }
4416
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004418 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004420 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004422
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4424 alg);
4425 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004426 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004428
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004430 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 if (is_encrypt) {
4434 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004435 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 slot->key.data,
4437 slot->key.bytes,
4438 alg);
4439 } else {
4440 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004441 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 slot->key.data,
4443 slot->key.bytes,
4444 alg);
4445 }
4446 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004447 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004449
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004450 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004451
4452exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00004453 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004456 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004458 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 } else {
4460 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004461 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004462
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004464}
4465
Paul Elliott302ff6b2021-04-20 18:10:30 +01004466/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004467psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4468 mbedtls_svc_key_id_t key,
4469 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004470{
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004472}
4473
4474/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004475psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4476 mbedtls_svc_key_id_t key,
4477 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004478{
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004480}
4481
4482/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004483psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4484 uint8_t *nonce,
4485 size_t nonce_size,
4486 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004487{
4488 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004489 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004490 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004491
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004492 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004493
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004495 status = PSA_ERROR_BAD_STATE;
4496 goto exit;
4497 }
4498
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004500 status = PSA_ERROR_BAD_STATE;
4501 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004502 }
4503
Paul Elliotte193ea82021-10-01 13:00:16 +01004504 /* For CCM, this size may not be correct according to the PSA
4505 * specification. The PSA Crypto 1.0.1 specification states:
4506 *
4507 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4508 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4509 *
4510 * However this restriction that L has to be the smallest integer is not
4511 * applied in practice, and it is not implementable here since the
4512 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4514 operation->alg);
4515 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004516 status = PSA_ERROR_BUFFER_TOO_SMALL;
4517 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004518 }
4519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 status = psa_generate_random(local_nonce, required_nonce_size);
4521 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004522 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004524
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004526
Paul Elliott39dc6b82021-05-11 19:16:09 +01004527exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 if (status == PSA_SUCCESS) {
4529 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004530 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 } else {
4532 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004533 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004536}
4537
4538/* Set the nonce for a multipart authenticated encryption or decryption
4539 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004540psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4541 const uint8_t *nonce,
4542 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004543{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004544 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4545
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004547 status = PSA_ERROR_BAD_STATE;
4548 goto exit;
4549 }
4550
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004552 status = PSA_ERROR_BAD_STATE;
4553 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004554 }
4555
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4557 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004558 status = PSA_ERROR_INVALID_ARGUMENT;
4559 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004560 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004561
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4563 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004564
Paul Elliott39dc6b82021-05-11 19:16:09 +01004565exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004567 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004568 } else {
4569 psa_aead_abort(operation);
4570 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004573}
4574
4575/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004576psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4577 size_t ad_length,
4578 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004579{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004580 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004583 status = PSA_ERROR_BAD_STATE;
4584 goto exit;
4585 }
4586
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 if (operation->lengths_set || operation->ad_started ||
4588 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004589 status = PSA_ERROR_BAD_STATE;
4590 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004591 }
4592
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004594#if defined(PSA_WANT_ALG_GCM)
4595 case PSA_ALG_GCM:
4596 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 * bits. Without the guard this code will generate warnings on 32bit
4598 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004599#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 if (((uint64_t) ad_length) >> 61 != 0 ||
4601 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004602 status = PSA_ERROR_INVALID_ARGUMENT;
4603 goto exit;
4604 }
Paul Elliott325d3742021-09-27 17:56:28 +01004605#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004606 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004607#endif /* PSA_WANT_ALG_GCM */
4608#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004609 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004611 status = PSA_ERROR_INVALID_ARGUMENT;
4612 goto exit;
4613 }
4614 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004615#endif /* PSA_WANT_ALG_CCM */
4616#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004617 case PSA_ALG_CHACHA20_POLY1305:
4618 /* No length restrictions for ChaChaPoly. */
4619 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004620#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004621 default:
4622 break;
4623 }
Paul Elliott325d3742021-09-27 17:56:28 +01004624
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4626 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004627
Paul Elliott39dc6b82021-05-11 19:16:09 +01004628exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004630 operation->ad_remaining = ad_length;
4631 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004632 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 } else {
4634 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004635 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004636
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004638}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004639
4640/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004641psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4642 const uint8_t *input,
4643 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004644{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004645 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4646
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004648 status = PSA_ERROR_BAD_STATE;
4649 goto exit;
4650 }
4651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004653 status = PSA_ERROR_BAD_STATE;
4654 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004655 }
4656
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 if (operation->lengths_set) {
4658 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004659 status = PSA_ERROR_INVALID_ARGUMENT;
4660 goto exit;
4661 }
4662
4663 operation->ad_remaining -= input_length;
4664 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004665#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004667 status = PSA_ERROR_BAD_STATE;
4668 goto exit;
4669 }
4670#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004671
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 status = psa_driver_wrapper_aead_update_ad(operation, input,
4673 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004674
Paul Elliott39dc6b82021-05-11 19:16:09 +01004675exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004677 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 } else {
4679 psa_aead_abort(operation);
4680 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004683}
4684
4685/* Encrypt or decrypt a message fragment in an active multipart AEAD
4686 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004687psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4688 const uint8_t *input,
4689 size_t input_length,
4690 uint8_t *output,
4691 size_t output_size,
4692 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004693{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004694 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004695
4696 *output_length = 0;
4697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004699 status = PSA_ERROR_BAD_STATE;
4700 goto exit;
4701 }
4702
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004704 status = PSA_ERROR_BAD_STATE;
4705 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004706 }
4707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004709 /* Additional data length was supplied, but not all the additional
4710 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004712 status = PSA_ERROR_INVALID_ARGUMENT;
4713 goto exit;
4714 }
4715
4716 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004718 status = PSA_ERROR_INVALID_ARGUMENT;
4719 goto exit;
4720 }
4721
4722 operation->body_remaining -= input_length;
4723 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004724#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004726 status = PSA_ERROR_BAD_STATE;
4727 goto exit;
4728 }
4729#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004730
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4732 output, output_size,
4733 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004734
Paul Elliott39dc6b82021-05-11 19:16:09 +01004735exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004737 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 } else {
4739 psa_aead_abort(operation);
4740 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004741
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004743}
4744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004746{
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 if (operation->id == 0 || !operation->nonce_set) {
4748 return PSA_ERROR_BAD_STATE;
4749 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4752 operation->body_remaining != 0)) {
4753 return PSA_ERROR_INVALID_ARGUMENT;
4754 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004755
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004757}
4758
Paul Elliott302ff6b2021-04-20 18:10:30 +01004759/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004760psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4761 uint8_t *ciphertext,
4762 size_t ciphertext_size,
4763 size_t *ciphertext_length,
4764 uint8_t *tag,
4765 size_t tag_size,
4766 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004767{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004768 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4769
Paul Elliott302ff6b2021-04-20 18:10:30 +01004770 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004771 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004772
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 status = psa_aead_final_checks(operation);
4774 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004775 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004777
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004779 status = PSA_ERROR_BAD_STATE;
4780 goto exit;
4781 }
4782
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4784 ciphertext_size,
4785 ciphertext_length,
4786 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004787
Paul Elliott39dc6b82021-05-11 19:16:09 +01004788exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004789
4790
Paul Elliottf47b0952021-05-21 18:02:33 +01004791 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004792 * the zero tag size, make sure the tag is set to something implausible.
4793 * Even if the operation succeeds, make sure we clear the rest of the
4794 * buffer to prevent potential leakage of anything previously placed in
4795 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004796 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004799
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004801}
4802
4803/* Finish authenticating and decrypting a message in a multipart AEAD
4804 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004805psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4806 uint8_t *plaintext,
4807 size_t plaintext_size,
4808 size_t *plaintext_length,
4809 const uint8_t *tag,
4810 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004811{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004812 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4813
Paul Elliott302ff6b2021-04-20 18:10:30 +01004814 *plaintext_length = 0;
4815
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 status = psa_aead_final_checks(operation);
4817 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004818 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004820
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004822 status = PSA_ERROR_BAD_STATE;
4823 goto exit;
4824 }
4825
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4827 plaintext_size,
4828 plaintext_length,
4829 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004830
4831exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004835}
4836
4837/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004838psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004839{
4840 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004843 /* The object has (apparently) been initialized but it is not (yet)
4844 * in use. It's ok to call abort on such an object, and there's
4845 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004847 }
4848
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004850
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004854}
4855
Gilles Peskinee59236f2018-01-27 23:32:46 +01004856/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004857/* Generators */
4858/****************************************************************/
4859
Przemek Stekiel69c46792022-06-10 12:59:51 +02004860#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004861 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004862 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05304863 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304864 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08004865#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004866#endif /* At least one builtin KDF */
4867
Przemek Stekiel69c46792022-06-10 12:59:51 +02004868#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004869 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4870 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4871static psa_status_t psa_key_derivation_start_hmac(
4872 psa_mac_operation_t *operation,
4873 psa_algorithm_t hash_alg,
4874 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02004876{
4877 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4880 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4881 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02004882
Steven Cooreman72f736a2021-05-07 14:14:37 +02004883 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02004885
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 status = psa_driver_wrapper_mac_sign_setup(operation,
4887 &attributes,
4888 hmac_key, hmac_key_length,
4889 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02004890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 psa_reset_key_attributes(&attributes);
4892 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02004893}
4894#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004895
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004896#define HKDF_STATE_INIT 0 /* no input yet */
4897#define HKDF_STATE_STARTED 1 /* got salt */
4898#define HKDF_STATE_KEYED 2 /* got key */
4899#define HKDF_STATE_OUTPUT 3 /* output started */
4900
Gilles Peskinecbe66502019-05-16 16:59:18 +02004901static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004903{
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4905 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4906 } else {
4907 return operation->alg;
4908 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004909}
4910
Gilles Peskine449bd832023-01-11 14:50:10 +01004911psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004912{
4913 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4915 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004916 /* The object has (apparently) been initialized but it is not
4917 * in use. It's ok to call abort on such an object, and there's
4918 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004920#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
4922 mbedtls_free(operation->ctx.hkdf.info);
4923 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4924 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004925#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004926#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4927 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4929 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4930 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4931 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004932 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02004934 }
4935
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004937 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01004939 }
4940
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004942 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01004944 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01004945#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004947 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02004949 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01004950#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02004951 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004952
4953 /* We leave the fields Ai and output_block to be erased safely by the
4954 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004956#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4957 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004958#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
4960 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
4961 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
4962 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004963#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304964#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05304965 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304966 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004967 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304968 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304969 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304970
4971 status = PSA_SUCCESS;
4972 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304973#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004974 {
4975 status = PSA_ERROR_BAD_STATE;
4976 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 mbedtls_platform_zeroize(operation, sizeof(*operation));
4978 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004979}
4980
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004981psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004983{
Gilles Peskine449bd832023-01-11 14:50:10 +01004984 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004985 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004987 }
4988
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004989 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004991}
4992
Gilles Peskine449bd832023-01-11 14:50:10 +01004993psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4994 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004995{
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 if (operation->alg == 0) {
4997 return PSA_ERROR_BAD_STATE;
4998 }
4999 if (capacity > operation->capacity) {
5000 return PSA_ERROR_INVALID_ARGUMENT;
5001 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005002 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005004}
5005
Przemek Stekiel69c46792022-06-10 12:59:51 +02005006#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005007/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005008static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5009 psa_algorithm_t kdf_alg,
5010 uint8_t *output,
5011 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005012{
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5014 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005015 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005016 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005017#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005019#else
5020 const uint8_t last_block = 0xff;
5021#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005022
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 if (hkdf->state < HKDF_STATE_KEYED ||
5024 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005025#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005027#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005028 )) {
5029 return PSA_ERROR_BAD_STATE;
5030 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005031 hkdf->state = HKDF_STATE_OUTPUT;
5032
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005034 /* Copy what remains of the current block */
5035 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005037 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 }
5039 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005040 output += n;
5041 output_length -= n;
5042 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005044 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005046 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005047 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005048 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005049 * object was corrupted or if this function is called directly
5050 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 if (hkdf->block_number == last_block) {
5052 return PSA_ERROR_BAD_STATE;
5053 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005054
5055 /* We need a new block */
5056 ++hkdf->block_number;
5057 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005058
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5060 hash_alg,
5061 hkdf->prk,
5062 hash_length);
5063 if (status != PSA_SUCCESS) {
5064 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005065 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005066
5067 if (hkdf->block_number != 1) {
5068 status = psa_mac_update(&hkdf->hmac,
5069 hkdf->output_block,
5070 hash_length);
5071 if (status != PSA_SUCCESS) {
5072 return status;
5073 }
5074 }
5075 status = psa_mac_update(&hkdf->hmac,
5076 hkdf->info,
5077 hkdf->info_length);
5078 if (status != PSA_SUCCESS) {
5079 return status;
5080 }
5081 status = psa_mac_update(&hkdf->hmac,
5082 &hkdf->block_number, 1);
5083 if (status != PSA_SUCCESS) {
5084 return status;
5085 }
5086 status = psa_mac_sign_finish(&hkdf->hmac,
5087 hkdf->output_block,
5088 sizeof(hkdf->output_block),
5089 &hmac_output_length);
5090 if (status != PSA_SUCCESS) {
5091 return status;
5092 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005093 }
5094
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005096}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005097#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005098
John Durkop07cc04a2020-11-16 22:08:34 -08005099#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5100 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005101static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5102 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005104{
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5106 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005107 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5108 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005109 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005110
Janos Follath7742fee2019-06-17 12:58:10 +01005111 /* We can't be wanting more output after block 0xff, otherwise
5112 * the capacity check in psa_key_derivation_output_bytes() would have
5113 * prevented this call. It could happen only if the operation
5114 * object was corrupted or if this function is called directly
5115 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 if (tls12_prf->block_number == 0xff) {
5117 return PSA_ERROR_CORRUPTION_DETECTED;
5118 }
Janos Follath7742fee2019-06-17 12:58:10 +01005119
5120 /* We need a new block */
5121 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005122 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005123
5124 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5125 *
5126 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5127 *
5128 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5129 * HMAC_hash(secret, A(2) + seed) +
5130 * HMAC_hash(secret, A(3) + seed) + ...
5131 *
5132 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005133 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005134 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005135 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005136 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005137 * is currently extracted as `output_block` and where i is
5138 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005139 */
5140
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 status = psa_key_derivation_start_hmac(&hmac,
5142 hash_alg,
5143 tls12_prf->secret,
5144 tls12_prf->secret_length);
5145 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005146 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005148
5149 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005151 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5152 * the variable seed and in this instance means it in the context of the
5153 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 status = psa_mac_update(&hmac,
5155 tls12_prf->label,
5156 tls12_prf->label_length);
5157 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005158 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 }
5160 status = psa_mac_update(&hmac,
5161 tls12_prf->seed,
5162 tls12_prf->seed_length);
5163 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005164 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 }
5166 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005167 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5169 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005170 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005172 }
5173
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 status = psa_mac_sign_finish(&hmac,
5175 tls12_prf->Ai, hash_length,
5176 &hmac_output_length);
5177 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005178 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 }
5180 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005181 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005183
5184 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 status = psa_key_derivation_start_hmac(&hmac,
5186 hash_alg,
5187 tls12_prf->secret,
5188 tls12_prf->secret_length);
5189 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005190 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 }
5192 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5193 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005194 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 }
5196 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5197 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005198 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 }
5200 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5201 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005202 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 }
5204 status = psa_mac_sign_finish(&hmac,
5205 tls12_prf->output_block, hash_length,
5206 &hmac_output_length);
5207 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005208 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005210
Janos Follath7742fee2019-06-17 12:58:10 +01005211
5212cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 cleanup_status = psa_mac_abort(&hmac);
5214 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005215 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005217
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005219}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005220
Janos Follath844eb0e2019-06-19 12:10:49 +01005221static psa_status_t psa_key_derivation_tls12_prf_read(
5222 psa_tls12_prf_key_derivation_t *tls12_prf,
5223 psa_algorithm_t alg,
5224 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005226{
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5228 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005229 psa_status_t status;
5230 uint8_t offset, length;
5231
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005233 case PSA_TLS12_PRF_STATE_LABEL_SET:
5234 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5235 break;
5236 case PSA_TLS12_PRF_STATE_OUTPUT:
5237 break;
5238 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005240 }
5241
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005243 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 if (tls12_prf->left_in_block == 0) {
5245 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5246 alg);
5247 if (status != PSA_SUCCESS) {
5248 return status;
5249 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005250
5251 continue;
5252 }
5253
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005255 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005257 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005259
5260 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005262 output += length;
5263 output_length -= length;
5264 tls12_prf->left_in_block -= length;
5265 }
5266
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005268}
John Durkop07cc04a2020-11-16 22:08:34 -08005269#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5270 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005271
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005272#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5273static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5274 psa_tls12_ecjpake_to_pms_t *ecjpake,
5275 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005277{
5278 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005279 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005280
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 if (output_length != 32) {
5282 return PSA_ERROR_INVALID_ARGUMENT;
5283 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5286 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5287 &output_size);
5288 if (status != PSA_SUCCESS) {
5289 return status;
5290 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005291
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 if (output_size != output_length) {
5293 return PSA_ERROR_GENERIC_ERROR;
5294 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005295
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005297}
5298#endif
5299
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305300#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305301static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5302 psa_pbkdf2_key_derivation_t *pbkdf2,
5303 psa_algorithm_t prf_alg,
5304 uint8_t prf_output_length,
5305 psa_key_attributes_t *attributes)
5306{
5307 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305308 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305309 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305310 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305311 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305312 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305313 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305314
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305315 mac_operation.is_sign = 1;
5316 mac_operation.mac_size = prf_output_length;
5317 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305318
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305319 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5320 attributes,
5321 pbkdf2->password,
5322 pbkdf2->password_length,
5323 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305324 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305325 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305326 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305327 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5328 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305329 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305330 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305331 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305332 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305333 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305334 }
5335 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5336 &mac_output_length);
5337 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305338 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305339 }
5340
5341 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305342 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305343 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305344 }
5345
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305346 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305347
5348 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305349 /* We are passing prf_output_length as mac_size because the driver
5350 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305351 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305352 status = psa_driver_wrapper_mac_compute(attributes,
5353 pbkdf2->password,
5354 pbkdf2->password_length,
5355 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305356 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305357 &mac_output_length);
5358 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305359 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305360 }
5361
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305362 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305363 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305364
5365cleanup:
5366 /* Zeroise buffers to clear sensitive data from memory. */
5367 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5368 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305369}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305370
5371static psa_status_t psa_key_derivation_pbkdf2_read(
5372 psa_pbkdf2_key_derivation_t *pbkdf2,
5373 psa_algorithm_t kdf_alg,
5374 uint8_t *output,
5375 size_t output_length)
5376{
5377 psa_status_t status;
5378 psa_algorithm_t prf_alg;
5379 uint8_t prf_output_length;
5380 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5381 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5382 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5383
5384 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5385 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5386 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5387 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305388 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5389 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305390 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305391 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305392 } else {
5393 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305394 }
5395
5396 switch (pbkdf2->state) {
5397 case PSA_PBKDF2_STATE_PASSWORD_SET:
5398 /* Initially we need a new block so bytes_used is equal to block size*/
5399 pbkdf2->bytes_used = prf_output_length;
5400 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5401 break;
5402 case PSA_PBKDF2_STATE_OUTPUT:
5403 break;
5404 default:
5405 return PSA_ERROR_BAD_STATE;
5406 }
5407
5408 while (output_length != 0) {
5409 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5410 if (n > output_length) {
5411 n = (uint8_t) output_length;
5412 }
5413 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5414 output += n;
5415 output_length -= n;
5416 pbkdf2->bytes_used += n;
5417
5418 if (output_length == 0) {
5419 break;
5420 }
5421
5422 /* We need a new block */
5423 pbkdf2->bytes_used = 0;
5424 pbkdf2->block_number++;
5425
5426 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5427 prf_output_length,
5428 &attributes);
5429 if (status != PSA_SUCCESS) {
5430 return status;
5431 }
5432 }
5433
5434 return PSA_SUCCESS;
5435}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305436#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305437
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005438psa_status_t psa_key_derivation_output_bytes(
5439 psa_key_derivation_operation_t *operation,
5440 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005442{
5443 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005445
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005447 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005449 }
5450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005452 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005453 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005454 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005455 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005456 goto exit;
5457 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005459 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005460 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005461 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5462 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005463 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005464 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005466 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005467 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005468
Przemek Stekiel69c46792022-06-10 12:59:51 +02005469#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5471 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5472 output, output_length);
5473 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005474#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005475#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5476 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5478 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5479 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5480 kdf_alg, output,
5481 output_length);
5482 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005483#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5484 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005485#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005487 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5489 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005490#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305491#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305492 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305493 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5494 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305495 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305496#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005497
Gilles Peskineeab56e42018-07-12 17:12:33 +02005498 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005499 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005501 }
5502
5503exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005505 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005506 * This allows us to differentiate between exhausted operations and
5507 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5508 * operations. */
5509 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005511 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005513 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005515}
5516
David Brown7807bf72021-02-09 16:28:23 -07005517#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005518static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005519{
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 if (data_size >= 8) {
5521 mbedtls_des_key_set_parity(data);
5522 }
5523 if (data_size >= 16) {
5524 mbedtls_des_key_set_parity(data + 8);
5525 }
5526 if (data_size >= 24) {
5527 mbedtls_des_key_set_parity(data + 16);
5528 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005529}
David Brown7807bf72021-02-09 16:28:23 -07005530#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005531
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005532/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 * ECC keys on a Weierstrass elliptic curve require the generation
5534 * of a private key which is an integer
5535 * in the range [1, N - 1], where N is the boundary of the private key domain:
5536 * N is the prime p for Diffie-Hellman, or the order of the
5537 * curve’s base point for ECC.
5538 *
5539 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5540 * This function generates the private key using the following process:
5541 *
5542 * 1. Draw a byte string of length ceiling(m/8) bytes.
5543 * 2. If m is not a multiple of 8, set the most significant
5544 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5545 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5546 * 4. If k > N - 2, discard the result and return to step 1.
5547 * 5. Output k + 1 as the private key.
5548 *
5549 * This method allows compliance to NIST standards, specifically the methods titled
5550 * Key-Pair Generation by Testing Candidates in the following publications:
5551 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5552 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5553 * Diffie-Hellman keys.
5554 *
5555 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5556 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5557 *
5558 * Note: Function allocates memory for *data buffer, so given *data should be
5559 * always NULL.
5560 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005561#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5562#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005563static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005564 psa_key_slot_t *slot,
5565 size_t bits,
5566 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005567 uint8_t **data
5568 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005569{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005570 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005571 mbedtls_mpi k;
5572 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005574 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005575 size_t m;
5576 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005577
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 mbedtls_mpi_init(&k);
5579 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005580
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005581 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005583 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01005584 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005585
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005587 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005588 goto cleanup;
5589 }
5590
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005591 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005593
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005595
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005596 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005597 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005598 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005599
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005600 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005601
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005602 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005604
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005605 /* Note: This function is always called with *data == NULL and it
5606 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 *data = mbedtls_calloc(1, m_bytes);
5608 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005609 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005610 goto cleanup;
5611 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005612
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005614 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005616 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005618
5619 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005621 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005622 * (8 * ceiling(m/8) - m) bits of the first byte in
5623 * the string to zero.
5624 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005626 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005627 }
5628
5629 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 * big-endian byte string.
5631 */
5632 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005633
5634 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 * Result of comparison is returned. When it indicates error
5636 * then this function is called again.
5637 */
5638 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005639 }
5640
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005641 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5643 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005644cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 if (ret != 0) {
5646 status = mbedtls_to_psa_error(ret);
5647 }
5648 if (status != PSA_SUCCESS) {
5649 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005650 *data = NULL;
5651 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005652 mbedtls_mpi_free(&k);
5653 mbedtls_mpi_free(&diff_N_2);
5654 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005655}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005656
5657/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5658 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5659 *
5660 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5661 * draw a 32-byte string and process it as specified in
5662 * Elliptic Curves for Security [RFC7748] §5.
5663 *
5664 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5665 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5666 *
5667 * Note: Function allocates memory for *data buffer, so given *data should be
5668 * always NULL.
5669 */
5670
5671static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5672 size_t bits,
5673 psa_key_derivation_operation_t *operation,
5674 uint8_t **data
5675 )
5676{
5677 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005678 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005679
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005681 case 255:
5682 output_length = 32;
5683 break;
5684 case 448:
5685 output_length = 56;
5686 break;
5687 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005689 break;
5690 }
5691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005693
Gilles Peskine449bd832023-01-11 14:50:10 +01005694 if (*data == NULL) {
5695 return PSA_ERROR_INSUFFICIENT_MEMORY;
5696 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005697
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005699
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005701 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005705 case 255:
5706 (*data)[0] &= 248;
5707 (*data)[31] &= 127;
5708 (*data)[31] |= 64;
5709 break;
5710 case 448:
5711 (*data)[0] &= 252;
5712 (*data)[55] |= 128;
5713 break;
5714 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005716 break;
5717 }
5718
5719 return status;
5720}
Valerio Setti86587ab2023-06-27 13:09:30 +02005721#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5722static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5723 psa_key_slot_t *slot, size_t bits,
5724 psa_key_derivation_operation_t *operation, uint8_t **data)
5725{
5726 (void) slot;
5727 (void) bits;
5728 (void) operation;
5729 (void) data;
5730 return PSA_ERROR_NOT_SUPPORTED;
5731}
5732
5733static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5734 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5735{
5736 (void) bits;
5737 (void) operation;
5738 (void) data;
5739 return PSA_ERROR_NOT_SUPPORTED;
5740}
5741#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5742#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005743
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005744static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005745 psa_key_slot_t *slot,
5746 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005748{
5749 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305751 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005752 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5755 return PSA_ERROR_INVALID_ARGUMENT;
5756 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005757
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005758#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005759 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5761 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5762 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005763 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5765 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005766 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 }
5768 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005769 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5771 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005772 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005774 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005775 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005776#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005777 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 if (key_type_is_raw_bytes(slot->attr.type)) {
5779 if (bits % 8 != 0) {
5780 return PSA_ERROR_INVALID_ARGUMENT;
5781 }
5782 data = mbedtls_calloc(1, bytes);
5783 if (data == NULL) {
5784 return PSA_ERROR_INSUFFICIENT_MEMORY;
5785 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 status = psa_key_derivation_output_bytes(operation, data, bytes);
5788 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005789 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 }
David Brown12ca5032021-02-11 11:02:00 -07005791#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5793 psa_des_set_key_parity(data, bytes);
5794 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005795#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 } else {
5797 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005798 }
Ronald Crondd04d422020-11-28 15:14:42 +01005799
Ronald Cronbf33c932020-11-28 18:06:53 +01005800 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005801
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005802 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005803 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 &storage_size);
5805 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305806 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 }
Archanad8a83dc2021-06-14 10:04:16 +05305808 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 status = psa_allocate_buffer_to_slot(slot, storage_size);
5810 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305811 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 }
Archanad8a83dc2021-06-14 10:04:16 +05305813
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005814 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 data, bytes,
5816 slot->key.data,
5817 slot->key.bytes,
5818 &slot->key.bytes, &bits);
5819 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005820 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005822
5823exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 mbedtls_free(data);
5825 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005826}
5827
Gilles Peskine092ce512024-02-20 12:31:24 +01005828static const psa_key_production_parameters_t default_production_parameters =
5829 PSA_KEY_PRODUCTION_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005830
Gilles Peskine092ce512024-02-20 12:31:24 +01005831int psa_key_production_parameters_are_default(
5832 const psa_key_production_parameters_t *params,
5833 size_t params_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005834{
Gilles Peskine092ce512024-02-20 12:31:24 +01005835 if (params->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005836 return 0;
5837 }
Gilles Peskine092ce512024-02-20 12:31:24 +01005838 if (params_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005839 return 0;
5840 }
5841 return 1;
5842}
5843
5844psa_status_t psa_key_derivation_output_key_ext(
5845 const psa_key_attributes_t *attributes,
5846 psa_key_derivation_operation_t *operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005847 const psa_key_production_parameters_t *params,
5848 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005849 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005850{
5851 psa_status_t status;
5852 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005853 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005854
Ronald Cron81709fc2020-11-14 12:10:32 +01005855 *key = MBEDTLS_SVC_KEY_ID_INIT;
5856
Gilles Peskine0f84d622019-09-12 19:03:13 +02005857 /* Reject any attempt to create a zero-length key so that we don't
5858 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 if (psa_get_key_bits(attributes) == 0) {
5860 return PSA_ERROR_INVALID_ARGUMENT;
5861 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005862
Gilles Peskine092ce512024-02-20 12:31:24 +01005863 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005864 return PSA_ERROR_INVALID_ARGUMENT;
5865 }
5866
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 if (operation->alg == PSA_ALG_NONE) {
5868 return PSA_ERROR_BAD_STATE;
5869 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 if (!operation->can_output_key) {
5872 return PSA_ERROR_NOT_PERMITTED;
5873 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5876 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005877#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005879 /* Deriving a key in a secure element is not implemented yet. */
5880 status = PSA_ERROR_NOT_SUPPORTED;
5881 }
5882#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 if (status == PSA_SUCCESS) {
5884 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005885 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005887 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 if (status == PSA_SUCCESS) {
5889 status = psa_finish_key_creation(slot, driver, key);
5890 }
5891 if (status != PSA_SUCCESS) {
5892 psa_fail_key_creation(slot, driver);
5893 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005894
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005896}
5897
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005898psa_status_t psa_key_derivation_output_key(
5899 const psa_key_attributes_t *attributes,
5900 psa_key_derivation_operation_t *operation,
5901 mbedtls_svc_key_id_t *key)
5902{
Gilles Peskinec81393b2024-02-14 20:51:28 +01005903 return psa_key_derivation_output_key_ext(attributes, operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005904 &default_production_parameters, 0,
Gilles Peskinec81393b2024-02-14 20:51:28 +01005905 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005906}
Gilles Peskineeab56e42018-07-12 17:12:33 +02005907
5908
5909/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005910/* Key derivation */
5911/****************************************************************/
5912
Steven Cooreman932ffb72021-02-15 12:14:32 +01005913#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005914static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005915{
5916#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5918 return 1;
5919 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005920#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005921#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5923 return 1;
5924 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005925#endif
5926#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5928 return 1;
5929 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005930#endif
5931#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5933 return 1;
5934 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005935#endif
5936#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5938 return 1;
5939 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005940#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005941#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5943 return 1;
5944 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005945#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05305946#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5947 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5948 return 1;
5949 }
5950#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05305951#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
5952 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5953 return 1;
5954 }
5955#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005957}
5958
Gilles Peskine449bd832023-01-11 14:50:10 +01005959static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005960{
5961 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 psa_status_t status = psa_hash_setup(&operation, alg);
5963 psa_hash_abort(&operation);
5964 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005965}
5966
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05305967static psa_status_t psa_key_derivation_set_maximum_capacity(
5968 psa_key_derivation_operation_t *operation,
5969 psa_algorithm_t kdf_alg)
5970{
5971#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5972 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5973 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
5974 return PSA_SUCCESS;
5975 }
5976#endif
5977#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
5978 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5979#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05305980 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05305981 PSA_KEY_TYPE_AES,
5982 128U,
5983 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05305984#else
5985 operation->capacity = SIZE_MAX;
5986#endif
5987 return PSA_SUCCESS;
5988 }
5989#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
5990
5991 /* After this point, if kdf_alg is not valid then value of hash_alg may be
5992 * invalid or meaningless but it does not affect this function */
5993 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
5994 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05305995 if (hash_size == 0) {
5996 return PSA_ERROR_NOT_SUPPORTED;
5997 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05305998
5999 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6000 * we might fail later, which is somewhat unfriendly and potentially
6001 * risk-prone. */
6002 psa_status_t status = psa_hash_try_support(hash_alg);
6003 if (status != PSA_SUCCESS) {
6004 return status;
6005 }
6006
6007#if defined(PSA_WANT_ALG_HKDF)
6008 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6009 operation->capacity = 255 * hash_size;
6010 } else
6011#endif
6012#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6013 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6014 operation->capacity = hash_size;
6015 } else
6016#endif
6017#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6018 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6019 operation->capacity = 255 * hash_size;
6020 } else
6021#endif
6022#if defined(PSA_WANT_ALG_TLS12_PRF)
6023 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6024 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6025 operation->capacity = SIZE_MAX;
6026 } else
6027#endif
6028#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6029 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6030 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6031 /* Master Secret is always 48 bytes
6032 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6033 operation->capacity = 48U;
6034 } else
6035#endif
6036#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6037 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6038#if (SIZE_MAX > UINT32_MAX)
6039 operation->capacity = UINT32_MAX * hash_size;
6040#else
6041 operation->capacity = SIZE_MAX;
6042#endif
6043 } else
6044#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6045 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306046 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306047 status = PSA_ERROR_NOT_SUPPORTED;
6048 }
6049 return status;
6050}
6051
Gilles Peskine969c5d62019-01-16 15:53:06 +01006052static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006053 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006055{
Janos Follath5fe19732019-06-20 15:09:30 +01006056 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6057 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006059
Gilles Peskine969c5d62019-01-16 15:53:06 +01006060 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 if (!is_kdf_alg_supported(kdf_alg)) {
6062 return PSA_ERROR_NOT_SUPPORTED;
6063 }
John Durkop07cc04a2020-11-16 22:08:34 -08006064
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306065 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6066 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306067 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006068}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006069
Gilles Peskine449bd832023-01-11 14:50:10 +01006070static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006071{
6072#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 if (alg == PSA_ALG_ECDH) {
6074 return PSA_SUCCESS;
6075 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006076#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006077#if defined(PSA_WANT_ALG_FFDH)
6078 if (alg == PSA_ALG_FFDH) {
6079 return PSA_SUCCESS;
6080 }
6081#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006082 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006084}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006085
6086static int psa_key_derivation_allows_free_form_secret_input(
6087 psa_algorithm_t kdf_alg)
6088{
6089#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6090 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6091 return 0;
6092 }
6093#endif
6094 (void) kdf_alg;
6095 return 1;
6096}
John Durkop07cc04a2020-11-16 22:08:34 -08006097#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006098
Gilles Peskine449bd832023-01-11 14:50:10 +01006099psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6100 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006101{
6102 psa_status_t status;
6103
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 if (operation->alg != 0) {
6105 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006106 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6109 return PSA_ERROR_INVALID_ARGUMENT;
6110 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6111#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6112 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6113 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6114 status = psa_key_agreement_try_support(ka_alg);
6115 if (status != PSA_SUCCESS) {
6116 return status;
6117 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006118 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6119 return PSA_ERROR_INVALID_ARGUMENT;
6120 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6122#else
6123 return PSA_ERROR_NOT_SUPPORTED;
6124#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6125 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6126#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6127 status = psa_key_derivation_setup_kdf(operation, alg);
6128#else
6129 return PSA_ERROR_NOT_SUPPORTED;
6130#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6131 } else {
6132 return PSA_ERROR_INVALID_ARGUMENT;
6133 }
6134
6135 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006136 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 }
6138 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006139}
6140
Przemek Stekiel69c46792022-06-10 12:59:51 +02006141#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006142static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6143 psa_algorithm_t kdf_alg,
6144 psa_key_derivation_step_t step,
6145 const uint8_t *data,
6146 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006147{
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006149 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006151 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006152#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6154 return PSA_ERROR_INVALID_ARGUMENT;
6155 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006156#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 if (hkdf->state != HKDF_STATE_INIT) {
6158 return PSA_ERROR_BAD_STATE;
6159 } else {
6160 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6161 hash_alg,
6162 data, data_length);
6163 if (status != PSA_SUCCESS) {
6164 return status;
6165 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006166 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006168 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006169 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006170#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006172 /* We shouldn't be in different state as HKDF_EXPAND only allows
6173 * two inputs: SECRET (this case) and INFO which does not modify
6174 * the state. It could happen only if the hkdf
6175 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 if (hkdf->state != HKDF_STATE_INIT) {
6177 return PSA_ERROR_BAD_STATE;
6178 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006179
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006180 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6182 return PSA_ERROR_INVALID_ARGUMENT;
6183 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006184
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 memcpy(hkdf->prk, data, data_length);
6186 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006187#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006188 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006189 /* HKDF: If no salt was provided, use an empty salt.
6190 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006192#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6194 return PSA_ERROR_BAD_STATE;
6195 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006196#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6198 hash_alg,
6199 NULL, 0);
6200 if (status != PSA_SUCCESS) {
6201 return status;
6202 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006203 hkdf->state = HKDF_STATE_STARTED;
6204 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 if (hkdf->state != HKDF_STATE_STARTED) {
6206 return PSA_ERROR_BAD_STATE;
6207 }
6208 status = psa_mac_update(&hkdf->hmac,
6209 data, data_length);
6210 if (status != PSA_SUCCESS) {
6211 return status;
6212 }
6213 status = psa_mac_sign_finish(&hkdf->hmac,
6214 hkdf->prk,
6215 sizeof(hkdf->prk),
6216 &data_length);
6217 if (status != PSA_SUCCESS) {
6218 return status;
6219 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006220 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006221
Gilles Peskine2b522db2019-04-12 15:11:49 +02006222 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006223 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006224#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006226 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006228 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006230#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006231 {
6232 /* Block 0 is empty, and the next block will be
6233 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006235 }
6236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006238 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006239#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6241 return PSA_ERROR_INVALID_ARGUMENT;
6242 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006243#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006244#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6246 hkdf->state == HKDF_STATE_INIT) {
6247 return PSA_ERROR_BAD_STATE;
6248 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006249#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 if (hkdf->state == HKDF_STATE_OUTPUT) {
6251 return PSA_ERROR_BAD_STATE;
6252 }
6253 if (hkdf->info_set) {
6254 return PSA_ERROR_BAD_STATE;
6255 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006256 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 if (data_length != 0) {
6258 hkdf->info = mbedtls_calloc(1, data_length);
6259 if (hkdf->info == NULL) {
6260 return PSA_ERROR_INSUFFICIENT_MEMORY;
6261 }
6262 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006263 }
6264 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006266 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006268 }
6269}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006270#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006271
John Durkop07cc04a2020-11-16 22:08:34 -08006272#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6273 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006274static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6275 const uint8_t *data,
6276 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006277{
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6279 return PSA_ERROR_BAD_STATE;
6280 }
Janos Follathf08e2652019-06-13 09:05:41 +01006281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 if (data_length != 0) {
6283 prf->seed = mbedtls_calloc(1, data_length);
6284 if (prf->seed == NULL) {
6285 return PSA_ERROR_INSUFFICIENT_MEMORY;
6286 }
Janos Follathf08e2652019-06-13 09:05:41 +01006287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006289 prf->seed_length = data_length;
6290 }
Janos Follathf08e2652019-06-13 09:05:41 +01006291
Gilles Peskine43f958b2020-12-13 14:55:14 +01006292 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006295}
6296
Gilles Peskine449bd832023-01-11 14:50:10 +01006297static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6298 const uint8_t *data,
6299 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006300{
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6302 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6303 return PSA_ERROR_BAD_STATE;
6304 }
Janos Follath81550542019-06-13 14:26:34 +01006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 if (data_length != 0) {
6307 prf->secret = mbedtls_calloc(1, data_length);
6308 if (prf->secret == NULL) {
6309 return PSA_ERROR_INSUFFICIENT_MEMORY;
6310 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006313 prf->secret_length = data_length;
6314 }
Janos Follath81550542019-06-13 14:26:34 +01006315
Gilles Peskine43f958b2020-12-13 14:55:14 +01006316 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006319}
6320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6322 const uint8_t *data,
6323 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006324{
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6326 return PSA_ERROR_BAD_STATE;
6327 }
Janos Follath63028dd2019-06-13 09:15:47 +01006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 if (data_length != 0) {
6330 prf->label = mbedtls_calloc(1, data_length);
6331 if (prf->label == NULL) {
6332 return PSA_ERROR_INSUFFICIENT_MEMORY;
6333 }
Janos Follath63028dd2019-06-13 09:15:47 +01006334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006336 prf->label_length = data_length;
6337 }
Janos Follath63028dd2019-06-13 09:15:47 +01006338
Gilles Peskine43f958b2020-12-13 14:55:14 +01006339 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006342}
6343
Gilles Peskine449bd832023-01-11 14:50:10 +01006344static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6345 psa_key_derivation_step_t step,
6346 const uint8_t *data,
6347 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006348{
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006350 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006352 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006354 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006356 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006358 }
6359}
John Durkop07cc04a2020-11-16 22:08:34 -08006360#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6361 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6362
6363#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6364static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6365 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006366 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006368{
6369 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6371 4 + data_length + prf->other_secret_length :
6372 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006373
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6375 return PSA_ERROR_INVALID_ARGUMENT;
6376 }
John Durkop07cc04a2020-11-16 22:08:34 -08006377
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 uint8_t *pms = mbedtls_calloc(1, pms_len);
6379 if (pms == NULL) {
6380 return PSA_ERROR_INSUFFICIENT_MEMORY;
6381 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006382 uint8_t *cur = pms;
6383
6384 /* pure-PSK:
6385 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006386 *
6387 * The premaster secret is formed as follows: if the PSK is N octets
6388 * long, concatenate a uint16 with the value N, N zero octets, a second
6389 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006390 *
6391 * mixed-PSK:
6392 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6393 * follows: concatenate a uint16 with the length of the other secret,
6394 * the other secret itself, uint16 with the length of PSK, and the
6395 * PSK itself.
6396 * For details please check:
6397 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6398 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6399 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006400 */
6401
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6403 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6404 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6405 if (prf->other_secret_length != 0) {
6406 memcpy(cur, prf->other_secret, prf->other_secret_length);
6407 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006408 cur += prf->other_secret_length;
6409 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 } else {
6411 *cur++ = MBEDTLS_BYTE_1(data_length);
6412 *cur++ = MBEDTLS_BYTE_0(data_length);
6413 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006414 cur += data_length;
6415 }
6416
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 *cur++ = MBEDTLS_BYTE_1(data_length);
6418 *cur++ = MBEDTLS_BYTE_0(data_length);
6419 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006420 cur += data_length;
6421
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006422 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006423
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006424 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006426}
Janos Follath6660f0e2019-06-17 08:44:03 +01006427
Przemek Stekiele47201b2022-04-19 13:53:28 +02006428static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6429 psa_tls12_prf_key_derivation_t *prf,
6430 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006432{
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6434 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006435 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006436
6437 if (data_length != 0) {
6438 prf->other_secret = mbedtls_calloc(1, data_length);
6439 if (prf->other_secret == NULL) {
6440 return PSA_ERROR_INSUFFICIENT_MEMORY;
6441 }
6442
6443 memcpy(prf->other_secret, data, data_length);
6444 prf->other_secret_length = data_length;
6445 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006446 prf->other_secret_length = 0;
6447 }
6448
6449 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6450
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006452}
6453
Janos Follath6660f0e2019-06-17 08:44:03 +01006454static psa_status_t psa_tls12_prf_psk_to_ms_input(
6455 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006456 psa_key_derivation_step_t step,
6457 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006459{
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006461 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 return psa_tls12_prf_psk_to_ms_set_key(prf,
6463 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006464 break;
6465 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6467 data,
6468 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006469 break;
6470 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006472 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006473
Przemek Stekiele47201b2022-04-19 13:53:28 +02006474 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006475}
John Durkop07cc04a2020-11-16 22:08:34 -08006476#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006477
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006478#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6479static psa_status_t psa_tls12_ecjpake_to_pms_input(
6480 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006481 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006482 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006484{
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6486 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6487 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006488 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006489
6490 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 if (data[0] != 0x04) {
6492 return PSA_ERROR_INVALID_ARGUMENT;
6493 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006494
6495 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006497
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006499}
6500#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306501
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306502#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306503static psa_status_t psa_pbkdf2_set_input_cost(
6504 psa_pbkdf2_key_derivation_t *pbkdf2,
6505 psa_key_derivation_step_t step,
6506 uint64_t data)
6507{
6508 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6509 return PSA_ERROR_INVALID_ARGUMENT;
6510 }
6511
6512 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6513 return PSA_ERROR_BAD_STATE;
6514 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306515
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306516 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306517 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306518 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306519
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306520 if (data == 0) {
6521 return PSA_ERROR_INVALID_ARGUMENT;
6522 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306523
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306524 pbkdf2->input_cost = data;
6525 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6526
6527 return PSA_SUCCESS;
6528}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306529
6530static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6531 const uint8_t *data,
6532 size_t data_length)
6533{
Gilles Peskineead17662023-08-20 22:02:51 +02006534 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6535 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6536 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6537 /* Appending to existing salt. No state change. */
6538 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306539 return PSA_ERROR_BAD_STATE;
6540 }
6541
Gilles Peskineca57d782023-07-20 19:08:06 +02006542 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006543 /* Appending an empty string, nothing to do. */
6544 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306545 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306546
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306547 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6548 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306549 return PSA_ERROR_INSUFFICIENT_MEMORY;
6550 }
6551
Gilles Peskineead17662023-08-20 22:02:51 +02006552 if (pbkdf2->salt_length != 0) {
6553 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6554 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306555 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306556 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306557 mbedtls_free(pbkdf2->salt);
6558 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306559 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306560 return PSA_SUCCESS;
6561}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306562
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306563#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306564static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306565 const uint8_t *input,
6566 size_t input_len,
6567 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306568 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306569{
6570 psa_status_t status = PSA_SUCCESS;
6571 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6572 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306573 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306574 } else {
6575 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306576 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306577 }
6578 return status;
6579}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306580#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306581
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306582#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306583static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6584 size_t input_len,
6585 uint8_t *output,
6586 size_t *output_len)
6587{
6588 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306589 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306590 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306591 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306592 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6593 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6594 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306595 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306596 * mac_size as the driver function sets mac_output_length = mac_size
6597 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306598 status = psa_driver_wrapper_mac_compute(&attributes,
6599 zeros, sizeof(zeros),
6600 PSA_ALG_CMAC, input, input_len,
6601 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306602 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6603 128U,
6604 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306605 output_len);
6606 } else {
6607 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306608 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306609 }
6610 return status;
6611}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306612#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306613
6614static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306615 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306616 const uint8_t *data,
6617 size_t data_length)
6618{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306619 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306620 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6621 return PSA_ERROR_BAD_STATE;
6622 }
6623
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306624#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306625 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6626 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6627 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6628 pbkdf2->password,
6629 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306630 } else
6631#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6632#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6633 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306634 status = psa_pbkdf2_cmac_set_password(data, data_length,
6635 pbkdf2->password,
6636 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306637 } else
6638#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6639 {
6640 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306641 }
6642
6643 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6644
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306645 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306646}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306647
6648static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306649 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306650 psa_key_derivation_step_t step,
6651 const uint8_t *data,
6652 size_t data_length)
6653{
6654 switch (step) {
6655 case PSA_KEY_DERIVATION_INPUT_SALT:
6656 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6657 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306658 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306659 default:
6660 return PSA_ERROR_INVALID_ARGUMENT;
6661 }
6662}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306663#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306664
Gilles Peskineb8965192019-09-24 16:21:10 +02006665/** Check whether the given key type is acceptable for the given
6666 * input step of a key derivation.
6667 *
6668 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6669 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6670 * Both secret and non-secret inputs can alternatively have the type
6671 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6672 * that the input was passed as a buffer rather than via a key object.
6673 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006674static int psa_key_derivation_check_input_type(
6675 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006677{
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006679 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 if (key_type == PSA_KEY_TYPE_DERIVE) {
6681 return PSA_SUCCESS;
6682 }
6683 if (key_type == PSA_KEY_TYPE_NONE) {
6684 return PSA_SUCCESS;
6685 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006686 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006687 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 if (key_type == PSA_KEY_TYPE_DERIVE) {
6689 return PSA_SUCCESS;
6690 }
6691 if (key_type == PSA_KEY_TYPE_NONE) {
6692 return PSA_SUCCESS;
6693 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006694 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006695 case PSA_KEY_DERIVATION_INPUT_LABEL:
6696 case PSA_KEY_DERIVATION_INPUT_SALT:
6697 case PSA_KEY_DERIVATION_INPUT_INFO:
6698 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
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;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306706 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6707 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6708 return PSA_SUCCESS;
6709 }
6710 if (key_type == PSA_KEY_TYPE_DERIVE) {
6711 return PSA_SUCCESS;
6712 }
6713 if (key_type == PSA_KEY_TYPE_NONE) {
6714 return PSA_SUCCESS;
6715 }
6716 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006717 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006719}
6720
Janos Follathb80a94e2019-06-12 15:54:46 +01006721static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006722 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006723 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006724 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006725 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006726 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006727{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006728 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006730
Gilles Peskine449bd832023-01-11 14:50:10 +01006731 status = psa_key_derivation_check_input_type(step, key_type);
6732 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006733 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006735
Przemek Stekiel69c46792022-06-10 12:59:51 +02006736#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006737 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6738 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6739 step, data, data_length);
6740 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006741#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006742#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6744 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6745 step, data, data_length);
6746 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006747#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6748#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6750 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6751 step, data, data_length);
6752 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006753#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006754#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006755 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006756 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006757 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6758 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006759#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306760#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306761 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306762 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6763 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306764 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306765#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006766 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006767 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006768 (void) data;
6769 (void) data_length;
6770 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006771 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006772 }
6773
Gilles Peskine224b0d62019-09-23 18:13:17 +02006774exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 if (status != PSA_SUCCESS) {
6776 psa_key_derivation_abort(operation);
6777 }
6778 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006779}
6780
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306781static psa_status_t psa_key_derivation_input_integer_internal(
6782 psa_key_derivation_operation_t *operation,
6783 psa_key_derivation_step_t step,
6784 uint64_t value)
6785{
6786 psa_status_t status;
6787 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6788
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306789#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306790 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306791 status = psa_pbkdf2_set_input_cost(
6792 &operation->ctx.pbkdf2, step, value);
6793 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306794#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306795 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306796 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306797 (void) value;
6798 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306799 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306800 }
6801
6802 if (status != PSA_SUCCESS) {
6803 psa_key_derivation_abort(operation);
6804 }
6805 return status;
6806}
6807
Janos Follath51f4a0f2019-06-14 11:35:55 +01006808psa_status_t psa_key_derivation_input_bytes(
6809 psa_key_derivation_operation_t *operation,
6810 psa_key_derivation_step_t step,
6811 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006813{
Gilles Peskine449bd832023-01-11 14:50:10 +01006814 return psa_key_derivation_input_internal(operation, step,
6815 PSA_KEY_TYPE_NONE,
6816 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006817}
6818
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306819psa_status_t psa_key_derivation_input_integer(
6820 psa_key_derivation_operation_t *operation,
6821 psa_key_derivation_step_t step,
6822 uint64_t value)
6823{
6824 return psa_key_derivation_input_integer_internal(operation, step, value);
6825}
6826
Janos Follath51f4a0f2019-06-14 11:35:55 +01006827psa_status_t psa_key_derivation_input_key(
6828 psa_key_derivation_operation_t *operation,
6829 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006831{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006832 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006833 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006834 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006835
Ronald Cron5c522922020-11-14 16:35:34 +01006836 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006837 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6838 if (status != PSA_SUCCESS) {
6839 psa_key_derivation_abort(operation);
6840 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006841 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006842
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306843 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6844 * permission to output to a key object. */
6845 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6846 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006847 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006849
Gilles Peskine449bd832023-01-11 14:50:10 +01006850 status = psa_key_derivation_input_internal(operation,
6851 step, slot->attr.type,
6852 slot->key.data,
6853 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006854
Ryan Everett5ac6fa72024-02-14 17:11:36 +00006855 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006856
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006858}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006859
6860
6861
6862/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006863/* Key agreement */
6864/****************************************************************/
6865
Gilles Peskine449bd832023-01-11 14:50:10 +01006866psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6867 const uint8_t *key_buffer,
6868 size_t key_buffer_size,
6869 psa_algorithm_t alg,
6870 const uint8_t *peer_key,
6871 size_t peer_key_length,
6872 uint8_t *shared_secret,
6873 size_t shared_secret_size,
6874 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006875{
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006877#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006878 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6880 key_buffer_size, alg,
6881 peer_key, peer_key_length,
6882 shared_secret,
6883 shared_secret_size,
6884 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006885#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006886
6887#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6888 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006889 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006890 peer_key,
6891 peer_key_length,
6892 key_buffer,
6893 key_buffer_size,
6894 shared_secret,
6895 shared_secret_size,
6896 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006897#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6898
Gilles Peskine01d718c2018-09-18 12:01:02 +02006899 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006900 (void) attributes;
6901 (void) key_buffer;
6902 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006903 (void) peer_key;
6904 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006905 (void) shared_secret;
6906 (void) shared_secret_size;
6907 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006909 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006910}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006911
Aditya Deshpande17845b82022-10-13 17:21:01 +01006912/** Internal function for raw key agreement
6913 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006914 * to the driver's implementation if a driver is present.
6915 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006916 * (psa_key_agreement_raw_builtin).
6917 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006918static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6919 psa_key_slot_t *private_key,
6920 const uint8_t *peer_key,
6921 size_t peer_key_length,
6922 uint8_t *shared_secret,
6923 size_t shared_secret_size,
6924 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006925{
Gilles Peskine449bd832023-01-11 14:50:10 +01006926 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6927 return PSA_ERROR_NOT_SUPPORTED;
6928 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006929
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006930 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006931 private_key->key.data,
6932 private_key->key.bytes, alg,
6933 peer_key, peer_key_length,
6934 shared_secret,
6935 shared_secret_size,
6936 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006937}
6938
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006939/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006940 * to potentially free embedded data structures and wipe confidential data.
6941 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006942static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6943 psa_key_derivation_step_t step,
6944 psa_key_slot_t *private_key,
6945 const uint8_t *peer_key,
6946 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006947{
6948 psa_status_t status;
Moritz Fischer967f8cd2024-03-02 00:55:06 +00006949 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
Gilles Peskine01d718c2018-09-18 12:01:02 +02006950 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006952
6953 /* Step 1: run the secret agreement algorithm to generate the shared
6954 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006955 status = psa_key_agreement_raw_internal(ka_alg,
6956 private_key,
6957 peer_key, peer_key_length,
6958 shared_secret,
6959 sizeof(shared_secret),
6960 &shared_secret_length);
6961 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006962 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006964
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006965 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006966 * the shared secret. A shared secret is permitted wherever a key
6967 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006968 status = psa_key_derivation_input_internal(operation, step,
6969 PSA_KEY_TYPE_DERIVE,
6970 shared_secret,
6971 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006972exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6974 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006975}
6976
Gilles Peskine449bd832023-01-11 14:50:10 +01006977psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6978 psa_key_derivation_step_t step,
6979 mbedtls_svc_key_id_t private_key,
6980 const uint8_t *peer_key,
6981 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006982{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006983 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006984 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006985 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006986
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6988 return PSA_ERROR_INVALID_ARGUMENT;
6989 }
Ronald Cron5c522922020-11-14 16:35:34 +01006990 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006991 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6992 if (status != PSA_SUCCESS) {
6993 return status;
6994 }
6995 status = psa_key_agreement_internal(operation, step,
6996 slot,
6997 peer_key, peer_key_length);
6998 if (status != PSA_SUCCESS) {
6999 psa_key_derivation_abort(operation);
7000 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007001 /* If a private key has been added as SECRET, we allow the derived
7002 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007004 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007006 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007007
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007008 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007009
Gilles Peskine449bd832023-01-11 14:50:10 +01007010 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007011}
7012
Gilles Peskine449bd832023-01-11 14:50:10 +01007013psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7014 mbedtls_svc_key_id_t private_key,
7015 const uint8_t *peer_key,
7016 size_t peer_key_length,
7017 uint8_t *output,
7018 size_t output_size,
7019 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007020{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007021 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007022 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007023 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007024 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007025
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007027 status = PSA_ERROR_INVALID_ARGUMENT;
7028 goto exit;
7029 }
Ronald Cron5c522922020-11-14 16:35:34 +01007030 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7032 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007033 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007035
Gilles Peskine3e561302022-04-14 00:17:15 +02007036 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7037 * for the output size. The PSA specification only guarantees that this
7038 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7039 * but it might be nice to allow smaller buffers if the output fits.
7040 * At the time of writing this comment, with only ECDH implemented,
7041 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7042 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7043 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007044 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7046 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007047 status = PSA_ERROR_BUFFER_TOO_SMALL;
7048 goto exit;
7049 }
7050
Gilles Peskine449bd832023-01-11 14:50:10 +01007051 status = psa_key_agreement_raw_internal(alg, slot,
7052 peer_key, peer_key_length,
7053 output, output_size,
7054 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007055
7056exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007058 /* If an error happens and is not handled properly, the output
7059 * may be used as a key to protect sensitive data. Arrange for such
7060 * a key to be random, which is likely to result in decryption or
7061 * verification errors. This is better than filling the buffer with
7062 * some constant data such as zeros, which would result in the data
7063 * being protected with a reproducible, easily knowable key.
7064 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007066 *output_length = output_size;
7067 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007068
Ryan Everetta103ec92024-01-31 13:59:57 +00007069 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007070
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007072}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007073
7074
Gilles Peskine30524eb2020-11-13 17:02:26 +01007075
Gilles Peskine86a440b2018-11-12 18:39:40 +01007076/****************************************************************/
7077/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007078/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007079
Gilles Peskine672a7712023-04-28 21:00:28 +02007080#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7081#include "entropy_poll.h"
7082#endif
7083
Gilles Peskine30524eb2020-11-13 17:02:26 +01007084/** Initialize the PSA random generator.
7085 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007086static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007087{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007088#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007090#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7091
Gilles Peskine30524eb2020-11-13 17:02:26 +01007092 /* Set default configuration if
7093 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007094 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007095 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007096 }
7097 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007098 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007099 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007100
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007102#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7103 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7104 /* The PSA entropy injection feature depends on using NV seed as an entropy
7105 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 mbedtls_entropy_add_source(&rng->entropy,
7107 mbedtls_nv_seed_poll, NULL,
7108 MBEDTLS_ENTROPY_BLOCK_SIZE,
7109 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007110#endif
7111
Valerio Setti061d4e42024-02-26 12:52:44 +01007112 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007113#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007114}
7115
7116/** Deinitialize the PSA random generator.
7117 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007118static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007119{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007120#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007121 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007122#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01007123 mbedtls_psa_drbg_free(&rng->drbg);
Valerio Setti83e0de82023-11-24 12:13:05 +01007124 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007125#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007126}
7127
7128/** Seed the PSA random generator.
7129 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007130static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007131{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007132#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7133 /* Do nothing: the external RNG seeds itself. */
7134 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007136#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007137 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01007138 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 drbg_seed, sizeof(drbg_seed) - 1);
7140 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007141#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007142}
7143
Gilles Peskine449bd832023-01-11 14:50:10 +01007144psa_status_t psa_generate_random(uint8_t *output,
7145 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007146{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007147 GUARD_MODULE_INITIALIZED;
7148
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007149#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7150
7151 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7153 output, output_size,
7154 &output_length);
7155 if (status != PSA_SUCCESS) {
7156 return status;
7157 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007158 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007159 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007160 if (output_length != output_size) {
7161 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7162 }
7163 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007164
7165#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7166
Gilles Peskine449bd832023-01-11 14:50:10 +01007167 while (output_size > 0) {
Valerio Setti718180c2024-02-27 11:58:39 +01007168 int ret = MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
Gilles Peskine71ddab92021-01-04 21:01:07 +01007169 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7171 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7172 output_size);
Valerio Setti718180c2024-02-27 11:58:39 +01007173#if defined(MBEDTLS_CTR_DRBG_C)
7174 ret = mbedtls_ctr_drbg_random(&global_data.rng.drbg, output, request_size);
7175#elif defined(MBEDTLS_HMAC_DRBG_C)
7176 ret = mbedtls_hmac_drbg_random(&global_data.rng.drbg, output, request_size);
7177#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 if (ret != 0) {
7179 return mbedtls_to_psa_error(ret);
7180 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007181 output_size -= request_size;
7182 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007183 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007185#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007186}
7187
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007188#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007189psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7190 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007191{
Gilles Peskine449bd832023-01-11 14:50:10 +01007192 if (global_data.initialized) {
7193 return PSA_ERROR_NOT_PERMITTED;
7194 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007195
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7197 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7198 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7199 return PSA_ERROR_INVALID_ARGUMENT;
7200 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007201
Gilles Peskine449bd832023-01-11 14:50:10 +01007202 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007203}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007204#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007205
Ronald Cron3772afe2021-02-08 16:10:05 +01007206/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007207 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007208 * \param type The key type
7209 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007210 *
7211 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007212 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007213 * \retval #PSA_ERROR_INVALID_ARGUMENT
7214 * The size in bits of the key is not valid.
7215 * \retval #PSA_ERROR_NOT_SUPPORTED
7216 * The type and/or the size in bits of the key or the combination of
7217 * the two is not supported.
7218 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007219static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007221{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007222 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007223
Gilles Peskine449bd832023-01-11 14:50:10 +01007224 if (key_type_is_raw_bytes(type)) {
7225 status = psa_validate_unstructured_key_bit_size(type, bits);
7226 if (status != PSA_SUCCESS) {
7227 return status;
7228 }
7229 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007230#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7232 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7233 return PSA_ERROR_NOT_SUPPORTED;
7234 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007235 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007236 return PSA_ERROR_NOT_SUPPORTED;
7237 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007238
Ronald Cron01b2aba2020-10-05 09:42:02 +02007239 /* Accept only byte-aligned keys, for the same reasons as
7240 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007241 if (bits % 8 != 0) {
7242 return PSA_ERROR_NOT_SUPPORTED;
7243 }
7244 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007245#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007246
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007247#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007248 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007249 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007250 return PSA_SUCCESS;
7251 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007252#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007253
Valerio Settia55f0422023-07-10 15:34:41 +02007254#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007255 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007256 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007257 return PSA_ERROR_NOT_SUPPORTED;
7258 }
7259 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007260#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007261 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007263 }
7264
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007266}
7267
Ronald Cron55ed0592020-10-05 10:30:40 +02007268psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007269 const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007270 const psa_key_production_parameters_t *params, size_t params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007272{
7273 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007274 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007275
Gilles Peskine7a18f962024-02-12 16:48:11 +01007276 /* Only used for RSA */
Gilles Peskine092ce512024-02-20 12:31:24 +01007277 (void) params;
7278 (void) params_data_length;
Gilles Peskine7a18f962024-02-12 16:48:11 +01007279
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 if (key_type_is_raw_bytes(type)) {
7281 status = psa_generate_random(key_buffer, key_buffer_size);
7282 if (status != PSA_SUCCESS) {
7283 return status;
7284 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007285
David Brown12ca5032021-02-11 11:02:00 -07007286#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 if (type == PSA_KEY_TYPE_DES) {
7288 psa_des_set_key_parity(key_buffer, key_buffer_size);
7289 }
David Brown8107e312021-02-12 08:08:46 -07007290#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007292
Valerio Setti76df8c12023-07-11 14:11:28 +02007293#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007294 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskinee22f6a92024-02-26 15:45:33 +01007295 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskine4c32b692024-02-16 00:11:09 +01007296 params, params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007297 key_buffer,
7298 key_buffer_size,
7299 key_buffer_length);
7300 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007301#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007302
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007303#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007304 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7305 return mbedtls_psa_ecp_generate_key(attributes,
7306 key_buffer,
7307 key_buffer_size,
7308 key_buffer_length);
7309 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007310#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007311
Valerio Settia55f0422023-07-10 15:34:41 +02007312#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007313 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7314 return mbedtls_psa_ffdh_generate_key(attributes,
7315 key_buffer,
7316 key_buffer_size,
7317 key_buffer_length);
7318 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007319#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007320 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 (void) key_buffer_length;
7322 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007323 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007324
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007326}
Darryl Green0c6575a2018-11-07 16:05:30 +00007327
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007328psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007329 const psa_key_production_parameters_t *params,
7330 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007331 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007332{
7333 psa_status_t status;
7334 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007335 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007336 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007337
Ronald Cron81709fc2020-11-14 12:10:32 +01007338 *key = MBEDTLS_SVC_KEY_ID_INIT;
7339
Gilles Peskine0f84d622019-09-12 19:03:13 +02007340 /* Reject any attempt to create a zero-length key so that we don't
7341 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007342 if (psa_get_key_bits(attributes) == 0) {
7343 return PSA_ERROR_INVALID_ARGUMENT;
7344 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007345
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007346 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007347 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007348 return PSA_ERROR_INVALID_ARGUMENT;
7349 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007350
Gilles Peskine7a18f962024-02-12 16:48:11 +01007351#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007352 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +01007353 if (params->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01007354 return PSA_ERROR_INVALID_ARGUMENT;
7355 }
7356 } else
7357#endif
Gilles Peskine092ce512024-02-20 12:31:24 +01007358 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007359 return PSA_ERROR_INVALID_ARGUMENT;
7360 }
7361
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7363 &slot, &driver);
7364 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007365 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007366 }
Gilles Peskine11792082019-08-06 18:36:36 +02007367
Ronald Cron977c2472020-10-13 08:32:21 +02007368 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307369 * storage ( thus not in the case of generating a key in a secure element
7370 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7371 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007372 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007373 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01007374 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007375 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007376 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007378 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007379 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007380
7381 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007382 attributes->type,
7383 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007384 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007385 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007386 attributes, &key_buffer_size);
7387 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007388 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007389 }
Ronald Cron977c2472020-10-13 08:32:21 +02007390 }
Ronald Cron977c2472020-10-13 08:32:21 +02007391
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7393 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007394 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 }
Ronald Cron977c2472020-10-13 08:32:21 +02007396 }
7397
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007399 params, params_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01007400 slot->key.data, slot->key.bytes,
7401 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01007402 if (status != PSA_SUCCESS) {
7403 psa_remove_key_data_from_memory(slot);
7404 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007405
Gilles Peskine11792082019-08-06 18:36:36 +02007406exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007407 if (status == PSA_SUCCESS) {
7408 status = psa_finish_key_creation(slot, driver, key);
7409 }
7410 if (status != PSA_SUCCESS) {
7411 psa_fail_key_creation(slot, driver);
7412 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007413
Gilles Peskine449bd832023-01-11 14:50:10 +01007414 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007415}
7416
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007417psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7418 mbedtls_svc_key_id_t *key)
7419{
7420 return psa_generate_key_ext(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007421 &default_production_parameters, 0,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007422 key);
7423}
7424
Gilles Peskinee59236f2018-01-27 23:32:46 +01007425/****************************************************************/
7426/* Module setup */
7427/****************************************************************/
7428
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007429#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007430psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 void (* entropy_init)(mbedtls_entropy_context *ctx),
7432 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007433{
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7435 return PSA_ERROR_BAD_STATE;
7436 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007437 global_data.rng.entropy_init = entropy_init;
7438 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007440}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007441#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007442
Gilles Peskine449bd832023-01-11 14:50:10 +01007443void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007444{
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 psa_wipe_all_key_slots();
Valerio Setti83e0de82023-11-24 12:13:05 +01007446 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7447 mbedtls_psa_random_free(&global_data.rng);
7448 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007449 /* Wipe all remaining data, including configuration.
7450 * In particular, this sets all state indicator to the value
7451 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007452 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007453
7454 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007456}
7457
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007458#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7459/** Recover a transaction that was interrupted by a power failure.
7460 *
7461 * This function is called during initialization, before psa_crypto_init()
7462 * returns. If this function returns a failure status, the initialization
7463 * fails.
7464 */
7465static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007466 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007467{
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007469 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7470 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 /* TODO - fall through to the failure case until this
7472 * is implemented.
7473 * https://github.com/ARMmbed/mbed-crypto/issues/218
7474 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007475 default:
7476 /* We found an unsupported transaction in the storage.
7477 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007479 }
7480}
7481#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7482
Gilles Peskine449bd832023-01-11 14:50:10 +01007483psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007484{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007485 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007486
Gilles Peskinec6b69072018-11-20 21:42:52 +01007487 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007488 if (global_data.initialized != 0) {
7489 return PSA_SUCCESS;
7490 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007491
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007492 /* Init drivers */
7493 status = psa_driver_wrapper_init();
7494 if (status != PSA_SUCCESS) {
7495 goto exit;
7496 }
7497 global_data.drivers_initialized = 1;
7498
Valerio Setti402cfba2023-11-13 10:24:32 +01007499 status = psa_initialize_key_slots();
7500 if (status != PSA_SUCCESS) {
7501 goto exit;
7502 }
7503
Gilles Peskine30524eb2020-11-13 17:02:26 +01007504 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007505 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007506 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007507 status = mbedtls_psa_random_seed(&global_data.rng);
7508 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007509 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007511 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007512
Gilles Peskine4b734222019-07-24 15:56:31 +02007513#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007514 status = psa_crypto_load_transaction();
7515 if (status == PSA_SUCCESS) {
7516 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7517 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007518 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007519 }
7520 status = psa_crypto_stop_transaction();
7521 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007522 /* There's no transaction to complete. It's all good. */
7523 status = PSA_SUCCESS;
7524 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007525#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007526
Gilles Peskinec6b69072018-11-20 21:42:52 +01007527 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007528 global_data.initialized = 1;
7529
7530exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 if (status != PSA_SUCCESS) {
7532 mbedtls_psa_crypto_free();
7533 }
7534 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007535}
7536
Yanray Wangffc3c482023-07-11 12:01:04 +08007537#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007538psa_status_t psa_crypto_driver_pake_get_password_len(
7539 const psa_crypto_driver_pake_inputs_t *inputs,
7540 size_t *password_len)
7541{
7542 if (inputs->password_len == 0) {
7543 return PSA_ERROR_BAD_STATE;
7544 }
7545
7546 *password_len = inputs->password_len;
7547
7548 return PSA_SUCCESS;
7549}
7550
7551psa_status_t psa_crypto_driver_pake_get_password(
7552 const psa_crypto_driver_pake_inputs_t *inputs,
7553 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7554{
7555 if (inputs->password_len == 0) {
7556 return PSA_ERROR_BAD_STATE;
7557 }
7558
7559 if (buffer_size < inputs->password_len) {
7560 return PSA_ERROR_BUFFER_TOO_SMALL;
7561 }
7562
7563 memcpy(buffer, inputs->password, inputs->password_len);
7564 *buffer_length = inputs->password_len;
7565
7566 return PSA_SUCCESS;
7567}
7568
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007569psa_status_t psa_crypto_driver_pake_get_user_len(
7570 const psa_crypto_driver_pake_inputs_t *inputs,
7571 size_t *user_len)
7572{
7573 if (inputs->user_len == 0) {
7574 return PSA_ERROR_BAD_STATE;
7575 }
7576
7577 *user_len = inputs->user_len;
7578
7579 return PSA_SUCCESS;
7580}
7581
7582psa_status_t psa_crypto_driver_pake_get_user(
7583 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007584 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007585{
7586 if (inputs->user_len == 0) {
7587 return PSA_ERROR_BAD_STATE;
7588 }
7589
Przemek Stekielc0e62502023-03-14 11:49:36 +01007590 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007591 return PSA_ERROR_BUFFER_TOO_SMALL;
7592 }
7593
Przemek Stekielc0e62502023-03-14 11:49:36 +01007594 memcpy(user_id, inputs->user, inputs->user_len);
7595 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007596
7597 return PSA_SUCCESS;
7598}
7599
7600psa_status_t psa_crypto_driver_pake_get_peer_len(
7601 const psa_crypto_driver_pake_inputs_t *inputs,
7602 size_t *peer_len)
7603{
7604 if (inputs->peer_len == 0) {
7605 return PSA_ERROR_BAD_STATE;
7606 }
7607
7608 *peer_len = inputs->peer_len;
7609
7610 return PSA_SUCCESS;
7611}
7612
7613psa_status_t psa_crypto_driver_pake_get_peer(
7614 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007615 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007616{
7617 if (inputs->peer_len == 0) {
7618 return PSA_ERROR_BAD_STATE;
7619 }
7620
Przemek Stekielc0e62502023-03-14 11:49:36 +01007621 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007622 return PSA_ERROR_BUFFER_TOO_SMALL;
7623 }
7624
Przemek Stekielc0e62502023-03-14 11:49:36 +01007625 memcpy(peer_id, inputs->peer, inputs->peer_len);
7626 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007627
7628 return PSA_SUCCESS;
7629}
7630
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007631psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7632 const psa_crypto_driver_pake_inputs_t *inputs,
7633 psa_pake_cipher_suite_t *cipher_suite)
7634{
7635 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7636 return PSA_ERROR_BAD_STATE;
7637 }
7638
7639 *cipher_suite = inputs->cipher_suite;
7640
7641 return PSA_SUCCESS;
7642}
7643
Neil Armstronga7d08c32022-06-01 18:21:20 +02007644psa_status_t psa_pake_setup(
7645 psa_pake_operation_t *operation,
7646 const psa_pake_cipher_suite_t *cipher_suite)
7647{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007648 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007649
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007650 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007651 status = PSA_ERROR_BAD_STATE;
7652 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007653 }
7654
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007655 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007656 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007657 status = PSA_ERROR_INVALID_ARGUMENT;
7658 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007659 }
7660
Przemek Stekiel51eac532022-12-07 11:04:51 +01007661 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7662
Przemek Stekiele12ed362022-12-21 12:54:46 +01007663 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007664 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7665 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007666 operation->data.inputs.cipher_suite = *cipher_suite;
7667
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007668#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007669 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007670 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007671 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007672
David Horstmann16f01512023-06-14 17:21:07 +01007673 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007674 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007675 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007676#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007677 {
7678 status = PSA_ERROR_NOT_SUPPORTED;
7679 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007680 }
7681
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007682 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7683
Przemek Stekiel51eac532022-12-07 11:04:51 +01007684 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007685exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007686 psa_pake_abort(operation);
7687 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007688}
7689
7690psa_status_t psa_pake_set_password_key(
7691 psa_pake_operation_t *operation,
7692 mbedtls_svc_key_id_t password)
7693{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007694 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007695 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7696 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007697 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007698
Przemek Stekiel51eac532022-12-07 11:04:51 +01007699 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007700 status = PSA_ERROR_BAD_STATE;
7701 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007702 }
7703
Przemek Stekiel6c764412022-11-22 14:05:12 +01007704 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7705 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007706 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007707 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007708 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007709 }
7710
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007711 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007712
Przemek Stekiel51eac532022-12-07 11:04:51 +01007713 if (type != PSA_KEY_TYPE_PASSWORD &&
7714 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7715 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007716 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007717 }
7718
Przemek Stekiel51eac532022-12-07 11:04:51 +01007719 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7720 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007721 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007722 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007723 }
7724
7725 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7726 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01007727 operation->data.inputs.attributes = slot->attr;
7728
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007729exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007730 if (status != PSA_SUCCESS) {
7731 psa_pake_abort(operation);
7732 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00007733 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007734 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007735}
7736
7737psa_status_t psa_pake_set_user(
7738 psa_pake_operation_t *operation,
7739 const uint8_t *user_id,
7740 size_t user_id_len)
7741{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007742 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007743
7744 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007745 status = PSA_ERROR_BAD_STATE;
7746 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007747 }
7748
Przemek Stekiel51eac532022-12-07 11:04:51 +01007749 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007750 status = PSA_ERROR_INVALID_ARGUMENT;
7751 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007752 }
7753
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007754 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007755 status = PSA_ERROR_BAD_STATE;
7756 goto exit;
7757 }
7758
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007759 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7760 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007761 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7762 goto exit;
7763 }
7764
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007765 memcpy(operation->data.inputs.user, user_id, user_id_len);
7766 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007767
7768 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007769exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007770 psa_pake_abort(operation);
7771 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007772}
7773
7774psa_status_t psa_pake_set_peer(
7775 psa_pake_operation_t *operation,
7776 const uint8_t *peer_id,
7777 size_t peer_id_len)
7778{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007779 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007780
7781 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007782 status = PSA_ERROR_BAD_STATE;
7783 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007784 }
7785
Przemek Stekiel51eac532022-12-07 11:04:51 +01007786 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007787 status = PSA_ERROR_INVALID_ARGUMENT;
7788 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007789 }
7790
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007791 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007792 status = PSA_ERROR_BAD_STATE;
7793 goto exit;
7794 }
7795
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007796 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7797 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007798 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7799 goto exit;
7800 }
7801
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007802 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7803 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007804
7805 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007806exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007807 psa_pake_abort(operation);
7808 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007809}
7810
7811psa_status_t psa_pake_set_role(
7812 psa_pake_operation_t *operation,
7813 psa_pake_role_t role)
7814{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007815 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007816
Przemek Stekiel51eac532022-12-07 11:04:51 +01007817 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007818 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007819 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007820 }
7821
Przemek Stekiel09104b82023-03-06 14:11:51 +01007822 switch (operation->alg) {
7823#if defined(PSA_WANT_ALG_JPAKE)
7824 case PSA_ALG_JPAKE:
7825 if (role == PSA_PAKE_ROLE_NONE) {
7826 return PSA_SUCCESS;
7827 }
7828 status = PSA_ERROR_INVALID_ARGUMENT;
7829 break;
7830#endif
7831 default:
7832 (void) role;
7833 status = PSA_ERROR_NOT_SUPPORTED;
7834 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007835 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007836exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007837 psa_pake_abort(operation);
7838 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007839}
7840
David Horstmanne7f21e62023-05-12 18:17:21 +01007841/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007842#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007843static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007844 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007845{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007846 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007847 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007848 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007849
David Horstmann024e5c52023-06-14 15:48:21 +01007850 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007851 is_x1 = (stage->outputs < 1);
7852 } else {
7853 is_x1 = (stage->inputs < 1);
7854 }
7855
David Horstmann74a3d8c2023-06-14 18:28:19 +01007856 key_share_step = is_x1 ?
7857 PSA_JPAKE_X1_STEP_KEY_SHARE :
7858 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007859 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007860 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7861 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7862 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7863 } else {
7864 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007865 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01007866 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01007867}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007868#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007869
Przemek Stekiel2797d372022-12-22 11:19:22 +01007870static psa_status_t psa_pake_complete_inputs(
7871 psa_pake_operation_t *operation)
7872{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007873 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007874 /* Create copy of the inputs on stack as inputs share memory
7875 with the driver context which will be setup by the driver. */
7876 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007877
Przemek Stekielfde11282023-03-13 16:06:09 +01007878 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007879 return PSA_ERROR_BAD_STATE;
7880 }
7881
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007882 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007883 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7884 return PSA_ERROR_BAD_STATE;
7885 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007886 }
7887
Przemek Stekiel18620a32023-01-17 16:34:52 +01007888 /* Clear driver context */
7889 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7890
7891 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007892
7893 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01007894 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007895
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007896 /* User and peer are translated to role. */
7897 mbedtls_free(inputs.user);
7898 mbedtls_free(inputs.peer);
7899
Przemek Stekiel2797d372022-12-22 11:19:22 +01007900 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007901#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007902 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007903 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007904 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007905#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007906 {
7907 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007908 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007909 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007910 return status;
7911}
7912
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007913#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01007914static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01007915 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01007916 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01007917 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007918{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007919 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7920 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7921 step != PSA_PAKE_STEP_ZK_PROOF) {
7922 return PSA_ERROR_INVALID_ARGUMENT;
7923 }
7924
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007925 psa_jpake_computation_stage_t *computation_stage =
7926 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007927
David Horstmann5da95602023-06-08 15:37:12 +01007928 if (computation_stage->round != PSA_JPAKE_FIRST &&
7929 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007930 return PSA_ERROR_BAD_STATE;
7931 }
7932
David Horstmanne7f21e62023-05-12 18:17:21 +01007933 /* Check that the step we are given is the one we were expecting */
7934 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007935 return PSA_ERROR_BAD_STATE;
7936 }
7937
David Horstmanne7f21e62023-05-12 18:17:21 +01007938 if (step == PSA_PAKE_STEP_KEY_SHARE &&
7939 computation_stage->inputs == 0 &&
7940 computation_stage->outputs == 0) {
7941 /* Start of the round, so function decides whether we are inputting
7942 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01007943 computation_stage->io_mode = io_mode;
7944 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007945 /* Middle of the round so the mode we are in must match the function
7946 * called by the user */
7947 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007948 }
7949
7950 return PSA_SUCCESS;
7951}
7952
David Horstmanne7f21e62023-05-12 18:17:21 +01007953static psa_status_t psa_jpake_epilogue(
7954 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01007955 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007956{
David Horstmanne7f21e62023-05-12 18:17:21 +01007957 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007958 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007959
David Horstmanne7f21e62023-05-12 18:17:21 +01007960 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
7961 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01007962 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007963 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01007964 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01007965 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01007966 }
7967 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01007968 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007969 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01007970 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01007971 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01007972 }
7973 }
David Horstmann246ec5a2023-06-27 10:33:06 +01007974 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
7975 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007976 /* End of a round, move to the next round */
7977 stage->inputs = 0;
7978 stage->outputs = 0;
7979 stage->round++;
7980 }
7981 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007982 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01007983 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007984 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01007985 return PSA_SUCCESS;
7986}
David Horstmanne7f21e62023-05-12 18:17:21 +01007987
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007988#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007989
Neil Armstronga7d08c32022-06-01 18:21:20 +02007990psa_status_t psa_pake_output(
7991 psa_pake_operation_t *operation,
7992 psa_pake_step_t step,
7993 uint8_t *output,
7994 size_t output_size,
7995 size_t *output_length)
7996{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007997 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007998 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007999 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008000
8001 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008002 status = psa_pake_complete_inputs(operation);
8003 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008004 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008005 }
8006 }
8007
8008 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008009 status = PSA_ERROR_BAD_STATE;
8010 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008011 }
8012
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008013 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008014 status = PSA_ERROR_INVALID_ARGUMENT;
8015 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008016 }
8017
Przemek Stekiele12ed362022-12-21 12:54:46 +01008018 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008019#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008020 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008021 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008022 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008023 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008024 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008025 driver_step = convert_jpake_computation_stage_to_driver_step(
8026 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008027 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008028#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008029 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008030 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008031 status = PSA_ERROR_NOT_SUPPORTED;
8032 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008033 }
8034
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008035 status = psa_driver_wrapper_pake_output(operation, driver_step,
8036 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008037
8038 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008039 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008040 }
8041
8042 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008043#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008044 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008045 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008046 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008047 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008048 }
8049 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008050#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008051 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008052 status = PSA_ERROR_NOT_SUPPORTED;
8053 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008054 }
8055
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008056 return PSA_SUCCESS;
8057exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008058 psa_pake_abort(operation);
8059 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008060}
8061
8062psa_status_t psa_pake_input(
8063 psa_pake_operation_t *operation,
8064 psa_pake_step_t step,
8065 const uint8_t *input,
8066 size_t input_length)
8067{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008068 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008069 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008070 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8071 operation->primitive,
8072 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008073
8074 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008075 status = psa_pake_complete_inputs(operation);
8076 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008077 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008078 }
8079 }
8080
8081 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008082 status = PSA_ERROR_BAD_STATE;
8083 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008084 }
8085
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008086 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008087 status = PSA_ERROR_INVALID_ARGUMENT;
8088 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008089 }
8090
Przemek Stekiele12ed362022-12-21 12:54:46 +01008091 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008092#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008093 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008094 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008095 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008096 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008097 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008098 driver_step = convert_jpake_computation_stage_to_driver_step(
8099 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008100 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008101#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008102 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008103 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008104 status = PSA_ERROR_NOT_SUPPORTED;
8105 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008106 }
8107
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008108 status = psa_driver_wrapper_pake_input(operation, driver_step,
8109 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008110
8111 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008112 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008113 }
8114
8115 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008116#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008117 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008118 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008119 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008120 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008121 }
8122 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008123#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008124 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008125 status = PSA_ERROR_NOT_SUPPORTED;
8126 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008127 }
8128
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008129 return PSA_SUCCESS;
8130exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008131 psa_pake_abort(operation);
8132 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008133}
8134
8135psa_status_t psa_pake_get_implicit_key(
8136 psa_pake_operation_t *operation,
8137 psa_key_derivation_operation_t *output)
8138{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008139 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008140 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008141 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008142 size_t shared_key_len = 0;
8143
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008144 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008145 status = PSA_ERROR_BAD_STATE;
8146 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008147 }
8148
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008149#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008150 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008151 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008152 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008153 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008154 status = PSA_ERROR_BAD_STATE;
8155 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008156 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008157 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008158#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008159 {
8160 status = PSA_ERROR_NOT_SUPPORTED;
8161 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008162 }
8163
Przemek Stekiel0c781802022-11-29 14:53:13 +01008164 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8165 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008166 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008167 &shared_key_len);
8168
8169 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008170 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008171 }
8172
8173 status = psa_key_derivation_input_bytes(output,
8174 PSA_KEY_DERIVATION_INPUT_SECRET,
8175 shared_key,
8176 shared_key_len);
8177
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008178 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008179exit:
8180 abort_status = psa_pake_abort(operation);
8181 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008182}
8183
8184psa_status_t psa_pake_abort(
8185 psa_pake_operation_t *operation)
8186{
Przemek Stekield69dca92023-01-31 19:59:20 +01008187 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008188
Przemek Stekield69dca92023-01-31 19:59:20 +01008189 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008190 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008191 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008192
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008193 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8194 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008195 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008196 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008197 }
8198 if (operation->data.inputs.user != NULL) {
8199 mbedtls_free(operation->data.inputs.user);
8200 }
8201 if (operation->data.inputs.peer != NULL) {
8202 mbedtls_free(operation->data.inputs.peer);
8203 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008204 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008205 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008206
Przemek Stekield69dca92023-01-31 19:59:20 +01008207 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008208}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008209#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008210
Gilles Peskinee59236f2018-01-27 23:32:46 +01008211#endif /* MBEDTLS_PSA_CRYPTO_C */